OSDN Git Service

6c8f596486df642b203b663909eb63bda78b955c
[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 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=
28         PIC=
29         SDIRS=
30         LDSCRIPT=
31         FINAL="yes"
32         MOVDAT=
33
34         while [ $# -ne 0 ]
35         do
36                 case "$1" in
37
38                 -elf2flt) ;;                           # we already know this
39                 -elf2flt*)FLTFLAGS="`expr \"$1\" : '-elf2flt=\(.*\)'` $FLTFLAGS";; 
40
41                 -move-rodata)
42                           MOVDAT="y";;                 # Put rodata in ROM if possible
43                          
44                 -shared-lib-id)
45                           shift; SHARED_ID="$1";;      # Shared library ID
46
47                 -o)       shift; OFILE="$1";;          # the final outfile
48                 -o*)      OFILE="`expr \"$1\" : '-o\(.*\)'`";;
49
50                 -T)       shift; LDSCRIPT="$1";;       # they have a linker script
51                 -c)       shift; LDSCRIPT="$1";;
52
53                 -L)       ARG1="$ARG1 $1"              # remember search dirs
54                           shift;
55                                   ARG1="$ARG1 $1"
56                                   SDIRS="$SDIRS -L$1"
57                                   ;;
58                 -L*)      ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";;
59
60                 -EB)      ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";; # arm big endian
61
62                 -r|-Ur)   FINAL=""                     # this is not a final link
63                           ARG1="$ARG1 $1"
64                                   ;;
65
66                 *)        ARG1="$ARG1 $1"
67                                   ;;
68                 esac
69                 shift
70         done
71
72         if [ "$FINAL" = "yes" ]
73         then
74                 NEWLDSCRIPT=`mktemp /tmp/flt-XXXXXX`
75                 SEDOP=" -e s/^R_RODAT// -e /^W_RODAT/d"
76                 if [ "$MOVDAT" ]
77                 then
78                         $LINKER -r -d -o $OFILE.elf2flt $ARG1  || exit $?
79                         if [ "`@target_alias@-objdump -h $OFILE.elf2flt | \
80                                         egrep -A1 '[.]rodata' | grep RELOC`" ]
81                         then
82                                 echo "warning: .rodata section contains relocations"
83                         else
84                                 SEDOP="-e /^R_RODAT/d -e s/^W_RODAT//"
85                         fi
86                 fi
87                 if [ "$SHARED_ID" ]
88                 then
89                         # Massage the linker script into something useful.  These
90                         # regexps are ugly due to some bizzare shell quoting rules.
91                         #    SEDOP="$SEDOP -e \"s/ORIGIN = 0x0,/ORIGIN = 0x${SHARED_ID}000000,/\""
92                         #    SEDOP="$SEDOP -e \"s/.text 0x0 :/.text 0x${SHARED_ID}000000 :/\""                  
93                         SEDOP="$SEDOP -e s/\\(ORIGIN.=.0\\)x0,/\\1x${SHARED_ID}000000,/"
94                         SEDOP="$SEDOP -e s/\\([.]text.0\\)x0[^0-9]:/\\1x${SHARED_ID}000000:/"
95                         
96                         if [ "$SHARED_ID" -gt 0 ]
97                         then
98                                 # Non application modules enter via main not _start
99                                 #    SEDOP="$SEDOP -e 's/ENTRY (_start)/ENTRY (main)/'"
100                                 SEDOP="$SEDOP -e s/\\(ENTRY.\\)(_start)/\1(lib_main)/"
101                         fi
102
103                         # Provide the magic parameter that defines the library data segment pointer offset
104                         ARG1="$ARG1 -defsym _current_shared_library_a5_offset_=`expr ${SHARED_ID} '*' -4 - 4`"
105                 fi
106                 
107                 # provide a default linker script, we usually need one
108                 [ -z "$LDSCRIPT" ] && LDSCRIPT="@prefix@/@target_alias@/lib/elf2flt.ld"
109
110                 # if we can find the linker script we preprocess it,  otherwise
111                 # we assume the user knows what they are doing
112                 if [ -f "$LDSCRIPT" ]; then
113                         sed $SEDOP < "$LDSCRIPT" > "$NEWLDSCRIPT"
114                         LDSCRIPT="$NEWLDSCRIPT"
115                 elif [ -f "@prefix@/@target_alias@/lib/$LDSCRIPT" ]; then
116                         sed $SEDOP < "@prefix@/@target_alias@/lib/$LDSCRIPT" > "$NEWLDSCRIPT"
117                         LDSCRIPT="$NEWLDSCRIPT"
118                 elif [ -f "@prefix@/@target_alias@/lib/ldscripts/$LDSCRIPT" ]; then
119                         sed $SEDOP < "@prefix@/@target_alias@/lib/ldscripts/$LDSCRIPT" > "$NEWLDSCRIPT"
120                         LDSCRIPT="$NEWLDSCRIPT"
121                 fi
122
123                 $LINKER -r -d -o $OFILE.elf2flt $ARG1                        || exit $?
124                 $LINKER $SDIRS -T $LDSCRIPT -Ur -o $OFILE.elf $OFILE.elf2flt || exit $?
125                 $LINKER $SDIRS -T $LDSCRIPT -o $OFILE.gdb $OFILE.elf2flt     || exit $?
126                 rm -f $OFILE.elf2flt
127                 if grep _GLOBAL_OFFSET_TABLE_ $OFILE.gdb > /dev/null 
128                 then
129                         $ELF2FLT $FLTFLAGS -o $OFILE -p $OFILE.gdb $OFILE.elf || exit $?
130                 else
131                         $ELF2FLT $FLTFLAGS -o $OFILE -r $OFILE.elf || exit $?
132                 fi
133                 rm -f $OFILE.elf  # not needed for any reason
134                 rm -f "$NEWLDSCRIPT"
135                 exit 0
136         fi
137
138         exec $LINKER -o $OFILE $ARG1
139 fi
140
141 #
142 # otherwise pretend we aren't here
143 #
144
145 exec $LINKER "$@"