link-vmlinux.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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} ${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} ${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_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
  112. kallsymopt="${kallsymopt} --symbol-prefix=_"
  113. fi
  114. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  115. kallsymopt="${kallsymopt} --all-symbols"
  116. fi
  117. if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
  118. kallsymopt="${kallsymopt} --absolute-percpu"
  119. fi
  120. if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  121. kallsymopt="${kallsymopt} --base-relative"
  122. fi
  123. local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  124. ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
  125. local afile="`basename ${2} .o`.S"
  126. ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
  127. ${CC} ${aflags} -c -o ${2} ${afile}
  128. }
  129. # Create map file with all symbols from ${1}
  130. # See mksymap for additional details
  131. mksysmap()
  132. {
  133. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  134. }
  135. sortextable()
  136. {
  137. ${objtree}/scripts/sortextable ${1}
  138. }
  139. # Delete output files in case of error
  140. cleanup()
  141. {
  142. rm -f .tmp_System.map
  143. rm -f .tmp_kallsyms*
  144. rm -f .tmp_vmlinux*
  145. rm -f built-in.a
  146. rm -f System.map
  147. rm -f vmlinux
  148. rm -f vmlinux.o
  149. }
  150. on_exit()
  151. {
  152. if [ $? -ne 0 ]; then
  153. cleanup
  154. fi
  155. }
  156. trap on_exit EXIT
  157. on_signals()
  158. {
  159. exit 1
  160. }
  161. trap on_signals HUP INT QUIT TERM
  162. #
  163. #
  164. # Use "make V=1" to debug this script
  165. case "${KBUILD_VERBOSE}" in
  166. *1*)
  167. set -x
  168. ;;
  169. esac
  170. if [ "$1" = "clean" ]; then
  171. cleanup
  172. exit 0
  173. fi
  174. # We need access to CONFIG_ symbols
  175. case "${KCONFIG_CONFIG}" in
  176. */*)
  177. . "${KCONFIG_CONFIG}"
  178. ;;
  179. *)
  180. # Force using a file from the current directory
  181. . "./${KCONFIG_CONFIG}"
  182. esac
  183. # Update version
  184. info GEN .version
  185. if [ -r .version ]; then
  186. VERSION=$(expr 0$(cat .version) + 1)
  187. echo $VERSION > .version
  188. else
  189. rm -f .version
  190. echo 1 > .version
  191. fi;
  192. # final build of init/
  193. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
  194. archive_builtin
  195. #link vmlinux.o
  196. info LD vmlinux.o
  197. modpost_link vmlinux.o
  198. # modpost vmlinux.o to check for section mismatches
  199. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
  200. kallsymso=""
  201. kallsyms_vmlinux=""
  202. if [ -n "${CONFIG_KALLSYMS}" ]; then
  203. # kallsyms support
  204. # Generate section listing all symbols and add it into vmlinux
  205. # It's a three step process:
  206. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  207. # but __kallsyms is empty.
  208. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  209. # the right size
  210. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  211. # the right size, but due to the added section, some
  212. # addresses have shifted.
  213. # From here, we generate a correct .tmp_kallsyms2.o
  214. # 3) That link may have expanded the kernel image enough that
  215. # more linker branch stubs / trampolines had to be added, which
  216. # introduces new names, which further expands kallsyms. Do another
  217. # pass if that is the case. In theory it's possible this results
  218. # in even more stubs, but unlikely.
  219. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  220. # other bugs.
  221. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  222. #
  223. # a) Verify that the System.map from vmlinux matches the map from
  224. # ${kallsymso}.
  225. kallsymso=.tmp_kallsyms2.o
  226. kallsyms_vmlinux=.tmp_vmlinux2
  227. # step 1
  228. vmlinux_link "" .tmp_vmlinux1
  229. kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  230. # step 2
  231. vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  232. kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
  233. # step 3
  234. size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
  235. size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)
  236. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  237. kallsymso=.tmp_kallsyms3.o
  238. kallsyms_vmlinux=.tmp_vmlinux3
  239. vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  240. kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  241. fi
  242. fi
  243. info LD vmlinux
  244. vmlinux_link "${kallsymso}" vmlinux
  245. if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  246. info SORTEX vmlinux
  247. sortextable vmlinux
  248. fi
  249. info SYSMAP System.map
  250. mksysmap vmlinux System.map
  251. # step a (see comment above)
  252. if [ -n "${CONFIG_KALLSYMS}" ]; then
  253. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  254. if ! cmp -s System.map .tmp_System.map; then
  255. echo >&2 Inconsistent kallsyms data
  256. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  257. exit 1
  258. fi
  259. fi