Skip to content

Commit

Permalink
[build] Use CC as a linker
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Falcato <[email protected]>
  • Loading branch information
heatd committed Apr 13, 2022
1 parent 8028512 commit 3f567bd
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion arch/arm/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/toolchain.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 20 additions & 6 deletions arch/microblaze/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
4 changes: 2 additions & 2 deletions arch/mips/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arch/or1k/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions arch/riscv/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion engine.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions make/build.mk
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,-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: $@)
Expand Down

0 comments on commit 3f567bd

Please sign in to comment.