This is a series of patches for clang compatibility: - Using flags needed flags and removing unsupported flags. - Adding support for clang's LLD linker. - Removing a variety of warnings. Bill Wendling (3): [klibc] Kbuild: use "libc.a" with clang [klibc] Kbuild: Add "-fcommon" for clang builds [klibc] Clean up clang warnings Michael Davidson (1): [klibc] Kbuild: add support for clang builds Stanislav Fomichev (1): [klibc] Kbuild: support llvm's lld Makefile | 3 +++ scripts/Kbuild.include | 4 ++++ scripts/Kbuild.klibc | 19 +++++++++++++++++-- usr/dash/eval.c | 8 ++++---- usr/dash/jobs.c | 4 ++-- usr/dash/output.c | 12 ++++++------ usr/kinit/initrd.c | 2 +- usr/kinit/ramdisk_load.c | 2 +- usr/klibc/arch/i386/MCONFIG | 2 +- usr/klibc/arch/mips64/MCONFIG | 2 +- usr/klibc/arch/ppc/MCONFIG | 2 +- usr/klibc/arch/ppc64/MCONFIG | 2 +- usr/klibc/arch/riscv64/MCONFIG | 2 +- usr/klibc/arch/sparc64/MCONFIG | 2 +- usr/klibc/arch/x86_64/MCONFIG | 14 +++++++++++--- 15 files changed, 55 insertions(+), 25 deletions(-) -- 2.26.0.rc2.310.g2932bb562d-goog
Bill Wendling
2020-Mar-27 22:12 UTC
[klibc] [PATCH 1/5] Kbuild: add support for clang builds
From: Michael Davidson <md at google.com> Add cc-name to klibc/scripts/Kbuild.include. Make optimization flags not supported by clang conditional on cc-name. Don't use -nostdinc when building klibc. While klibc doesn't (shouldn't) use the standard header files supplied by the toolchain, it does still need to be able to find the compiler-specific header file <stdarg.h>. With gcc this can be achieved by specifying "-iwithprefix include" in addition to "-nostdinc". clang behaves differently and the easiest work around is to simply not specify "-nostdinc" and rely on the fact that the various include paths have been specified correctly so that the only header file actually pulled in from the standard location is <stdarg.h>. See b/35394554 for more details. Disable the use of --print-libgcc when using clang. Explictly specifying the path to compiler specific libraries appears to be unnecessary since klibc never specifies -nostdlib when linking binaries, meaning that any such libraries should already be available. Signed-off-by: Bill Wendling <morbo at google.com> --- scripts/Kbuild.include | 4 ++++ scripts/Kbuild.klibc | 17 +++++++++++++++-- usr/klibc/arch/x86_64/MCONFIG | 10 ++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index a048ec7a4c89..de31a0c5efa5 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -127,6 +127,10 @@ cc-option-align = $(subst -functions=0,,\ cc-disable-warning = $(call try-run,\ $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1))) +# cc-name +# Expands to either gcc or clang +cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc) + # cc-version # Usage gcc-ver := $(call cc-version) cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc index afc9a546feca..411d7ea9bea6 100644 --- a/scripts/Kbuild.klibc +++ b/scripts/Kbuild.klibc @@ -107,8 +107,15 @@ KLIBCOBJCOPY := $(OBJCOPY) KLIBCOBJDUMP := $(OBJDUMP) # klibc include paths -KLIBCCPPFLAGS := -nostdinc -iwithprefix include \ - -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \ +ifeq ($(cc-name),clang) +# clang can't find the compiler-specific header <stdarg.h> if -nostdinc +# is specified so we have to omit it along with the -iwithprefix option. +# See b/35394554. +KLIBCCPPFLAGS :+else +KLIBCCPPFLAGS := -nostdinc -iwithprefix include +endif +KLIBCCPPFLAGS += -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \ -I$(KLIBCINC)/bits$(KLIBCBITSIZE) \ -I$(KLIBCOBJ)/../include \ -I$(KLIBCINC) @@ -128,7 +135,13 @@ KLIBCCFLAGS += $(KLIBCCPPFLAGS) $(KLIBCREQFLAGS) $(KLIBCARCHREQFLAGS) \ KLIBCAFLAGS += -D__ASSEMBLY__ -Wa,--noexecstack $(KLIBCCFLAGS) KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note +# KLIBCLIBGCC_DEF appears to be unnecessary since klibc never uses -nostdlib +# when linking binaries, which means that any compiler specific libraries +# should be available automatically, no need to speciy an explicit path. +# Don't attempt to set it if we are using clang. +ifneq ($(cc-name),clang) KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc) +endif KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF) KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o KLIBCLIBC := $(KLIBCOBJ)/libc.a diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index c5f2fa265a26..77e2ad279884 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -15,13 +15,19 @@ # debugging using gdb. # KLIBCARCHREQFLAGS = -m64 -KLIBCOPTFLAGS += -Os -fomit-frame-pointer -mno-sse \ - -falign-functions=1 -falign-jumps=1 -falign-loops=1 +KLIBCOPTFLAGS += -Os -fomit-frame-pointer -mno-sse ifeq ($(DEBUG),y) KLIBCOPTFLAGS += -g else KLIBCOPTFLAGS += -fno-asynchronous-unwind-tables endif + +ifneq ($(cc-name),clang) +# These options are not supported by clang but only result in a warning +# so we can't use a cc-option test to filter them. +KLIBCOPTFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 +endif + KLIBCBITSIZE = 64 KLIBCLDFLAGS = -m elf_x86_64 -- 2.26.0.rc2.310.g2932bb562d-goog
From: Stanislav Fomichev <sdf at google.com> Clang emits relocations that our outdated binutils ld doesn't support. Let's teach klibc some ld.lld quirks. Signed-off-by: Stanislav Fomichev <sdf at google.com> Signed-off-by: Bill Wendling <morbo at google.com> --- Makefile | 3 +++ usr/klibc/arch/i386/MCONFIG | 2 +- usr/klibc/arch/mips64/MCONFIG | 2 +- usr/klibc/arch/ppc/MCONFIG | 2 +- usr/klibc/arch/ppc64/MCONFIG | 2 +- usr/klibc/arch/riscv64/MCONFIG | 2 +- usr/klibc/arch/sparc64/MCONFIG | 2 +- usr/klibc/arch/x86_64/MCONFIG | 2 +- 8 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c99b962fd2a3..52d7cd2cf791 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ export NM := $(KLIBCROSS)nm export OBJCOPY := $(KLIBCROSS)objcopy export OBJDUMP := $(KLIBCROSS)objdump +LLD := $(shell $(LD) --version 2>&1 | grep LLD) +export IMAGE_BASE=$(if $(LLD),--image-base,-Ttext-segment) + NOSTDINC_FLAGS := -nostdlib -nostdinc -isystem $(shell $(CC) -print-file-name=include) ARCH := $(shell uname -m | sed -e s/i.86/i386/ \ diff --git a/usr/klibc/arch/i386/MCONFIG b/usr/klibc/arch/i386/MCONFIG index 0704669500f0..baa7e59854f8 100644 --- a/usr/klibc/arch/i386/MCONFIG +++ b/usr/klibc/arch/i386/MCONFIG @@ -30,4 +30,4 @@ KLIBCBITSIZE = 32 # This address needs to be reachable using normal inter-module # calls, and work on the memory models for this architecture # 96 MB - normal binaries start at 128 MB -KLIBCSHAREDFLAGS = -Ttext-segment 0x06000000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x06000000 diff --git a/usr/klibc/arch/mips64/MCONFIG b/usr/klibc/arch/mips64/MCONFIG index 6a4b41b2f85c..3d7f20da08be 100644 --- a/usr/klibc/arch/mips64/MCONFIG +++ b/usr/klibc/arch/mips64/MCONFIG @@ -20,4 +20,4 @@ KLIBCBITSIZE = 64 # load address, use the very top of the 256M region (minus 2MB) # # Use -Ttext-segment so that the special .MIPS* sections are moved as well. -KLIBCSHAREDFLAGS = -Ttext-segment 0x12FE00000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x12FE00000 diff --git a/usr/klibc/arch/ppc/MCONFIG b/usr/klibc/arch/ppc/MCONFIG index 8220f6b93fa4..ec859e8941d9 100644 --- a/usr/klibc/arch/ppc/MCONFIG +++ b/usr/klibc/arch/ppc/MCONFIG @@ -20,7 +20,7 @@ KLIBCBITSIZE = 32 # calls, and work on the memory models for this architecture # 256-16 MB - normal binaries start at 256 MB, and jumps are limited # to +/- 16 MB -KLIBCSHAREDFLAGS = -Ttext-segment 0x0f800000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x0f800000 # The asm include files live in asm-powerpc KLIBCASMARCH = powerpc diff --git a/usr/klibc/arch/ppc64/MCONFIG b/usr/klibc/arch/ppc64/MCONFIG index 0315110ca379..442d452bf11b 100644 --- a/usr/klibc/arch/ppc64/MCONFIG +++ b/usr/klibc/arch/ppc64/MCONFIG @@ -18,7 +18,7 @@ KLIBCBITSIZE = 64 # calls, and work on the memory models for this architecture # 256-16 MB - normal binaries start at 256 MB, and jumps are limited # to +/- 16 MB -KLIBCSHAREDFLAGS = -Ttext-segment 0x0f000000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x0f000000 # The asm include files live in asm-powerpc KLIBCASMARCH = powerpc diff --git a/usr/klibc/arch/riscv64/MCONFIG b/usr/klibc/arch/riscv64/MCONFIG index 61681509d89d..2ea257548417 100644 --- a/usr/klibc/arch/riscv64/MCONFIG +++ b/usr/klibc/arch/riscv64/MCONFIG @@ -17,4 +17,4 @@ endif KLIBCBITSIZE = 64 # Normal binaries start at 64 KB, so start the libary at 2 MB. -KLIBCSHAREDFLAGS =-Ttext-segment 0x00200000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x00200000 diff --git a/usr/klibc/arch/sparc64/MCONFIG b/usr/klibc/arch/sparc64/MCONFIG index c83398559f84..767232fce80e 100644 --- a/usr/klibc/arch/sparc64/MCONFIG +++ b/usr/klibc/arch/sparc64/MCONFIG @@ -18,4 +18,4 @@ KLIBCLDFLAGS = -m elf64_sparc # calls, and work on the memory models for this architecture # Normal binaries start at 1 MB; the linker wants 1 MB alignment, # and call instructions have a 30-bit signed offset, << 2. -KLIBCSHAREDFLAGS = -Ttext-segment 0x80000000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x80000000 diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index 77e2ad279884..ac1ac074c00a 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -41,4 +41,4 @@ KLIBCLDFLAGS = -m elf_x86_64 # The old default was max-page-size=0x100000, but that also results # in a broken layout with binutils 2.30. Since there's no # architectural page size betwen 4 KB and 2MB, set it to 4 KB. -KLIBCSHAREDFLAGS = -Ttext-segment 0x00200000 -z max-page-size=0x1000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x00200000 -z max-page-size=0x1000 -- 2.26.0.rc2.310.g2932bb562d-goog
Clang doesn't have a suitable replacement for libgcc readily available. Supply one that we know exists. Use "-fno-builtin-bcmp" to prevent clang from calling a function that doesn't exist. Signed-off-by: Bill Wendling <morbo at google.com> --- scripts/Kbuild.klibc | 2 ++ usr/klibc/arch/x86_64/MCONFIG | 2 ++ 2 files changed, 4 insertions(+) diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc index 411d7ea9bea6..ba3f389626b3 100644 --- a/scripts/Kbuild.klibc +++ b/scripts/Kbuild.klibc @@ -141,6 +141,8 @@ KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note # Don't attempt to set it if we are using clang. ifneq ($(cc-name),clang) KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc) +else +KLIBCLIBGCC_DEF := $(KLIBCOBJ)/libc.a endif KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF) KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index ac1ac074c00a..a6ba7c85c68b 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -26,6 +26,8 @@ ifneq ($(cc-name),clang) # These options are not supported by clang but only result in a warning # so we can't use a cc-option test to filter them. KLIBCOPTFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 +else +KLIBCOPTFLAGS += -fno-builtin-bcmp endif KLIBCBITSIZE = 64 -- 2.26.0.rc2.310.g2932bb562d-goog
Bill Wendling
2020-Mar-27 22:12 UTC
[klibc] [PATCH 4/5] Kbuild: Add "-fcommon" for clang builds
Clang defaults to "-fno-common" which causes linking errors because of duplicate symbols in the BSS section. Signed-off-by: Bill Wendling <morbo at google.com> --- usr/klibc/arch/x86_64/MCONFIG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index a6ba7c85c68b..46d29985bfd6 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -27,7 +27,7 @@ ifneq ($(cc-name),clang) # so we can't use a cc-option test to filter them. KLIBCOPTFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 else -KLIBCOPTFLAGS += -fno-builtin-bcmp +KLIBCOPTFLAGS += -fno-builtin-bcmp -fcommon endif KLIBCBITSIZE = 64 -- 2.26.0.rc2.310.g2932bb562d-goog
- Convert the "old style" GNU field designator extension to the new style. - Use proper indexing into a string literal. - Use "%s" for the format string in "fmtstr". - Add or remove casts of the "void *" argument of ioctl. - Convert "if (!exitstatus == isor)" to "if ((!exitstatus) == isor)" which retains the current semantics, but may not be what the programmer intended. Signed-off-by: Bill Wendling <morbo at google.com> --- usr/dash/eval.c | 8 ++++---- usr/dash/jobs.c | 4 ++-- usr/dash/output.c | 12 ++++++------ usr/kinit/initrd.c | 2 +- usr/kinit/ramdisk_load.c | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/usr/dash/eval.c b/usr/dash/eval.c index ae83508ba160..2fa1a59995da 100644 --- a/usr/dash/eval.c +++ b/usr/dash/eval.c @@ -103,8 +103,8 @@ STATIC int bltincmd(int, char **); STATIC const struct builtincmd bltin = { - name: nullstr, - builtin: bltincmd + .name = nullstr, + .builtin = bltincmd }; @@ -274,7 +274,7 @@ checkexit: n->nbinary.ch1, (flags | ((isor >> 1) - 1)) & EV_TESTED ); - if (!exitstatus == isor) + if ((!exitstatus) == isor) break; if (!evalskip) { n = n->nbinary.ch2; @@ -1081,7 +1081,7 @@ eprintlist(struct output *out, struct strlist *sp, int sep) while (sp) { const char *p; - p = " %s" + (1 - sep); + p = &" %s"[1 - sep]; sep |= 1; outfmt(out, p, sp->text); sp = sp->next; diff --git a/usr/dash/jobs.c b/usr/dash/jobs.c index 009bbfeee47e..776983b708d6 100644 --- a/usr/dash/jobs.c +++ b/usr/dash/jobs.c @@ -426,7 +426,7 @@ sprint_status(char *s, int status, int sigonly) goto out; #endif } - col = fmtstr(s, 32, strsignal(st)); + col = fmtstr(s, 32, "%s", strsignal(st)); #ifdef WCOREDUMP if (WCOREDUMP(status)) { col += fmtstr(s + col, 16, " (core dumped)"); @@ -1394,7 +1394,7 @@ cmdputs(const char *s) str = "${"; goto dostr; case CTLENDVAR: - str = "\"}" + !(quoted & 1); + str = &"\"}"[!(quoted & 1)]; quoted >>= 1; subtype = 0; goto dostr; diff --git a/usr/dash/output.c b/usr/dash/output.c index f62e7eab0b4e..bb7c6ada155d 100644 --- a/usr/dash/output.c +++ b/usr/dash/output.c @@ -71,27 +71,27 @@ #ifdef USE_GLIBC_STDIO struct output output = { - stream: 0, nextc: 0, end: 0, buf: 0, bufsize: 0, fd: 1, flags: 0 + .stream = 0, .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = 1, .flags = 0 }; struct output errout = { - stream: 0, nextc: 0, end: 0, buf: 0, bufsize: 0, fd: 2, flags: 0 + .stream = 0, .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = 2, .flags = 0 } #ifdef notyet struct output memout = { - stream: 0, nextc: 0, end: 0, buf: 0, bufsize: 0, fd: MEM_OUT, flags: 0 + .stream = 0, .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = MEM_OUT, .flags = 0 }; #endif #else struct output output = { - nextc: 0, end: 0, buf: 0, bufsize: OUTBUFSIZ, fd: 1, flags: 0 + .nextc = 0, .end = 0, .buf = 0, .bufsize = OUTBUFSIZ, .fd = 1, .flags = 0 }; struct output errout = { - nextc: 0, end: 0, buf: 0, bufsize: 0, fd: 2, flags: 0 + .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = 2, .flags = 0 }; struct output preverrout; #ifdef notyet struct output memout = { - nextc: 0, end: 0, buf: 0, bufsize: 0, fd: MEM_OUT, flags: 0 + .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = MEM_OUT, .flags = 0 }; #endif #endif diff --git a/usr/kinit/initrd.c b/usr/kinit/initrd.c index 7eece2ce5488..5833f2f2c01f 100644 --- a/usr/kinit/initrd.c +++ b/usr/kinit/initrd.c @@ -153,7 +153,7 @@ static int run_linuxrc(int argc, char *argv[], dev_t root_dev) int olddev = open(ramdisk_name, O_RDWR); umount2("/old", MNT_DETACH); if (olddev < 0 || - ioctl(olddev, BLKFLSBUF, (long)0) || + ioctl(olddev, BLKFLSBUF, 0) || close(olddev)) { fprintf(stderr, "%s: Cannot flush initrd contents\n", diff --git a/usr/kinit/ramdisk_load.c b/usr/kinit/ramdisk_load.c index f43339c8fc74..e3e15d81f81b 100644 --- a/usr/kinit/ramdisk_load.c +++ b/usr/kinit/ramdisk_load.c @@ -34,7 +34,7 @@ static int change_disk(const char *devpath, int rfd, int disk) ioctl(rfd, CDROMEJECT, 0); } else { /* Non-ejectable floppy */ - ioctl(rfd, FDRESET, FD_RESET_IF_NEEDED); + ioctl(rfd, FDRESET, (void *)FD_RESET_IF_NEEDED); } } close(rfd); -- 2.26.0.rc2.310.g2932bb562d-goog
Update commit messages. Bill Wendling (3): [klibc] Kbuild: use "libc.a" with clang [klibc] Kbuild: Add "-fcommon" for clang builds [klibc] Clean up clang warnings Michael Davidson (1): [klibc] Kbuild: add support for clang builds Stanislav Fomichev (1): [klibc] Kbuild: support clang's lld Makefile | 3 +++ scripts/Kbuild.include | 4 ++++ scripts/Kbuild.klibc | 19 +++++++++++++++++-- usr/dash/eval.c | 8 ++++---- usr/dash/jobs.c | 4 ++-- usr/dash/output.c | 12 ++++++------ usr/kinit/initrd.c | 2 +- usr/kinit/ramdisk_load.c | 2 +- usr/klibc/arch/i386/MCONFIG | 2 +- usr/klibc/arch/mips64/MCONFIG | 2 +- usr/klibc/arch/ppc/MCONFIG | 2 +- usr/klibc/arch/ppc64/MCONFIG | 2 +- usr/klibc/arch/riscv64/MCONFIG | 2 +- usr/klibc/arch/sparc64/MCONFIG | 2 +- usr/klibc/arch/x86_64/MCONFIG | 14 +++++++++++--- 15 files changed, 55 insertions(+), 25 deletions(-) -- 2.26.0.rc2.310.g2932bb562d-goog
Bill Wendling
2020-Mar-27 22:29 UTC
[klibc] [PATCH v2 1/5] Kbuild: add support for clang builds
From: Michael Davidson <md at google.com> Add cc-name to klibc/scripts/Kbuild.include. Make optimization flags not supported by clang conditional on cc-name. Don't use -nostdinc when building klibc. While klibc doesn't (shouldn't) use the standard header files supplied by the toolchain, it does still need to be able to find the compiler-specific header file <stdarg.h>. With gcc this can be achieved by specifying "-iwithprefix include" in addition to "-nostdinc". clang behaves differently and the easiest work around is to simply not specify "-nostdinc" and rely on the fact that the various include paths have been specified correctly so that the only header file actually pulled in from the standard location is <stdarg.h>. Disable the use of --print-libgcc when using clang. Explictly specifying the path to compiler specific libraries appears to be unnecessary since klibc never specifies -nostdlib when linking binaries, meaning that any such libraries should already be available. Signed-off-by: Bill Wendling <morbo at google.com> --- scripts/Kbuild.include | 4 ++++ scripts/Kbuild.klibc | 17 +++++++++++++++-- usr/klibc/arch/x86_64/MCONFIG | 10 ++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index a048ec7a4c89..de31a0c5efa5 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -127,6 +127,10 @@ cc-option-align = $(subst -functions=0,,\ cc-disable-warning = $(call try-run,\ $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1))) +# cc-name +# Expands to either gcc or clang +cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc) + # cc-version # Usage gcc-ver := $(call cc-version) cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC)) diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc index afc9a546feca..411d7ea9bea6 100644 --- a/scripts/Kbuild.klibc +++ b/scripts/Kbuild.klibc @@ -107,8 +107,15 @@ KLIBCOBJCOPY := $(OBJCOPY) KLIBCOBJDUMP := $(OBJDUMP) # klibc include paths -KLIBCCPPFLAGS := -nostdinc -iwithprefix include \ - -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \ +ifeq ($(cc-name),clang) +# clang can't find the compiler-specific header <stdarg.h> if -nostdinc +# is specified so we have to omit it along with the -iwithprefix option. +# See b/35394554. +KLIBCCPPFLAGS :+else +KLIBCCPPFLAGS := -nostdinc -iwithprefix include +endif +KLIBCCPPFLAGS += -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \ -I$(KLIBCINC)/bits$(KLIBCBITSIZE) \ -I$(KLIBCOBJ)/../include \ -I$(KLIBCINC) @@ -128,7 +135,13 @@ KLIBCCFLAGS += $(KLIBCCPPFLAGS) $(KLIBCREQFLAGS) $(KLIBCARCHREQFLAGS) \ KLIBCAFLAGS += -D__ASSEMBLY__ -Wa,--noexecstack $(KLIBCCFLAGS) KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note +# KLIBCLIBGCC_DEF appears to be unnecessary since klibc never uses -nostdlib +# when linking binaries, which means that any compiler specific libraries +# should be available automatically, no need to speciy an explicit path. +# Don't attempt to set it if we are using clang. +ifneq ($(cc-name),clang) KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc) +endif KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF) KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o KLIBCLIBC := $(KLIBCOBJ)/libc.a diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index c5f2fa265a26..77e2ad279884 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -15,13 +15,19 @@ # debugging using gdb. # KLIBCARCHREQFLAGS = -m64 -KLIBCOPTFLAGS += -Os -fomit-frame-pointer -mno-sse \ - -falign-functions=1 -falign-jumps=1 -falign-loops=1 +KLIBCOPTFLAGS += -Os -fomit-frame-pointer -mno-sse ifeq ($(DEBUG),y) KLIBCOPTFLAGS += -g else KLIBCOPTFLAGS += -fno-asynchronous-unwind-tables endif + +ifneq ($(cc-name),clang) +# These options are not supported by clang but only result in a warning +# so we can't use a cc-option test to filter them. +KLIBCOPTFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 +endif + KLIBCBITSIZE = 64 KLIBCLDFLAGS = -m elf_x86_64 -- 2.26.0.rc2.310.g2932bb562d-goog
From: Stanislav Fomichev <sdf at google.com> Clang's lld prefers the use of "-image-base" instead of "-Ttext-segment". Signed-off-by: Stanislav Fomichev <sdf at google.com> Signed-off-by: Bill Wendling <morbo at google.com> --- Makefile | 3 +++ usr/klibc/arch/i386/MCONFIG | 2 +- usr/klibc/arch/mips64/MCONFIG | 2 +- usr/klibc/arch/ppc/MCONFIG | 2 +- usr/klibc/arch/ppc64/MCONFIG | 2 +- usr/klibc/arch/riscv64/MCONFIG | 2 +- usr/klibc/arch/sparc64/MCONFIG | 2 +- usr/klibc/arch/x86_64/MCONFIG | 2 +- 8 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c99b962fd2a3..52d7cd2cf791 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ export NM := $(KLIBCROSS)nm export OBJCOPY := $(KLIBCROSS)objcopy export OBJDUMP := $(KLIBCROSS)objdump +LLD := $(shell $(LD) --version 2>&1 | grep LLD) +export IMAGE_BASE=$(if $(LLD),--image-base,-Ttext-segment) + NOSTDINC_FLAGS := -nostdlib -nostdinc -isystem $(shell $(CC) -print-file-name=include) ARCH := $(shell uname -m | sed -e s/i.86/i386/ \ diff --git a/usr/klibc/arch/i386/MCONFIG b/usr/klibc/arch/i386/MCONFIG index 0704669500f0..baa7e59854f8 100644 --- a/usr/klibc/arch/i386/MCONFIG +++ b/usr/klibc/arch/i386/MCONFIG @@ -30,4 +30,4 @@ KLIBCBITSIZE = 32 # This address needs to be reachable using normal inter-module # calls, and work on the memory models for this architecture # 96 MB - normal binaries start at 128 MB -KLIBCSHAREDFLAGS = -Ttext-segment 0x06000000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x06000000 diff --git a/usr/klibc/arch/mips64/MCONFIG b/usr/klibc/arch/mips64/MCONFIG index 6a4b41b2f85c..3d7f20da08be 100644 --- a/usr/klibc/arch/mips64/MCONFIG +++ b/usr/klibc/arch/mips64/MCONFIG @@ -20,4 +20,4 @@ KLIBCBITSIZE = 64 # load address, use the very top of the 256M region (minus 2MB) # # Use -Ttext-segment so that the special .MIPS* sections are moved as well. -KLIBCSHAREDFLAGS = -Ttext-segment 0x12FE00000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x12FE00000 diff --git a/usr/klibc/arch/ppc/MCONFIG b/usr/klibc/arch/ppc/MCONFIG index 8220f6b93fa4..ec859e8941d9 100644 --- a/usr/klibc/arch/ppc/MCONFIG +++ b/usr/klibc/arch/ppc/MCONFIG @@ -20,7 +20,7 @@ KLIBCBITSIZE = 32 # calls, and work on the memory models for this architecture # 256-16 MB - normal binaries start at 256 MB, and jumps are limited # to +/- 16 MB -KLIBCSHAREDFLAGS = -Ttext-segment 0x0f800000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x0f800000 # The asm include files live in asm-powerpc KLIBCASMARCH = powerpc diff --git a/usr/klibc/arch/ppc64/MCONFIG b/usr/klibc/arch/ppc64/MCONFIG index 0315110ca379..442d452bf11b 100644 --- a/usr/klibc/arch/ppc64/MCONFIG +++ b/usr/klibc/arch/ppc64/MCONFIG @@ -18,7 +18,7 @@ KLIBCBITSIZE = 64 # calls, and work on the memory models for this architecture # 256-16 MB - normal binaries start at 256 MB, and jumps are limited # to +/- 16 MB -KLIBCSHAREDFLAGS = -Ttext-segment 0x0f000000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x0f000000 # The asm include files live in asm-powerpc KLIBCASMARCH = powerpc diff --git a/usr/klibc/arch/riscv64/MCONFIG b/usr/klibc/arch/riscv64/MCONFIG index 61681509d89d..2ea257548417 100644 --- a/usr/klibc/arch/riscv64/MCONFIG +++ b/usr/klibc/arch/riscv64/MCONFIG @@ -17,4 +17,4 @@ endif KLIBCBITSIZE = 64 # Normal binaries start at 64 KB, so start the libary at 2 MB. -KLIBCSHAREDFLAGS =-Ttext-segment 0x00200000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x00200000 diff --git a/usr/klibc/arch/sparc64/MCONFIG b/usr/klibc/arch/sparc64/MCONFIG index c83398559f84..767232fce80e 100644 --- a/usr/klibc/arch/sparc64/MCONFIG +++ b/usr/klibc/arch/sparc64/MCONFIG @@ -18,4 +18,4 @@ KLIBCLDFLAGS = -m elf64_sparc # calls, and work on the memory models for this architecture # Normal binaries start at 1 MB; the linker wants 1 MB alignment, # and call instructions have a 30-bit signed offset, << 2. -KLIBCSHAREDFLAGS = -Ttext-segment 0x80000000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x80000000 diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index 77e2ad279884..ac1ac074c00a 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -41,4 +41,4 @@ KLIBCLDFLAGS = -m elf_x86_64 # The old default was max-page-size=0x100000, but that also results # in a broken layout with binutils 2.30. Since there's no # architectural page size betwen 4 KB and 2MB, set it to 4 KB. -KLIBCSHAREDFLAGS = -Ttext-segment 0x00200000 -z max-page-size=0x1000 +KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x00200000 -z max-page-size=0x1000 -- 2.26.0.rc2.310.g2932bb562d-goog
Bill Wendling
2020-Mar-27 22:29 UTC
[klibc] [PATCH v2 3/5] Kbuild: use "libc.a" with clang
Clang doesn't have a suitable replacement for libgcc readily available. Supply one that we know exists. Use "-fno-builtin-bcmp" to prevent clang from calling a function that doesn't exist. Signed-off-by: Bill Wendling <morbo at google.com> --- scripts/Kbuild.klibc | 2 ++ usr/klibc/arch/x86_64/MCONFIG | 2 ++ 2 files changed, 4 insertions(+) diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc index 411d7ea9bea6..ba3f389626b3 100644 --- a/scripts/Kbuild.klibc +++ b/scripts/Kbuild.klibc @@ -141,6 +141,8 @@ KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note # Don't attempt to set it if we are using clang. ifneq ($(cc-name),clang) KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc) +else +KLIBCLIBGCC_DEF := $(KLIBCOBJ)/libc.a endif KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF) KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index ac1ac074c00a..a6ba7c85c68b 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -26,6 +26,8 @@ ifneq ($(cc-name),clang) # These options are not supported by clang but only result in a warning # so we can't use a cc-option test to filter them. KLIBCOPTFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 +else +KLIBCOPTFLAGS += -fno-builtin-bcmp endif KLIBCBITSIZE = 64 -- 2.26.0.rc2.310.g2932bb562d-goog
Bill Wendling
2020-Mar-27 22:29 UTC
[klibc] [PATCH v2 4/5] Kbuild: Add "-fcommon" for clang builds
Clang defaults to "-fno-common" which causes linking errors because of duplicate symbols in the BSS section. Signed-off-by: Bill Wendling <morbo at google.com> --- usr/klibc/arch/x86_64/MCONFIG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG index a6ba7c85c68b..46d29985bfd6 100644 --- a/usr/klibc/arch/x86_64/MCONFIG +++ b/usr/klibc/arch/x86_64/MCONFIG @@ -27,7 +27,7 @@ ifneq ($(cc-name),clang) # so we can't use a cc-option test to filter them. KLIBCOPTFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 else -KLIBCOPTFLAGS += -fno-builtin-bcmp +KLIBCOPTFLAGS += -fno-builtin-bcmp -fcommon endif KLIBCBITSIZE = 64 -- 2.26.0.rc2.310.g2932bb562d-goog
- Convert the "old style" GNU field designator extension to the new style. - Use proper indexing into a string literal. - Use "%s" for the format string in "fmtstr". - Add or remove casts of the "void *" argument of ioctl. - Convert "if (!exitstatus == isor)" to "if ((!exitstatus) == isor)" which retains the current semantics, but may not be what the programmer intended. Signed-off-by: Bill Wendling <morbo at google.com> --- usr/dash/eval.c | 8 ++++---- usr/dash/jobs.c | 4 ++-- usr/dash/output.c | 12 ++++++------ usr/kinit/initrd.c | 2 +- usr/kinit/ramdisk_load.c | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/usr/dash/eval.c b/usr/dash/eval.c index ae83508ba160..2fa1a59995da 100644 --- a/usr/dash/eval.c +++ b/usr/dash/eval.c @@ -103,8 +103,8 @@ STATIC int bltincmd(int, char **); STATIC const struct builtincmd bltin = { - name: nullstr, - builtin: bltincmd + .name = nullstr, + .builtin = bltincmd }; @@ -274,7 +274,7 @@ checkexit: n->nbinary.ch1, (flags | ((isor >> 1) - 1)) & EV_TESTED ); - if (!exitstatus == isor) + if ((!exitstatus) == isor) break; if (!evalskip) { n = n->nbinary.ch2; @@ -1081,7 +1081,7 @@ eprintlist(struct output *out, struct strlist *sp, int sep) while (sp) { const char *p; - p = " %s" + (1 - sep); + p = &" %s"[1 - sep]; sep |= 1; outfmt(out, p, sp->text); sp = sp->next; diff --git a/usr/dash/jobs.c b/usr/dash/jobs.c index 009bbfeee47e..776983b708d6 100644 --- a/usr/dash/jobs.c +++ b/usr/dash/jobs.c @@ -426,7 +426,7 @@ sprint_status(char *s, int status, int sigonly) goto out; #endif } - col = fmtstr(s, 32, strsignal(st)); + col = fmtstr(s, 32, "%s", strsignal(st)); #ifdef WCOREDUMP if (WCOREDUMP(status)) { col += fmtstr(s + col, 16, " (core dumped)"); @@ -1394,7 +1394,7 @@ cmdputs(const char *s) str = "${"; goto dostr; case CTLENDVAR: - str = "\"}" + !(quoted & 1); + str = &"\"}"[!(quoted & 1)]; quoted >>= 1; subtype = 0; goto dostr; diff --git a/usr/dash/output.c b/usr/dash/output.c index f62e7eab0b4e..bb7c6ada155d 100644 --- a/usr/dash/output.c +++ b/usr/dash/output.c @@ -71,27 +71,27 @@ #ifdef USE_GLIBC_STDIO struct output output = { - stream: 0, nextc: 0, end: 0, buf: 0, bufsize: 0, fd: 1, flags: 0 + .stream = 0, .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = 1, .flags = 0 }; struct output errout = { - stream: 0, nextc: 0, end: 0, buf: 0, bufsize: 0, fd: 2, flags: 0 + .stream = 0, .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = 2, .flags = 0 } #ifdef notyet struct output memout = { - stream: 0, nextc: 0, end: 0, buf: 0, bufsize: 0, fd: MEM_OUT, flags: 0 + .stream = 0, .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = MEM_OUT, .flags = 0 }; #endif #else struct output output = { - nextc: 0, end: 0, buf: 0, bufsize: OUTBUFSIZ, fd: 1, flags: 0 + .nextc = 0, .end = 0, .buf = 0, .bufsize = OUTBUFSIZ, .fd = 1, .flags = 0 }; struct output errout = { - nextc: 0, end: 0, buf: 0, bufsize: 0, fd: 2, flags: 0 + .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = 2, .flags = 0 }; struct output preverrout; #ifdef notyet struct output memout = { - nextc: 0, end: 0, buf: 0, bufsize: 0, fd: MEM_OUT, flags: 0 + .nextc = 0, .end = 0, .buf = 0, .bufsize = 0, .fd = MEM_OUT, .flags = 0 }; #endif #endif diff --git a/usr/kinit/initrd.c b/usr/kinit/initrd.c index 7eece2ce5488..5833f2f2c01f 100644 --- a/usr/kinit/initrd.c +++ b/usr/kinit/initrd.c @@ -153,7 +153,7 @@ static int run_linuxrc(int argc, char *argv[], dev_t root_dev) int olddev = open(ramdisk_name, O_RDWR); umount2("/old", MNT_DETACH); if (olddev < 0 || - ioctl(olddev, BLKFLSBUF, (long)0) || + ioctl(olddev, BLKFLSBUF, 0) || close(olddev)) { fprintf(stderr, "%s: Cannot flush initrd contents\n", diff --git a/usr/kinit/ramdisk_load.c b/usr/kinit/ramdisk_load.c index f43339c8fc74..e3e15d81f81b 100644 --- a/usr/kinit/ramdisk_load.c +++ b/usr/kinit/ramdisk_load.c @@ -34,7 +34,7 @@ static int change_disk(const char *devpath, int rfd, int disk) ioctl(rfd, CDROMEJECT, 0); } else { /* Non-ejectable floppy */ - ioctl(rfd, FDRESET, FD_RESET_IF_NEEDED); + ioctl(rfd, FDRESET, (void *)FD_RESET_IF_NEEDED); } } close(rfd); -- 2.26.0.rc2.310.g2932bb562d-goog
Bill Wendling
2020-Mar-29 11:38 UTC
[klibc] [kvm-unit-tests PATCH v3 0/4] Update patch set
- Renamed IMAGE_BASE to LD_IMAGE_BASE_OPT. - Moved "-fcommon" to KLIBCREQFLAGS in scripts/Kbuild.klibc. - Remove "dash" warning fixes which have been upstreamed. - Conditionalize the inclusion of compiler flags by using the proper compiler name or "cc-option". - Added "-Werror" to "cc-option" to catch flags that cause warnings. - Retain "-nostdinc -iwithprefix include" and use the proper clang flag to get the path to find "stdarg.h". - Move "-fno-builtin-bcmp" to KLIBCREQFLAGS in scripts/Kbuild.klibc. Bill Wendling (4): [klibc] Kbuild: add support for clang builds [klibc] Kbuild: support clang's lld [klibc] Kbuild: Add "-fcommon" for clang builds [klibc] Clean up clang warnings Makefile | 3 +++ scripts/Kbuild.include | 8 ++++++-- scripts/Kbuild.klibc | 14 ++++++++++---- usr/kinit/initrd.c | 2 +- usr/kinit/ramdisk_load.c | 2 +- usr/klibc/Kbuild | 4 ++-- usr/klibc/arch/i386/MCONFIG | 2 +- usr/klibc/arch/mips64/MCONFIG | 5 +++-- usr/klibc/arch/ppc/MCONFIG | 2 +- usr/klibc/arch/ppc64/MCONFIG | 2 +- usr/klibc/arch/riscv64/MCONFIG | 2 +- usr/klibc/arch/sparc64/MCONFIG | 2 +- usr/klibc/arch/x86_64/MCONFIG | 8 +++++--- 13 files changed, 36 insertions(+), 20 deletions(-) -- 2.26.0.rc2.310.g2932bb562d-goog
Reasonably Related Threads
- [PATCH 0/5] Clang compatibility patches
- [PATCH v2 1/5] Kbuild: add support for clang builds
- [PATCH v2 3/5] Kbuild: use "libc.a" with clang
- [PATCH klibc 1/3] Revert " Kbuild: Tell gas we don't want executable stacks"
- [PATCH klibc 0/4] Fix the KLIBCKERNEL{SRC,OBJ} mess