3 # allow us to do flat processing if the flag -Wl,-elf2flt or -elf2flt to
4 # the 'C' compiler or linker respectively
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
9 # Copyright (C) 2002,2003 David McCullough <davidm@snapgear.com>
10 # Copyright (C) 2000, Lineo. davidm@lineo.com
12 # This is Free Software, under the GNU Public Licence v2 or greater.
15 LINKER="$0.real" # the original renamed-linker
16 ELF2FLT="`expr $0 : '\(.*\)ld'`elf2flt"
17 NM="`expr $0 : '\(.*\)ld'`nm"
18 TOOLDIR="`dirname $0`" # let gcc find the tools for us
19 OBJCOPY="`expr $0 : '\(.*\)ld'`objcopy"
20 [ -f "$OBJCOPY" ] || OBJCOPY="$TOOLDIR/../../bin/@target_alias@-objcopy"
21 OBJDUMP="`expr $OBJCOPY : '\(.*\)objcopy'`objdump"
22 LDSCRIPTPATH="@binutils_ldscript_dir@" # and the scripts
26 # check TOOLDIR from prefix/bin/ or prefix/target-alias/bin/
27 [ -d "${LDSCRIPTPATH}" ] || LDSCRIPTPATH="${TOOLDIR}/../lib"
30 # if we have the elf2flt options, run it
33 if expr "$*" : ".*-elf2flt.*" > /dev/null
50 -elf2flt) ;; # we already know this
51 -elf2flt*)FLTFLAGS="`expr \"$1\" : '-elf2flt=\(.*\)'` $FLTFLAGS";;
54 MOVDAT="y";; # Put rodata in ROM if possible
57 shift; SHARED_ID="$1";; # Shared library ID
59 -o) shift; OFILE="$1";; # the final outfile
60 -o*) OFILE="`expr \"$1\" : '-o\(.*\)'`";;
62 -T) shift; LDSCRIPT="$1";; # they have a linker script
63 -c) shift; LDSCRIPT="$1";;
65 -L) ARG1="$ARG1 $1" # remember search dirs
70 -L*) ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";;
72 -EB) ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";; # arm big endian
74 -relax) ;; # eat this for microblaze
76 -r|-Ur) FINAL="" # this is not a final link
84 -m) shift; EMUL="-m $1";; # ld emulations for h8300
93 if [ "$FINAL" = "yes" ]
95 [ "$VERBOSE" = "y" ] && set -x
96 ARG1="$ARG1 $FINAL_ONLY"
97 NEWLDSCRIPT=`mktemp /tmp/flt-XXXXXX`
98 SEDOP=" -e s/^R_RODAT// -e /^W_RODAT/d"
102 $LINKER -r -d -o "$OFILE.elf2flt" $ARG1 || exit $?
103 if [ "`$OBJDUMP -h "$OFILE.elf2flt" | \
104 egrep -A1 '[.]rodata' | grep RELOC`" ]
106 echo "warning: .rodata section contains relocations"
108 SEDOP="-e /^R_RODAT/d -e s/^W_RODAT//"
113 # Massage the linker script into something useful. These
114 # regexps are ugly due to some bizzare shell quoting rules.
115 # SEDOP="$SEDOP -e \"s/ORIGIN = 0x0,/ORIGIN = 0x${SHARED_ID}000000,/\""
116 # SEDOP="$SEDOP -e \"s/.text 0x0 :/.text 0x${SHARED_ID}000000 :/\""
117 SEDOP="$SEDOP -e s/\\(ORIGIN.=.0\\)x0,/\\1x${SHARED_ID}000000,/"
118 SEDOP="$SEDOP -e s/\\([.]text.0\\)x0[^0-9]:/\\1x${SHARED_ID}000000:/"
120 if [ "$SHARED_ID" -gt 0 ]
122 # Non application modules enter via main not _start
123 # SEDOP="$SEDOP -e 's/ENTRY (_start)/ENTRY (main)/'"
124 SEDOP="$SEDOP -e s/\\(ENTRY.\\)(_start)/\1(lib_main)/"
125 OBJCOPYOP="--localize-hidden --weaken"
128 # Provide the magic parameter that defines the library data segment pointer offset
129 case "@target_cpu@" in
130 h8300) GOT_OFFSET="__current_shared_library_er5_offset_";;
131 *) GOT_OFFSET="_current_shared_library_a5_offset_";;
133 ARG1="$ARG1 -defsym $GOT_OFFSET=`expr ${SHARED_ID} '*' -4 - 4`"
135 if [ "@emit_relocs@" = "yes" ]
137 SEDOP="$SEDOP -e s/^SINGLE_LINK://"
139 SEDOP="$SEDOP -e /^SINGLE_LINK:/d"
142 # provide a default linker script, we usually need one
143 [ -z "$LDSCRIPT" ] && LDSCRIPT="${LDSCRIPTPATH}/elf2flt.ld"
145 # if we can find the linker script we preprocess it, otherwise
146 # we assume the user knows what they are doing
147 if [ -f "$LDSCRIPT" ]; then
148 sed $SEDOP < "$LDSCRIPT" > "$NEWLDSCRIPT"
149 LDSCRIPT="$NEWLDSCRIPT"
150 elif [ -f "${LDSCRIPTPATH}/$LDSCRIPT" ]; then
151 sed $SEDOP < "${LDSCRIPTPATH}/$LDSCRIPT" > "$NEWLDSCRIPT"
152 LDSCRIPT="$NEWLDSCRIPT"
153 elif [ -f "${LDSCRIPTPATH}/ldscripts/$LDSCRIPT" ]; then
154 sed $SEDOP < "${LDSCRIPTPATH}/ldscripts/$LDSCRIPT" > "$NEWLDSCRIPT"
155 LDSCRIPT="$NEWLDSCRIPT"
158 if [ "@emit_relocs@" = "yes" ]
160 $LINKER $EMUL $SDIRS -T $LDSCRIPT -q -o "$OFILE.gdb" $ARG1 ||exit $?
162 FLTFLAGS="$FLTFLAGS -a"
164 if [ "@got_check@" = "no" ]
166 $LINKER $EMUL $SDIRS -T $LDSCRIPT -Ur -d -o "$OFILE.elf" $ARG1 ||exit $?
167 $LINKER $EMUL $SDIRS -T $LDSCRIPT -o "$OFILE.gdb" $ARG1 ||exit $?
169 $LINKER $EMUL -r -d -o "$OFILE.elf2flt" $ARG1 ||exit $?
170 $LINKER $EMUL $SDIRS -T $LDSCRIPT -Ur -o "$OFILE.elf" "$OFILE.elf2flt" ||exit $?
171 $LINKER $EMUL $SDIRS -T $LDSCRIPT -o "$OFILE.gdb" "$OFILE.elf2flt" ||exit $?
172 rm -f "$OFILE.elf2flt"
176 if $NM "$OFILE.gdb" | grep _GLOBAL_OFFSET_TABLE_ > /dev/null
178 $ELF2FLT $FLTFLAGS -o "$OFILE" -p "$OFILE.gdb" "$RFILE" || exit $?
180 $ELF2FLT $FLTFLAGS -o "$OFILE" -r "$RFILE" || exit $?
184 if $OBJCOPY $OBJCOPYOP --help > /dev/null 2>&1
186 $OBJCOPY $OBJCOPYOP "$OFILE.gdb" ||exit $?
188 case " $OBJCOPYOP " in
189 *" --localize-hidden "*)
190 SYMS=`mktemp /tmp/flt-XXXXXX`
191 $OBJDUMP --syms "$OFILE.gdb" > "$SYMS" ||exit $?
192 sed -n 's/.*\(\.hidden\|\.internal\) \(.*\)/-L \2/p' < "$SYMS" > "$SYMS.hidden" ||exit $?
193 if [ -s "$SYMS.hidden" ]
195 xargs ${VERBOSE:+-t} $OBJCOPY "$OFILE.gdb" < "$SYMS.hidden" ||exit $?
197 rm -f "$SYMS" "$SYMS.hidden"
200 case " $OBJCOPYOP " in
202 $OBJCOPY --weaken "$OFILE.gdb" ||exit $?
207 [ "$RFILE" = "$OFILE.gdb" ] || rm -f "$RFILE" # not needed for any reason
212 exec $LINKER -o "$OFILE" $ARG1
216 # otherwise pretend we aren't here