Get codebase completely working with LLVM

You can now build Cosmopolitan with Clang:

    make -j8 MODE=llvm
    o/llvm/examples/hello.com

The assembler and linker code is now friendly to LLVM too.
So it's not needed to configure Clang to use binutils under
the hood. If you love LLVM then you can now use pure LLVM.
This commit is contained in:
Justine Tunney 2021-02-08 09:19:00 -08:00
parent 0e36cb3ac4
commit e75ffde09e
4528 changed files with 7776 additions and 11640 deletions

View file

@ -82,7 +82,7 @@ struct Header {
static void MakeHeader(struct Header *h, const char *name, int ref, int mode,
int size) {
size_t n;
char buf[21];
char buf[24];
memset(h, ' ', sizeof(*h));
n = strlen(name);
memcpy(h->name, name, n);

View file

@ -37,8 +37,7 @@ OVERVIEW\n\
\n\
DESCRIPTION\n\
\n\
This launches gcc or clang while filtering out\n\
flags they whine about.\n\
This launches gcc or clang after scrubbing flags.\n\
\n\
EXAMPLE\n\
\n\
@ -77,6 +76,87 @@ int ccversion;
struct Flags flags;
struct Command command;
const char *const kGccOnlyFlags[] = {
"--nocompress-debug-sections",
"--noexecstack",
"-Wa,--nocompress-debug-sections",
"-Wa,--noexecstack",
"-Wno-unused-but-set-variable",
"-Wunsafe-loop-optimizations",
"-fbranch-target-load-optimize",
"-fcx-limited-range",
"-fdelete-dead-exceptions",
"-femit-struct-debug-baseonly",
"-fipa-pta",
"-fivopts",
"-flimit-function-alignment",
"-fmerge-constants",
"-fmodulo-sched",
"-fmodulo-sched-allow-regmoves",
"-fno-align-jumps",
"-fno-align-labels",
"-fno-align-loops",
"-fno-fp-int-builtin-inexact",
"-fno-gnu-unique",
"-fno-gnu-unique",
"-fno-instrument-functions",
"-fno-whole-program",
"-fopt-info-vec",
"-fopt-info-vec-missed",
"-freg-struct-return",
"-freschedule-modulo-scheduled-loops",
"-frounding-math",
"-fsched2-use-superblocks",
"-fschedule-insns",
"-fschedule-insns2",
"-fshrink-wrap",
"-fshrink-wrap-separate",
"-fsignaling-nans",
"-fstack-clash-protection",
"-ftracer",
"-ftrapv",
"-ftree-loop-im",
"-ftree-loop-vectorize",
"-funsafe-loop-optimizations",
"-fversion-loops-for-strides",
"-fwhole-program",
"-gdescribe-dies",
"-gstabs",
"-mcall-ms2sysv-xlogues",
"-mdispatch-scheduler",
"-mfpmath=sse+387",
"-mmitigate-rop",
"-mno-fentry",
};
bool IsGccOnlyFlag(const char *s) {
int m, l, r, x;
l = 0;
r = ARRAYLEN(kGccOnlyFlags) - 1;
while (l <= r) {
m = (l + r) >> 1;
x = strcmp(s, kGccOnlyFlags[m]);
if (x < 0) {
r = m - 1;
} else if (x > 0) {
l = m + 1;
} else {
return true;
}
}
if (startswith(s, "-ffixed-")) return true;
if (startswith(s, "-fcall-saved")) return true;
if (startswith(s, "-fcall-used")) return true;
if (startswith(s, "-fgcse-")) return true;
if (startswith(s, "-fvect-cost-model=")) return true;
if (startswith(s, "-fsimd-cost-model=")) return true;
if (startswith(s, "-fopt-info")) return true;
if (startswith(s, "-mstringop-strategy=")) return true;
if (startswith(s, "-mpreferred-stack-boundary=")) return true;
if (startswith(s, "-Wframe-larger-than=")) return true;
return false;
}
void AddFlag(char *s) {
size_t n;
flags.p = realloc(flags.p, ++flags.n * sizeof(*flags.p));
@ -123,8 +203,8 @@ int main(int argc, char *argv[]) {
}
ccversion = atoi(firstnonnull(emptytonull(getenv("CCVERSION")), "4"));
isgcc = strstr(basename(cc), "gcc");
isclang = strstr(basename(cc), "clang");
isgcc = !!strstr(basename(cc), "gcc");
isclang = !!strstr(basename(cc), "clang");
iscc = isgcc | isclang;
for (i = 1; i < argc; ++i) {
@ -137,10 +217,13 @@ int main(int argc, char *argv[]) {
AddFlag((outpath = argv[++i]));
continue;
}
if (iscc) {
if (!iscc) {
AddFlag(argv[i]);
continue;
}
if (isclang && IsGccOnlyFlag(argv[i])) {
continue;
}
if (!strcmp(argv[i], "-w")) {
AddFlag(argv[i]);
AddFlag("-D__W__");
@ -207,64 +290,9 @@ int main(int argc, char *argv[]) {
} else if (startswith(argv[i], "-R") ||
!strcmp(argv[i], "-fsave-optimization-record")) {
if (isclang) AddFlag(argv[i]);
} else if (isclang &&
(!strcmp(argv[i], "-gstabs") || !strcmp(argv[i], "-ftrapv") ||
!strcmp(argv[i], "-fsignaling-nans") ||
!strcmp(argv[i], "-fcx-limited-range") ||
!strcmp(argv[i], "-fno-fp-int-builtin-inexact") ||
!strcmp(argv[i], "-Wno-unused-but-set-variable") ||
!strcmp(argv[i], "-Wunsafe-loop-optimizations") ||
!strcmp(argv[i], "-mdispatch-scheduler") ||
!strcmp(argv[i], "-ftracer") ||
!strcmp(argv[i], "-frounding-math") ||
!strcmp(argv[i], "-fmerge-constants") ||
!strcmp(argv[i], "-fmodulo-sched") ||
!strcmp(argv[i], "-fopt-info-vec") ||
!strcmp(argv[i], "-fopt-info-vec-missed") ||
!strcmp(argv[i], "-fmodulo-sched-allow-regmoves") ||
!strcmp(argv[i], "-freschedule-modulo-scheduled-loops") ||
!strcmp(argv[i], "-fipa-pta") ||
!strcmp(argv[i], "-fsched2-use-superblocks") ||
!strcmp(argv[i], "-fbranch-target-load-optimize") ||
!strcmp(argv[i], "-fdelete-dead-exceptions") ||
!strcmp(argv[i], "-funsafe-loop-optimizations") ||
!strcmp(argv[i], "-mmitigate-rop") ||
!strcmp(argv[i], "-fno-align-jumps") ||
!strcmp(argv[i], "-fno-align-labels") ||
!strcmp(argv[i], "-fno-align-loops") ||
!strcmp(argv[i], "-fivopts") ||
!strcmp(argv[i], "-fschedule-insns") ||
!strcmp(argv[i], "-fno-semantic-interposition") ||
!strcmp(argv[i], "-mno-fentry") ||
!strcmp(argv[i], "-fversion-loops-for-strides") ||
!strcmp(argv[i], "-femit-struct-debug-baseonly") ||
!strcmp(argv[i], "-ftree-loop-vectorize") ||
!strcmp(argv[i], "-gdescribe-dies") ||
!strcmp(argv[i], "-flimit-function-alignment") ||
!strcmp(argv[i], "-ftree-loop-im") ||
!strcmp(argv[i], "-fno-instrument-functions") ||
!strcmp(argv[i], "-fstack-clash-protection") ||
!strcmp(argv[i], "-mfpmath=sse+387") ||
!strcmp(argv[i], "-Wa,--noexecstack") ||
!strcmp(argv[i], "-freg-struct-return") ||
!strcmp(argv[i], "-mcall-ms2sysv-xlogues") ||
startswith(argv[i], "-ffixed-") ||
startswith(argv[i], "-fcall-saved") ||
startswith(argv[i], "-fcall-used") ||
startswith(argv[i], "-fgcse-") ||
strstr(argv[i], "shrink-wrap") ||
strstr(argv[i], "schedule-insns2") ||
startswith(argv[i], "-fvect-cost-model=") ||
startswith(argv[i], "-fsimd-cost-model=") ||
startswith(argv[i], "-fopt-info") ||
startswith(argv[i], "-mstringop-strategy=") ||
startswith(argv[i], "-mpreferred-stack-boundary=") ||
strstr(argv[i], "gnu-unique") ||
startswith(argv[i], "-Wframe-larger-than=") ||
strstr(argv[i], "whole-program") ||
startswith(argv[i], "-Wa,--size-check=") ||
startswith(argv[i], "-Wa,--listing"))) {
/* ignore flag so clang won't whine */
} else if (isclang && startswith(argv[i], "--debug-prefix-map")) {
/* llvm doesn't provide a gas interface so simulate w/ clang */
AddFlag(xasprintf("-f%s", argv[i] + 2));
} else {
AddFlag(argv[i]);
}
@ -272,7 +300,7 @@ int main(int argc, char *argv[]) {
if (iscc) {
if (isclang) {
AddFlag("-fno-integrated-as");
/* AddFlag("-fno-integrated-as"); */
AddFlag("-Wno-unused-command-line-argument");
AddFlag("-Wno-incompatible-pointer-types-discards-qualifiers");
}
@ -304,6 +332,9 @@ int main(int argc, char *argv[]) {
AddFlag("-fsanitize=undefined");
AddFlag("-fno-data-sections");
}
if (wantframe) {
AddFlag("-fno-omit-frame-pointer");
}
}
AddFlag(NULL);

View file

@ -94,10 +94,11 @@ static char *DisError(struct Dis *d, char *p) {
}
static char *DisAddr(struct Dis *d, char *p) {
if (-0x80000000 <= d->addr && d->addr <= 0x7fffffff) {
return p + uint64toarray_fixed16(d->addr, p, 32);
int64_t x = d->addr;
if (-2147483648 <= x && x <= 2147483647) {
return p + uint64toarray_fixed16(x, p, 32);
} else {
return p + uint64toarray_fixed16(d->addr, p, 48);
return p + uint64toarray_fixed16(x, p, 48);
}
}

View file

@ -24,9 +24,9 @@
.long \linux
.endm
/ Lookup table translating errnos between systems.
/
/ @see libc/sysv/systemfive.S
// Lookup table translating errnos between systems.
//
// @see libc/sysv/systemfive.S
.rodata
.align 8
kLinuxErrnos:

View file

@ -65,7 +65,11 @@ static unsigned GetSpacePrefixLen(const char *p, size_t n) {
static unsigned GetSpaceStarPrefixLen(const char *p, size_t n) {
int i;
i = GetSpacePrefixLen(p, n);
return i < n && (p[i] == '*' || p[i] == '/') ? i + 1 : 0;
if (i < n && (p[i] == '*' || p[i] == '/')) {
return p[i + 1] == '/' ? i + 2 : i + 1;
} else {
return 0;
}
}
static unsigned GetTagLen(const char *p, size_t n) {

View file

@ -356,7 +356,8 @@ void OpenObject(struct Package *pkg, struct Object *obj, int mode, int prot,
CHECK_NE(-1, close(fd));
CHECK(IsElf64Binary(obj->elf, obj->size), "path=%`'s",
&pkg->strings.p[obj->path]);
CHECK_NOTNULL((obj->strs = GetElfStringTable(obj->elf, obj->size)));
CHECK_NOTNULL((obj->strs = GetElfStringTable(obj->elf, obj->size)), "on %s",
&pkg->strings.p[obj->path]);
CHECK_NOTNULL(
(obj->syms = GetElfSymbolTable(obj->elf, obj->size, &obj->symcount)));
CHECK_NE(0, obj->symcount);

View file

@ -145,12 +145,12 @@ void CheckExists(const char *path) {
nodiscard char *MakeDeployScript(struct addrinfo *remotenic, size_t combytes) {
const char *ip4 = (const char *)&remotenic->ai_addr4->sin_addr;
return xasprintf("mkdir -p o/ &&\n"
"dd bs=%zu count=%zu of=o/runitd.$$.com 2>/dev/null &&\n"
"exec <&- &&\n"
"chmod +x o/runitd.$$.com &&\n"
"o/runitd.$$.com -rdl%hhu.%hhu.%hhu.%hhu -p %hu &&\n"
"rm -f o/runitd.$$.com\n",
return xasprintf("mkdir -p o/ && "
"dd bs=%zu count=%zu of=o/runitd.$$.com 2>/dev/null && "
"exec <&- && "
"chmod +x o/runitd.$$.com && "
"o/runitd.$$.com -rdl%hhu.%hhu.%hhu.%hhu -p %hu && "
"rm -f o/runitd.$$.com",
GreatestTwoDivisor(combytes),
combytes ? combytes / GreatestTwoDivisor(combytes) : 0,
ip4[0], ip4[1], ip4[2], ip4[3], g_runitdport);

View file

@ -317,7 +317,6 @@ void HandleClient(void) {
CHECK_LE(wrote, got);
} while ((got -= wrote));
}
LOGIFNEG1(shutdown(g_clifd, SHUT_RD));
LOGIFNEG1(close(g_exefd));
/* run program, tee'ing stderr to both log and client */
@ -376,7 +375,6 @@ void HandleClient(void) {
/* let client know how it went */
LOGIFNEG1(unlink(g_exepath));
SendExitMessage(g_clifd, exitcode);
LOGIFNEG1(shutdown(g_clifd, SHUT_RDWR));
LOGIFNEG1(close(g_clifd));
_exit(0);
}

View file

@ -26,6 +26,7 @@
#include "libc/fmt/conv.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
@ -282,7 +283,7 @@ static void printelfrelocations(void) {
GetElfSectionHeaderAddress(elf, st->st_size, shdr->sh_info)
->sh_name),
rela->r_offset, symbolname, rela->r_addend >= 0 ? '+' : '-',
abs(rela->r_addend));
ABS(rela->r_addend));
printf("%s_%zu_%zu:\n", ".Lrela", i, j);
show(".quad", format(b1, "%#lx", rela->r_offset), "rela->r_offset");
show(".long",

View file

@ -47,8 +47,8 @@ kPollNamesRo:
.endobj kPollNamesRo,globl,hidden
.previous
/ Mapping of poll() flags to their string names.
/ @see RecreateFlags()
// Mapping of poll() flags to their string names.
// @see RecreateFlags()
.initbss 301,_init_kPollNames
kPollNames:
.rept .Lrows
@ -62,10 +62,11 @@ kPollNames:
.init.start 301,_init_kPollNames
pushpop .Lrows,%rcx # relocate ROBSS b/c -fPIE crap
0: lodsl
mov (%rbx,%rax),%rax # read what systemfive.S decoded
.weak _base
mov _base(%rax),%rax # read what systemfive.S decoded
stosq
lodsl
add %rbx,%rax # %rbx is image base (cosmo abi)
add $_base,%rax
stosq
.loop 0b
add $16,%rdi

View file

@ -15,6 +15,7 @@
"__GNUC_MINOR__"
"__GNUC_PATCHLEVEL__"
"__GNUC__"
"__APPLE__"
"__GNUG__"
"__INCLUDE_LEVEL__"
"__INTMAX_MAX__"

View file

@ -137,6 +137,7 @@
;; M-3 C-c C-c Compile w/ MODE=rel
;; M-4 C-c C-c Compile w/ MODE=dbg
;; M-5 C-c C-c Compile w/ MODE=""
;; M-8 C-c C-c Compile w/ llvm
;; M-9 C-c C-c Compile w/ chibicc
(defun cosmo-intest (&optional file-name)
@ -152,6 +153,7 @@
((eq arg 3) "rel")
((eq arg 4) "dbg")
((eq arg 5) "")
((eq arg 8) "llvm")
(default default)
((cosmo-intest) "dbg")
(t "")))

View file

@ -22,6 +22,7 @@ TOOL_HASH_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_UNICODE \
LIBC_STUBS
TOOL_HASH_DEPS := \

View file

@ -38,6 +38,6 @@ gcc -O -s -static -nostdlib -nostdinc \
-Wl,-T,ape.lds -include cosmopolitan.h \
-o HELLO.COM.DBG HELLO.C crt.o ape.o cosmopolitan.a
objcopy -SO binary HELLO.COM.DBG HELLO.COM
objcopy -S -O binary HELLO.COM.DBG HELLO.COM
ls -hal ./HELLO.COM
./HELLO.COM

View file

@ -362,7 +362,7 @@ static struct Cell derasterize(unsigned char block[CN][YS * XS]) {
unsigned char bf[1u << MC][2];
rgb2lin(CN * YS * XS, lb[0], block[0]);
n = combinecolors(bf, block);
best = -1u;
best = FLT_MAX;
cell.rune = 0;
for (i = 0; i < n; ++i) {
b = bf[i][0];

View file

@ -18,15 +18,15 @@
*/
#include "libc/macros.h"
/ Duplicates chrominance samples horizontally, e.g.
/
/ 12345678--------
/ 1122334455667788
/
/ @param %edi is size of %rsi array in bytes
/ @param %rsi is char[edi/16][16] output and %rsi==%rdx is OK
/ @param %rdx is char[edi/16][8] input
/ @return %rax is %rsi
// Duplicates chrominance samples horizontally, e.g.
//
// 12345678--------
// 1122334455667788
//
// @param %edi is size of %rsi array in bytes
// @param %rsi is char[edi/16][16] output and %rsi==%rdx is OK
// @param %rdx is char[edi/16][8] input
// @return %rax is %rsi
doublechrominance:
.leafprologue
.profilable

View file

@ -78,7 +78,7 @@ static const char *GetStorageSpecifier(const char *type, int *out_width,
static void EmitSection(long yn, long xn, int w, int arrayalign, int emit(),
void *a) {
char alignstr[20];
char alignstr[21];
uint64toarray_radix10(arrayalign, alignstr);
if (arrayalign <= 8 && yn * xn * w == 8) {
emit("\t.rodata.cst", a);
@ -105,7 +105,7 @@ void *FormatStringTableAsAssembly(long yn, long xn, const char *const T[yn][xn],
const char *name, const char *scope) {
int w, align;
const char *storage;
char ynstr[20], xnstr[20];
char ynstr[21], xnstr[21];
name = firstnonnull(name, "M");
storage = GetStorageSpecifier(firstnonnull(type, "long"), &w, &align);
uint64toarray_radix10(yn, ynstr);

View file

@ -23,7 +23,7 @@
void *FormatStringTableAsCode(long yn, long xn, const char *const T[yn][xn],
int emit(), void *arg, const char *type,
const char *name, const char *ignored) {
char ynstr[20], xnstr[20];
char ynstr[21], xnstr[21];
uint64toarray_radix10(yn, ynstr);
uint64toarray_radix10(xn, xnstr);
emit(type, arg);

View file

@ -287,7 +287,7 @@ static int16_t pcmscale_[PLM_AUDIO_SAMPLES_PER_FRAME * 2 / 8][8];
static bool fullclear_, historyclear_, tuned_, yonly_, gotvideo_;
static int homerow_, lastrow_, playfd_, infd_, outfd_, nullfd_, speakerfails_;
static char host_[DNS_NAME_MAX + 1], status_[7][200], logpath_[PATH_MAX],
fifopath_[PATH_MAX], chansstr_[16], sratestr_[16], port_[8];
fifopath_[PATH_MAX], chansstr_[32], sratestr_[32], port_[32];
static void OnCtrlC(void) {
longjmp(jb_, 1);