akaros/scripts/link-kernel.sh
<<
>>
Prefs
   1#!/bin/bash
   2# Kernel linking script
   3#
   4# This isn't the worlds most elegant link command.  link-kernel takes the obj
   5# output name, then the linker script, then everything else you'd dump on the
   6# ld command line, including linker options and objects to link together.
   7#
   8# We link extra times to resolve the reflected symbol table addresses.  After
   9# the first link, we can generate the reflected symbol table of the appropriate
  10# size.  Once we link it into the kernel, some addresses may have shifted, so
  11# we repeat the creation of the ksyms and relink.  For more info, check out
  12# Linux's kallsyms, their build script, and
  13# http://stackoverflow.com/questions/11254891/can-a-running-c-program-access-its-own-symbol-table
  14
  15gen_symtab_obj()
  16{
  17        $NM -n $KERNEL_OBJECT > $KSYM_MAP
  18        awk 'BEGIN{ print "#include <kdebug.h>";
  19                    print "struct symtab_entry gbl_symtab[]={" }
  20             { if(NF==3){print "{\"" $3 "\", 0x" $1 "},"}}
  21             END{print "{0,0} };"}' $KSYM_MAP > $KSYM_C
  22        $CC $NOSTDINC_FLAGS $AKAROSINCLUDE $CFLAGS_KERNEL -o $KSYM_O -c $KSYM_C
  23}
  24
  25KERNEL_OBJECT=$1
  26shift
  27LINKER_SCRIPT=$1
  28shift
  29REMAINING_ARGS=$@
  30
  31KSYM_MAP=$OBJDIR/kern/ksyms.map
  32KSYM_C=$OBJDIR/kern/ksyms-refl.c
  33KSYM_O=$OBJDIR/kern/ksyms-refl.o
  34
  35# Use "make V=1" to debug this script (from Linux)
  36case "${KBUILD_VERBOSE}" in
  37*1*)
  38        set -x
  39        ;;
  40esac
  41
  42# Generates the first version of $KERNEL_OBJECT
  43$LD -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS
  44
  45# Generates a C and obj file with a table of the correct size, with relocs
  46gen_symtab_obj
  47
  48# Links the syms with the kernel and inserts the glb_symtab in the kernel.
  49$LD -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS $KSYM_O
  50
  51# Need to recheck/compute the symbols (table size won't change)
  52gen_symtab_obj
  53
  54# Final link
  55$LD -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS $KSYM_O
  56
  57# And objdump for debugging
  58$OBJDUMP -S $KERNEL_OBJECT > $KERNEL_OBJECT.asm
  59