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
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 LDSCRIPTPATH="${TOOLDIR}/../@target_alias@/lib" # and the scripts
21 # if we have the elf2flt options, run it
24 if expr "$*" : ".*-elf2flt.*" > /dev/null
41 -elf2flt) ;; # we already know this
42 -elf2flt*)FLTFLAGS="`expr \"$1\" : '-elf2flt=\(.*\)'` $FLTFLAGS";;
45 MOVDAT="y";; # Put rodata in ROM if possible
48 shift; SHARED_ID="$1";; # Shared library ID
50 -o) shift; OFILE="$1";; # the final outfile
51 -o*) OFILE="`expr \"$1\" : '-o\(.*\)'`";;
53 -T) shift; LDSCRIPT="$1";; # they have a linker script
54 -c) shift; LDSCRIPT="$1";;
56 -L) ARG1="$ARG1 $1" # remember search dirs
61 -L*) ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";;
63 -EB) ARG1="$ARG1 $1"; SDIRS="$SDIRS $1";; # arm big endian
65 -relax) ;; # eat this for microblaze
67 -r|-Ur) FINAL="" # this is not a final link
81 if [ "$FINAL" = "yes" ]
83 [ "$VERBOSE" = "y" ] && set -x
84 ARG1="$ARG1 $FINAL_ONLY"
85 NEWLDSCRIPT=`mktemp /tmp/flt-XXXXXX`
86 SEDOP=" -e s/^R_RODAT// -e /^W_RODAT/d"
89 $LINKER -r -d -o "$OFILE.elf2flt" $ARG1 || exit $?
90 if [ "`@target_alias@-objdump -h "$OFILE.elf2flt" | \
91 egrep -A1 '[.]rodata' | grep RELOC`" ]
93 echo "warning: .rodata section contains relocations"
95 SEDOP="-e /^R_RODAT/d -e s/^W_RODAT//"
100 # Massage the linker script into something useful. These
101 # regexps are ugly due to some bizzare shell quoting rules.
102 # SEDOP="$SEDOP -e \"s/ORIGIN = 0x0,/ORIGIN = 0x${SHARED_ID}000000,/\""
103 # SEDOP="$SEDOP -e \"s/.text 0x0 :/.text 0x${SHARED_ID}000000 :/\""
104 SEDOP="$SEDOP -e s/\\(ORIGIN.=.0\\)x0,/\\1x${SHARED_ID}000000,/"
105 SEDOP="$SEDOP -e s/\\([.]text.0\\)x0[^0-9]:/\\1x${SHARED_ID}000000:/"
107 if [ "$SHARED_ID" -gt 0 ]
109 # Non application modules enter via main not _start
110 # SEDOP="$SEDOP -e 's/ENTRY (_start)/ENTRY (main)/'"
111 SEDOP="$SEDOP -e s/\\(ENTRY.\\)(_start)/\1(lib_main)/"
114 # Provide the magic parameter that defines the library data segment pointer offset
115 ARG1="$ARG1 -defsym _current_shared_library_a5_offset_=`expr ${SHARED_ID} '*' -4 - 4`"
118 # provide a default linker script, we usually need one
119 [ -z "$LDSCRIPT" ] && LDSCRIPT="${LDSCRIPTPATH}/elf2flt.ld"
121 # if we can find the linker script we preprocess it, otherwise
122 # we assume the user knows what they are doing
123 if [ -f "$LDSCRIPT" ]; then
124 sed $SEDOP < "$LDSCRIPT" > "$NEWLDSCRIPT"
125 LDSCRIPT="$NEWLDSCRIPT"
126 elif [ -f "${LDSCRIPTPATH}/$LDSCRIPT" ]; then
127 sed $SEDOP < "${LDSCRIPTPATH}/$LDSCRIPT" > "$NEWLDSCRIPT"
128 LDSCRIPT="$NEWLDSCRIPT"
129 elif [ -f "${LDSCRIPTPATH}/ldscripts/$LDSCRIPT" ]; then
130 sed $SEDOP < "${LDSCRIPTPATH}/ldscripts/$LDSCRIPT" > "$NEWLDSCRIPT"
131 LDSCRIPT="$NEWLDSCRIPT"
134 if [ "@got_check@" = "no" ]
136 $LINKER $SDIRS -T $LDSCRIPT -Ur -d -o "$OFILE.elf" $ARG1 ||exit $?
137 $LINKER $SDIRS -T $LDSCRIPT -o "$OFILE.gdb" $ARG1 ||exit $?
139 $LINKER -r -d -o "$OFILE.elf2flt" $ARG1 ||exit $?
140 $LINKER $SDIRS -T $LDSCRIPT -Ur -o "$OFILE.elf" "$OFILE.elf2flt" ||exit $?
141 $LINKER $SDIRS -T $LDSCRIPT -o "$OFILE.gdb" "$OFILE.elf2flt" ||exit $?
142 rm -f "$OFILE.elf2flt"
144 if grep _GLOBAL_OFFSET_TABLE_ "$OFILE.gdb" > /dev/null
146 $ELF2FLT $FLTFLAGS -o "$OFILE" -p "$OFILE.gdb" "$OFILE.elf" || exit $?
148 $ELF2FLT $FLTFLAGS -o "$OFILE" -r "$OFILE.elf" || exit $?
150 rm -f "$OFILE.elf" # not needed for any reason
155 exec $LINKER -o "$OFILE" $ARG1
159 # otherwise pretend we aren't here