Skip to content

Commit 958f2e5

Browse files
elkablotrini
authored andcommitted
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 <[email protected]> Reviewed-by: Simon Glass <[email protected]>
1 parent 1445836 commit 958f2e5

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1713,9 +1713,9 @@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(ARCH)/Makefile.postlink)
17131713
quiet_cmd_u-boot__ ?= LD $@
17141714
cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@ \
17151715
-T u-boot.lds $(u-boot-init) \
1716-
--start-group \
1716+
--whole-archive \
17171717
$(u-boot-main) \
1718-
--end-group \
1718+
--no-whole-archive \
17191719
$(PLATFORM_LIBS) -Map u-boot.map; \
17201720
$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
17211721

arch/sandbox/config.mk

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ PLATFORM_CPPFLAGS += $(shell $(SDL_CONFIG) --cflags)
1717
endif
1818

1919
cmd_u-boot__ = $(CC) -o $@ -Wl,-T u-boot.lds $(u-boot-init) \
20-
-Wl,--start-group $(u-boot-main) -Wl,--end-group \
20+
-Wl,--whole-archive \
21+
$(u-boot-main) \
22+
-Wl,--no-whole-archive \
2123
$(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot.map
2224

2325
cmd_u-boot-spl = (cd $(obj) && $(CC) -o $(SPL_BIN) -Wl,-T u-boot-spl.lds \
2426
$(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
25-
-Wl,--start-group $(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
26-
$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) -Wl,--end-group \
27+
-Wl,--whole-archive \
28+
$(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
29+
$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
30+
-Wl,--no-whole-archive \
2731
$(PLATFORM_LIBS) -Wl,-Map -Wl,u-boot-spl.map -Wl,--gc-sections)
2832

2933
CONFIG_ARCH_DEVICE_TREE := sandbox

scripts/Makefile.build

+8-8
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,11 @@ $(sort $(subdir-obj-y)): $(subdir-ym) ;
331331
# Rule to compile a set of .o files into one .o file
332332
#
333333
ifdef builtin-target
334-
quiet_cmd_link_o_target = LD $@
334+
quiet_cmd_link_o_target = AR $@
335335
# If the list of objects to link is empty, just create an empty built-in.o
336336
cmd_link_o_target = $(if $(strip $(obj-y)),\
337-
$(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
338-
$(cmd_secanalysis),\
339-
rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
337+
rm -f $@; $(AR) cDPrsT $@ $(filter $(obj-y), $^), \
338+
rm -f $@; $(AR) cDPrsT$(KBUILD_ARFLAGS) $@)
340339

341340
$(builtin-target): $(obj-y) FORCE
342341
$(call if_changed,link_o_target)
@@ -362,7 +361,7 @@ $(modorder-target): $(subdir-ym) FORCE
362361
#
363362
ifdef lib-target
364363
quiet_cmd_link_l_target = AR $@
365-
cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
364+
cmd_link_l_target = rm -f $@; $(AR) cDPrsT$(KBUILD_ARFLAGS) $@ $(lib-y)
366365

367366
$(lib-target): $(lib-y) FORCE
368367
$(call if_changed,link_l_target)
@@ -382,10 +381,11 @@ $(filter $(addprefix $(obj)/, \
382381
$($(subst $(obj)/,,$(@:.o=-objs))) \
383382
$($(subst $(obj)/,,$(@:.o=-y)))), $^)
384383

385-
quiet_cmd_link_multi-y = LD $@
386-
cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
387384

388-
quiet_cmd_link_multi-m = LD [M] $@
385+
quiet_cmd_link_multi-y = AR $@
386+
cmd_link_multi-y = rm -f $@; $(AR) cDPrsT$(KBUILD_ARFLAGS) $@ $(link_multi_deps)
387+
388+
quiet_cmd_link_multi-m = AR [M] $@
389389
cmd_link_multi-m = $(cmd_link_multi-y)
390390

391391
$(multi-used-y): FORCE

scripts/Makefile.spl

+2-2
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,10 @@ quiet_cmd_u-boot-spl ?= LD $@
456456
cd $(obj) && \
457457
$(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_$(@F)) \
458458
$(patsubst $(obj)/%,%,$(u-boot-spl-init)) \
459-
--start-group \
459+
--whole-archive \
460460
$(patsubst $(obj)/%,%,$(u-boot-spl-main)) \
461461
$(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
462-
--end-group \
462+
--no-whole-archive \
463463
$(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN) \
464464
)
465465

0 commit comments

Comments
 (0)