link-vmlinux.sh 7.7 KB

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