Skip to content

Commit

Permalink
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 Jul 23, 2022
1 parent 3dff26a commit 2d193b3
Show file tree
Hide file tree
Showing 10 changed files with 37 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 @@ -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)

Expand Down
26 changes: 20 additions & 6 deletions arch/microblaze/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
4 changes: 2 additions & 2 deletions arch/mips/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
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 @@ -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
Expand Down
9 changes: 6 additions & 3 deletions make/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: $@)
Expand Down

0 comments on commit 2d193b3

Please sign in to comment.