Back to home page

LXR

 
 

    


0001 #!/bin/sh
0002 #
0003 # link vmlinux
0004 #
0005 # vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
0006 # $(KBUILD_VMLINUX_MAIN). Most are built-in.o files from top-level directories
0007 # in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
0008 # Ordering when linking is important, and $(KBUILD_VMLINUX_INIT) must be first.
0009 #
0010 # vmlinux
0011 #   ^
0012 #   |
0013 #   +-< $(KBUILD_VMLINUX_INIT)
0014 #   |   +--< init/version.o + more
0015 #   |
0016 #   +--< $(KBUILD_VMLINUX_MAIN)
0017 #   |    +--< drivers/built-in.o mm/built-in.o + more
0018 #   |
0019 #   +-< ${kallsymso} (see description in KALLSYMS section)
0020 #
0021 # vmlinux version (uname -v) cannot be updated during normal
0022 # descending-into-subdirs phase since we do not yet know if we need to
0023 # update vmlinux.
0024 # Therefore this step is delayed until just before final link of vmlinux.
0025 #
0026 # System.map is generated to document addresses of all kernel symbols
0027 
0028 # Error out on error
0029 set -e
0030 
0031 # Nice output in kbuild format
0032 # Will be supressed by "make -s"
0033 info()
0034 {
0035         if [ "${quiet}" != "silent_" ]; then
0036                 printf "  %-7s %s\n" ${1} ${2}
0037         fi
0038 }
0039 
0040 # Thin archive build here makes a final archive with
0041 # symbol table and indexes from vmlinux objects, which can be
0042 # used as input to linker.
0043 #
0044 # Traditional incremental style of link does not require this step
0045 #
0046 # built-in.o output file
0047 #
0048 archive_builtin()
0049 {
0050         if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
0051                 info AR built-in.o
0052                 rm -f built-in.o;
0053                 ${AR} rcsT${KBUILD_ARFLAGS} built-in.o                  \
0054                                         ${KBUILD_VMLINUX_INIT}          \
0055                                         ${KBUILD_VMLINUX_MAIN}
0056         fi
0057 }
0058 
0059 # Link of vmlinux.o used for section mismatch analysis
0060 # ${1} output file
0061 modpost_link()
0062 {
0063         local objects
0064 
0065         if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
0066                 objects="--whole-archive built-in.o"
0067         else
0068                 objects="${KBUILD_VMLINUX_INIT}                         \
0069                         --start-group                                   \
0070                         ${KBUILD_VMLINUX_MAIN}                          \
0071                         --end-group"
0072         fi
0073         ${LD} ${LDFLAGS} -r -o ${1} ${objects}
0074 }
0075 
0076 # Link of vmlinux
0077 # ${1} - optional extra .o files
0078 # ${2} - output file
0079 vmlinux_link()
0080 {
0081         local lds="${objtree}/${KBUILD_LDS}"
0082         local objects
0083 
0084         if [ "${SRCARCH}" != "um" ]; then
0085                 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
0086                         objects="--whole-archive built-in.o ${1}"
0087                 else
0088                         objects="${KBUILD_VMLINUX_INIT}                 \
0089                                 --start-group                           \
0090                                 ${KBUILD_VMLINUX_MAIN}                  \
0091                                 --end-group                             \
0092                                 ${1}"
0093                 fi
0094 
0095                 ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}             \
0096                         -T ${lds} ${objects}
0097         else
0098                 if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
0099                         objects="-Wl,--whole-archive built-in.o ${1}"
0100                 else
0101                         objects="${KBUILD_VMLINUX_INIT}                 \
0102                                 -Wl,--start-group                       \
0103                                 ${KBUILD_VMLINUX_MAIN}                  \
0104                                 -Wl,--end-group                         \
0105                                 ${1}"
0106                 fi
0107 
0108                 ${CC} ${CFLAGS_vmlinux} -o ${2}                         \
0109                         -Wl,-T,${lds}                                   \
0110                         ${objects}                                      \
0111                         -lutil -lrt -lpthread
0112                 rm -f linux
0113         fi
0114 }
0115 
0116 
0117 # Create ${2} .o file with all symbols from the ${1} object file
0118 kallsyms()
0119 {
0120         info KSYM ${2}
0121         local kallsymopt;
0122 
0123         if [ -n "${CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX}" ]; then
0124                 kallsymopt="${kallsymopt} --symbol-prefix=_"
0125         fi
0126 
0127         if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then
0128                 kallsymopt="${kallsymopt} --all-symbols"
0129         fi
0130 
0131         if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then
0132                 kallsymopt="${kallsymopt} --absolute-percpu"
0133         fi
0134 
0135         if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then
0136                 kallsymopt="${kallsymopt} --base-relative"
0137         fi
0138 
0139         local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL}               \
0140                       ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}"
0141 
0142         local afile="`basename ${2} .o`.S"
0143 
0144         ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${afile}
0145         ${CC} ${aflags} -c -o ${2} ${afile}
0146 }
0147 
0148 # Create map file with all symbols from ${1}
0149 # See mksymap for additional details
0150 mksysmap()
0151 {
0152         ${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
0153 }
0154 
0155 sortextable()
0156 {
0157         ${objtree}/scripts/sortextable ${1}
0158 }
0159 
0160 # Delete output files in case of error
0161 cleanup()
0162 {
0163         rm -f .old_version
0164         rm -f .tmp_System.map
0165         rm -f .tmp_kallsyms*
0166         rm -f .tmp_version
0167         rm -f .tmp_vmlinux*
0168         rm -f built-in.o
0169         rm -f System.map
0170         rm -f vmlinux
0171         rm -f vmlinux.o
0172 }
0173 
0174 on_exit()
0175 {
0176         if [ $? -ne 0 ]; then
0177                 cleanup
0178         fi
0179 }
0180 trap on_exit EXIT
0181 
0182 on_signals()
0183 {
0184         exit 1
0185 }
0186 trap on_signals HUP INT QUIT TERM
0187 
0188 #
0189 #
0190 # Use "make V=1" to debug this script
0191 case "${KBUILD_VERBOSE}" in
0192 *1*)
0193         set -x
0194         ;;
0195 esac
0196 
0197 if [ "$1" = "clean" ]; then
0198         cleanup
0199         exit 0
0200 fi
0201 
0202 # We need access to CONFIG_ symbols
0203 case "${KCONFIG_CONFIG}" in
0204 */*)
0205         . "${KCONFIG_CONFIG}"
0206         ;;
0207 *)
0208         # Force using a file from the current directory
0209         . "./${KCONFIG_CONFIG}"
0210 esac
0211 
0212 # Update version
0213 info GEN .version
0214 if [ ! -r .version ]; then
0215         rm -f .version;
0216         echo 1 >.version;
0217 else
0218         mv .version .old_version;
0219         expr 0$(cat .old_version) + 1 >.version;
0220 fi;
0221 
0222 # final build of init/
0223 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}"
0224 
0225 archive_builtin
0226 
0227 #link vmlinux.o
0228 info LD vmlinux.o
0229 modpost_link vmlinux.o
0230 
0231 # modpost vmlinux.o to check for section mismatches
0232 ${MAKE} -f "${srctree}/scripts/Makefile.modpost" vmlinux.o
0233 
0234 kallsymso=""
0235 kallsyms_vmlinux=""
0236 if [ -n "${CONFIG_KALLSYMS}" ]; then
0237 
0238         # kallsyms support
0239         # Generate section listing all symbols and add it into vmlinux
0240         # It's a three step process:
0241         # 1)  Link .tmp_vmlinux1 so it has all symbols and sections,
0242         #     but __kallsyms is empty.
0243         #     Running kallsyms on that gives us .tmp_kallsyms1.o with
0244         #     the right size
0245         # 2)  Link .tmp_vmlinux2 so it now has a __kallsyms section of
0246         #     the right size, but due to the added section, some
0247         #     addresses have shifted.
0248         #     From here, we generate a correct .tmp_kallsyms2.o
0249         # 3)  That link may have expanded the kernel image enough that
0250         #     more linker branch stubs / trampolines had to be added, which
0251         #     introduces new names, which further expands kallsyms. Do another
0252         #     pass if that is the case. In theory it's possible this results
0253         #     in even more stubs, but unlikely.
0254         #     KALLSYMS_EXTRA_PASS=1 may also used to debug or work around
0255         #     other bugs.
0256         # 4)  The correct ${kallsymso} is linked into the final vmlinux.
0257         #
0258         # a)  Verify that the System.map from vmlinux matches the map from
0259         #     ${kallsymso}.
0260 
0261         kallsymso=.tmp_kallsyms2.o
0262         kallsyms_vmlinux=.tmp_vmlinux2
0263 
0264         # step 1
0265         vmlinux_link "" .tmp_vmlinux1
0266         kallsyms .tmp_vmlinux1 .tmp_kallsyms1.o
0267 
0268         # step 2
0269         vmlinux_link .tmp_kallsyms1.o .tmp_vmlinux2
0270         kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o
0271 
0272         # step 3
0273         size1=$(stat -c "%s" .tmp_kallsyms1.o)
0274         size2=$(stat -c "%s" .tmp_kallsyms2.o)
0275 
0276         if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
0277                 kallsymso=.tmp_kallsyms3.o
0278                 kallsyms_vmlinux=.tmp_vmlinux3
0279 
0280                 vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
0281 
0282                 kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
0283         fi
0284 fi
0285 
0286 info LD vmlinux
0287 vmlinux_link "${kallsymso}" vmlinux
0288 
0289 if [ -n "${CONFIG_BUILDTIME_EXTABLE_SORT}" ]; then
0290         info SORTEX vmlinux
0291         sortextable vmlinux
0292 fi
0293 
0294 info SYSMAP System.map
0295 mksysmap vmlinux System.map
0296 
0297 # step a (see comment above)
0298 if [ -n "${CONFIG_KALLSYMS}" ]; then
0299         mksysmap ${kallsyms_vmlinux} .tmp_System.map
0300 
0301         if ! cmp -s System.map .tmp_System.map; then
0302                 echo >&2 Inconsistent kallsyms data
0303                 echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
0304                 exit 1
0305         fi
0306 fi
0307 
0308 # We made a new kernel - delete old version file
0309 rm -f .old_version