3de06ee018f83e04069566f9de1365bdbc01e377
[grub.git] / grub-core / genmod.sh.in
1 #! @BUILD_SHEBANG@
2 set -e
3
4 # Copyright (C) 2010 Free Software Foundation, Inc.
5 #
6 # This gensymlist.sh is free software; the author
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
12 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 # PARTICULAR PURPOSE.
14
15 #
16 # Example:
17 #
18 # genmod.sh moddep.lst normal.module build-grub-module-verifier normal.mod
19 #
20
21 moddep=$1
22 infile=$2
23 outfile=$4
24
25 tmpfile=${outfile}.tmp
26 modname=`echo $infile | sed -e 's@\.module.*$@@'`
27
28 if ! grep ^$modname: $moddep >/dev/null; then
29     echo "warning: moddep.lst has no dependencies for $modname" >&2
30     exit 0
31 fi
32
33 deps=`grep ^$modname: $moddep | sed s@^.*:@@`
34
35 # remove old files if any
36 rm -f $tmpfile $outfile
37
38 if test x@TARGET_APPLE_LINKER@ != x1; then
39     # stripout .modname and .moddeps sections from input module
40     @TARGET_OBJCOPY@ -R .modname -R .moddeps $infile $tmpfile
41
42     # Attach .modname and .moddeps sections
43     t1=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
44     printf "$modname\0" >$t1
45
46     t2=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
47     for dep in $deps; do printf "$dep\0" >> $t2; done
48
49     if test -n "$deps"; then
50         @TARGET_OBJCOPY@ --add-section .modname=$t1 --add-section .moddeps=$t2 $tmpfile
51     else
52         @TARGET_OBJCOPY@ --add-section .modname=$t1 $tmpfile
53     fi
54     rm -f $t1 $t2
55
56         if test x@platform@ != xemu; then
57             @TARGET_STRIP@ --strip-unneeded \
58                 -K grub_mod_init -K grub_mod_fini \
59                 -K _grub_mod_init -K _grub_mod_fini \
60                 -R .note.gnu.gold-version -R .note.GNU-stack \
61                 -R .note -R .comment -R .ARM.exidx $tmpfile || exit 1
62         fi
63         if ! test -z "${TARGET_OBJ2ELF}"; then
64             "${TARGET_OBJ2ELF}" $tmpfile || exit 1
65         fi
66 else
67     tmpfile2=${outfile}.tmp2
68     t1=${outfile}.t1.c
69     t2=${outfile}.t2.c
70
71     # remove old files if any
72     rm -f $t1 $t2
73
74     cp $infile $tmpfile
75
76     # Attach .modname and .moddeps sections
77     echo "char modname[]  __attribute__ ((section(\"_modname, _modname\"))) = \"$modname\";" >$t1
78
79     for dep in $deps; do echo "char moddep_$dep[] __attribute__ ((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
80
81     if test -n "$deps"; then
82         @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r,-d
83     else
84         @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $tmpfile -Wl,-r,-d
85     fi
86     rm -f $t1 $t2 $tmpfile
87     mv $tmpfile2 $tmpfile
88
89         cp $tmpfile $tmpfile.bin
90         @TARGET_OBJCONV@ -f@TARGET_MODULE_FORMAT@ \
91             -nr:_grub_mod_init:grub_mod_init \
92             -nr:_grub_mod_fini:grub_mod_fini \
93             -wd1106 -nu -nd $tmpfile.bin $tmpfile || exit 1
94         rm -f $tmpfile.bin
95 fi
96 if test x@platform@ != xemu; then
97     ./build-grub-module-verifier@BUILD_EXEEXT@ $tmpfile @target_cpu@ @platform@
98 fi
99 mv $tmpfile $outfile