link-vmlinux.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 [ -n "${CONFIG_CPU_UNRET_ENTRY}" ]; then
  114. objtoolopt="${objtoolopt} --unret"
  115. fi
  116. if [ -z "${CONFIG_FRAME_POINTER}" ]; then
  117. objtoolopt="${objtoolopt} --no-fp"
  118. fi
  119. if [ -n "${CONFIG_GCOV_KERNEL}" ] || [ -n "${CONFIG_LTO_CLANG}" ]; then
  120. objtoolopt="${objtoolopt} --no-unreachable"
  121. fi
  122. if [ -n "${CONFIG_RETPOLINE}" ]; then
  123. objtoolopt="${objtoolopt} --retpoline"
  124. fi
  125. if [ -n "${CONFIG_X86_SMAP}" ]; then
  126. objtoolopt="${objtoolopt} --uaccess"
  127. fi
  128. if [ -n "${CONFIG_SLS}" ]; then
  129. objtoolopt="${objtoolopt} --sls"
  130. fi
  131. info OBJTOOL ${1}
  132. tools/objtool/objtool ${objtoolcmd} ${objtoolopt} ${1}
  133. fi
  134. }
  135. # Link of vmlinux
  136. # ${1} - output file
  137. # ${2}, ${3}, ... - optional extra .o files
  138. vmlinux_link()
  139. {
  140. local lds="${objtree}/${KBUILD_LDS}"
  141. local output=${1}
  142. local objects
  143. local strip_debug
  144. info LD ${output}
  145. # skip output file argument
  146. shift
  147. # The kallsyms linking does not need debug symbols included.
  148. if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
  149. strip_debug=-Wl,--strip-debug
  150. fi
  151. if [ "${SRCARCH}" != "um" ]; then
  152. if [ -n "${CONFIG_LTO_CLANG}" ]; then
  153. # Use vmlinux.o instead of performing the slow LTO
  154. # link again.
  155. objects="--whole-archive \
  156. vmlinux.o \
  157. --no-whole-archive \
  158. ${@}"
  159. else
  160. objects="--whole-archive \
  161. ${KBUILD_VMLINUX_OBJS} \
  162. --no-whole-archive \
  163. --start-group \
  164. ${KBUILD_VMLINUX_LIBS} \
  165. --end-group \
  166. ${@}"
  167. fi
  168. ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
  169. ${strip_debug#-Wl,} \
  170. -o ${output} \
  171. -T ${lds} ${objects}
  172. else
  173. objects="-Wl,--whole-archive \
  174. ${KBUILD_VMLINUX_OBJS} \
  175. -Wl,--no-whole-archive \
  176. -Wl,--start-group \
  177. ${KBUILD_VMLINUX_LIBS} \
  178. -Wl,--end-group \
  179. ${@}"
  180. ${CC} ${CFLAGS_vmlinux} \
  181. ${strip_debug} \
  182. -o ${output} \
  183. -Wl,-T,${lds} \
  184. ${objects} \
  185. -lutil -lrt -lpthread
  186. rm -f linux
  187. fi
  188. }
  189. # generate .BTF typeinfo from DWARF debuginfo
  190. # ${1} - vmlinux image
  191. # ${2} - file to dump raw BTF data into
  192. gen_btf()
  193. {
  194. local pahole_ver
  195. if ! [ -x "$(command -v ${PAHOLE})" ]; then
  196. echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
  197. return 1
  198. fi
  199. pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
  200. if [ "${pahole_ver}" -lt "116" ]; then
  201. echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16"
  202. return 1
  203. fi
  204. vmlinux_link ${1}
  205. info "BTF" ${2}
  206. LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
  207. # Create ${2} which contains just .BTF section but no symbols. Add
  208. # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
  209. # deletes all symbols including __start_BTF and __stop_BTF, which will
  210. # be redefined in the linker script. Add 2>/dev/null to suppress GNU
  211. # objcopy warnings: "empty loadable segment detected at ..."
  212. ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
  213. --strip-all ${1} ${2} 2>/dev/null
  214. # Change e_type to ET_REL so that it can be used to link final vmlinux.
  215. # GNU ld 2.35+ and lld do not allow an ET_EXEC input.
  216. if [ -n "${CONFIG_CPU_BIG_ENDIAN}" ]; then
  217. et_rel='\0\1'
  218. else
  219. et_rel='\1\0'
  220. fi
  221. printf "${et_rel}" | dd of=${2} conv=notrunc bs=1 seek=16 status=none
  222. }
  223. # Create ${2} .S file with all symbols from the ${1} object file
  224. kallsyms()
  225. {
  226. local kallsymopt;
  227. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  228. kallsymopt="${kallsymopt} --all-symbols"
  229. fi
  230. if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
  231. kallsymopt="${kallsymopt} --absolute-percpu"
  232. fi
  233. if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  234. kallsymopt="${kallsymopt} --base-relative"
  235. fi
  236. info KSYMS ${2}
  237. if [ -n "${CONFIG_CFI_CLANG}" ]; then
  238. ${PERL} ${srctree}/scripts/generate_cfi_kallsyms.pl ${1} | \
  239. sort -n > .tmp_kallsyms
  240. else
  241. ${NM} -n ${1} > .tmp_kallsyms
  242. fi
  243. scripts/kallsyms ${kallsymopt} < .tmp_kallsyms > ${2}
  244. }
  245. # Perform one step in kallsyms generation, including temporary linking of
  246. # vmlinux.
  247. kallsyms_step()
  248. {
  249. kallsymso_prev=${kallsymso}
  250. kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
  251. kallsymso=${kallsyms_vmlinux}.o
  252. kallsyms_S=${kallsyms_vmlinux}.S
  253. vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
  254. kallsyms ${kallsyms_vmlinux} ${kallsyms_S}
  255. info AS ${kallsymso}
  256. ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
  257. ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  258. -c -o ${kallsymso} ${kallsyms_S}
  259. }
  260. # Create map file with all symbols from ${1}
  261. # See mksymap for additional details
  262. mksysmap()
  263. {
  264. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  265. }
  266. sorttable()
  267. {
  268. ${objtree}/scripts/sorttable ${1}
  269. }
  270. # Delete output files in case of error
  271. cleanup()
  272. {
  273. rm -f .btf.*
  274. rm -f .tmp_System.map
  275. rm -f .tmp_kallsyms
  276. rm -f .tmp_initcalls.lds
  277. rm -f .tmp_symversions.lds
  278. rm -f .tmp_vmlinux*
  279. rm -f System.map
  280. rm -f vmlinux
  281. rm -f vmlinux.o
  282. }
  283. on_exit()
  284. {
  285. if [ $? -ne 0 ]; then
  286. cleanup
  287. fi
  288. }
  289. trap on_exit EXIT
  290. on_signals()
  291. {
  292. exit 1
  293. }
  294. trap on_signals HUP INT QUIT TERM
  295. # Use "make V=1" to debug this script
  296. case "${KBUILD_VERBOSE}" in
  297. *1*)
  298. set -x
  299. ;;
  300. esac
  301. if [ "$1" = "clean" ]; then
  302. cleanup
  303. exit 0
  304. fi
  305. # We need access to CONFIG_ symbols
  306. . include/config/auto.conf
  307. # Update version
  308. info GEN .version
  309. if [ -r .version ]; then
  310. VERSION=$(expr 0$(cat .version) + 1)
  311. echo $VERSION > .version
  312. else
  313. rm -f .version
  314. echo 1 > .version
  315. fi;
  316. # final build of init/
  317. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
  318. #link vmlinux.o
  319. modpost_link vmlinux.o
  320. objtool_link vmlinux.o
  321. # modpost vmlinux.o to check for section mismatches
  322. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1
  323. info MODINFO modules.builtin.modinfo
  324. ${OBJCOPY} -j .modinfo -O binary vmlinux.o modules.builtin.modinfo
  325. info GEN modules.builtin
  326. # The second line aids cases where multiple modules share the same object.
  327. tr '\0' '\n' < modules.builtin.modinfo | sed -n 's/^[[:alnum:]:_]*\.file=//p' |
  328. tr ' ' '\n' | uniq | sed -e 's:^:kernel/:' -e 's/$/.ko/' > modules.builtin
  329. btf_vmlinux_bin_o=""
  330. if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then
  331. btf_vmlinux_bin_o=.btf.vmlinux.bin.o
  332. if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
  333. echo >&2 "Failed to generate BTF for vmlinux"
  334. echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
  335. exit 1
  336. fi
  337. fi
  338. kallsymso=""
  339. kallsymso_prev=""
  340. kallsyms_vmlinux=""
  341. if [ -n "${CONFIG_KALLSYMS}" ]; then
  342. # kallsyms support
  343. # Generate section listing all symbols and add it into vmlinux
  344. # It's a three step process:
  345. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  346. # but __kallsyms is empty.
  347. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  348. # the right size
  349. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  350. # the right size, but due to the added section, some
  351. # addresses have shifted.
  352. # From here, we generate a correct .tmp_kallsyms2.o
  353. # 3) That link may have expanded the kernel image enough that
  354. # more linker branch stubs / trampolines had to be added, which
  355. # introduces new names, which further expands kallsyms. Do another
  356. # pass if that is the case. In theory it's possible this results
  357. # in even more stubs, but unlikely.
  358. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  359. # other bugs.
  360. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  361. #
  362. # a) Verify that the System.map from vmlinux matches the map from
  363. # ${kallsymso}.
  364. kallsyms_step 1
  365. kallsyms_step 2
  366. # step 3
  367. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
  368. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  369. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  370. kallsyms_step 3
  371. fi
  372. fi
  373. vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
  374. # fill in BTF IDs
  375. if [ -n "${CONFIG_DEBUG_INFO_BTF}" -a -n "${CONFIG_BPF}" ]; then
  376. info BTFIDS vmlinux
  377. ${RESOLVE_BTFIDS} vmlinux
  378. fi
  379. if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then
  380. info SORTTAB vmlinux
  381. if ! sorttable vmlinux; then
  382. echo >&2 Failed to sort kernel tables
  383. exit 1
  384. fi
  385. fi
  386. info SYSMAP System.map
  387. mksysmap vmlinux System.map
  388. # step a (see comment above)
  389. if [ -n "${CONFIG_KALLSYMS}" ]; then
  390. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  391. if ! cmp -s System.map .tmp_System.map; then
  392. echo >&2 Inconsistent kallsyms data
  393. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  394. exit 1
  395. fi
  396. fi