History

kbuild: refactor variables in scripts/link-vmlinux.sh

Clean up the variables in scripts/link-vmlinux.sh - Specify the extra objects directly in vmlinux_link() - Move the AS rule to kallsyms() - Set kallsymso and btf_vmlinux_bin_o where they are generated - Remove unneeded variable, kallsymso_prev - Introduce the btf_data variable - Introduce the strip_debug flag instead of checking the output name No functional change intended.

Masahiro Yamada committed 2 years ago

Commit ddf41321 parent 995150e1 file · +34 −31Browse files

Unified · Split

scripts/link-vmlinux.sh+34−31
@@ -45,7 +45,6 @@ info() · 44 unmodified lines
4545
4646 # Link of vmlinux
4747 # ${1} - output file
48−# ${2}, ${3}, ... - optional extra .o files
4948 vmlinux_link()
5049 {
5150 local output=${1}
@@ -90,7 +89,7 @@ vmlinux_link() · 38 unmodified lines
9089 ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"
9190
9291 # The kallsyms linking does not need debug symbols included.
93− if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
92+ if [ -n "${strip_debug}" ] ; then
9493 ldflags="${ldflags} ${wl}--strip-debug"
9594 fi
9695
@@ -101,7 +100,7 @@ vmlinux_link() · 4 unmodified lines
101100 ${ld} ${ldflags} -o ${output} \
102101 ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \
103102 ${wl}--start-group ${libs} ${wl}--end-group \
104− $@ ${ldlibs}
103+ ${kallsymso} ${btf_vmlinux_bin_o} ${ldlibs}
105104 }
106105
107106 # generate .BTF typeinfo from DWARF debuginfo
@@ -110,6 +109,7 @@ vmlinux_link() · 2 unmodified lines
110109 gen_btf()
111110 {
112111 local pahole_ver
112+ local btf_data=${2}
113113
114114 if ! [ -x "$(command -v ${PAHOLE})" ]; then
115115 echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
@@ -124,16 +124,16 @@ gen_btf() · 8 unmodified lines
124124
125125 vmlinux_link ${1}
126126
127− info "BTF" ${2}
127+ info BTF "${btf_data}"
128128 LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
129129
130− # Create ${2} which contains just .BTF section but no symbols. Add
130+ # Create ${btf_data} which contains just .BTF section but no symbols. Add
131131 # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
132132 # deletes all symbols including __start_BTF and __stop_BTF, which will
133133 # be redefined in the linker script. Add 2>/dev/null to suppress GNU
134134 # objcopy warnings: "empty loadable segment detected at ..."
135135 ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
136− --strip-all ${1} ${2} 2>/dev/null
136+ --strip-all ${1} "${btf_data}" 2>/dev/null
137137 # Change e_type to ET_REL so that it can be used to link final vmlinux.
138138 # GNU ld 2.35+ and lld do not allow an ET_EXEC input.
139139 if is_enabled CONFIG_CPU_BIG_ENDIAN; then
@@ -141,10 +141,12 @@ gen_btf() · 1 unmodified line
141141 else
142142 et_rel='\1\0'
143143 fi
144− printf "${et_rel}" | dd of=${2} conv=notrunc bs=1 seek=16 status=none
144+ printf "${et_rel}" | dd of="${btf_data}" conv=notrunc bs=1 seek=16 status=none
145+
146+ btf_vmlinux_bin_o=${btf_data}
145147 }
146148
147−# Create ${2} .S file with all symbols from the ${1} object file
149+# Create ${2}.o file with all symbols from the ${1} object file
148150 kallsyms()
149151 {
150152 local kallsymopt;
@@ -165,27 +167,25 @@ kallsyms() · 14 unmodified lines
165167 kallsymopt="${kallsymopt} --lto-clang"
166168 fi
167169
168− info KSYMS ${2}
169− scripts/kallsyms ${kallsymopt} ${1} > ${2}
170+ info KSYMS "${2}.S"
171+ scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S"
172+
173+ info AS "${2}.o"
174+ ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
175+ ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} -c -o "${2}.o" "${2}.S"
176+
177+ kallsymso=${2}.o
170178 }
171179
172180 # Perform one step in kallsyms generation, including temporary linking of
173181 # vmlinux.
174182 kallsyms_step()
175183 {
176− kallsymso_prev=${kallsymso}
177184 kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
178− kallsymso=${kallsyms_vmlinux}.o
179− kallsyms_S=${kallsyms_vmlinux}.S
180−
181− vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
182− mksysmap ${kallsyms_vmlinux} ${kallsyms_vmlinux}.syms
183− kallsyms ${kallsyms_vmlinux}.syms ${kallsyms_S}
184185
185− info AS ${kallsymso}
186− ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
187− ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
188− -c -o ${kallsymso} ${kallsyms_S}
186+ vmlinux_link "${kallsyms_vmlinux}"
187+ mksysmap "${kallsyms_vmlinux}" "${kallsyms_vmlinux}.syms"
188+ kallsyms "${kallsyms_vmlinux}.syms" "${kallsyms_vmlinux}"
189189 }
190190
191191 # Create map file with all symbols from ${1}
@@ -223,19 +223,18 @@ fi · 31 unmodified lines
223223
224224 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
225225
226−btf_vmlinux_bin_o=""
226+btf_vmlinux_bin_o=
227+kallsymso=
228+strip_debug=
229+
227230 if is_enabled CONFIG_DEBUG_INFO_BTF; then
228− btf_vmlinux_bin_o=.btf.vmlinux.bin.o
229− if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
231+ if ! gen_btf .tmp_vmlinux.btf .btf.vmlinux.bin.o ; then
230232 echo >&2 "Failed to generate BTF for vmlinux"
231233 echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
232234 exit 1
233235 fi
234236 fi
235237
236−kallsymso=""
237−kallsymso_prev=""
238−kallsyms_vmlinux=""
239238 if is_enabled CONFIG_KALLSYMS; then
240239
241240 # kallsyms support
@@ -261,11 +260,13 @@ if is_enabled CONFIG_KALLSYMS; then · 19 unmodified lines
261260 # a) Verify that the System.map from vmlinux matches the map from
262261 # ${kallsymso}.
263262
263+ # The kallsyms linking does not need debug symbols included.
264+ strip_debug=1
265+
264266 kallsyms_step 1
265− kallsyms_step 2
267+ size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
266268
267− # step 3
268− size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
269+ kallsyms_step 2
269270 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
270271
271272 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
@@ -273,7 +274,9 @@ if is_enabled CONFIG_KALLSYMS; then · 1 unmodified line
273274 fi
274275 fi
275276
276−vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
277+strip_debug=
278+
279+vmlinux_link vmlinux
277280
278281 # fill in BTF IDs
279282 if is_enabled CONFIG_DEBUG_INFO_BTF && is_enabled CONFIG_BPF; then