link-vmlinux.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 ${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_SYMBOL_PREFIX}" ]; then
  70. kallsymopt="${kallsymopt} \
  71. --symbol-prefix=${CONFIG_SYMBOL_PREFIX}"
  72. fi
  73. if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
  74. kallsymopt="${kallsymopt} --all-symbols"
  75. fi
  76. local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \
  77. ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
  78. ${NM} -n ${1} | \
  79. scripts/kallsyms ${kallsymopt} | \
  80. ${CC} ${aflags} -c -o ${2} -x assembler-with-cpp -
  81. }
  82. # Create map file with all symbols from ${1}
  83. # See mksymap for additional details
  84. mksysmap()
  85. {
  86. ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
  87. }
  88. sortextable()
  89. {
  90. ${objtree}/scripts/sortextable ${1}
  91. }
  92. # Delete output files in case of error
  93. trap cleanup SIGHUP SIGINT SIGQUIT SIGTERM ERR
  94. cleanup()
  95. {
  96. rm -f .old_version
  97. rm -f .tmp_System.map
  98. rm -f .tmp_kallsyms*
  99. rm -f .tmp_version
  100. rm -f .tmp_vmlinux*
  101. rm -f System.map
  102. rm -f vmlinux
  103. rm -f vmlinux.o
  104. }
  105. #
  106. #
  107. # Use "make V=1" to debug this script
  108. case "${KBUILD_VERBOSE}" in
  109. *1*)
  110. set -x
  111. ;;
  112. esac
  113. if [ "$1" = "clean" ]; then
  114. cleanup
  115. exit 0
  116. fi
  117. # We need access to CONFIG_ symbols
  118. case "${KCONFIG_CONFIG}" in
  119. */*)
  120. . "${KCONFIG_CONFIG}"
  121. ;;
  122. *)
  123. # Force using a file from the current directory
  124. . "./${KCONFIG_CONFIG}"
  125. esac
  126. #link vmlinux.o
  127. info LD vmlinux.o
  128. modpost_link vmlinux.o
  129. # modpost vmlinux.o to check for section mismatches
  130. ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
  131. # Update version
  132. info GEN .version
  133. if [ ! -r .version ]; then
  134. rm -f .version;
  135. echo 1 >.version;
  136. else
  137. mv .version .old_version;
  138. expr 0$(cat .old_version) + 1 >.version;
  139. fi;
  140. # final build of init/
  141. ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
  142. kallsymso=""
  143. kallsyms_vmlinux=""
  144. if [ -n "${CONFIG_KALLSYMS}" ]; then
  145. # kallsyms support
  146. # Generate section listing all symbols and add it into vmlinux
  147. # It's a three step process:
  148. # 1) Link .tmp_vmlinux1 so it has all symbols and sections,
  149. # but __kallsyms is empty.
  150. # Running kallsyms on that gives us .tmp_kallsyms1.o with
  151. # the right size
  152. # 2) Link .tmp_vmlinux2 so it now has a __kallsyms section of
  153. # the right size, but due to the added section, some
  154. # addresses have shifted.
  155. # From here, we generate a correct .tmp_kallsyms2.o
  156. # 2a) We may use an extra pass as this has been necessary to
  157. # woraround some alignment related bugs.
  158. # KALLSYMS_EXTRA_PASS=1 is used to trigger this.
  159. # 3) The correct ${kallsymso} is linked into the final vmlinux.
  160. #
  161. # a) Verify that the System.map from vmlinux matches the map from
  162. # ${kallsymso}.
  163. kallsymso=.tmp_kallsyms2.o
  164. kallsyms_vmlinux=.tmp_vmlinux2
  165. # step 1
  166. vmlinux_link "" .tmp_vmlinux1
  167. kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
  168. # step 2
  169. vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
  170. kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
  171. # step 2a
  172. if [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
  173. kallsymso=.tmp_kallsyms3.o
  174. kallsyms_vmlinux=.tmp_vmlinux3
  175. vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
  176. kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
  177. fi
  178. fi
  179. info LD vmlinux
  180. vmlinux_link "${kallsymso}" vmlinux
  181. if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
  182. info SORTEX vmlinux
  183. sortextable vmlinux
  184. fi
  185. info SYSMAP System.map
  186. mksysmap vmlinux System.map
  187. # step a (see comment above)
  188. if [ -n "${CONFIG_KALLSYMS}" ]; then
  189. mksysmap ${kallsyms_vmlinux} .tmp_System.map
  190. if ! cmp -s System.map .tmp_System.map; then
  191. echo >&2 Inconsistent kallsyms data
  192. echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
  193. cleanup
  194. exit 1
  195. fi
  196. fi
  197. # We made a new kernel - delete old version file
  198. rm -f .old_version