OSDN Git Service

Patch from Bernardo Innocenti <bernie@develer.com>:
[uclinux-h8/elf2flt.git] / ld-elf2flt.in
1 #!/bin/sh
2 #
3 # allow us to do flat processing if the flag -Wl,-elf2flt or -elf2flt to
4 # the 'C' compiler or linker respectively
5 #
6 # uses the env. var FLTFLAGS as extra parameters to pass to elf2flt
7 # arguments given like -Wl,-elf2flt="-b 10000 -v" are given before FLTFLAGS
8 #
9 # Copyright (C) 2002,2003 David McCullough <davidm@snapgear.com>
10 # Copyright (C) 2000, Lineo. davidm@lineo.com
11 #
12
13 LINKER="$0.real"            # the original renamed-linker
14 ELF2FLT="`expr $0 : '\(.*\)ld'`elf2flt"
15 TOOLDIR="`dirname $0`"      # let gcc find the tools for us
16 SHARED_ID=""
17 NEWLDSCRIPT=""
18
19 #
20 # if we have the elf2flt options, run it
21 #
22
23 if expr "$*" : ".*-elf2flt.*" > /dev/null
24 then
25         ARG1=
26         ARG2=
27         OFILE="a.out"
28         PIC=
29         SDIRS=
30         LDSCRIPT=
31         FINAL="yes"
32         FINAL_ONLY=
33         MOVDAT=
34
35         while [ $# -ne 0 ]
36         do
37                 case "$1" in
38
39                 -elf2flt) ;;                           # we already know this
40                 -elf2flt*)FLTFLAGS="`expr \"$1\" : '-elf2flt=\(.*\)'` $FLTFLAGS";; 
41
42                 -move-rodata)
43                           MOVDAT="y";;                 # Put rodata in ROM if possible
44                          
45                 -shared-lib-id)
46                           shift; SHARED_ID="$1";;      # Shared library ID
47
48                 -o)       shift; OFILE="$1";;          # the final outfile
49                 -o*)      OFILE="`expr \"$1\" : '-o\(.*\)'`";;
50
51                 -T)       shift; LDSCRIPT="$1";;       # they have a linker script
52                 -c)       shift; LDSCRIPT="$1";;
53
54                 -L)       ARG1="$ARG1 $1"              # remember search dirs
55                           shift;
56                                   ARG1="$ARG1 $1"
57                                   SDIRS="$SDIRS -L$1"
58                                   ;;
59                 -L*)      ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";;
60
61                 -EB)      ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";; # arm big endian
62
63                 -relax)   ;;                           # eat this for microblaze
64
65                 -r|-Ur)   FINAL=""                     # this is not a final link
66                           ARG1="$ARG1 $1"
67                                   ;;
68
69                 *)        ARG1="$ARG1 $1"
70                                   ;;
71                 esac
72                 shift
73         done
74
75         if [ "$FINAL" = "yes" ]
76         then
77                 ARG1="$ARG1 $FINAL_ONLY"
78                 NEWLDSCRIPT=`mktemp /tmp/flt-XXXXXX`
79                 SEDOP=" -e s/^R_RODAT// -e /^W_RODAT/d"
80                 if [ "$MOVDAT" ]
81                 then
82                         $LINKER -r -d -o "$OFILE.elf2flt" $ARG1  || exit $?
83                         if [ "`@target_alias@-objdump -h "$OFILE.elf2flt" | \
84                                         egrep -A1 '[.]rodata' | grep RELOC`" ]
85                         then
86                                 echo "warning: .rodata section contains relocations"
87                         else
88                                 SEDOP="-e /^R_RODAT/d -e s/^W_RODAT//"
89                         fi
90                 fi
91                 if [ "$SHARED_ID" ]
92                 then
93                         # Massage the linker script into something useful.  These
94                         # regexps are ugly due to some bizzare shell quoting rules.
95                         #    SEDOP="$SEDOP -e \"s/ORIGIN = 0x0,/ORIGIN = 0x${SHARED_ID}000000,/\""
96                         #    SEDOP="$SEDOP -e \"s/.text 0x0 :/.text 0x${SHARED_ID}000000 :/\""                  
97                         SEDOP="$SEDOP -e s/\\(ORIGIN.=.0\\)x0,/\\1x${SHARED_ID}000000,/"
98                         SEDOP="$SEDOP -e s/\\([.]text.0\\)x0[^0-9]:/\\1x${SHARED_ID}000000:/"
99                         
100                         if [ "$SHARED_ID" -gt 0 ]
101                         then
102                                 # Non application modules enter via main not _start
103                                 #    SEDOP="$SEDOP -e 's/ENTRY (_start)/ENTRY (main)/'"
104                                 SEDOP="$SEDOP -e s/\\(ENTRY.\\)(_start)/\1(lib_main)/"
105                         fi
106
107                         # Provide the magic parameter that defines the library data segment pointer offset
108                         ARG1="$ARG1 -defsym _current_shared_library_a5_offset_=`expr ${SHARED_ID} '*' -4 - 4`"
109                 fi
110                 
111                 # provide a default linker script, we usually need one
112                 [ -z "$LDSCRIPT" ] && LDSCRIPT="@prefix@/@target_alias@/lib/elf2flt.ld"
113
114                 # if we can find the linker script we preprocess it,  otherwise
115                 # we assume the user knows what they are doing
116                 if [ -f "$LDSCRIPT" ]; then
117                         sed $SEDOP < "$LDSCRIPT" > "$NEWLDSCRIPT"
118                         LDSCRIPT="$NEWLDSCRIPT"
119                 elif [ -f "@prefix@/@target_alias@/lib/$LDSCRIPT" ]; then
120                         sed $SEDOP < "@prefix@/@target_alias@/lib/$LDSCRIPT" > "$NEWLDSCRIPT"
121                         LDSCRIPT="$NEWLDSCRIPT"
122                 elif [ -f "@prefix@/@target_alias@/lib/ldscripts/$LDSCRIPT" ]; then
123                         sed $SEDOP < "@prefix@/@target_alias@/lib/ldscripts/$LDSCRIPT" > "$NEWLDSCRIPT"
124                         LDSCRIPT="$NEWLDSCRIPT"
125                 fi
126
127                 if [ "@got_check@" = "no" ]
128                 then
129                  $LINKER $SDIRS -T $LDSCRIPT -Ur -d -o "$OFILE.elf" $ARG1         ||exit $?
130                  $LINKER $SDIRS -T $LDSCRIPT -o "$OFILE.gdb" $ARG1                ||exit $?
131                 else
132                  $LINKER -r -d -o "$OFILE.elf2flt" $ARG1                          ||exit $?
133                  $LINKER $SDIRS -T $LDSCRIPT -Ur -o "$OFILE.elf" "$OFILE.elf2flt" ||exit $?
134                  $LINKER $SDIRS -T $LDSCRIPT -o "$OFILE.gdb" "$OFILE.elf2flt"     ||exit $?
135                  rm -f "$OFILE.elf2flt"
136                 fi
137                 if grep _GLOBAL_OFFSET_TABLE_ "$OFILE.gdb" > /dev/null 
138                 then
139                         $ELF2FLT $FLTFLAGS -o "$OFILE" -p "$OFILE.gdb" "$OFILE.elf" || exit $?
140                 else
141                         $ELF2FLT $FLTFLAGS -o "$OFILE" -r "$OFILE.elf" || exit $?
142                 fi
143                 rm -f "$OFILE.elf"  # not needed for any reason
144                 rm -f "$NEWLDSCRIPT"
145                 exit 0
146         fi
147
148         exec $LINKER -o "$OFILE" $ARG1
149 fi
150
151 #
152 # otherwise pretend we aren't here
153 #
154
155 exec $LINKER "$@"