link-vmlinux.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. VMLINUX="$4"
  33. is_enabled() {
  34. grep -q "^$1=y" include/config/auto.conf
  35. }
  36. # Nice output in kbuild format
  37. # Will be supressed by "make -s"
  38. info()
  39. {
  40. printf " %-7s %s\n" "${1}" "${2}"
  41. }
  42. # Link of vmlinux
  43. # ${1} - output file
  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 ||
  56. is_enabled CONFIG_KLP_BUILD; then
  57. # Use vmlinux.o instead of performing the slow LTO link again.
  58. objs=vmlinux.o
  59. libs=
  60. else
  61. objs=vmlinux.a
  62. libs="${KBUILD_VMLINUX_LIBS}"
  63. fi
  64. if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then
  65. objs="${objs} .builtin-dtbs.o"
  66. fi
  67. objs="${objs} .vmlinux.export.o"
  68. objs="${objs} init/version-timestamp.o"
  69. if [ "${SRCARCH}" = "um" ]; then
  70. wl=-Wl,
  71. ld="${CC}"
  72. ldflags="${CFLAGS_vmlinux}"
  73. ldlibs="-lutil -lrt -lpthread"
  74. else
  75. wl=
  76. ld="${LD}"
  77. ldflags="${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux}"
  78. ldlibs=
  79. fi
  80. ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"
  81. # The kallsyms linking does not need debug symbols included.
  82. if [ -n "${strip_debug}" ] ; then
  83. ldflags="${ldflags} ${wl}--strip-debug"
  84. fi
  85. if [ -n "${generate_map}" ]; then
  86. ldflags="${ldflags} ${wl}-Map=vmlinux.map"
  87. fi
  88. ${ld} ${ldflags} -o ${output} \
  89. ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \
  90. ${wl}--start-group ${libs} ${wl}--end-group \
  91. ${kallsymso} ${btf_vmlinux_bin_o} ${arch_vmlinux_o} ${ldlibs}
  92. }
  93. # Create ${2}.o file with all symbols from the ${1} object file
  94. kallsyms()
  95. {
  96. local kallsymopt;
  97. if is_enabled CONFIG_KALLSYMS_ALL; then
  98. kallsymopt="${kallsymopt} --all-symbols"
  99. fi
  100. if is_enabled CONFIG_64BIT || is_enabled CONFIG_RELOCATABLE; then
  101. kallsymopt="${kallsymopt} --pc-relative"
  102. fi
  103. info KSYMS "${2}.S"
  104. scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S"
  105. info AS "${2}.o"
  106. ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
  107. ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} -c -o "${2}.o" "${2}.S"
  108. kallsymso=${2}.o
  109. }
  110. # Perform kallsyms for the given temporary vmlinux.
  111. sysmap_and_kallsyms()
  112. {
  113. mksysmap "${1}" "${1}.syms"
  114. kallsyms "${1}.syms" "${1}.kallsyms"
  115. kallsyms_sysmap=${1}.syms
  116. }
  117. # Create map file with all symbols from ${1}
  118. # See mksymap for additional details
  119. mksysmap()
  120. {
  121. info NM ${2}
  122. ${NM} -n "${1}" | sed -f "${srctree}/scripts/mksysmap" > "${2}"
  123. }
  124. sorttable()
  125. {
  126. ${NM} -S ${1} > .tmp_vmlinux.nm-sort
  127. ${objtree}/scripts/sorttable -s .tmp_vmlinux.nm-sort ${1}
  128. }
  129. cleanup()
  130. {
  131. rm -f .btf.*
  132. rm -f .tmp_vmlinux.nm-sort
  133. rm -f System.map
  134. rm -f vmlinux
  135. rm -f vmlinux.map
  136. }
  137. # Use "make V=1" to debug this script
  138. case "${KBUILD_VERBOSE}" in
  139. *1*)
  140. set -x
  141. ;;
  142. esac
  143. if [ "$1" = "clean" ]; then
  144. cleanup
  145. exit 0
  146. fi
  147. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
  148. arch_vmlinux_o=
  149. if is_enabled CONFIG_ARCH_WANTS_PRE_LINK_VMLINUX; then
  150. arch_vmlinux_o=arch/${SRCARCH}/tools/vmlinux.arch.o
  151. fi
  152. btf_vmlinux_bin_o=
  153. btfids_vmlinux=
  154. kallsymso=
  155. strip_debug=
  156. generate_map=
  157. # Use "make UT=1" to trigger warnings on unused tracepoints
  158. case "${WARN_ON_UNUSED_TRACEPOINTS}" in
  159. *1*)
  160. ${objtree}/scripts/tracepoint-update vmlinux.o
  161. ;;
  162. esac
  163. if is_enabled CONFIG_KALLSYMS; then
  164. true > .tmp_vmlinux0.syms
  165. kallsyms .tmp_vmlinux0.syms .tmp_vmlinux0.kallsyms
  166. fi
  167. if is_enabled CONFIG_KALLSYMS || is_enabled CONFIG_DEBUG_INFO_BTF; then
  168. # The kallsyms linking does not need debug symbols, but the BTF does.
  169. if ! is_enabled CONFIG_DEBUG_INFO_BTF; then
  170. strip_debug=1
  171. fi
  172. vmlinux_link .tmp_vmlinux1
  173. fi
  174. if is_enabled CONFIG_DEBUG_INFO_BTF; then
  175. info BTF .tmp_vmlinux1
  176. if ! ${CONFIG_SHELL} ${srctree}/scripts/gen-btf.sh .tmp_vmlinux1; then
  177. echo >&2 "Failed to generate BTF for vmlinux"
  178. echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
  179. exit 1
  180. fi
  181. btf_vmlinux_bin_o=.tmp_vmlinux1.btf.o
  182. btfids_vmlinux=.tmp_vmlinux1.BTF_ids
  183. fi
  184. if is_enabled CONFIG_KALLSYMS; then
  185. # kallsyms support
  186. # Generate section listing all symbols and add it into vmlinux
  187. # It's a four step process:
  188. # 0) Generate a dummy __kallsyms with empty symbol list.
  189. # 1) Link .tmp_vmlinux1.kallsyms so it has all symbols and sections,
  190. # with a dummy __kallsyms.
  191. # Running kallsyms on that gives us .tmp_vmlinux1.kallsyms.o with
  192. # the right size
  193. # 2) Link .tmp_vmlinux2.kallsyms so it now has a __kallsyms section of
  194. # the right size, but due to the added section, some
  195. # addresses have shifted.
  196. # From here, we generate a correct .tmp_vmlinux2.kallsyms.o
  197. # 3) That link may have expanded the kernel image enough that
  198. # more linker branch stubs / trampolines had to be added, which
  199. # introduces new names, which further expands kallsyms. Do another
  200. # pass if that is the case. In theory it's possible this results
  201. # in even more stubs, but unlikely.
  202. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  203. # other bugs.
  204. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  205. #
  206. # a) Verify that the System.map from vmlinux matches the map from
  207. # ${kallsymso}.
  208. # The kallsyms linking does not need debug symbols included.
  209. strip_debug=1
  210. sysmap_and_kallsyms .tmp_vmlinux1
  211. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  212. vmlinux_link .tmp_vmlinux2
  213. sysmap_and_kallsyms .tmp_vmlinux2
  214. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  215. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  216. vmlinux_link .tmp_vmlinux3
  217. sysmap_and_kallsyms .tmp_vmlinux3
  218. fi
  219. fi
  220. strip_debug=
  221. if is_enabled CONFIG_VMLINUX_MAP; then
  222. generate_map=1
  223. fi
  224. vmlinux_link "${VMLINUX}"
  225. if is_enabled CONFIG_DEBUG_INFO_BTF; then
  226. info BTFIDS ${VMLINUX}
  227. ${RESOLVE_BTFIDS} --patch_btfids ${btfids_vmlinux} ${VMLINUX}
  228. fi
  229. mksysmap "${VMLINUX}" System.map
  230. if is_enabled CONFIG_BUILDTIME_TABLE_SORT; then
  231. info SORTTAB "${VMLINUX}"
  232. if ! sorttable "${VMLINUX}"; then
  233. echo >&2 Failed to sort kernel tables
  234. exit 1
  235. fi
  236. fi
  237. # step a (see comment above)
  238. if is_enabled CONFIG_KALLSYMS; then
  239. if ! cmp -s System.map "${kallsyms_sysmap}"; then
  240. echo >&2 Inconsistent kallsyms data
  241. echo >&2 'Try "make KALLSYMS_EXTRA_PASS=1" as a workaround'
  242. exit 1
  243. fi
  244. fi
  245. # For fixdep
  246. echo "${VMLINUX}: $0" > ".${VMLINUX}.d"