link-vmlinux.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_INIT) and
  7. # $(KBUILD_VMLINUX_MAIN) and $(KBUILD_VMLINUX_LIBS). Most are built-in.a files
  8. # from top-level directories in the kernel tree, others are specified in
  9. # arch/$(ARCH)/Makefile. Ordering when linking is important, and
  10. # $(KBUILD_VMLINUX_INIT) must be first. $(KBUILD_VMLINUX_LIBS) are archives
  11. # which are linked conditionally (not within --whole-archive), and do not
  12. # require symbol indexes added.
  13. #
  14. # vmlinux
  15. # ^
  16. # |
  17. # +-< $(KBUILD_VMLINUX_INIT)
  18. # | +--< init/version.o + more
  19. # |
  20. # +--< $(KBUILD_VMLINUX_MAIN)
  21. # | +--< drivers/built-in.a mm/built-in.a + more
  22. # |
  23. # +--< $(KBUILD_VMLINUX_LIBS)
  24. # | +--< lib/lib.a + more
  25. # |
  26. # +-< ${kallsymso} (see description in KALLSYMS section)
  27. #
  28. # vmlinux version (uname -v) cannot be updated during normal
  29. # descending-into-subdirs phase since we do not yet know if we need to
  30. # update vmlinux.
  31. # Therefore this step is delayed until just before final link of vmlinux.
  32. #
  33. # System.map is generated to document addresses of all kernel symbols
  34. # Error out on error
  35. set -e
  36. # Nice output in kbuild format
  37. # Will be supressed by "make -s"
  38. info()
  39. {
  40. if [ "${quiet}" != "silent_" ]; then
  41. printf " %-7s %s\n" ${1} ${2}
  42. fi
  43. }
  44. # Thin archive build here makes a final archive with symbol table and indexes
  45. # from vmlinux objects INIT and MAIN, which can be used as input to linker.
  46. # KBUILD_VMLINUX_LIBS archives should already have symbol table and indexes
  47. # added.
  48. #
  49. # Traditional incremental style of link does not require this step
  50. #
  51. # built-in.a output file
  52. #
  53. archive_builtin()
  54. {
  55. info AR built-in.a
  56. rm -f built-in.a;
  57. ${AR} rcsTP${KBUILD_ARFLAGS} built-in.a \
  58. ${KBUILD_VMLINUX_INIT} \
  59. ${KBUILD_VMLINUX_MAIN}
  60. }
  61. # Link of vmlinux.o used for section mismatch analysis
  62. # ${1} output file
  63. modpost_link()
  64. {
  65. local objects
  66. objects="--whole-archive \
  67. built-in.a \
  68. --no-whole-archive \
  69. --start-group \
  70. ${KBUILD_VMLINUX_LIBS} \
  71. --end-group"
  72. ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
  73. }
  74. # Link of vmlinux
  75. # ${1} - optional extra .o files
  76. # ${2} - output file
  77. vmlinux_link()
  78. {
  79. local lds="${objtree}/${KBUILD_LDS}"
  80. local objects
  81. if [ "${SRCARCH}" != "um" ]; then
  82. objects="--whole-archive \
  83. built-in.a \
  84. --no-whole-archive \
  85. --start-group \
  86. ${KBUILD_VMLINUX_LIBS} \
  87. --end-group \
  88. ${1}"
  89. ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
  90. -T ${lds} ${objects}
  91. else
  92. objects="-Wl,--whole-archive \
  93. built-in.a \
  94. -Wl,--no-whole-archive \
  95. -Wl,--start-group \
  96. ${KBUILD_VMLINUX_LIBS} \
  97. -Wl,--end-group \
  98. ${1}"
  99. ${CC} ${CFLAGS_vmlinux} -o ${2} \
  100. -Wl,-T,${lds} \
  101. ${objects} \
  102. -lutil -lrt -lpthread
  103. rm -f linux
  104. fi
  105. }
  106. # Create ${2} .o file with all symbols from the ${1} object file
  107. kallsyms()
  108. {
  109. info KSYM ${2}
  110. local kallsymopt;
  111. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  112. kallsymopt="${kallsymopt} --all-symbols"
  113. fi
  114. if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
  115. kallsymopt="${kallsymopt} --absolute-percpu"
  116. fi
  117. if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  118. kallsymopt="${kallsymopt} --base-relative"
  119. fi
  120. local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  121. ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
  122. local afile="`basename ${2} .o`.S"
  123. ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
  124. ${CC} ${aflags} -c -o ${2} ${afile}
  125. }
  126. # Create map file with all symbols from ${1}
  127. # See mksymap for additional details
  128. mksysmap()
  129. {
  130. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  131. }
  132. sortextable()
  133. {
  134. ${objtree}/scripts/sortextable ${1}
  135. }
  136. # Delete output files in case of error
  137. cleanup()
  138. {
  139. rm -f .tmp_System.map
  140. rm -f .tmp_kallsyms*
  141. rm -f .tmp_vmlinux*
  142. rm -f built-in.a
  143. rm -f System.map
  144. rm -f vmlinux
  145. rm -f vmlinux.o
  146. }
  147. on_exit()
  148. {
  149. if [ $? -ne 0 ]; then
  150. cleanup
  151. fi
  152. }
  153. trap on_exit EXIT
  154. on_signals()
  155. {
  156. exit 1
  157. }
  158. trap on_signals HUP INT QUIT TERM
  159. #
  160. #
  161. # Use "make V=1" to debug this script
  162. case "${KBUILD_VERBOSE}" in
  163. *1*)
  164. set -x
  165. ;;
  166. esac
  167. if [ "$1" = "clean" ]; then
  168. cleanup
  169. exit 0
  170. fi
  171. # We need access to CONFIG_ symbols
  172. case "${KCONFIG_CONFIG}" in
  173. */*)
  174. . "${KCONFIG_CONFIG}"
  175. ;;
  176. *)
  177. # Force using a file from the current directory
  178. . "./${KCONFIG_CONFIG}"
  179. esac
  180. # Update version
  181. info GEN .version
  182. if [ -r .version ]; then
  183. VERSION=$(expr 0$(cat .version) + 1)
  184. echo $VERSION > .version
  185. else
  186. rm -f .version
  187. echo 1 > .version
  188. fi;
  189. # final build of init/
  190. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
  191. archive_builtin
  192. #link vmlinux.o
  193. info LD vmlinux.o
  194. modpost_link vmlinux.o
  195. # modpost vmlinux.o to check for section mismatches
  196. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
  197. kallsymso=""
  198. kallsyms_vmlinux=""
  199. if [ -n "${CONFIG_KALLSYMS}" ]; then
  200. # kallsyms support
  201. # Generate section listing all symbols and add it into vmlinux
  202. # It's a three step process:
  203. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  204. # but __kallsyms is empty.
  205. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  206. # the right size
  207. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  208. # the right size, but due to the added section, some
  209. # addresses have shifted.
  210. # From here, we generate a correct .tmp_kallsyms2.o
  211. # 3) That link may have expanded the kernel image enough that
  212. # more linker branch stubs / trampolines had to be added, which
  213. # introduces new names, which further expands kallsyms. Do another
  214. # pass if that is the case. In theory it's possible this results
  215. # in even more stubs, but unlikely.
  216. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  217. # other bugs.
  218. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  219. #
  220. # a) Verify that the System.map from vmlinux matches the map from
  221. # ${kallsymso}.
  222. kallsymso=.tmp_kallsyms2.o
  223. kallsyms_vmlinux=.tmp_vmlinux2
  224. # step 1
  225. vmlinux_link "" .tmp_vmlinux1
  226. kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  227. # step 2
  228. vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  229. kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
  230. # step 3
  231. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
  232. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
  233. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  234. kallsymso=.tmp_kallsyms3.o
  235. kallsyms_vmlinux=.tmp_vmlinux3
  236. vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  237. kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  238. fi
  239. fi
  240. info LD vmlinux
  241. vmlinux_link "${kallsymso}" vmlinux
  242. if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  243. info SORTEX vmlinux
  244. sortextable vmlinux
  245. fi
  246. info SYSMAP System.map
  247. mksysmap vmlinux System.map
  248. # step a (see comment above)
  249. if [ -n "${CONFIG_KALLSYMS}" ]; then
  250. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  251. if ! cmp -s System.map .tmp_System.map; then
  252. echo >&2 Inconsistent kallsyms data
  253. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  254. exit 1
  255. fi
  256. fi