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
45 45
46# Link of vmlinux46# Link of vmlinux
47# ${1} - output file47# ${1} - output file
48# ${2}, ${3}, ... - optional extra .o files
49vmlinux_link()48vmlinux_link()
50{49{
51 local output=${1}50 local output=${1}
@@ -90,7 +89,7 @@ vmlinux_link() · 38 unmodified lines
90 ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"89 ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"
91 90
92 # The kallsyms linking does not need debug symbols included.91 # The kallsyms linking does not need debug symbols included.
93 if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then92 if [ -n "${strip_debug}" ] ; then
94 ldflags="${ldflags} ${wl}--strip-debug"93 ldflags="${ldflags} ${wl}--strip-debug"
95 fi94 fi
96 95
@@ -101,7 +100,7 @@ vmlinux_link() · 4 unmodified lines
101 ${ld} ${ldflags} -o ${output} \100 ${ld} ${ldflags} -o ${output} \
102 ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \101 ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \
103 ${wl}--start-group ${libs} ${wl}--end-group \102 ${wl}--start-group ${libs} ${wl}--end-group \
104 $@ ${ldlibs}103 ${kallsymso} ${btf_vmlinux_bin_o} ${ldlibs}
105}104}
106 105
107# generate .BTF typeinfo from DWARF debuginfo106# generate .BTF typeinfo from DWARF debuginfo
@@ -110,6 +109,7 @@ vmlinux_link() · 2 unmodified lines
110gen_btf()109gen_btf()
111{110{
112 local pahole_ver111 local pahole_ver
112 local btf_data=${2}
113 113
114 if ! [ -x "$(command -v ${PAHOLE})" ]; then114 if ! [ -x "$(command -v ${PAHOLE})" ]; then
115 echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"115 echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
@@ -124,16 +124,16 @@ gen_btf() · 8 unmodified lines
124 124
125 vmlinux_link ${1}125 vmlinux_link ${1}
126 126
127 info "BTF" ${2}127 info BTF "${btf_data}"
128 LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}128 LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
129 129
130 # Create ${2} which contains just .BTF section but no symbols. Add130 # Create ${btf_data} which contains just .BTF section but no symbols. Add
131 # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all131 # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
132 # deletes all symbols including __start_BTF and __stop_BTF, which will132 # deletes all symbols including __start_BTF and __stop_BTF, which will
133 # be redefined in the linker script. Add 2>/dev/null to suppress GNU133 # be redefined in the linker script. Add 2>/dev/null to suppress GNU
134 # objcopy warnings: "empty loadable segment detected at ..."134 # objcopy warnings: "empty loadable segment detected at ..."
135 ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \135 ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
136 --strip-all ${1} ${2} 2>/dev/null136 --strip-all ${1} "${btf_data}" 2>/dev/null
137 # Change e_type to ET_REL so that it can be used to link final vmlinux.137 # Change e_type to ET_REL so that it can be used to link final vmlinux.
138 # GNU ld 2.35+ and lld do not allow an ET_EXEC input.138 # GNU ld 2.35+ and lld do not allow an ET_EXEC input.
139 if is_enabled CONFIG_CPU_BIG_ENDIAN; then139 if is_enabled CONFIG_CPU_BIG_ENDIAN; then
@@ -141,10 +141,12 @@ gen_btf() · 1 unmodified line
141 else141 else
142 et_rel='\1\0'142 et_rel='\1\0'
143 fi143 fi
144 printf "${et_rel}" | dd of=${2} conv=notrunc bs=1 seek=16 status=none144 printf "${et_rel}" | dd of="${btf_data}" conv=notrunc bs=1 seek=16 status=none
145
146 btf_vmlinux_bin_o=${btf_data}
145}147}
146 148
147# Create ${2} .S file with all symbols from the ${1} object file149# Create ${2}.o file with all symbols from the ${1} object file
148kallsyms()150kallsyms()
149{151{
150 local kallsymopt;152 local kallsymopt;
@@ -165,27 +167,25 @@ kallsyms() · 14 unmodified lines
165 kallsymopt="${kallsymopt} --lto-clang"167 kallsymopt="${kallsymopt} --lto-clang"
166 fi168 fi
167 169
168 info KSYMS ${2}170 info KSYMS "${2}.S"
169 scripts/kallsyms ${kallsymopt} ${1} > ${2}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
170}178}
171 179
172# Perform one step in kallsyms generation, including temporary linking of180# Perform one step in kallsyms generation, including temporary linking of
173# vmlinux.181# vmlinux.
174kallsyms_step()182kallsyms_step()
175{183{
176 kallsymso_prev=${kallsymso}
177 kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}184 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}
184 185
185 info AS ${kallsymso}186 vmlinux_link "${kallsyms_vmlinux}"
186 ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \187 mksysmap "${kallsyms_vmlinux}" "${kallsyms_vmlinux}.syms"
187 ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \188 kallsyms "${kallsyms_vmlinux}.syms" "${kallsyms_vmlinux}"
188 -c -o ${kallsymso} ${kallsyms_S}
189}189}
190 190
191# Create map file with all symbols from ${1}191# Create map file with all symbols from ${1}
@@ -223,19 +223,18 @@ fi · 31 unmodified lines
223 223
224${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o224${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
225 225
226btf_vmlinux_bin_o=""226btf_vmlinux_bin_o=
227kallsymso=
228strip_debug=
229
227if is_enabled CONFIG_DEBUG_INFO_BTF; then230if is_enabled CONFIG_DEBUG_INFO_BTF; then
228 btf_vmlinux_bin_o=.btf.vmlinux.bin.o231 if ! gen_btf .tmp_vmlinux.btf .btf.vmlinux.bin.o ; then
229 if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
230 echo >&2 "Failed to generate BTF for vmlinux"232 echo >&2 "Failed to generate BTF for vmlinux"
231 echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"233 echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
232 exit 1234 exit 1
233 fi235 fi
234fi236fi
235 237
236kallsymso=""
237kallsymso_prev=""
238kallsyms_vmlinux=""
239if is_enabled CONFIG_KALLSYMS; then238if is_enabled CONFIG_KALLSYMS; then
240 239
241 # kallsyms support240 # kallsyms support
@@ -261,11 +260,13 @@ if is_enabled CONFIG_KALLSYMS; then · 19 unmodified lines
261 # a) Verify that the System.map from vmlinux matches the map from260 # a) Verify that the System.map from vmlinux matches the map from
262 # ${kallsymso}.261 # ${kallsymso}.
263 262
263 # The kallsyms linking does not need debug symbols included.
264 strip_debug=1
265
264 kallsyms_step 1266 kallsyms_step 1
265 kallsyms_step 2267 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
266 268
267 # step 3269 kallsyms_step 2
268 size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
269 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})270 size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
270 271
271 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then272 if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
@@ -273,7 +274,9 @@ if is_enabled CONFIG_KALLSYMS; then · 1 unmodified line
273 fi274 fi
274fi275fi
275 276
276vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}277strip_debug=
278
279vmlinux_link vmlinux
277 280
278# fill in BTF IDs281# fill in BTF IDs
279if is_enabled CONFIG_DEBUG_INFO_BTF && is_enabled CONFIG_BPF; then282if is_enabled CONFIG_DEBUG_INFO_BTF && is_enabled CONFIG_BPF; then