This is perfered way to get correct LD and AS flags set, as well as preprocessor definitions on cross builds.
Also mandatory for potential clang build. Signed-off-by: Jiaxun Yang <jiaxun.y...@flygoat.com> --- Makefile | 35 +++++++++++++++++------------------ scripts/test-build.sh | 12 ++++++------ 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 1e03a0e5af9a974b0c8c0c8c95269b7635737315..31579992da6f045dbc4a1bc1143ef7ff553127b9 100644 --- a/Makefile +++ b/Makefile @@ -14,16 +14,12 @@ export KCONFIG_AUTOHEADER := autoconf.h export KCONFIG_CONFIG := $(CURDIR)/.config export LC_ALL := C -CROSS_PREFIX := +CROSS_PREFIX := CC=$(CROSS_PREFIX)gcc -AS=$(CROSS_PREFIX)as -LD=$(CROSS_PREFIX)ld OBJCOPY=$(CROSS_PREFIX)objcopy OBJDUMP=$(CROSS_PREFIX)objdump STRIP=$(CROSS_PREFIX)strip PYTHON=python -CPP=cpp -LD32BIT_FLAG:=-melf_i386 # Source files SRCBOTH=misc.c stacks.c output.c string.c block.c cdrom.c disk.c \ @@ -62,9 +58,10 @@ COMMONCFLAGS := -I$(OUT) -Isrc -Os -MD -g \ -m32 -march=i386 -mregparm=3 -mpreferred-stack-boundary=2 \ -minline-all-stringops -fomit-frame-pointer \ -freg-struct-return -ffreestanding -fno-delete-null-pointer-checks \ - -ffunction-sections -fdata-sections -fno-common -fno-merge-constants -COMMONCFLAGS += $(call cc-option,$(CC),-nopie,) + -ffunction-sections -fdata-sections -fno-common -static -nostdlib \ + -fno-merge-constants COMMONCFLAGS += $(call cc-option,$(CC),-fno-pie,) +COMMONCFLAGS += $(call cc-option,$(CC),-nopie,) COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,) COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,) COMMONCFLAGS += $(call cc-option,$(CC),-fstack-check=no,) @@ -81,6 +78,8 @@ CFLAGS32SEG := $(CFLAGSSEG) -DMODE16=0 CFLAGS16 := $(CFLAGSSEG) -DMODE16=1 \ $(call cc-option,$(CC),-m16,-Wa$(COMMA)src/code16gcc.s) \ $(call cc-option,$(CC),--param large-stack-frame=4,-fno-inline) +LD32BIT_FLAG:= $(COMMONCFLAGS) -Wl,-melf_i386 \ + $(call cc-option,$(CC),-Wl$(COMMA)--no-warn-rwx-segments) # Run with "make V=1" to see the actual compile commands ifdef V @@ -109,7 +108,8 @@ all: $(target-y) ################ Common build rules # Verify the build environment works. -TESTGCC:=$(shell OUT="$(OUT)" CC="$(CC)" LD="$(LD)" scripts/test-build.sh) +TESTGCC:=$(shell OUT="$(OUT)" CC="$(CC)" CFLAGS16="$(CFLAGS16)" \ + LD32BIT_FLAG="$(LD32BIT_FLAG)" scripts/test-build.sh) ifeq "$(TESTGCC)" "-1" $(error "Please upgrade the build environment") endif @@ -141,8 +141,7 @@ $(OUT)%.o: %.c $(OUT)autoconf.h $(OUT)%.lds: %.lds.S @echo " Precompiling $@" - $(Q)$(CPP) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $@ - + $(Q)$(CC) -E $(COMMONCFLAGS) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $@ ################ Main BIOS build rules @@ -164,10 +163,10 @@ $(OUT)romlayout.o: src/romlayout.S $(OUT)autoconf.h $(OUT)asm-offsets.h $(OUT)romlayout16.lds: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)ccode16.o $(OUT)romlayout.o src/version.c scripts/layoutrom.py scripts/buildversion.py @echo " Building ld scripts" - $(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autoversion.h + $(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autoversion.h $(Q)$(CC) $(CFLAGS32FLAT) -c src/version.c -o $(OUT)version.o - $(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o - $(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode16.o $(OUT)romlayout.o -o $(OUT)code16.o + $(Q)$(CC) $(LD32BIT_FLAG) -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o + $(Q)$(CC) $(LD32BIT_FLAG) -r $(OUT)ccode16.o $(OUT)romlayout.o -o $(OUT)code16.o $(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump $(Q)$(OBJDUMP) -thr $(OUT)code32seg.o > $(OUT)code32seg.o.objdump $(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump @@ -179,15 +178,15 @@ $(OUT)romlayout32seg.lds $(OUT)romlayout32flat.lds $(OUT)code32flat.o $(OUT)code $(OUT)rom16.o: $(OUT)code16.o $(OUT)romlayout16.lds @echo " Linking $@" - $(Q)$(LD) -T $(OUT)romlayout16.lds $< -o $@ + $(Q)$(CC) $(LD32BIT_FLAG) -T $(OUT)romlayout16.lds $< -o $@ $(OUT)rom32seg.o: $(OUT)code32seg.o $(OUT)romlayout32seg.lds @echo " Linking $@" - $(Q)$(LD) -T $(OUT)romlayout32seg.lds $< -o $@ + $(Q)$(CC) $(LD32BIT_FLAG) -T $(OUT)romlayout32seg.lds $< -o $@ $(OUT)rom.o: $(OUT)rom16.noexec.o $(OUT)rom32seg.noexec.o $(OUT)code32flat.o $(OUT)romlayout32flat.lds @echo " Linking $@" - $(Q)$(LD) -N -T $(OUT)romlayout32flat.lds $(OUT)rom16.noexec.o $(OUT)rom32seg.noexec.o $(OUT)code32flat.o -o $@ + $(Q)$(CC) $(LD32BIT_FLAG) -Wl,-N -T $(OUT)romlayout32flat.lds $(OUT)rom16.noexec.o $(OUT)rom32seg.noexec.o $(OUT)code32flat.o -o $@ $(OUT)bios.bin.prep: $(OUT)rom.o scripts/checkrom.py @echo " Prepping $@" @@ -225,7 +224,7 @@ $(OUT)vgaccode16.raw.s: $(OUT)autoconf.h $(patsubst %.c, $(OUT)%.o,$(SRCVGA)) ; $(OUT)vgaccode16.o: $(OUT)vgaccode16.raw.s scripts/vgafixup.py @echo " Fixup VGA rom assembler" $(Q)$(PYTHON) ./scripts/vgafixup.py $< $(OUT)vgaccode16.s - $(Q)$(AS) --32 src/code16gcc.s $(OUT)vgaccode16.s -o $@ + $(Q)$(CC) -c $(CFLAGS16) $(OUT)vgaccode16.s -o $@ else $(OUT)vgaccode16.o: $(OUT)autoconf.h $(patsubst %.c, $(OUT)%.o,$(SRCVGA)) ; $(call whole-compile, $(CFLAGS16) -Isrc, $(SRCVGA),$@) endif @@ -238,7 +237,7 @@ $(OUT)vgarom.o: $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgasrc/vgalayout.lds v @echo " Linking $@" $(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" -t "$(CC);$(AS);$(LD);$(OBJCOPY);$(OBJDUMP);$(STRIP)" $(OUT)autovgaversion.h $(Q)$(CC) $(CFLAGS16) -c vgasrc/vgaversion.c -o $(OUT)vgaversion.o - $(Q)$(LD) --gc-sections -T $(OUT)vgasrc/vgalayout.lds $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgaversion.o -o $@ + $(Q)$(CC) $(LD32BIT_FLAG) -Wl,--gc-sections -T $(OUT)vgasrc/vgalayout.lds $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgaversion.o -o $@ $(OUT)vgabios.bin.raw: $(OUT)vgarom.o @echo " Extracting binary $@" diff --git a/scripts/test-build.sh b/scripts/test-build.sh index 25cc2f20ef85da4e9fb0d7c6aedd3290a86c5b60..2aba2b0ad1976bec30d74bbbeff68fe0ee16f38e 100755 --- a/scripts/test-build.sh +++ b/scripts/test-build.sh @@ -27,7 +27,7 @@ SECTIONS } } EOF -$CC -O -g -c $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1 +$CC $CFLAGS16 -O -g -c $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "Unable to execute the C compiler ($CC)." >&2 echo "" >&2 @@ -35,7 +35,7 @@ if [ $? -ne 0 ]; then echo -1 exit 0 fi -$LD -T $TMPFILE1_ld $TMPFILE1o -o $TMPFILE2o > /dev/null 2>&1 +$CC $LD32BIT_FLAG -v -T $TMPFILE1_ld $TMPFILE1o -o $TMPFILE2o > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "The version of LD on this system ($LD) does not properly handle" >&2 echo "alignments. As a result, this project can not be built." >&2 @@ -50,7 +50,7 @@ fi # Test for "-fwhole-program". Older versions of gcc (pre v4.1) don't # support the whole-program optimization - detect that. -$CC -fwhole-program -S -o /dev/null -xc /dev/null > /dev/null 2>&1 +$CC -m32 -fwhole-program -S -o /dev/null -xc /dev/null > /dev/null 2>&1 if [ $? -ne 0 ]; then echo " Working around no -fwhole-program" >&2 echo 2 @@ -67,14 +67,14 @@ void __attribute__((externally_visible)) t1() { } extern unsigned char v1; unsigned char v1 __attribute__((section(".data16.foo.19"))) __attribute__((externally_visible)); EOF -$CC -Os -c -fwhole-program $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1 +$CC -m32 -Os -c -fwhole-program $TMPFILE1 -o $TMPFILE1o > /dev/null 2>&1 cat - > $TMPFILE2 <<EOF void t1(); extern unsigned char v1; int __attribute__((externally_visible)) main() { t1(); return v1; } EOF -$CC -Os -c -fwhole-program $TMPFILE2 -o $TMPFILE2o > /dev/null 2>&1 -$CC -nostdlib -Os $TMPFILE1o $TMPFILE2o -o $TMPFILE3o > /dev/null 2>&1 +$CC -m32 -Os -c -fwhole-program $TMPFILE2 -o $TMPFILE2o > /dev/null 2>&1 +$CC ${LD32BIT_FLAG} -Os $TMPFILE1o $TMPFILE2o -o $TMPFILE3o > /dev/null 2>&1 if [ $? -ne 0 ]; then echo " Working around non-functional -fwhole-program" >&2 echo 2 -- Git-154) _______________________________________________ SeaBIOS mailing list -- seabios@seabios.org To unsubscribe send an email to seabios-le...@seabios.org