link-vmlinux.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.o 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.o mm/built-in.o + 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.o output file
  52. #
  53. archive_builtin()
  54. {
  55. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  56. info AR built-in.o
  57. rm -f built-in.o;
  58. ${AR} rcsTP${KBUILD_ARFLAGS} built-in.o \
  59. ${KBUILD_VMLINUX_INIT} \
  60. ${KBUILD_VMLINUX_MAIN}
  61. fi
  62. }
  63. # Link of vmlinux.o used for section mismatch analysis
  64. # ${1} output file
  65. modpost_link()
  66. {
  67. local objects
  68. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  69. objects="--whole-archive \
  70. built-in.o \
  71. --no-whole-archive \
  72. --start-group \
  73. ${KBUILD_VMLINUX_LIBS} \
  74. --end-group"
  75. else
  76. objects="${KBUILD_VMLINUX_INIT} \
  77. --start-group \
  78. ${KBUILD_VMLINUX_MAIN} \
  79. ${KBUILD_VMLINUX_LIBS} \
  80. --end-group"
  81. fi
  82. ${LD} ${LDFLAGS} -r -o ${1} ${objects}
  83. }
  84. # Link of vmlinux
  85. # ${1} - optional extra .o files
  86. # ${2} - output file
  87. vmlinux_link()
  88. {
  89. local lds="${objtree}/${KBUILD_LDS}"
  90. local objects
  91. if [ "${SRCARCH}" != "um" ]; then
  92. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  93. objects="--whole-archive \
  94. built-in.o \
  95. --no-whole-archive \
  96. --start-group \
  97. ${KBUILD_VMLINUX_LIBS} \
  98. --end-group \
  99. ${1}"
  100. else
  101. objects="${KBUILD_VMLINUX_INIT} \
  102. --start-group \
  103. ${KBUILD_VMLINUX_MAIN} \
  104. ${KBUILD_VMLINUX_LIBS} \
  105. --end-group \
  106. ${1}"
  107. fi
  108. ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
  109. -T ${lds} ${objects}
  110. else
  111. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  112. objects="-Wl,--whole-archive \
  113. built-in.o \
  114. -Wl,--no-whole-archive \
  115. -Wl,--start-group \
  116. ${KBUILD_VMLINUX_LIBS} \
  117. -Wl,--end-group \
  118. ${1}"
  119. else
  120. objects="${KBUILD_VMLINUX_INIT} \
  121. -Wl,--start-group \
  122. ${KBUILD_VMLINUX_MAIN} \
  123. ${KBUILD_VMLINUX_LIBS} \
  124. -Wl,--end-group \
  125. ${1}"
  126. fi
  127. ${CC} ${CFLAGS_vmlinux} -o ${2} \
  128. -Wl,-T,${lds} \
  129. ${objects} \
  130. -lutil -lrt -lpthread
  131. rm -f linux
  132. fi
  133. }
  134. # Create ${2} .o file with all symbols from the ${1} object file
  135. kallsyms()
  136. {
  137. info KSYM ${2}
  138. local kallsymopt;
  139. if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
  140. kallsymopt="${kallsymopt} --symbol-prefix=_"
  141. fi
  142. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  143. kallsymopt="${kallsymopt} --all-symbols"
  144. fi
  145. if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
  146. kallsymopt="${kallsymopt} --absolute-percpu"
  147. fi
  148. if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  149. kallsymopt="${kallsymopt} --base-relative"
  150. fi
  151. local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  152. ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
  153. local afile="`basename ${2} .o`.S"
  154. ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
  155. ${CC} ${aflags} -c -o ${2} ${afile}
  156. }
  157. # Create map file with all symbols from ${1}
  158. # See mksymap for additional details
  159. mksysmap()
  160. {
  161. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  162. }
  163. sortextable()
  164. {
  165. ${objtree}/scripts/sortextable ${1}
  166. }
  167. # Delete output files in case of error
  168. cleanup()
  169. {
  170. rm -f .tmp_System.map
  171. rm -f .tmp_kallsyms*
  172. rm -f .tmp_vmlinux*
  173. rm -f built-in.o
  174. rm -f System.map
  175. rm -f vmlinux
  176. rm -f vmlinux.o
  177. }
  178. on_exit()
  179. {
  180. if [ $? -ne 0 ]; then
  181. cleanup
  182. fi
  183. }
  184. trap on_exit EXIT
  185. on_signals()
  186. {
  187. exit 1
  188. }
  189. trap on_signals HUP INT QUIT TERM
  190. #
  191. #
  192. # Use "make V=1" to debug this script
  193. case "${KBUILD_VERBOSE}" in
  194. *1*)
  195. set -x
  196. ;;
  197. esac
  198. if [ "$1" = "clean" ]; then
  199. cleanup
  200. exit 0
  201. fi
  202. # We need access to CONFIG_ symbols
  203. case "${KCONFIG_CONFIG}" in
  204. */*)
  205. . "${KCONFIG_CONFIG}"
  206. ;;
  207. *)
  208. # Force using a file from the current directory
  209. . "./${KCONFIG_CONFIG}"
  210. esac
  211. # Update version
  212. info GEN .version
  213. if [ -r .version ]; then
  214. VERSION=$(expr 0$(cat .version) + 1)
  215. echo $VERSION > .version
  216. else
  217. rm -f .version
  218. echo 1 > .version
  219. fi;
  220. # final build of init/
  221. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
  222. archive_builtin
  223. #link vmlinux.o
  224. info LD vmlinux.o
  225. modpost_link vmlinux.o
  226. # modpost vmlinux.o to check for section mismatches
  227. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
  228. kallsymso=""
  229. kallsyms_vmlinux=""
  230. if [ -n "${CONFIG_KALLSYMS}" ]; then
  231. # kallsyms support
  232. # Generate section listing all symbols and add it into vmlinux
  233. # It's a three step process:
  234. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  235. # but __kallsyms is empty.
  236. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  237. # the right size
  238. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  239. # the right size, but due to the added section, some
  240. # addresses have shifted.
  241. # From here, we generate a correct .tmp_kallsyms2.o
  242. # 3) That link may have expanded the kernel image enough that
  243. # more linker branch stubs / trampolines had to be added, which
  244. # introduces new names, which further expands kallsyms. Do another
  245. # pass if that is the case. In theory it's possible this results
  246. # in even more stubs, but unlikely.
  247. # KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
  248. # other bugs.
  249. # 4) The correct ${kallsymso} is linked into the final vmlinux.
  250. #
  251. # a) Verify that the System.map from vmlinux matches the map from
  252. # ${kallsymso}.
  253. kallsymso=.tmp_kallsyms2.o
  254. kallsyms_vmlinux=.tmp_vmlinux2
  255. # step 1
  256. vmlinux_link "" .tmp_vmlinux1
  257. kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  258. # step 2
  259. vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  260. kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
  261. # step 3
  262. size1=$(stat -c "%s" .tmp_kallsyms1.o)
  263. size2=$(stat -c "%s" .tmp_kallsyms2.o)
  264. if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  265. kallsymso=.tmp_kallsyms3.o
  266. kallsyms_vmlinux=.tmp_vmlinux3
  267. vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  268. kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  269. fi
  270. fi
  271. info LD vmlinux
  272. vmlinux_link "${kallsymso}" vmlinux
  273. if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  274. info SORTEX vmlinux
  275. sortextable vmlinux
  276. fi
  277. info SYSMAP System.map
  278. mksysmap vmlinux System.map
  279. # step a (see comment above)
  280. if [ -n "${CONFIG_KALLSYMS}" ]; then
  281. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  282. if ! cmp -s System.map .tmp_System.map; then
  283. echo >&2 Inconsistent kallsyms data
  284. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  285. exit 1
  286. fi
  287. fi