link-vmlinux.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # link vmlinux
  5. #
  6. # vmlinux is linked from the objects in vmlinux.a and $(KBUILD_VMLINUX_LIBS).
  7. # vmlinux.a contains objects that are linked unconditionally.
  8. # $(KBUILD_VMLINUX_LIBS) are archives which are linked conditionally
  9. # (not within --whole-archive), and do not require symbol indexes added.
  10. #
  11. # vmlinux
  12. # ^
  13. # |
  14. # +--< vmlinux.a
  15. # |
  16. # +--< $(KBUILD_VMLINUX_LIBS)
  17. # | +--< lib/lib.a + more
  18. # |
  19. # +-< ${kallsymso} (see description in KALLSYMS section)
  20. #
  21. # vmlinux version (uname -v) cannot be updated during normal
  22. # descending-into-subdirs phase since we do not yet know if we need to
  23. # update vmlinux.
  24. # Therefore this step is delayed until just before final link of vmlinux.
  25. #
  26. # System.map is generated to document addresses of all kernel symbols
  27. # Error out on error
  28. set -e
  29. LD="$1"
  30. KBUILD_LDFLAGS="$2"
  31. LDFLAGS_vmlinux="$3"
  32. is_enabled() {
  33. grep -q "^$1=y" include/config/auto.conf
  34. }
  35. # Nice output in kbuild format
  36. # Will be supressed by "make -s"
  37. info()
  38. {
  39. printf " %-7s %s\n" "${1}" "${2}"
  40. }
  41. # Link of vmlinux
  42. # ${1} - output file
  43. # ${2}, ${3}, ... - optional extra .o files
  44. vmlinux_link()
  45. {
  46. local output=${1}
  47. local objs
  48. local libs
  49. local ld
  50. local ldflags
  51. local ldlibs
  52. info LD ${output}
  53. # skip output file argument
  54. shift
  55. if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
  56. # Use vmlinux.o instead of performing the slow LTO link again.
  57. objs=vmlinux.o
  58. libs=
  59. else
  60. objs=vmlinux.a
  61. libs="${KBUILD_VMLINUX_LIBS}"
  62. fi
  63. if is_enabled CONFIG_MODULES; then
  64. objs="${objs} .vmlinux.export.o"
  65. fi
  66. objs="${objs} init/version-timestamp.o"
  67. if [ "${SRCARCH}" = "um" ]; then
  68. wl=-Wl,
  69. ld="${CC}"
  70. ldflags="${CFLAGS_vmlinux}"
  71. ldlibs="-lutil -lrt -lpthread"
  72. else
  73. wl=
  74. ld="${LD}"
  75. ldflags="${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux}"
  76. ldlibs=
  77. fi
  78. ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"
  79. # The kallsyms linking does not need debug symbols included.
  80. if [ "$output" != "${output#.tmp_vmlinux.kallsyms}" ] ; then
  81. ldflags="${ldflags} ${wl}--strip-debug"
  82. fi
  83. if is_enabled CONFIG_VMLINUX_MAP; then
  84. ldflags="${ldflags} ${wl}-Map=${output}.map"
  85. fi
  86. ${ld} ${ldflags} -o ${output} \
  87. ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \
  88. ${wl}--start-group ${libs} ${wl}--end-group \
  89. $@ ${ldlibs}
  90. }
  91. # generate .BTF typeinfo from DWARF debuginfo
  92. # ${1} - vmlinux image
  93. # ${2} - file to dump raw BTF data into
  94. gen_btf()
  95. {
  96. local pahole_ver
  97. if ! [ -x "$(command -v ${PAHOLE})" ]; then
  98. echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available"
  99. return 1
  100. fi
  101. pahole_ver=$(${PAHOLE} --version | sed -E 's/v([0-9]+)\.([0-9]+)/\1\2/')
  102. if [ "${pahole_ver}" -lt "116" ]; then
  103. echo >&2 "BTF: ${1}: pahole version $(${PAHOLE} --version) is too old, need at least v1.16"
  104. return 1
  105. fi
  106. vmlinux_link ${1}
  107. info "BTF" ${2}
  108. LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
  109. # Create ${2} which contains just .BTF section but no symbols. Add
  110. # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
  111. # deletes all symbols including __start_BTF and __stop_BTF, which will
  112. # be redefined in the linker script. Add 2>/dev/null to suppress GNU
  113. # objcopy warnings: "empty loadable segment detected at ..."
  114. ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
  115. --strip-all ${1} ${2} 2>/dev/null
  116. # Change e_type to ET_REL so that it can be used to link final vmlinux.
  117. # Unlike GNU ld, lld does not allow an ET_EXEC input.
  118. printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
  119. }
  120. # Create ${2} .S file with all symbols from the ${1} object file
  121. kallsyms()
  122. {
  123. local kallsymopt;
  124. if is_enabled CONFIG_KALLSYMS_ALL; then
  125. kallsymopt="${kallsymopt} --all-symbols"
  126. fi
  127. if is_enabled CONFIG_KALLSYMS_ABSOLUTE_PERCPU; then
  128. kallsymopt="${kallsymopt} --absolute-percpu"
  129. fi
  130. if is_enabled CONFIG_KALLSYMS_BASE_RELATIVE; then
  131. kallsymopt="${kallsymopt} --base-relative"
  132. fi
  133. if is_enabled CONFIG_LTO_CLANG; then
  134. kallsymopt="${kallsymopt} --lto-clang"
  135. fi
  136. info KSYMS ${2}
  137. scripts/kallsyms ${kallsymopt} ${1} > ${2}
  138. }
  139. # Perform one step in kallsyms generation, including temporary linking of
  140. # vmlinux.
  141. kallsyms_step()
  142. {
  143. kallsymso_prev=${kallsymso}
  144. kallsyms_vmlinux=.tmp_vmlinux.kallsyms${1}
  145. kallsymso=${kallsyms_vmlinux}.o
  146. kallsyms_S=${kallsyms_vmlinux}.S
  147. vmlinux_link ${kallsyms_vmlinux} "${kallsymso_prev}" ${btf_vmlinux_bin_o}
  148. mksysmap ${kallsyms_vmlinux} ${kallsyms_vmlinux}.syms
  149. kallsyms ${kallsyms_vmlinux}.syms ${kallsyms_S}
  150. info AS ${kallsyms_S}
  151. ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
  152. ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  153. -c -o ${kallsymso} ${kallsyms_S}
  154. }
  155. # Create map file with all symbols from ${1}
  156. # See mksymap for additional details
  157. mksysmap()
  158. {
  159. info NM ${2}
  160. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  161. }
  162. sorttable()
  163. {
  164. ${objtree}/scripts/sorttable ${1}
  165. }
  166. # Delete output files in case of error
  167. cleanup()
  168. {
  169. rm -f .btf.*
  170. rm -f System.map
  171. rm -f vmlinux
  172. rm -f vmlinux.map
  173. }
  174. # Use "make V=1" to debug this script
  175. case "${KBUILD_VERBOSE}" in
  176. *1*)
  177. set -x
  178. ;;
  179. esac
  180. if [ "$1" = "clean" ]; then
  181. cleanup
  182. exit 0
  183. fi
  184. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
  185. btf_vmlinux_bin_o=""
  186. if is_enabled CONFIG_DEBUG_INFO_BTF; then
  187. btf_vmlinux_bin_o=.btf.vmlinux.bin.o
  188. if ! gen_btf .tmp_vmlinux.btf $btf_vmlinux_bin_o ; then
  189. echo >&2 "Failed to generate BTF for vmlinux"
  190. echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
  191. exit 1
  192. fi
  193. fi
  194. kallsymso=""
  195. kallsymso_prev=""
  196. kallsyms_vmlinux=""
  197. if is_enabled CONFIG_KALLSYMS; then
  198. # kallsyms support
  199. # Generate section listing all symbols and add it into vmlinux
  200. # It's a three step process:
  201. # 1) Link .tmp_vmlinux.kallsyms1 so it has all symbols and sections,
  202. # but __kallsyms is empty.
  203. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  204. # the right size
  205. # 2) Link .tmp_vmlinux.kallsyms2 so it now has a __kallsyms section of
  206. # the right size, but due to the added section, some
  207. # addresses have shifted.
  208. # From here, we generate a correct .tmp_vmlinux.kallsyms2.o
  209. # 3) That link may have expanded the kernel image enough that
  210. # more linker branch stubs / trampolines had to be added, which
  211. # introduces new names, which further expands kallsyms. Do another
  212. # pass if that is the case. In theory it's possible this results
  213. # in even more stubs, but unlikely.
  214. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  215. # other bugs.
  216. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  217. #
  218. # a) Verify that the System.map from vmlinux matches the map from
  219. # ${kallsymso}.
  220. kallsyms_step 1
  221. kallsyms_step 2
  222. # step 3
  223. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso_prev})
  224. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  225. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  226. kallsyms_step 3
  227. fi
  228. fi
  229. vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o}
  230. # fill in BTF IDs
  231. if is_enabled CONFIG_DEBUG_INFO_BTF && is_enabled CONFIG_BPF; then
  232. info BTFIDS vmlinux
  233. ${RESOLVE_BTFIDS} vmlinux
  234. fi
  235. mksysmap vmlinux System.map
  236. if is_enabled CONFIG_BUILDTIME_TABLE_SORT; then
  237. info SORTTAB vmlinux
  238. if ! sorttable vmlinux; then
  239. echo >&2 Failed to sort kernel tables
  240. exit 1
  241. fi
  242. fi
  243. # step a (see comment above)
  244. if is_enabled CONFIG_KALLSYMS; then
  245. if ! cmp -s System.map ${kallsyms_vmlinux}.syms; then
  246. echo >&2 Inconsistent kallsyms data
  247. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  248. exit 1
  249. fi
  250. fi
  251. # For fixdep
  252. echo "vmlinux: $0" > .vmlinux.d