From fe6007b25867cf3674985949b0337bd75a0f6600 Mon Sep 17 00:00:00 2001 From: Pedro Falcato Date: Wed, 13 Apr 2022 05:07:49 +0100 Subject: [PATCH] [build] Use CC as a linker Signed-off-by: Pedro Falcato --- arch/arm/rules.mk | 2 +- arch/arm/toolchain.mk | 2 +- arch/arm64/rules.mk | 2 +- arch/microblaze/rules.mk | 26 ++++++++++++++++++++------ arch/mips/rules.mk | 4 ++-- arch/or1k/rules.mk | 2 +- arch/riscv/rules.mk | 6 +++--- arch/x86/rules.mk | 2 +- engine.mk | 2 +- make/build.mk | 11 ++++++++--- 10 files changed, 39 insertions(+), 20 deletions(-) diff --git a/arch/arm/rules.mk b/arch/arm/rules.mk index 027e5e018a..58a74575cc 100644 --- a/arch/arm/rules.mk +++ b/arch/arm/rules.mk @@ -325,7 +325,7 @@ ARCH_COMPILEFLAGS += $(ARCH_$(ARCH)_COMPILEFLAGS) GLOBAL_COMPILEFLAGS += $(THUMBINTERWORK) # set the max page size to something more reasonable (defaults to 64K or above) -ARCH_LDFLAGS += -z max-page-size=4096 +ARCH_LDFLAGS += -Wl,-z,max-page-size=4096 # find the direct path to libgcc.a for our particular multilib variant LIBGCC := $(shell $(TOOLCHAIN_PREFIX)gcc $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) $(THUMBCFLAGS) -print-libgcc-file-name) diff --git a/arch/arm/toolchain.mk b/arch/arm/toolchain.mk index dcab37ee14..af6ca85bc4 100644 --- a/arch/arm/toolchain.mk +++ b/arch/arm/toolchain.mk @@ -111,7 +111,7 @@ ARCH_arm_COMPILEFLAGS += -march=armv7-r ARCH_arm_COMPILEFLAGS += -mcpu=$(ARM_CPU) ARCH_arm_COMPILEFLAGS += -mbig-endian ARCH_arm_COMPILEFLAGS += -mfpu=vfpv3-d16 -mfloat-abi=hard -GLOBAL_MODULE_LDFLAGS += -EB +GLOBAL_MODULE_LDFLAGS += -Wl,-EB endif ifeq ($(ARM_CPU),armemu) ARCH_arm_COMPILEFLAGS += -march=armv7-a diff --git a/arch/arm64/rules.mk b/arch/arm64/rules.mk index 7d7f1cc5b4..b75b1a4c2d 100644 --- a/arch/arm64/rules.mk +++ b/arch/arm64/rules.mk @@ -94,7 +94,7 @@ $(info TOOLCHAIN_PREFIX = $(TOOLCHAIN_PREFIX)) ARCH_COMPILEFLAGS += $(ARCH_$(ARCH)_COMPILEFLAGS) ARCH_COMPILEFLAGS += -fno-omit-frame-pointer -ARCH_LDFLAGS += -z max-page-size=4096 +ARCH_LDFLAGS += -Wl,-z,max-page-size=4096 LIBGCC := $(shell $(TOOLCHAIN_PREFIX)gcc $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) -print-libgcc-file-name) diff --git a/arch/microblaze/rules.mk b/arch/microblaze/rules.mk index bd8e6a12d3..e4ca19f574 100644 --- a/arch/microblaze/rules.mk +++ b/arch/microblaze/rules.mk @@ -30,8 +30,8 @@ LITTLE_ENDIAN ?= 0 ifneq ($(LITTLE_ENDIAN),0) ARCH_COMPILEFLAGS += -mlittle-endian -ARCH_LDFLAGS += -EL -GLOBAL_MODULE_LDFLAGS += -EL +ARCH_LDFLAGS += -Wl,-EL +GLOBAL_MODULE_LDFLAGS += -Wl,-EL endif LIBGCC := $(shell $(TOOLCHAIN_PREFIX)gcc $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) $(GLOBAL_COMPILEFLAGS) -print-libgcc-file-name) @@ -42,7 +42,9 @@ cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`"; ARCH_OPTFLAGS := -O2 -ARCH_LDFLAGS += -relax +# -L$(BUILDDIR) is needed so the linker can find the xilinx.ld it wants +ARCH_LDFLAGS += -Wl,-relax -L$(BUILDDIR) +GLOBAL_MODULE_LDFLAGS += -L$(BUILDDIR) KERNEL_BASE ?= $(MEMBASE) KERNEL_LOAD_OFFSET ?= 0 @@ -54,9 +56,11 @@ GLOBAL_DEFINES += \ # potentially generated files that should be cleaned out with clean make rule GENERATED += \ - $(BUILDDIR)/linker.ld + $(BUILDDIR)/linker.ld \ + $(BUILDDIR)/xilinx.ld + +# Rules for generating the linker scripts -# rules for generating the linker $(BUILDDIR)/linker.ld: $(LOCAL_DIR)/linker.ld $(wildcard arch/*.ld) linkerscript.phony @echo generating $@ @$(MKDIR) @@ -66,6 +70,16 @@ $(BUILDDIR)/linker.ld: $(LOCAL_DIR)/linker.ld $(wildcard arch/*.ld) linkerscript linkerscript.phony: .PHONY: linkerscript.phony -LINKER_SCRIPT += $(BUILDDIR)/linker.ld +# Note: This is all messy and horrible +# GCC needs to find a xilinx.ld, we usually pass the LINKER_SCRIPT as -dT (default linker script), and so +# does GCC when it can't find a -T option in the command line +# Because of that, we pass the actual linker script as linker.ld + +$(BUILDDIR)/xilinx.ld: + $(NOECHO)touch $@ + +EXTRA_LINKER_SCRIPTS += $(BUILDDIR)/linker.ld + +LINKER_SCRIPT += $(BUILDDIR)/xilinx.ld include make/module.mk diff --git a/arch/mips/rules.mk b/arch/mips/rules.mk index 49ab0c3076..337c7bcfd2 100644 --- a/arch/mips/rules.mk +++ b/arch/mips/rules.mk @@ -32,8 +32,8 @@ LITTLE_ENDIAN ?= 0 ifneq ($(LITTLE_ENDIAN),0) ARCH_COMPILEFLAGS += -EL ARCH_ASFLAGS += -EL -ARCH_LDFLAGS += -EL -GLOBAL_MODULE_LDFLAGS += -EL +ARCH_LDFLAGS += -Wl,-EL +GLOBAL_MODULE_LDFLAGS += -Wl,-EL endif ARCH_COMPILEFLAGS += -mno-gpopt diff --git a/arch/or1k/rules.mk b/arch/or1k/rules.mk index dc8340702b..106b066218 100644 --- a/arch/or1k/rules.mk +++ b/arch/or1k/rules.mk @@ -25,7 +25,7 @@ cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`"; ARCH_OPTFLAGS := -O2 -ARCH_LDFLAGS += -relax +ARCH_LDFLAGS += -Wl,-relax LIBGCC := $(shell $(TOOLCHAIN_PREFIX)gcc $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) -print-libgcc-file-name) $(info LIBGCC = $(LIBGCC)) diff --git a/arch/riscv/rules.mk b/arch/riscv/rules.mk index 31f8d63d44..f3a64d7347 100644 --- a/arch/riscv/rules.mk +++ b/arch/riscv/rules.mk @@ -137,12 +137,12 @@ endif ifeq ($(SUBARCH),32) ARCH_COMPILEFLAGS := -march=rv32imac -mabi=ilp32 # override machine for ld -r -GLOBAL_MODULE_LDFLAGS += -m elf32lriscv +GLOBAL_MODULE_LDFLAGS += -Wl,-m,elf32lriscv else ifeq ($(SUBARCH),64) GLOBAL_DEFINES += IS_64BIT=1 ARCH_COMPILEFLAGS := -march=rv64imac -mabi=lp64 -mcmodel=medany # override machine for ld -r -GLOBAL_MODULE_LDFLAGS += -m elf64lriscv +GLOBAL_MODULE_LDFLAGS += -Wl,-m,elf64lriscv else $(error SUBARCH not set or set to something unknown) endif @@ -184,7 +184,7 @@ LINKER_SCRIPT += $(BUILDDIR)/linker-twosegment.ld # a paged sytem would. # NOTE: 8 seems to be about as far as you can go. experienced some extra stuffed words # when using 4. -ARCH_LDFLAGS += -z max-page-size=8 +ARCH_LDFLAGS += -Wl,-z,max-page-size=8 else GLOBAL_DEFINES += ARCH_RISCV_TWOSEGMENT=0 LINKER_SCRIPT += $(BUILDDIR)/linker-onesegment.ld diff --git a/arch/x86/rules.mk b/arch/x86/rules.mk index cff427cd50..1222d6e37a 100644 --- a/arch/x86/rules.mk +++ b/arch/x86/rules.mk @@ -95,7 +95,7 @@ GLOBAL_CFLAGS += $(call cc-option,$(CC),-fno-stack-protector,) ARCH_COMPILEFLAGS += -fasynchronous-unwind-tables ARCH_COMPILEFLAGS += -gdwarf-2 ARCH_COMPILEFLAGS += -fno-pic -ARCH_LDFLAGS += -z max-page-size=4096 +ARCH_LDFLAGS += -Wl,-z,max-page-size=4096 ifeq ($(SUBARCH),x86-64) ARCH_COMPILEFLAGS += -fno-stack-protector diff --git a/engine.mk b/engine.mk index 0250bcf085..182ed4b18c 100644 --- a/engine.mk +++ b/engine.mk @@ -214,7 +214,7 @@ endif # default to no ccache CCACHE ?= CC := $(CCACHE) $(TOOLCHAIN_PREFIX)gcc -LD := $(TOOLCHAIN_PREFIX)ld +LD := $(TOOLCHAIN_PREFIX)gcc OBJDUMP := $(TOOLCHAIN_PREFIX)objdump OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy CPPFILT := $(TOOLCHAIN_PREFIX)c++filt diff --git a/make/build.mk b/make/build.mk index 0efe81b680..82622ab6f9 100644 --- a/make/build.mk +++ b/make/build.mk @@ -1,9 +1,12 @@ # use linker garbage collection, if requested ifeq ($(WITH_LINKER_GC),1) GLOBAL_COMPILEFLAGS += -ffunction-sections -fdata-sections -GLOBAL_LDFLAGS += --gc-sections +GLOBAL_LDFLAGS += -Wl,--gc-sections endif +GLOBAL_LDFLAGS += -static -ffreestanding -nostartfiles -nostdlib -fuse-ld=bfd +GLOBAL_MODULE_LDFLAGS += -static -ffreestanding -nostartfiles -nostdlib -fuse-ld=bfd + ifneq (,$(EXTRA_BUILDRULES)) -include $(EXTRA_BUILDRULES) endif @@ -19,12 +22,14 @@ $(OUTELF).hex: $(OUTELF) $(info generating hex file: $@) $(NOECHO)$(OBJCOPY) -O ihex $< $@ +comma = , + $(OUTELF): $(ALLMODULE_OBJS) $(EXTRA_OBJS) $(LINKER_SCRIPT) $(EXTRA_LINKER_SCRIPTS) $(info linking $@) $(NOECHO)$(SIZE) -t --common $(sort $(ALLMODULE_OBJS)) $(EXTRA_OBJS) - $(NOECHO)$(LD) $(GLOBAL_LDFLAGS) $(ARCH_LDFLAGS) -dT $(LINKER_SCRIPT) \ + $(NOECHO)$(LD) $(GLOBAL_LDFLAGS) $(ARCH_LDFLAGS) -Wl,--verbose -v -Wl,-dT$(LINKER_SCRIPT) \ $(addprefix -T,$(EXTRA_LINKER_SCRIPTS)) \ - $(ALLMODULE_OBJS) $(EXTRA_OBJS) $(LIBGCC) -Map=$(OUTELF).map -o $@ + $(ALLMODULE_OBJS) $(EXTRA_OBJS) $(LIBGCC) -Wl,-Map=$(OUTELF).map -o $@ $(OUTELF).sym: $(OUTELF) $(info generating symbols: $@)