.mod files: Strip annobin annotations and .eh_frame, and their relocations
[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 .gnu.build.attributes \
62                 -R .rel.gnu.build.attributes \
63                 -R .rela.gnu.build.attributes \
64                 -R .eh_frame -R .rela.eh_frame -R .rel.eh_frame \
65                 -R .note -R .comment -R .ARM.exidx $tmpfile || exit 1
66         fi
67         if ! test -z "${TARGET_OBJ2ELF}"; then
68             "${TARGET_OBJ2ELF}" $tmpfile || exit 1
69         fi
70 else
71     tmpfile2=${outfile}.tmp2
72     t1=${outfile}.t1.c
73     t2=${outfile}.t2.c
74
75     # remove old files if any
76     rm -f $t1 $t2
77
78     cp $infile $tmpfile
79
80     # Attach .modname and .moddeps sections
81     echo "char modname[]  __attribute__ ((section(\"_modname, _modname\"))) = \"$modname\";" >$t1
82
83     for dep in $deps; do echo "char moddep_$dep[] __attribute__ ((section(\"_moddeps, _moddeps\"))) = \"$dep\";" >>$t2; done
84
85     if test -n "$deps"; then
86         @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $t2 $tmpfile -Wl,-r,-d
87     else
88         @TARGET_CC@ @TARGET_LDFLAGS@ -ffreestanding -nostdlib -o $tmpfile2 $t1 $tmpfile -Wl,-r,-d
89     fi
90     rm -f $t1 $t2 $tmpfile
91     mv $tmpfile2 $tmpfile
92
93         cp $tmpfile $tmpfile.bin
94         @TARGET_OBJCONV@ -f@TARGET_MODULE_FORMAT@ \
95             -nr:_grub_mod_init:grub_mod_init \
96             -nr:_grub_mod_fini:grub_mod_fini \
97             -wd1106 -nu -nd $tmpfile.bin $tmpfile || exit 1
98         rm -f $tmpfile.bin
99 fi
100 if test x@platform@ != xemu; then
101     ./build-grub-module-verifier@BUILD_EXEEXT@ $tmpfile @target_cpu@ @platform@
102 fi
103 mv $tmpfile $outfile