link-vmlinux.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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). Most are built-in.o files from top-level directories
  7. # in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
  8. # Ordering when linking is important, and $(KBUILD_VMLINUX_INIT) must be first.
  9. #
  10. # vmlinux
  11. # ^
  12. # |
  13. # +-< $(KBUILD_VMLINUX_INIT)
  14. # | +--< init/version.o + more
  15. # |
  16. # +--< $(KBUILD_VMLINUX_MAIN)
  17. # | +--< drivers/built-in.o mm/built-in.o + 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. # Nice output in kbuild format
  30. # Will be supressed by "make -s"
  31. info()
  32. {
  33. if [ "${quiet}" != "silent_" ]; then
  34. printf " %-7s %s\n" ${1} ${2}
  35. fi
  36. }
  37. # Thin archive build here makes a final archive with
  38. # symbol table and indexes from vmlinux objects, which can be
  39. # used as input to linker.
  40. #
  41. # Traditional incremental style of link does not require this step
  42. #
  43. # built-in.o output file
  44. #
  45. archive_builtin()
  46. {
  47. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  48. info AR built-in.o
  49. rm -f built-in.o;
  50. ${AR} rcsT${KBUILD_ARFLAGS} built-in.o \
  51. ${KBUILD_VMLINUX_INIT} \
  52. ${KBUILD_VMLINUX_MAIN}
  53. fi
  54. }
  55. # Link of vmlinux.o used for section mismatch analysis
  56. # ${1} output file
  57. modpost_link()
  58. {
  59. local objects
  60. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  61. objects="--whole-archive built-in.o"
  62. else
  63. objects="${KBUILD_VMLINUX_INIT} \
  64. --start-group \
  65. ${KBUILD_VMLINUX_MAIN} \
  66. --end-group"
  67. fi
  68. ${LD} ${LDFLAGS} -r -o ${1} ${objects}
  69. }
  70. # Link of vmlinux
  71. # ${1} - optional extra .o files
  72. # ${2} - output file
  73. vmlinux_link()
  74. {
  75. local lds="${objtree}/${KBUILD_LDS}"
  76. local objects
  77. if [ "${SRCARCH}" != "um" ]; then
  78. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  79. objects="--whole-archive built-in.o ${1}"
  80. else
  81. objects="${KBUILD_VMLINUX_INIT} \
  82. --start-group \
  83. ${KBUILD_VMLINUX_MAIN} \
  84. --end-group \
  85. ${1}"
  86. fi
  87. ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
  88. -T ${lds} ${objects}
  89. else
  90. if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
  91. objects="-Wl,--whole-archive built-in.o ${1}"
  92. else
  93. objects="${KBUILD_VMLINUX_INIT} \
  94. -Wl,--start-group \
  95. ${KBUILD_VMLINUX_MAIN} \
  96. -Wl,--end-group \
  97. ${1}"
  98. fi
  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 .old_version
  143. rm -f .tmp_System.map
  144. rm -f .tmp_kallsyms*
  145. rm -f .tmp_version
  146. rm -f .tmp_vmlinux*
  147. rm -f built-in.o
  148. rm -f System.map
  149. rm -f vmlinux
  150. rm -f vmlinux.o
  151. }
  152. on_exit()
  153. {
  154. if [ $? -ne 0 ]; then
  155. cleanup
  156. fi
  157. }
  158. trap on_exit EXIT
  159. on_signals()
  160. {
  161. exit 1
  162. }
  163. trap on_signals HUP INT QUIT TERM
  164. #
  165. #
  166. # Use "make V=1" to debug this script
  167. case "${KBUILD_VERBOSE}" in
  168. *1*)
  169. set -x
  170. ;;
  171. esac
  172. if [ "$1" = "clean" ]; then
  173. cleanup
  174. exit 0
  175. fi
  176. # We need access to CONFIG_ symbols
  177. case "${KCONFIG_CONFIG}" in
  178. */*)
  179. . "${KCONFIG_CONFIG}"
  180. ;;
  181. *)
  182. # Force using a file from the current directory
  183. . "./${KCONFIG_CONFIG}"
  184. esac
  185. archive_builtin
  186. #link vmlinux.o
  187. info LD vmlinux.o
  188. modpost_link vmlinux.o
  189. # modpost vmlinux.o to check for section mismatches
  190. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
  191. # Update version
  192. info GEN .version
  193. if [ ! -r .version ]; then
  194. rm -f .version;
  195. echo 1 >.version;
  196. else
  197. mv .version .old_version;
  198. expr 0$(cat .old_version) + 1 >.version;
  199. fi;
  200. # final build of init/
  201. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
  202. kallsymso=""
  203. kallsyms_vmlinux=""
  204. if [ -n "${CONFIG_KALLSYMS}" ]; then
  205. # kallsyms support
  206. # Generate section listing all symbols and add it into vmlinux
  207. # It's a three step process:
  208. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  209. # but __kallsyms is empty.
  210. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  211. # the right size
  212. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  213. # the right size, but due to the added section, some
  214. # addresses have shifted.
  215. # From here, we generate a correct .tmp_kallsyms2.o
  216. # 2a) We may use an extra pass as this has been necessary to
  217. # woraround some alignment related bugs.
  218. # KALLSYMS_EXTRA_PASS=1 is used to trigger this.
  219. # 3) The correct ${kallsymso} is linked into the final vmlinux.
  220. #
  221. # a) Verify that the System.map from vmlinux matches the map from
  222. # ${kallsymso}.
  223. kallsymso=.tmp_kallsyms2.o
  224. kallsyms_vmlinux=.tmp_vmlinux2
  225. # step 1
  226. vmlinux_link "" .tmp_vmlinux1
  227. kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  228. # step 2
  229. vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  230. kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
  231. # step 2a
  232. if [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  233. kallsymso=.tmp_kallsyms3.o
  234. kallsyms_vmlinux=.tmp_vmlinux3
  235. vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  236. kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  237. fi
  238. fi
  239. info LD vmlinux
  240. vmlinux_link "${kallsymso}" vmlinux
  241. if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  242. info SORTEX vmlinux
  243. sortextable vmlinux
  244. fi
  245. info SYSMAP System.map
  246. mksysmap vmlinux System.map
  247. # step a (see comment above)
  248. if [ -n "${CONFIG_KALLSYMS}" ]; then
  249. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  250. if ! cmp -s System.map .tmp_System.map; then
  251. echo >&2 Inconsistent kallsyms data
  252. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  253. exit 1
  254. fi
  255. fi
  256. # We made a new kernel - delete old version file
  257. rm -f .old_version