build: use thin archives instead of incremental linking

Currently we use incremental linking (ld -r) to link several object
files from one directory into one built-in.o object file containing the
linked code from that directory (and its subdirectories).

Linux has, some time ago, moved to thin archives instead.

Thin archives are archives (.a) that do not really contain the object
files, only references to them.

Using thin archives instead of incremental linking
- saves disk space
- apparently works better with dead code elimination
- makes things easier for LTO

The third point is the important one for us. With incremental linking
there are several options how to do LTO, and that would unnecessarily
complicate things.

We have to use the --whole-archive/--no-whole-archive linking option
instead of --start-group/--end-group, otherwise linking may fail because
of unresolved symbols, or the resulting binary will be unusable.

We also need to use the P flag for ar, otherwise final linking may fail.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Marek Behún 2021-05-20 13:24:02 +02:00 committed by Tom Rini
parent 1445836ca7
commit 958f2e57ef
4 changed files with 19 additions and 15 deletions

View File

@ -1713,9 +1713,9 @@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(ARCH)/Makefile.postlink)
quiet_cmd_u-boot__ ?= LD $@ quiet_cmd_u-boot__ ?= LD $@
cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@ \ cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
-T u-boot.lds $(u-boot-init) \ -T u-boot.lds $(u-boot-init) \
--start-group \ --whole-archive \
$(u-boot-main) \ $(u-boot-main) \
--end-group \ --no-whole-archive \
$(PLATFORM_LIBS) -Map u-boot.map; \ $(PLATFORM_LIBS) -Map u-boot.map; \
$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)

View File

@ -17,13 +17,17 @@ PLATFORM_CPPFLAGS += $(shell $(SDL_CONFIG) --cflags)
endif endif
cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \ cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \
-Wl,--start-group $(u-boot-main) -Wl,--end-group \ -Wl,--whole-archive \
$(u-boot-main) \
-Wl,--no-whole-archive \
$(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot.map $(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot.map
cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \ cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \
$(patsubst $(obj)/%,%,$(u-boot-spl-init)) \ $(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
-Wl,--start-group $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \ -Wl,--whole-archive \
$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) -Wl,--end-group \ $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
-Wl,--no-whole-archive \
$(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot-spl.map -Wl,--gc-sections) $(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot-spl.map -Wl,--gc-sections)
CONFIG_ARCH_DEVICE_TREE := sandbox CONFIG_ARCH_DEVICE_TREE := sandbox

View File

@ -331,12 +331,11 @@ $(sort $(subdir-obj-y)): $(subdir-ym) ;
# Rule to compile a set of .o files into one .o file # Rule to compile a set of .o files into one .o file
# #
ifdef builtin-target ifdef builtin-target
quiet_cmd_link_o_target = LD $@ quiet_cmd_link_o_target = AR $@
# If the list of objects to link is empty, just create an empty built-in.o # If the list of objects to link is empty, just create an empty built-in.o
cmd_link_o_target = $(if $(strip $(obj-y)),\ cmd_link_o_target = $(if $(strip $(obj-y)),\
$(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \ rm -f $@; $(AR) cDPrsT $@ $(filter $(obj-y), $^), \
$(cmd_secanalysis),\ rm -f $@; $(AR) cDPrsT$(KBUILD_ARFLAGS) $@)
rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
$(builtin-target): $(obj-y) FORCE $(builtin-target): $(obj-y) FORCE
$(call if_changed,link_o_target) $(call if_changed,link_o_target)
@ -362,7 +361,7 @@ $(modorder-target): $(subdir-ym) FORCE
# #
ifdef lib-target ifdef lib-target
quiet_cmd_link_l_target = AR $@ quiet_cmd_link_l_target = AR $@
cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y) cmd_link_l_target = rm -f $@; $(AR) cDPrsT$(KBUILD_ARFLAGS) $@ $(lib-y)
$(lib-target): $(lib-y) FORCE $(lib-target): $(lib-y) FORCE
$(call if_changed,link_l_target) $(call if_changed,link_l_target)
@ -382,10 +381,11 @@ $(filter $(addprefix $(obj)/, \
$($(subst $(obj)/,,$(@:.o=-objs))) \ $($(subst $(obj)/,,$(@:.o=-objs))) \
$($(subst $(obj)/,,$(@:.o=-y)))), $^) $($(subst $(obj)/,,$(@:.o=-y)))), $^)
quiet_cmd_link_multi-y = LD $@
cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
quiet_cmd_link_multi-m = LD [M] $@ quiet_cmd_link_multi-y = AR $@
cmd_link_multi-y = rm -f $@; $(AR) cDPrsT$(KBUILD_ARFLAGS) $@ $(link_multi_deps)
quiet_cmd_link_multi-m = AR [M] $@
cmd_link_multi-m = $(cmd_link_multi-y) cmd_link_multi-m = $(cmd_link_multi-y)
$(multi-used-y): FORCE $(multi-used-y): FORCE

View File

@ -456,10 +456,10 @@ quiet_cmd_u-boot-spl ?= LD $@
cd $(obj) && \ cd $(obj) && \
$(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_$(@F)) \ $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_$(@F)) \
$(patsubst $(obj)/%,%,$(u-boot-spl-init)) \ $(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
--start-group \ --whole-archive \
$(patsubst $(obj)/%,%,$(u-boot-spl-main)) \ $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \ $(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
--end-group \ --no-whole-archive \
$(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN) \ $(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN) \
) )