mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-02 07:50:31 +00:00
Introduce new fatcosmocc command
This new script is an alternative to the `cosmocc` command. It's still a work in progress. It abstracts all the gory details of building separate copies of your executable and then running the apelink.com program.
This commit is contained in:
parent
8fc778162e
commit
d53c335a45
35 changed files with 1151 additions and 525 deletions
|
@ -77,6 +77,8 @@
|
|||
"\n" \
|
||||
" -o OUTPUT set output path\n" \
|
||||
"\n" \
|
||||
" -s never embed symbol table\n" \
|
||||
"\n" \
|
||||
" -l PATH bundle ape loader executable [repeatable]\n" \
|
||||
" if no ape loaders are specified then your\n" \
|
||||
" executable will self-modify its header on\n" \
|
||||
|
@ -86,7 +88,7 @@
|
|||
" processors running the xnu kernel so that\n" \
|
||||
" it can be compiled on the fly by xcode\n" \
|
||||
"\n" \
|
||||
" -s BITS set OS support vector\n" \
|
||||
" -V BITS set OS support vector\n" \
|
||||
"\n" \
|
||||
" the default value is -1 which sets the bits\n" \
|
||||
" for all supported operating systems to true\n" \
|
||||
|
@ -232,6 +234,7 @@ struct Assets {
|
|||
static int outfd;
|
||||
static int hashes;
|
||||
static const char *prog;
|
||||
static bool want_stripped;
|
||||
static int support_vector;
|
||||
static int macholoadcount;
|
||||
static const char *outpath;
|
||||
|
@ -246,6 +249,7 @@ static bool dont_path_lookup_ape_loader;
|
|||
_Alignas(4096) static char prologue[1048576];
|
||||
static uint8_t hashpool[BLAKE2B256_DIGEST_LENGTH];
|
||||
static const char *macos_silicon_loader_source_path;
|
||||
static const char *macos_silicon_loader_source_text;
|
||||
static char *macos_silicon_loader_source_ddarg_skip;
|
||||
static char *macos_silicon_loader_source_ddarg_size;
|
||||
|
||||
|
@ -329,6 +333,16 @@ static const char *DescribePhdrType(Elf64_Word p_type) {
|
|||
}
|
||||
}
|
||||
|
||||
static bool IsBinary(const char *p, size_t n) {
|
||||
size_t i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if ((p[i] & 255) < '\t') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void BlendHashes(uint8_t out[static BLAKE2B256_DIGEST_LENGTH],
|
||||
uint8_t inp[static BLAKE2B256_DIGEST_LENGTH]) {
|
||||
int i;
|
||||
|
@ -349,6 +363,28 @@ static void HashInputString(const char *str) {
|
|||
HashInput(str, strlen(str));
|
||||
}
|
||||
|
||||
static char *LoadSourceCode(const char *path) {
|
||||
int fd;
|
||||
size_t i;
|
||||
char *text;
|
||||
ssize_t rc, size;
|
||||
if ((fd = open(path, O_RDONLY)) == -1) DieSys(path);
|
||||
if ((size = lseek(fd, 0, SEEK_END)) == -1) DieSys(path);
|
||||
text = Malloc(size + 1);
|
||||
text[size] = 0;
|
||||
for (i = 0; i < size; i += rc) {
|
||||
if ((rc = pread(fd, text, size - i, i)) <= 0) {
|
||||
DieSys(path);
|
||||
}
|
||||
if (IsBinary(text, rc)) {
|
||||
Die(path, "source code contains binary data");
|
||||
}
|
||||
}
|
||||
if (close(fd)) DieSys(path);
|
||||
HashInput(text, size);
|
||||
return text;
|
||||
}
|
||||
|
||||
static void Pwrite(const void *data, size_t size, uint64_t offset) {
|
||||
ssize_t rc;
|
||||
const char *p, *e;
|
||||
|
@ -928,13 +964,17 @@ static void GetOpts(int argc, char *argv[]) {
|
|||
char *endptr;
|
||||
int opt, bits;
|
||||
bool got_support_vector = false;
|
||||
while ((opt = getopt(argc, argv, "hvgGBo:s:l:S:M:")) != -1) {
|
||||
while ((opt = getopt(argc, argv, "hvgsGBo:l:S:M:V:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'o':
|
||||
outpath = optarg;
|
||||
break;
|
||||
case 's':
|
||||
HashInputString("-s");
|
||||
want_stripped = true;
|
||||
break;
|
||||
case 'V':
|
||||
HashInputString("-V");
|
||||
HashInputString(optarg);
|
||||
if (ParseSupportVector(optarg, &bits)) {
|
||||
support_vector |= bits;
|
||||
|
@ -945,22 +985,30 @@ static void GetOpts(int argc, char *argv[]) {
|
|||
got_support_vector = true;
|
||||
break;
|
||||
case 'l':
|
||||
HashInputString("-l");
|
||||
AddLoader(optarg);
|
||||
break;
|
||||
case 'S':
|
||||
HashInputString("-S");
|
||||
HashInputString(optarg);
|
||||
custom_sh_code = optarg;
|
||||
break;
|
||||
case 'B':
|
||||
HashInputString("-B");
|
||||
force_bypass_binfmt_misc = true;
|
||||
break;
|
||||
case 'g':
|
||||
HashInputString("-g");
|
||||
generate_debuggable_binary = true;
|
||||
break;
|
||||
case 'G':
|
||||
HashInputString("-G");
|
||||
dont_path_lookup_ape_loader = true;
|
||||
break;
|
||||
case 'M':
|
||||
HashInputString("-M");
|
||||
macos_silicon_loader_source_path = optarg;
|
||||
macos_silicon_loader_source_text = LoadSourceCode(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
tinyprint(0, VERSION, NULL);
|
||||
|
@ -1740,7 +1788,7 @@ static void CopyZips(Elf64_Off offset) {
|
|||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *p;
|
||||
int i, opt;
|
||||
int i, j, opt;
|
||||
Elf64_Off offset;
|
||||
char empty[64] = {0};
|
||||
Elf64_Xword prologue_bytes;
|
||||
|
@ -1776,11 +1824,26 @@ int main(int argc, char *argv[]) {
|
|||
for (i = 0; i < loaders.n; ++i) {
|
||||
OpenLoader(loaders.p + i);
|
||||
}
|
||||
for (i = 0; i < loaders.n; ++i) {
|
||||
for (j = i + 1; j < loaders.n; ++j) {
|
||||
if (loaders.p[i].os == loaders.p[j].os &&
|
||||
loaders.p[i].machine == loaders.p[j].machine) {
|
||||
Die(prog, "multiple ape loaders specified for the same platform");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// open input files
|
||||
for (i = optind; i < argc; ++i) {
|
||||
OpenInput(argv[i]);
|
||||
}
|
||||
for (i = 0; i < inputs.n; ++i) {
|
||||
for (j = i + 1; j < inputs.n; ++j) {
|
||||
if (inputs.p[i].elf->e_machine == inputs.p[j].elf->e_machine) {
|
||||
Die(prog, "multiple executables passed for the same machine type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validate input files
|
||||
for (i = 0; i < inputs.n; ++i) {
|
||||
|
@ -1789,15 +1852,17 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
// load symbols
|
||||
for (i = 0; i < inputs.n; ++i) {
|
||||
struct Input *in = inputs.p + i;
|
||||
if (GetElfSymbol(in, "__zipos_get")) {
|
||||
LoadSymbols(in->elf, in->size, in->path);
|
||||
} else {
|
||||
tinyprint(2, in->path,
|
||||
": warning: won't generate symbol table unless "
|
||||
"__static_yoink(\"zipos\") is linked\n",
|
||||
NULL);
|
||||
if (!want_stripped) {
|
||||
for (i = 0; i < inputs.n; ++i) {
|
||||
struct Input *in = inputs.p + i;
|
||||
if (GetElfSymbol(in, "__zipos_get")) {
|
||||
LoadSymbols(in->elf, in->size, in->path);
|
||||
} else {
|
||||
tinyprint(2, in->path,
|
||||
": warning: won't generate symbol table unless "
|
||||
"__static_yoink(\"zipos\") is linked\n",
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2046,11 +2111,7 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
p = stpcpy(p, "fi\n"); // if [ -d /Applications ]; then
|
||||
} else {
|
||||
if (macos_silicon_loader_source_path) {
|
||||
Die(macos_silicon_loader_source_path,
|
||||
"won't embed macos arm64 ape loader source code because xnu isn't "
|
||||
"in the support vector");
|
||||
}
|
||||
macos_silicon_loader_source_path = 0;
|
||||
}
|
||||
|
||||
// extract the ape loader for open platforms
|
||||
|
@ -2131,25 +2192,16 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
// concatenate ape loader source code
|
||||
if (macos_silicon_loader_source_path) {
|
||||
int fd;
|
||||
char *map;
|
||||
ssize_t size;
|
||||
char *compressed_data;
|
||||
size_t compressed_size;
|
||||
fd = open(macos_silicon_loader_source_path, O_RDONLY);
|
||||
if (fd == -1) DieSys(macos_silicon_loader_source_path);
|
||||
size = lseek(fd, 0, SEEK_END);
|
||||
if (size == -1) DieSys(macos_silicon_loader_source_path);
|
||||
map = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (map == MAP_FAILED) DieSys(macos_silicon_loader_source_path);
|
||||
compressed_data = Gzip(map, size, &compressed_size);
|
||||
compressed_data =
|
||||
Gzip(macos_silicon_loader_source_text,
|
||||
strlen(macos_silicon_loader_source_text), &compressed_size);
|
||||
FixupWordAsDecimal(macos_silicon_loader_source_ddarg_skip, offset);
|
||||
FixupWordAsDecimal(macos_silicon_loader_source_ddarg_size, compressed_size);
|
||||
Pwrite(compressed_data, compressed_size, offset);
|
||||
offset += compressed_size;
|
||||
free(compressed_data);
|
||||
munmap(map, size);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
// add the zip files
|
||||
|
|
184
tool/build/march-native.c
Normal file
184
tool/build/march-native.c
Normal file
|
@ -0,0 +1,184 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/nexgen32e/x86info.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "third_party/getopt/getopt.internal.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Command for printing `-march=native` flags.
|
||||
*
|
||||
* In recent years (2023) compilers have decided to remove support for
|
||||
* the `-march=native` flag, even on x86. That's unfortunate, since as
|
||||
* we can see below, grokking all the various microarchitecture is not
|
||||
* something a compiler should reasonably expect from users especially
|
||||
* not for a flag as important as this one, which can have a night and
|
||||
* day impact for apps that do scientific computing.
|
||||
*
|
||||
* This is a tiny program, that makes it easy for shell scripts to get
|
||||
* these flags.
|
||||
*/
|
||||
|
||||
#define VERSION \
|
||||
"-march=native flag printer v0.1\n" \
|
||||
"copyright 2023 justine alexandra roberts tunney\n"
|
||||
|
||||
#define USAGE \
|
||||
"usage: march-native.com [-hvc]\n" \
|
||||
" -h show help\n" \
|
||||
" -v show version\n" \
|
||||
" -c assume we're using clang (not gcc)\n"
|
||||
|
||||
static bool isclang;
|
||||
|
||||
static void GetOpts(int argc, char *argv[]) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "hvc")) != -1) {
|
||||
switch (opt) {
|
||||
case 'c':
|
||||
isclang = true;
|
||||
break;
|
||||
case 'v':
|
||||
tinyprint(1, VERSION, NULL);
|
||||
exit(0);
|
||||
case 'h':
|
||||
tinyprint(1, VERSION, USAGE, NULL);
|
||||
exit(0);
|
||||
default:
|
||||
tinyprint(2, VERSION, USAGE, NULL);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Puts(const char *s) {
|
||||
tinyprint(1, s, "\n", NULL);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
GetOpts(argc, argv);
|
||||
#ifdef __x86_64__
|
||||
struct X86ProcessorModel *model;
|
||||
if (X86_HAVE(XOP)) Puts("-mxop");
|
||||
if (X86_HAVE(SSE4A)) Puts("-msse4a");
|
||||
if (X86_HAVE(SSE3)) Puts("-msse3");
|
||||
if (X86_HAVE(SSSE3)) Puts("-mssse3");
|
||||
if (X86_HAVE(SSE4_1)) Puts("-msse4.1");
|
||||
if (X86_HAVE(SSE4_2)) Puts("-msse4.2");
|
||||
if (X86_HAVE(AVX)) Puts("-mavx");
|
||||
if (X86_HAVE(AVX2)) {
|
||||
Puts("-mavx2");
|
||||
if (!isclang) {
|
||||
Puts("-msse2avx");
|
||||
Puts("-Wa,-msse2avx");
|
||||
}
|
||||
}
|
||||
if (X86_HAVE(AVX512F)) Puts("-mavx512f");
|
||||
if (X86_HAVE(AVX512PF)) Puts("-mavx512pf");
|
||||
if (X86_HAVE(AVX512ER)) Puts("-mavx512er");
|
||||
if (X86_HAVE(AVX512CD)) Puts("-mavx512cd");
|
||||
if (X86_HAVE(AVX512VL)) Puts("-mavx512vl");
|
||||
if (X86_HAVE(AVX512BW)) Puts("-mavx512bw");
|
||||
if (X86_HAVE(AVX512DQ)) Puts("-mavx512dq");
|
||||
if (X86_HAVE(AVX512IFMA)) Puts("-mavx512ifma");
|
||||
if (X86_HAVE(AVX512VBMI)) Puts("-mavx512vbmi");
|
||||
if (X86_HAVE(SHA)) Puts("-msha");
|
||||
if (X86_HAVE(AES)) Puts("-maes");
|
||||
if (X86_HAVE(VAES)) Puts("-mvaes");
|
||||
if (X86_HAVE(PCLMUL)) Puts("-mpclmul");
|
||||
if (X86_HAVE(FSGSBASE)) Puts("-mfsgsbase");
|
||||
if (X86_HAVE(F16C)) Puts("-mf16c");
|
||||
if (X86_HAVE(FMA)) Puts("-mfma");
|
||||
if (X86_HAVE(POPCNT)) Puts("-mpopcnt");
|
||||
if (X86_HAVE(BMI)) Puts("-mbmi");
|
||||
if (X86_HAVE(BMI2)) Puts("-mbmi2");
|
||||
if (X86_HAVE(ADX)) Puts("-madx");
|
||||
if (X86_HAVE(FXSR)) Puts("-mfxsr");
|
||||
if ((model = getx86processormodel(kX86ProcessorModelKey))) {
|
||||
switch (model->march) {
|
||||
case X86_MARCH_CORE2:
|
||||
Puts("-march=core2");
|
||||
break;
|
||||
case X86_MARCH_NEHALEM:
|
||||
Puts("-march=nehalem");
|
||||
break;
|
||||
case X86_MARCH_WESTMERE:
|
||||
Puts("-march=westmere");
|
||||
break;
|
||||
case X86_MARCH_SANDYBRIDGE:
|
||||
Puts("-march=sandybridge");
|
||||
break;
|
||||
case X86_MARCH_IVYBRIDGE:
|
||||
Puts("-march=ivybridge");
|
||||
break;
|
||||
case X86_MARCH_HASWELL:
|
||||
Puts("-march=haswell");
|
||||
break;
|
||||
case X86_MARCH_BROADWELL:
|
||||
Puts("-march=broadwell");
|
||||
break;
|
||||
case X86_MARCH_SKYLAKE:
|
||||
case X86_MARCH_KABYLAKE:
|
||||
Puts("-march=skylake");
|
||||
break;
|
||||
case X86_MARCH_CANNONLAKE:
|
||||
Puts("-march=cannonlake");
|
||||
break;
|
||||
case X86_MARCH_ICELAKE:
|
||||
if (model->grade >= X86_GRADE_SERVER) {
|
||||
Puts("-march=icelake-server");
|
||||
} else {
|
||||
Puts("-march=icelake-client");
|
||||
}
|
||||
break;
|
||||
case X86_MARCH_TIGERLAKE:
|
||||
Puts("-march=tigerlake");
|
||||
break;
|
||||
case X86_MARCH_BONNELL:
|
||||
case X86_MARCH_SALTWELL:
|
||||
Puts("-march=bonnell");
|
||||
break;
|
||||
case X86_MARCH_SILVERMONT:
|
||||
case X86_MARCH_AIRMONT:
|
||||
Puts("-march=silvermont");
|
||||
break;
|
||||
case X86_MARCH_GOLDMONT:
|
||||
Puts("-march=goldmont");
|
||||
break;
|
||||
case X86_MARCH_GOLDMONTPLUS:
|
||||
Puts("-march=goldmont-plus");
|
||||
break;
|
||||
case X86_MARCH_TREMONT:
|
||||
Puts("-march=tremont");
|
||||
break;
|
||||
case X86_MARCH_KNIGHTSLANDING:
|
||||
Puts("-march=knl");
|
||||
break;
|
||||
case X86_MARCH_KNIGHTSMILL:
|
||||
Puts("-march=knm");
|
||||
break;
|
||||
}
|
||||
}
|
||||
#elif defined(__aarch64__)
|
||||
// TODO(jart): How can we determine CPU features on AARCH64?
|
||||
#else
|
||||
// otherwise do nothing (it's usually best)
|
||||
#endif
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue