link-vmlinux.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. # Link of vmlinux.o used for section mismatch analysis
  38. # ${1} output file
  39. modpost_link()
  40. {
  41. ${LD} ${LDFLAGS} -r -o ${1} ${KBUILD_VMLINUX_INIT} \
  42. --start-group ${KBUILD_VMLINUX_MAIN} --end-group
  43. }
  44. # Link of vmlinux
  45. # ${1} - optional extra .o files
  46. # ${2} - output file
  47. vmlinux_link()
  48. {
  49. local lds="${objtree}/${KBUILD_LDS}"
  50. if [ "${SRCARCH}" != "um" ]; then
  51. ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
  52. -T ${lds} ${KBUILD_VMLINUX_INIT} \
  53. --start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1}
  54. else
  55. ${CC} ${CFLAGS_vmlinux} -o ${2} \
  56. -Wl,-T,${lds} ${KBUILD_VMLINUX_INIT} \
  57. -Wl,--start-group \
  58. ${KBUILD_VMLINUX_MAIN} \
  59. -Wl,--end-group \
  60. -lutil -lrt -lpthread ${1}
  61. rm -f linux
  62. fi
  63. }
  64. # Create ${2} .o file with all symbols from the ${1} object file
  65. kallsyms()
  66. {
  67. info KSYM ${2}
  68. local kallsymopt;
  69. if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
  70. kallsymopt="${kallsymopt} --symbol-prefix=_"
  71. fi
  72. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  73. kallsymopt="${kallsymopt} --all-symbols"
  74. fi
  75. if [ -n "${CONFIG_ARM}" ] && [ -z "${CONFIG_XIP_KERNEL}" ] && [ -n "${CONFIG_PAGE_OFFSET}" ]; then
  76. kallsymopt="${kallsymopt} --page-offset=$CONFIG_PAGE_OFFSET"
  77. fi
  78. if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
  79. kallsymopt="${kallsymopt} --absolute-percpu"
  80. fi
  81. if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
  82. kallsymopt="${kallsymopt} --base-relative"
  83. fi
  84. local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  85. ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
  86. local afile="`basename ${2} .o`.S"
  87. ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
  88. ${CC} ${aflags} -c -o ${2} ${afile}
  89. }
  90. # Create map file with all symbols from ${1}
  91. # See mksymap for additional details
  92. mksysmap()
  93. {
  94. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  95. }
  96. sortextable()
  97. {
  98. ${objtree}/scripts/sortextable ${1}
  99. }
  100. # Delete output files in case of error
  101. cleanup()
  102. {
  103. rm -f .old_version
  104. rm -f .tmp_System.map
  105. rm -f .tmp_kallsyms*
  106. rm -f .tmp_version
  107. rm -f .tmp_vmlinux*
  108. rm -f System.map
  109. rm -f vmlinux
  110. rm -f vmlinux.o
  111. }
  112. on_exit()
  113. {
  114. if [ $? -ne 0 ]; then
  115. cleanup
  116. fi
  117. }
  118. trap on_exit EXIT
  119. on_signals()
  120. {
  121. exit 1
  122. }
  123. trap on_signals HUP INT QUIT TERM
  124. #
  125. #
  126. # Use "make V=1" to debug this script
  127. case "${KBUILD_VERBOSE}" in
  128. *1*)
  129. set -x
  130. ;;
  131. esac
  132. if [ "$1" = "clean" ]; then
  133. cleanup
  134. exit 0
  135. fi
  136. # We need access to CONFIG_ symbols
  137. case "${KCONFIG_CONFIG}" in
  138. */*)
  139. . "${KCONFIG_CONFIG}"
  140. ;;
  141. *)
  142. # Force using a file from the current directory
  143. . "./${KCONFIG_CONFIG}"
  144. esac
  145. #link vmlinux.o
  146. info LD vmlinux.o
  147. modpost_link vmlinux.o
  148. # modpost vmlinux.o to check for section mismatches
  149. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
  150. # Update version
  151. info GEN .version
  152. if [ ! -r .version ]; then
  153. rm -f .version;
  154. echo 1 >.version;
  155. else
  156. mv .version .old_version;
  157. expr 0$(cat .old_version) + 1 >.version;
  158. fi;
  159. # final build of init/
  160. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
  161. kallsymso=""
  162. kallsyms_vmlinux=""
  163. if [ -n "${CONFIG_KALLSYMS}" ]; then
  164. # kallsyms support
  165. # Generate section listing all symbols and add it into vmlinux
  166. # It's a three step process:
  167. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  168. # but __kallsyms is empty.
  169. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  170. # the right size
  171. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  172. # the right size, but due to the added section, some
  173. # addresses have shifted.
  174. # From here, we generate a correct .tmp_kallsyms2.o
  175. # 2a) We may use an extra pass as this has been necessary to
  176. # woraround some alignment related bugs.
  177. # KALLSYMS_EXTRA_PASS=1 is used to trigger this.
  178. # 3) The correct ${kallsymso} is linked into the final vmlinux.
  179. #
  180. # a) Verify that the System.map from vmlinux matches the map from
  181. # ${kallsymso}.
  182. kallsymso=.tmp_kallsyms2.o
  183. kallsyms_vmlinux=.tmp_vmlinux2
  184. # step 1
  185. vmlinux_link "" .tmp_vmlinux1
  186. kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  187. # step 2
  188. vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  189. kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
  190. # step 2a
  191. if [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  192. kallsymso=.tmp_kallsyms3.o
  193. kallsyms_vmlinux=.tmp_vmlinux3
  194. vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  195. kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  196. fi
  197. fi
  198. info LD vmlinux
  199. vmlinux_link "${kallsymso}" vmlinux
  200. if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  201. info SORTEX vmlinux
  202. sortextable vmlinux
  203. fi
  204. info SYSMAP System.map
  205. mksysmap vmlinux System.map
  206. # step a (see comment above)
  207. if [ -n "${CONFIG_KALLSYMS}" ]; then
  208. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  209. if ! cmp -s System.map .tmp_System.map; then
  210. echo >&2 Inconsistent kallsyms data
  211. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  212. exit 1
  213. fi
  214. fi
  215. # We made a new kernel - delete old version file
  216. rm -f .old_version