From 2d193b38cdbdcfeb9a4e03b5274b322a7d6b78de Mon Sep 17 00:00:00 2001 From: Pedro Falcato Date: Wed, 13 Apr 2022 05:07:49 +0100 Subject: [PATCH] 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 | 9 ++++++--- 10 files changed, 37 insertions(+), 20 deletions(-) diff --git a/arch/arm/rules.mk b/arch/arm/rules.mk index 027e5e018..58a74575c 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 dcab37ee1..af6ca85bc 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 173a75fd6..f5770e18e 100644 --- a/arch/arm64/rules.mk +++ b/arch/arm64/rules.mk @@ -96,7 +96,7 @@ ARCH_COMPILEFLAGS += -fno-omit-frame-pointer ARCH_COMPILEFLAGS_NOFLOAT := -mgeneral-regs-only ARCH_COMPILEFLAGS_FLOAT := -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 296f53cf4..d0fcd60d5 100644 --- a/arch/microblaze/rules.mk +++ b/arch/microblaze/rules.mk @@ -28,8 +28,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) @@ -40,7 +40,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 @@ -52,9 +54,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) @@ -64,6 +68,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 1ea27b4d2..301b96456 100644 --- a/arch/mips/rules.mk +++ b/arch/mips/rules.mk @@ -31,8 +31,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 dc8340702..106b06621 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 c2c2efb8b..8d7089c98 100644 --- a/arch/riscv/rules.mk +++ b/arch/riscv/rules.mk @@ -149,7 +149,7 @@ ifeq ($(SUBARCH),32) endif # 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 @@ -163,7 +163,7 @@ else ifeq ($(SUBARCH),64) endif # 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 @@ -213,7 +213,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 b4daa089e..d135fe6cc 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 ef581f477..ee90e2469 100644 --- a/engine.mk +++ b/engine.mk @@ -218,7 +218,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 84dca4a19..4125e07a4 100644 --- a/make/build.mk +++ b/make/build.mk @@ -2,10 +2,13 @@ WITH_LINKER_GC ?= false ifeq (true,$(call TOBOOL,$(WITH_LINKER_GC))) GLOBAL_COMPILEFLAGS += -ffunction-sections -fdata-sections -GLOBAL_LDFLAGS += --gc-sections +GLOBAL_LDFLAGS += -Wl,--gc-sections GLOBAL_DEFINES += LINKER_GC=1 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 @@ -24,9 +27,9 @@ $(OUTELF).hex: $(OUTELF) $(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,-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: $@)