link-vmlinux.sh 7.2 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. vmlinux_link()
  44. {
  45. local output=${1}
  46. local objs
  47. local libs
  48. local ld
  49. local ldflags
  50. local ldlibs
  51. info LD ${output}
  52. # skip output file argument
  53. shift
  54. if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
  55. # Use vmlinux.o instead of performing the slow LTO link again.
  56. objs=vmlinux.o
  57. libs=
  58. else
  59. objs=vmlinux.a
  60. libs="${KBUILD_VMLINUX_LIBS}"
  61. fi
  62. if is_enabled CONFIG_MODULES; then
  63. objs="${objs} .vmlinux.export.o"
  64. fi
  65. objs="${objs} init/version-timestamp.o"
  66. if [ "${SRCARCH}" = "um" ]; then
  67. wl=-Wl,
  68. ld="${CC}"
  69. ldflags="${CFLAGS_vmlinux}"
  70. ldlibs="-lutil -lrt -lpthread"
  71. else
  72. wl=
  73. ld="${LD}"
  74. ldflags="${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux}"
  75. ldlibs=
  76. fi
  77. ldflags="${ldflags} ${wl}--script=${objtree}/${KBUILD_LDS}"
  78. # The kallsyms linking does not need debug symbols included.
  79. if [ -n "${strip_debug}" ] ; then
  80. ldflags="${ldflags} ${wl}--strip-debug"
  81. fi
  82. if is_enabled CONFIG_VMLINUX_MAP; then
  83. ldflags="${ldflags} ${wl}-Map=${output}.map"
  84. fi
  85. ${ld} ${ldflags} -o ${output} \
  86. ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \
  87. ${wl}--start-group ${libs} ${wl}--end-group \
  88. ${kallsymso} ${btf_vmlinux_bin_o} ${ldlibs}
  89. }
  90. # generate .BTF typeinfo from DWARF debuginfo
  91. # ${1} - vmlinux image
  92. gen_btf()
  93. {
  94. local btf_data=${1}.btf.o
  95. info BTF "${btf_data}"
  96. LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1}
  97. # Create ${btf_data} which contains just .BTF section but no symbols. Add
  98. # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all
  99. # deletes all symbols including __start_BTF and __stop_BTF, which will
  100. # be redefined in the linker script. Add 2>/dev/null to suppress GNU
  101. # objcopy warnings: "empty loadable segment detected at ..."
  102. ${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
  103. --strip-all ${1} "${btf_data}" 2>/dev/null
  104. # Change e_type to ET_REL so that it can be used to link final vmlinux.
  105. # GNU ld 2.35+ and lld do not allow an ET_EXEC input.
  106. if is_enabled CONFIG_CPU_BIG_ENDIAN; then
  107. et_rel='\0\1'
  108. else
  109. et_rel='\1\0'
  110. fi
  111. printf "${et_rel}" | dd of="${btf_data}" conv=notrunc bs=1 seek=16 status=none
  112. btf_vmlinux_bin_o=${btf_data}
  113. }
  114. # Create ${2}.o file with all symbols from the ${1} object file
  115. kallsyms()
  116. {
  117. local kallsymopt;
  118. if is_enabled CONFIG_KALLSYMS_ALL; then
  119. kallsymopt="${kallsymopt} --all-symbols"
  120. fi
  121. if is_enabled CONFIG_KALLSYMS_ABSOLUTE_PERCPU; then
  122. kallsymopt="${kallsymopt} --absolute-percpu"
  123. fi
  124. info KSYMS "${2}.S"
  125. scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S"
  126. info AS "${2}.o"
  127. ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \
  128. ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} -c -o "${2}.o" "${2}.S"
  129. kallsymso=${2}.o
  130. }
  131. # Perform kallsyms for the given temporary vmlinux.
  132. sysmap_and_kallsyms()
  133. {
  134. mksysmap "${1}" "${1}.syms"
  135. kallsyms "${1}.syms" "${1}.kallsyms"
  136. kallsyms_sysmap=${1}.syms
  137. }
  138. # Create map file with all symbols from ${1}
  139. # See mksymap for additional details
  140. mksysmap()
  141. {
  142. info NM ${2}
  143. ${NM} -n "${1}" | sed -f "${srctree}/scripts/mksysmap" > "${2}"
  144. }
  145. sorttable()
  146. {
  147. ${objtree}/scripts/sorttable ${1}
  148. }
  149. cleanup()
  150. {
  151. rm -f .btf.*
  152. rm -f System.map
  153. rm -f vmlinux
  154. rm -f vmlinux.map
  155. }
  156. # Use "make V=1" to debug this script
  157. case "${KBUILD_VERBOSE}" in
  158. *1*)
  159. set -x
  160. ;;
  161. esac
  162. if [ "$1" = "clean" ]; then
  163. cleanup
  164. exit 0
  165. fi
  166. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init init/version-timestamp.o
  167. btf_vmlinux_bin_o=
  168. kallsymso=
  169. strip_debug=
  170. if is_enabled CONFIG_KALLSYMS; then
  171. true > .tmp_vmlinux.kallsyms0.syms
  172. kallsyms .tmp_vmlinux.kallsyms0.syms .tmp_vmlinux0.kallsyms
  173. fi
  174. if is_enabled CONFIG_KALLSYMS || is_enabled CONFIG_DEBUG_INFO_BTF; then
  175. # The kallsyms linking does not need debug symbols, but the BTF does.
  176. if ! is_enabled CONFIG_DEBUG_INFO_BTF; then
  177. strip_debug=1
  178. fi
  179. vmlinux_link .tmp_vmlinux1
  180. fi
  181. if is_enabled CONFIG_DEBUG_INFO_BTF; then
  182. if ! gen_btf .tmp_vmlinux1; then
  183. echo >&2 "Failed to generate BTF for vmlinux"
  184. echo >&2 "Try to disable CONFIG_DEBUG_INFO_BTF"
  185. exit 1
  186. fi
  187. fi
  188. if is_enabled CONFIG_KALLSYMS; then
  189. # kallsyms support
  190. # Generate section listing all symbols and add it into vmlinux
  191. # It's a four step process:
  192. # 0) Generate a dummy __kallsyms with empty symbol list.
  193. # 1) Link .tmp_vmlinux.kallsyms1 so it has all symbols and sections,
  194. # with a dummy __kallsyms.
  195. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  196. # the right size
  197. # 2) Link .tmp_vmlinux.kallsyms2 so it now has a __kallsyms section of
  198. # the right size, but due to the added section, some
  199. # addresses have shifted.
  200. # From here, we generate a correct .tmp_vmlinux.kallsyms2.o
  201. # 3) That link may have expanded the kernel image enough that
  202. # more linker branch stubs / trampolines had to be added, which
  203. # introduces new names, which further expands kallsyms. Do another
  204. # pass if that is the case. In theory it's possible this results
  205. # in even more stubs, but unlikely.
  206. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  207. # other bugs.
  208. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  209. #
  210. # a) Verify that the System.map from vmlinux matches the map from
  211. # ${kallsymso}.
  212. # The kallsyms linking does not need debug symbols included.
  213. strip_debug=1
  214. sysmap_and_kallsyms .tmp_vmlinux1
  215. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  216. vmlinux_link .tmp_vmlinux2
  217. sysmap_and_kallsyms .tmp_vmlinux2
  218. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" ${kallsymso})
  219. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  220. vmlinux_link .tmp_vmlinux3
  221. sysmap_and_kallsyms .tmp_vmlinux3
  222. fi
  223. fi
  224. strip_debug=
  225. vmlinux_link vmlinux
  226. # fill in BTF IDs
  227. if is_enabled CONFIG_DEBUG_INFO_BTF; then
  228. info BTFIDS vmlinux
  229. ${RESOLVE_BTFIDS} vmlinux
  230. fi
  231. mksysmap vmlinux System.map
  232. if is_enabled CONFIG_BUILDTIME_TABLE_SORT; then
  233. info SORTTAB vmlinux
  234. if ! sorttable vmlinux; then
  235. echo >&2 Failed to sort kernel tables
  236. exit 1
  237. fi
  238. fi
  239. # step a (see comment above)
  240. if is_enabled CONFIG_KALLSYMS; then
  241. if ! cmp -s System.map "${kallsyms_sysmap}"; then
  242. echo >&2 Inconsistent kallsyms data
  243. echo >&2 'Try "make KALLSYMS_EXTRA_PASS=1" as a workaround'
  244. exit 1
  245. fi
  246. fi
  247. # For fixdep
  248. echo "vmlinux: $0" > .vmlinux.d