link-vmlinux.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # link vmlinux
  5. #
  6. # vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_OBJS) and
  7. # $(KBUILD_VMLINUX_LIBS). Most are built-in.a files from top-level directories
  8. # in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
  9. # $(KBUILD_VMLINUX_LIBS) are archives which are linked conditionally
  10. # (not within --whole-archive), and do not require symbol indexes added.
  11. #
  12. # vmlinux
  13. # ^
  14. # |
  15. # +--< $(KBUILD_VMLINUX_OBJS)
  16. # | +--< init/built-in.a drivers/built-in.a mm/built-in.a + more
  17. # |
  18. # +--< $(KBUILD_VMLINUX_LIBS)
  19. # | +--< lib/lib.a + more
  20. # |
  21. # +-< ${kallsymso} (see description in KALLSYMS section)
  22. #
  23. # vmlinux version (uname -v) cannot be updated during normal
  24. # descending-into-subdirs phase since we do not yet know if we need to
  25. # update vmlinux.
  26. # Therefore this step is delayed until just before final link of vmlinux.
  27. #
  28. # System.map is generated to document addresses of all kernel symbols
  29. # Error out on error
  30. set -e
  31. LD="$1"
  32. KBUILD_LDFLAGS="$2"
  33. LDFLAGS_vmlinux="$3"
  34. # Nice output in kbuild format
  35. # Will be supressed by "make -s"
  36. info()
  37. {
  38. if [ "${quiet}" != "silent_" ]; then
  39. printf " %-7s %s\n" "${1}" "${2}"
  40. fi
  41. }
  42. # Generate a linker script to ensure correct ordering of initcalls.
  43. gen_initcalls()
  44. {
  45. info GEN .tmp_initcalls.lds
  46. ${PYTHON} ${srctree}/scripts/jobserver-exec \
  47. ${PERL} ${srctree}/scripts/generate_initcall_order.pl \
  48. ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS} \
  49. > .tmp_initcalls.lds
  50. }
  51. # If CONFIG_LTO_CLANG is selected, collect generated symbol versions into
  52. # .tmp_symversions.lds
  53. gen_symversions()
  54. {
  55. info GEN .tmp_symversions.lds
  56. rm -f .tmp_symversions.lds
  57. for o in ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS}; do
  58. if [ -f ${o}.symversions ]; then
  59. cat ${o}.symversions >> .tmp_symversions.lds
  60. fi
  61. done
  62. }
  63. # Link of vmlinux.o used for section mismatch analysis
  64. # ${1} output file
  65. modpost_link()
  66. {
  67. local objects
  68. local lds=""
  69. objects="--whole-archive \
  70. ${KBUILD_VMLINUX_OBJS} \
  71. --no-whole-archive \
  72. --start-group \
  73. ${KBUILD_VMLINUX_LIBS} \
  74. --end-group"
  75. if [ -n "${CONFIG_LTO_CLANG}" ]; then
  76. gen_initcalls
  77. lds="-T .tmp_initcalls.lds"
  78. if [ -n "${CONFIG_MODVERSIONS}" ]; then
  79. gen_symversions
  80. lds="${lds} -T .tmp_symversions.lds"
  81. fi
  82. # This might take a while, so indicate that we're doing
  83. # an LTO link
  84. info LTO ${1}
  85. else
  86. info LD ${1}
  87. fi
  88. ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${lds} ${objects}
  89. }
  90. objtool_link()
  91. {
  92. local objtoolcmd;
  93. local objtoolopt;
  94. if [ "${CONFIG_LTO_CLANG} ${CONFIG_STACK_VALIDATION}" = "y y" ]; then
  95. # Don't perform vmlinux validation unless explicitly requested,
  96. # but run objtool on vmlinux.o now that we have an object file.
  97. if [ -n "${CONFIG_UNWINDER_ORC}" ]; then
  98. objtoolcmd="orc generate"
  99. fi
  100. objtoolopt="${objtoolopt} --duplicate"
  101. if [ -n "${CONFIG_FTRACE_MCOUNT_USE_OBJTOOL}" ]; then
  102. objtoolopt="${objtoolopt} --mcount"
  103. fi
  104. fi
  105. if [ -n "${CONFIG_VMLINUX_VALIDATION}" ]; then
  106. objtoolopt="${objtoolopt} --noinstr"
  107. fi
  108. if [ -n "${objtoolopt}" ]; then
  109. if [ -z "${objtoolcmd}" ]; then
  110. objtoolcmd="check"
  111. fi
  112. objtoolopt="${objtoolopt} --vmlinux"
  113. if [ -z "${CONFIG_FRAME_POINTER}" ]; then
  114. objtoolopt="${objtoolopt} --no-fp"
  115. fi
  116. if [ -n "${CONFIG_GCOV_KERNEL}" ] || [ -n "${CONFIG_LTO_CLANG}" ]; then
  117. objtoolopt="${objtoolopt} --no-unreachable"
  118. fi
  119. if [ -n "${CONFIG_RETPOLINE}" ]; then
  120. objtoolopt="${objtoolopt} --retpoline"
  121. fi
  122. if [ -n "${CONFIG_X86_SMAP}" ]; then
  123. objtoolopt="${objtoolopt} --uaccess"
  124. fi
  125. info OBJTOOL ${1}
  126. tools/objtool/objtool ${objtoolcmd} ${objtoolopt} ${1}
  127. fi
  128. }
  129. # Link of vmlinux
  130. # ${1} - output file
  131. # ${2}, ${3}, ... - optional extra .o files
  132. vmlinux_link()
  133. {
  134. local lds="${objtree}/${KBUILD_LDS}"
  135. local output=${1}
  136. local objects
  137. local strip_debug
  138. info LD ${output}
  139. # skip output file argument
  140. shift
  141. # The kallsyms linking does not need debug symbols included.
  142. if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
  143. strip_debug=-Wl,--strip-debug
  144. fi
  145. if [ "${SRCARCH}" != "um" ]; then
  146. if [ -n "${CONFIG_LTO_CLANG}" ]; then
  147. # Use vmlinux.o instead of performing the slow LTO
  148. # link again.
  149. objects="--whole-archive \
  150. vmlinux.o \
  151. --no-whole-archive \
  152. ${@}"
  153. else
  154. objects="--whole-archive \
  155. ${KBUILD_VMLINUX_OBJS} \
  156. --no-whole-archive \
  157. --start-group \
  158. ${KBUILD_VMLINUX_LIBS} \
  159. --end-group \
  160. ${@}"
  161. fi
  162. ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
  163. ${strip_debug#-Wl,} \
  164. -o ${output} \
  165. -T ${lds} ${objects}
  166. else
  167. objects="-Wl,--whole-archive \
  168. ${KBUILD_VMLINUX_OBJS} \
  169. -Wl,--no-whole-archive \
  170. -Wl,--start-group \
  171. ${KBUILD_VMLINUX_LIBS} \
  172. -Wl,--end-group \
  173. ${@}"
  174. ${CC} ${CFLAGS_vmlinux} \
  175. ${strip_debug} \
  176. -o ${output} \
  177. -Wl,-T,${lds} \
  178. ${objects} \
  179. -lutil -lrt -lpthread
  180. rm -f linux
  181. fi
  182. }
  183. # generate .BTF typeinfo from DWARF debuginfo
  184. # ${1} - vmlinux image
  185. # ${2} - file to dump raw BTF data into
  186. gen_btf()
  187. {
  188. local pahole_ver
  189. if ! [ -x "$(command -v ${PAHOLE})" ]; then
  190. echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
  191. return 1
  192. fi
  193. pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
  194. if [ "${pahole_ver}" -lt "116" ]; then
  195. echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16"
  196. return 1
  197. fi
  198. vmlinux_link ${1}
  199. info "BTF" ${2}
  200. LLVM_OBJCOPY=${OBJCOPY} ${PAHOLE} -J ${1}
  201. # Create ${2} which contains just .BTF section but no symbols. Add
  202. # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
  203. # deletes all symbols including __start_BTF and __stop_BTF, which will
  204. # be redefined in the linker script. Add 2>/dev/null to suppress GNU
  205. # objcopy warnings: "empty loadable segment detected at ..."
  206. ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
  207. --strip-all ${1} ${2} 2>/dev/null
  208. # Change e_type to ET_REL so that it can be used to link final vmlinux.
  209. # Unlike GNU ld, lld does not allow an ET_EXEC input.
  210. printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
  211. }
  212. # Create ${2} .S file with all symbols from the ${1} object file
  213. kallsyms()
  214. {
  215. local kallsymopt;
  216. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  217. kallsymopt="${kallsymopt} --all-symbols"
  218. fi
  219. if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
  220. kallsymopt="${kallsymopt} --absolute-percpu"
  221. fi
  222. if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  223. kallsymopt="${kallsymopt} --base-relative"
  224. fi
  225. info KSYMS ${2}
  226. if [ -n "${CONFIG_CFI_CLANG}" ]; then
  227. ${PERL} ${srctree}/scripts/generate_cfi_kallsyms.pl ${1} | \
  228. sort -n > .tmp_kallsyms
  229. else
  230. ${NM} -n ${1} > .tmp_kallsyms
  231. fi
  232. scripts/kallsyms ${kallsymopt} < .tmp_kallsyms > ${2}
  233. }
  234. # Perform one step in kallsyms generation, including temporary linking of
  235. # vmlinux.
  236. kallsyms_step()
  237. {
  238. kallsymso_prev=${kallsymso}
  239. kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
  240. kallsymso=${kallsyms_vmlinux}.o
  241. kallsyms_S=${kallsyms_vmlinux}.S
  242. vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
  243. kallsyms ${kallsyms_vmlinux} ${kallsyms_S}
  244. info AS ${kallsyms_S}
  245. ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
  246. ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  247. -c -o ${kallsymso} ${kallsyms_S}
  248. }
  249. # Create map file with all symbols from ${1}
  250. # See mksymap for additional details
  251. mksysmap()
  252. {
  253. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  254. }
  255. sorttable()
  256. {
  257. ${objtree}/scripts/sorttable ${1}
  258. }
  259. # Delete output files in case of error
  260. cleanup()
  261. {
  262. rm -f .btf.*
  263. rm -f .tmp_System.map
  264. rm -f .tmp_kallsyms
  265. rm -f .tmp_initcalls.lds
  266. rm -f .tmp_symversions.lds
  267. rm -f .tmp_vmlinux*
  268. rm -f System.map
  269. rm -f vmlinux
  270. rm -f vmlinux.o
  271. }
  272. on_exit()
  273. {
  274. if [ $? -ne 0 ]; then
  275. cleanup
  276. fi
  277. }
  278. trap on_exit EXIT
  279. on_signals()
  280. {
  281. exit 1
  282. }
  283. trap on_signals HUP INT QUIT TERM
  284. # Use "make V=1" to debug this script
  285. case "${KBUILD_VERBOSE}" in
  286. *1*)
  287. set -x
  288. ;;
  289. esac
  290. if [ "$1" = "clean" ]; then
  291. cleanup
  292. exit 0
  293. fi
  294. # We need access to CONFIG_ symbols
  295. . include/config/auto.conf
  296. # Update version
  297. info GEN .version
  298. if [ -r .version ]; then
  299. VERSION=$(expr 0$(cat .version) + 1)
  300. echo $VERSION > .version
  301. else
  302. rm -f .version
  303. echo 1 > .version
  304. fi;
  305. # final build of init/
  306. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
  307. #link vmlinux.o
  308. modpost_link vmlinux.o
  309. objtool_link vmlinux.o
  310. # modpost vmlinux.o to check for section mismatches
  311. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1
  312. info MODINFO modules.builtin.modinfo
  313. ${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
  314. info GEN modules.builtin
  315. # The second line aids cases where multiple modules share the same object.
  316. tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file=//p' |
  317. tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$/.ko/' > modules.builtin
  318. btf_vmlinux_bin_o=""
  319. if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
  320. btf_vmlinux_bin_o=.btf.vmlinux.bin.o
  321. if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
  322. echo >&2 "Failed to generate BTF for vmlinux"
  323. echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
  324. exit 1
  325. fi
  326. fi
  327. kallsymso=""
  328. kallsymso_prev=""
  329. kallsyms_vmlinux=""
  330. if [ -n "${CONFIG_KALLSYMS}" ]; then
  331. # kallsyms support
  332. # Generate section listing all symbols and add it into vmlinux
  333. # It's a three step process:
  334. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  335. # but __kallsyms is empty.
  336. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  337. # the right size
  338. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  339. # the right size, but due to the added section, some
  340. # addresses have shifted.
  341. # From here, we generate a correct .tmp_kallsyms2.o
  342. # 3) That link may have expanded the kernel image enough that
  343. # more linker branch stubs / trampolines had to be added, which
  344. # introduces new names, which further expands kallsyms. Do another
  345. # pass if that is the case. In theory it's possible this results
  346. # in even more stubs, but unlikely.
  347. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  348. # other bugs.
  349. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  350. #
  351. # a) Verify that the System.map from vmlinux matches the map from
  352. # ${kallsymso}.
  353. kallsyms_step 1
  354. kallsyms_step 2
  355. # step 3
  356. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
  357. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  358. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  359. kallsyms_step 3
  360. fi
  361. fi
  362. vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
  363. # fill in BTF IDs
  364. if [ -n "${CONFIG_DEBUG_INFO_BTF}" -a -n "${CONFIG_BPF}" ]; then
  365. info BTFIDS vmlinux
  366. ${RESOLVE_BTFIDS} vmlinux
  367. fi
  368. if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then
  369. info SORTTAB vmlinux
  370. if ! sorttable vmlinux; then
  371. echo >&2 Failed to sort kernel tables
  372. exit 1
  373. fi
  374. fi
  375. info SYSMAP System.map
  376. mksysmap vmlinux System.map
  377. # step a (see comment above)
  378. if [ -n "${CONFIG_KALLSYMS}" ]; then
  379. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  380. if ! cmp -s System.map .tmp_System.map; then
  381. echo >&2 Inconsistent kallsyms data
  382. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  383. exit 1
  384. fi
  385. fi