Skip to content

Commit ac4f067

Browse files
ardbiesheuvelmasahir0y
authored andcommitted
kbuild: Create intermediate vmlinux build with relocations preserved
The imperative paradigm used to build vmlinux, extract some info from it or perform some checks on it, and subsequently modify it again goes against the declarative paradigm that is usually employed for defining make rules. In particular, the Makefile.postlink files that consume their input via an output rule result in some dodgy logic in the decompressor makefiles for RISC-V and x86, given that the vmlinux.relocs input file needed to generate the arch-specific relocation tables may not exist or be out of date, but cannot be constructed using the ordinary Make dependency based rules, because the info needs to be extracted while vmlinux is in its ephemeral, non-stripped form. So instead, for architectures that require the static relocations that are emitted into vmlinux when passing --emit-relocs to the linker, and are subsequently stripped out again, introduce an intermediate vmlinux target called vmlinux.unstripped, and organize the reset of the build logic accordingly: - vmlinux.unstripped is created only once, and not updated again - build rules under arch/*/boot can depend on vmlinux.unstripped without running the risk of the data disappearing or being out of date - the final vmlinux generated by the build is not bloated with static relocations that are never needed again after the build completes. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 9b400d1 commit ac4f067

File tree

9 files changed

+30
-34
lines changed

9 files changed

+30
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ modules.order
6565
/vmlinux.32
6666
/vmlinux.map
6767
/vmlinux.symvers
68+
/vmlinux.unstripped
6869
/vmlinux-gdb.py
6970
/vmlinuz
7071
/System.map

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ endif # CONFIG_MODULES
15691569
# Directories & files removed with 'make clean'
15701570
CLEAN_FILES += vmlinux.symvers modules-only.symvers \
15711571
modules.builtin modules.builtin.modinfo modules.nsdeps \
1572-
modules.builtin.ranges vmlinux.o.map \
1572+
modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \
15731573
compile_commands.json rust/test \
15741574
rust-project.json .vmlinux.objs .vmlinux.export.c \
15751575
.builtin-dtbs-list .builtin-dtb.S

arch/mips/Makefile.postlink

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ quiet_cmd_relocs = RELOCS $@
2222

2323
# `@true` prevents complaint when there is nothing to be done
2424

25-
vmlinux: FORCE
25+
vmlinux vmlinux.unstripped: FORCE
2626
@true
2727
ifeq ($(CONFIG_CPU_LOONGSON3_WORKAROUNDS),y)
2828
$(call if_changed,ls3_llsc)

arch/riscv/Makefile.postlink

+1-10
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,17 @@ __archpost:
1010

1111
-include include/config/auto.conf
1212
include $(srctree)/scripts/Kbuild.include
13-
include $(srctree)/scripts/Makefile.lib
1413

1514
quiet_cmd_relocs_check = CHKREL $@
1615
cmd_relocs_check = \
1716
$(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@"
1817

19-
ifdef CONFIG_RELOCATABLE
20-
quiet_cmd_cp_vmlinux_relocs = CPREL vmlinux.relocs
21-
cmd_cp_vmlinux_relocs = cp vmlinux vmlinux.relocs
22-
23-
endif
24-
2518
# `@true` prevents complaint when there is nothing to be done
2619

27-
vmlinux: FORCE
20+
vmlinux vmlinux.unstripped: FORCE
2821
@true
2922
ifdef CONFIG_RELOCATABLE
3023
$(call if_changed,relocs_check)
31-
$(call if_changed,cp_vmlinux_relocs)
32-
$(call if_changed,strip_relocs)
3324
endif
3425

3526
clean:

arch/riscv/boot/Makefile

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ $(obj)/xipImage: vmlinux FORCE
3232
endif
3333

3434
ifdef CONFIG_RELOCATABLE
35-
vmlinux.relocs: vmlinux
36-
@ (! [ -f vmlinux.relocs ] && echo "vmlinux.relocs can't be found, please remove vmlinux and try again") || true
37-
38-
$(obj)/Image: vmlinux.relocs FORCE
35+
$(obj)/Image: vmlinux.unstripped FORCE
3936
else
4037
$(obj)/Image: vmlinux FORCE
4138
endif

arch/s390/Makefile.postlink

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ __archpost:
1111

1212
-include include/config/auto.conf
1313
include $(srctree)/scripts/Kbuild.include
14-
include $(srctree)/scripts/Makefile.lib
1514

1615
CMD_RELOCS=arch/s390/tools/relocs
1716
OUT_RELOCS = arch/s390/boot
@@ -20,9 +19,8 @@ quiet_cmd_relocs = RELOCS $(OUT_RELOCS)/relocs.S
2019
mkdir -p $(OUT_RELOCS); \
2120
$(CMD_RELOCS) $@ > $(OUT_RELOCS)/relocs.S
2221

23-
vmlinux: FORCE
22+
vmlinux.unstripped: FORCE
2423
$(call cmd,relocs)
25-
$(call cmd,strip_relocs)
2624

2725
clean:
2826
@rm -f $(OUT_RELOCS)/relocs.S

arch/x86/Makefile.postlink

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@ __archpost:
1111

1212
-include include/config/auto.conf
1313
include $(srctree)/scripts/Kbuild.include
14-
include $(srctree)/scripts/Makefile.lib
1514

1615
CMD_RELOCS = arch/x86/tools/relocs
1716
OUT_RELOCS = arch/x86/boot/compressed
18-
quiet_cmd_relocs = RELOCS $(OUT_RELOCS)/$@.relocs
17+
quiet_cmd_relocs = RELOCS $(OUT_RELOCS)/vmlinux.relocs
1918
cmd_relocs = \
2019
mkdir -p $(OUT_RELOCS); \
21-
$(CMD_RELOCS) $@ > $(OUT_RELOCS)/$@.relocs; \
20+
$(CMD_RELOCS) $@ > $(OUT_RELOCS)/vmlinux.relocs; \
2221
$(CMD_RELOCS) --abs-relocs $@
2322

2423
# `@true` prevents complaint when there is nothing to be done
2524

26-
vmlinux: FORCE
25+
vmlinux vmlinux.unstripped: FORCE
2726
@true
2827
ifeq ($(CONFIG_X86_NEED_RELOCS),y)
2928
$(call cmd,relocs)
30-
$(call cmd,strip_relocs)
3129
endif
3230

3331
clean:

scripts/Makefile.lib

-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@ quiet_cmd_ar = AR $@
371371
quiet_cmd_objcopy = OBJCOPY $@
372372
cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
373373

374-
quiet_cmd_strip_relocs = RSTRIP $@
375-
cmd_strip_relocs = $(OBJCOPY) --remove-section='.rel*' $@
376-
377374
# Gzip
378375
# ---------------------------------------------------------------------------
379376

scripts/Makefile.vmlinux

+21-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ include $(srctree)/scripts/Makefile.lib
99

1010
targets :=
1111

12+
ifdef CONFIG_ARCH_VMLINUX_NEEDS_RELOCS
13+
vmlinux-final := vmlinux.unstripped
14+
15+
quiet_cmd_strip_relocs = RSTRIP $@
16+
cmd_strip_relocs = $(OBJCOPY) --remove-section='.rel*' $< $@
17+
18+
vmlinux: $(vmlinux-final) FORCE
19+
$(call if_changed,strip_relocs)
20+
21+
targets += vmlinux
22+
else
23+
vmlinux-final := vmlinux
24+
endif
25+
1226
%.o: %.c FORCE
1327
$(call if_changed_rule,cc_o_c)
1428

@@ -47,19 +61,19 @@ targets += .builtin-dtbs-list
4761

4862
ifdef CONFIG_GENERIC_BUILTIN_DTB
4963
targets += .builtin-dtbs.S .builtin-dtbs.o
50-
vmlinux: .builtin-dtbs.o
64+
$(vmlinux-final): .builtin-dtbs.o
5165
endif
5266

5367
# vmlinux
5468
# ---------------------------------------------------------------------------
5569

5670
ifdef CONFIG_MODULES
5771
targets += .vmlinux.export.o
58-
vmlinux: .vmlinux.export.o
72+
$(vmlinux-final): .vmlinux.export.o
5973
endif
6074

6175
ifdef CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX
62-
vmlinux: arch/$(SRCARCH)/tools/vmlinux.arch.o
76+
$(vmlinux-final): arch/$(SRCARCH)/tools/vmlinux.arch.o
6377

6478
arch/$(SRCARCH)/tools/vmlinux.arch.o: vmlinux.o FORCE
6579
$(Q)$(MAKE) $(build)=arch/$(SRCARCH)/tools $@
@@ -72,11 +86,11 @@ cmd_link_vmlinux = \
7286
$< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)" "$@"; \
7387
$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
7488

75-
targets += vmlinux
76-
vmlinux: scripts/link-vmlinux.sh vmlinux.o $(KBUILD_LDS) FORCE
89+
targets += $(vmlinux-final)
90+
$(vmlinux-final): scripts/link-vmlinux.sh vmlinux.o $(KBUILD_LDS) FORCE
7791
+$(call if_changed_dep,link_vmlinux)
7892
ifdef CONFIG_DEBUG_INFO_BTF
79-
vmlinux: $(RESOLVE_BTFIDS)
93+
$(vmlinux-final): $(RESOLVE_BTFIDS)
8094
endif
8195

8296
ifdef CONFIG_BUILDTIME_TABLE_SORT
@@ -96,7 +110,7 @@ modules.builtin.ranges: $(srctree)/scripts/generate_builtin_ranges.awk \
96110
modules.builtin vmlinux.map vmlinux.o.map FORCE
97111
$(call if_changed,modules_builtin_ranges)
98112

99-
vmlinux.map: vmlinux
113+
vmlinux.map: $(vmlinux-final)
100114
@:
101115

102116
endif

0 commit comments

Comments
 (0)