Home | History | Annotate | Line # | Download | only in binutils
embedspu.sh revision 1.1
      1  1.1  christos #! /bin/sh 
      2  1.1  christos # Embed an SPU ELF executable into a PowerPC object file.
      3  1.1  christos #
      4  1.1  christos # Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
      5  1.1  christos #
      6  1.1  christos # This file is part of GNU Binutils.
      7  1.1  christos #
      8  1.1  christos # This program is free software; you can redistribute it and/or modify
      9  1.1  christos # it under the terms of the GNU General Public License as published by
     10  1.1  christos # the Free Software Foundation; either version 3 of the License, or
     11  1.1  christos # (at your option) any later version.
     12  1.1  christos #
     13  1.1  christos # This program is distributed in the hope that it will be useful,
     14  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  1.1  christos # GNU General Public License for more details.
     17  1.1  christos #
     18  1.1  christos # You should have received a copy of the GNU General Public License
     19  1.1  christos # along with this program; if not, write to the Free Software
     20  1.1  christos # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
     21  1.1  christos # 02110-1301, USA.
     22  1.1  christos 
     23  1.1  christos usage ()
     24  1.1  christos {
     25  1.1  christos   echo "Usage: embedspu [flags] symbol_name input_filename output_filename"
     26  1.1  christos   echo
     27  1.1  christos   echo "        input_filename:  SPU ELF executable to be embedded"
     28  1.1  christos   echo "        output_filename: Resulting PowerPC object file"
     29  1.1  christos   echo "        symbol_name:     Name of program handle struct to be defined"
     30  1.1  christos   echo "        flags:           GCC flags defining PowerPC object file format"
     31  1.1  christos   echo "                         (e.g. -m32 or -m64)"
     32  1.1  christos   exit 1
     33  1.1  christos }
     34  1.1  christos 
     35  1.1  christos program_transform_name=
     36  1.1  christos mydir=`dirname "$0"`
     37  1.1  christos 
     38  1.1  christos find_prog ()
     39  1.1  christos {
     40  1.1  christos   prog=`echo $1 | sed "$program_transform_name"`
     41  1.1  christos   prog="$mydir/$prog"
     42  1.1  christos   test -x "$prog" && return 0
     43  1.1  christos   prog="$mydir/$1"
     44  1.1  christos   test -x "$prog" && return 0
     45  1.1  christos   prog=`echo $1 | sed "$program_transform_name"`
     46  1.1  christos   which $prog > /dev/null 2> /dev/null && return 0
     47  1.1  christos   return 1
     48  1.1  christos }
     49  1.1  christos 
     50  1.1  christos SYMBOL=
     51  1.1  christos INFILE=
     52  1.1  christos OUTFILE=
     53  1.1  christos FLAGS=
     54  1.1  christos 
     55  1.1  christos parse_args ()
     56  1.1  christos {
     57  1.1  christos   while test -n "$1"; do
     58  1.1  christos     case "$1" in
     59  1.1  christos       -*) FLAGS="${FLAGS} $1" ;;
     60  1.1  christos       *)  if test -z "$SYMBOL"; then
     61  1.1  christos 	    SYMBOL="$1"
     62  1.1  christos 	  elif test -z "$INFILE"; then
     63  1.1  christos 	    INFILE="$1"
     64  1.1  christos 	  elif test -z "$OUTFILE"; then
     65  1.1  christos 	    OUTFILE="$1"
     66  1.1  christos 	  else
     67  1.1  christos 	    echo "Too many arguments!"
     68  1.1  christos 	    usage
     69  1.1  christos 	  fi ;;
     70  1.1  christos     esac
     71  1.1  christos     shift
     72  1.1  christos   done
     73  1.1  christos   if test -z "$OUTFILE"; then
     74  1.1  christos     usage
     75  1.1  christos   fi
     76  1.1  christos   if test ! -r "$INFILE"; then
     77  1.1  christos     echo "${INFILE}: File not found"
     78  1.1  christos     usage
     79  1.1  christos   fi
     80  1.1  christos }
     81  1.1  christos 
     82  1.1  christos main ()
     83  1.1  christos {
     84  1.1  christos   parse_args "$@"
     85  1.1  christos 
     86  1.1  christos   # Find a powerpc gcc.  Support running from a combined tree build.
     87  1.1  christos   if test -x "$mydir/../gcc/xgcc"; then
     88  1.1  christos     CC="$mydir/../gcc/xgcc -B$mydir/../gcc/"
     89  1.1  christos   else
     90  1.1  christos     find_prog gcc
     91  1.1  christos     if test $? -ne 0; then
     92  1.1  christos       echo "Cannot find $prog"
     93  1.1  christos       exit 1
     94  1.1  christos     fi
     95  1.1  christos     CC="$prog"
     96  1.1  christos   fi
     97  1.1  christos 
     98  1.1  christos   # Find readelf.  Any old readelf should do.
     99  1.1  christos   find_prog readelf
    100  1.1  christos   if test $? -ne 0; then
    101  1.1  christos     if which readelf > /dev/null 2> /dev/null; then
    102  1.1  christos       prog=readelf
    103  1.1  christos     else
    104  1.1  christos       echo "Cannot find $prog"
    105  1.1  christos       exit 1
    106  1.1  christos     fi
    107  1.1  christos   fi
    108  1.1  christos   READELF="$prog"
    109  1.1  christos 
    110  1.1  christos   # Sanity check the input file
    111  1.1  christos   if ! ${READELF} -h ${INFILE} | grep 'Class:.*ELF32' >/dev/null 2>/dev/null \
    112  1.1  christos      || ! ${READELF} -h ${INFILE} | grep 'Type:.*EXEC' >/dev/null 2>/dev/null \
    113  1.1  christos      || ! ${READELF} -h ${INFILE} | egrep 'Machine:.*(SPU|17)' >/dev/null 2>/dev/null
    114  1.1  christos   then
    115  1.1  christos     echo "${INFILE}: Does not appear to be an SPU executable"
    116  1.1  christos     exit 1
    117  1.1  christos   fi
    118  1.1  christos 
    119  1.1  christos   toe=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\.toe *[PROGN]*BITS *\([0-9a-f]*\).*,\1 \2,p'`
    120  1.1  christos   toe_addr=`echo $toe | sed -n -e 's,.* ,,p'`
    121  1.1  christos   toe=`echo $toe | sed -n -e 's, .*,,p'`
    122  1.1  christos   has_ea=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\._ea *PROGBITS.*,\1,p'`
    123  1.1  christos   # For loaded sections, pick off section number, address, and file offset
    124  1.1  christos   sections=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *[^ ]* *PROGBITS *\([0-9a-f]*\) *\([0-9a-f]*\).*,\1 \2 \3,p'`
    125  1.1  christos   sections=`echo ${sections}`
    126  1.1  christos   # For relocation sections, pick off file offset and info (points to
    127  1.1  christos   # section where relocs apply)
    128  1.1  christos   relas=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *[0-9]*\] *[^ ]* *RELA *[0-9a-f]* *0*\([0-9a-f][0-9a-f]*\).* \([0-9a-f][0-9a-f]*\) *[0-9a-f][0-9a-f]*$,\1 \2,p'`
    129  1.1  christos   relas=`echo ${relas}`
    130  1.1  christos 
    131  1.1  christos   # Build embedded SPU image.
    132  1.1  christos   # 1. The whole SPU ELF file is written to .rodata.speelf
    133  1.1  christos   # 2. Symbols starting with the string "_EAR_" in the SPU ELF image are
    134  1.1  christos   #    special.  They allow an SPU program to access corresponding symbols
    135  1.1  christos   #    (ie. minus the _EAR_ prefix), in the PowerPC program.  _EAR_ without
    136  1.1  christos   #    a suffix is used to refer to the addrress of the SPU image in
    137  1.1  christos   #    PowerPC address space.  _EAR_* symbols must all be defined in .toe
    138  1.1  christos   #    at 16 byte intervals, or they must be defined in other non-bss
    139  1.1  christos   #    sections.
    140  1.1  christos   #    Find all _EAR_ symbols in .toe using readelf, sort by address, and
    141  1.1  christos   #    write the address of the corresponding PowerPC symbol in a table
    142  1.1  christos   #    built in .data.spetoe.  For _EAR_ symbols not in .toe, create
    143  1.1  christos   #    .reloc commands to relocate their location directly.
    144  1.1  christos   # 3. Look for R_SPU_PPU32 and R_SPU_PPU64 relocations in the SPU ELF image
    145  1.1  christos   #    and create .reloc commands for them.
    146  1.1  christos   # 4. Write a struct spe_program_handle to .data.
    147  1.1  christos   # 5. Write a table of _SPUEAR_ symbols.
    148  1.1  christos   ${CC} ${FLAGS} -x assembler-with-cpp -nostartfiles -nostdlib \
    149  1.1  christos 	-Wa,-mbig -Wa,-noexecstack -Wl,-r -Wl,-x -o ${OUTFILE} - <<EOF
    150  1.1  christos  .section .data.spetoe,"aw",@progbits
    151  1.1  christos  .p2align 7
    152  1.1  christos __spetoe__:
    153  1.1  christos `${READELF} -s -W ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
    154  1.1  christos 'BEGIN { \
    155  1.1  christos 	addr = strtonum ("0x" "'${toe_addr:-0}'"); \
    156  1.1  christos 	split ("'"${sections}"'", s, " "); \
    157  1.1  christos 	for (i = 1; i in s; i += 3) { \
    158  1.1  christos 	    sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
    159  1.1  christos 	} \
    160  1.1  christos } \
    161  1.1  christos $7 == "'${toe}'" && strtonum ("0x" $2) != addr { \
    162  1.1  christos 	print "#error Symbol " $8 " not in 16 byte element toe array!"; \
    163  1.1  christos } \
    164  1.1  christos $7 == "'${toe}'" { \
    165  1.1  christos 	addr = addr + 16; \
    166  1.1  christos } \
    167  1.1  christos $7 == "'${toe}'" { \
    168  1.1  christos 	print "#ifdef _LP64"; \
    169  1.1  christos 	print " .quad " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0"; \
    170  1.1  christos 	print "#else"; \
    171  1.1  christos 	print " .int 0, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0, 0"; \
    172  1.1  christos 	print "#endif"; \
    173  1.1  christos } \
    174  1.1  christos $7 != "'${toe}'" && $7 in sec_off { \
    175  1.1  christos 	print "#ifdef _LP64"; \
    176  1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] ", R_PPC64_ADDR64, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
    177  1.1  christos 	print "#else"; \
    178  1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] + 4 ", R_PPC_ADDR32, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
    179  1.1  christos 	print "#endif"; \
    180  1.1  christos 	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
    181  1.1  christos } \
    182  1.1  christos $7 != "'${toe}'" && ! $7 in sec_off { \
    183  1.1  christos 	print "#error Section not found for " $8; \
    184  1.1  christos } \
    185  1.1  christos '`
    186  1.1  christos `test -z "${relas}" || ${READELF} -r -W ${INFILE} | awk \
    187  1.1  christos 'BEGIN { \
    188  1.1  christos 	split ("'"${sections}"'", s, " "); \
    189  1.1  christos 	for (i = 1; i in s; i += 3) { \
    190  1.1  christos 	    sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
    191  1.1  christos 	} \
    192  1.1  christos 	split ("'"${relas}"'", s, " "); \
    193  1.1  christos 	for (i = 1; i in s; i += 2) { \
    194  1.1  christos 	    rela[s[i]] = strtonum (s[i+1]); \
    195  1.1  christos 	} \
    196  1.1  christos } \
    197  1.1  christos /^Relocation section/ { \
    198  1.1  christos 	sec = substr($6, 3); \
    199  1.1  christos } \
    200  1.1  christos $3 ~ /R_SPU_PPU/ { \
    201  1.1  christos 	print "#ifdef _LP64"; \
    202  1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] ", R_PPC64_ADDR" substr($3, 10) ", " ($5 != "" ? $5 "+0x" $7 : "__speelf__ + 0x" $4); \
    203  1.1  christos 	print "#else"; \
    204  1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] + (substr($3, 10) == "64" ? 4 : 0)", R_PPC_ADDR32, " ($5 != "" ? $5 "+0x" $7 : "__speelf__ + 0x" $4); \
    205  1.1  christos 	print "#endif"; \
    206  1.1  christos 	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
    207  1.1  christos } \
    208  1.1  christos $3 ~ /unrecognized:/ { \
    209  1.1  christos 	print "#ifdef _LP64"; \
    210  1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] ", R_PPC64_ADDR" ($4 == "f" ? "64" : "32") ", " ($6 != "" ? $6 "+0x" $8 : "__speelf__ + 0x" $5); \
    211  1.1  christos 	print "#else"; \
    212  1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] + ($4 == "f" ? 4 : 0)", R_PPC_ADDR32, " ($6 != "" ? $6 "+0x" $8 : "__speelf__ + 0x" $5); \
    213  1.1  christos 	print "#endif"; \
    214  1.1  christos 	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
    215  1.1  christos } \
    216  1.1  christos '`
    217  1.1  christos #if ${has_ea:-0}
    218  1.1  christos  .section .data.speelf,"aw",@progbits
    219  1.1  christos #elif defined (HAS_RELOCS) && (defined (__PIC__) || defined (__PIE__))
    220  1.1  christos  .section .data.rel.ro.speelf,"a",@progbits
    221  1.1  christos #else
    222  1.1  christos  .section .rodata.speelf,"a",@progbits
    223  1.1  christos #endif
    224  1.1  christos  .p2align 7
    225  1.1  christos __speelf__:
    226  1.1  christos  .incbin "${INFILE}"
    227  1.1  christos 
    228  1.1  christos  .section .data.spehandle,"aw",@progbits
    229  1.1  christos  .globl ${SYMBOL}
    230  1.1  christos  .type ${SYMBOL}, @object
    231  1.1  christos # fill in a struct spe_program_handle
    232  1.1  christos #ifdef _LP64
    233  1.1  christos  .p2align 3
    234  1.1  christos ${SYMBOL}:
    235  1.1  christos  .int 24
    236  1.1  christos  .int 0
    237  1.1  christos  .quad __speelf__
    238  1.1  christos  .quad __spetoe__
    239  1.1  christos #else
    240  1.1  christos  .p2align 2
    241  1.1  christos ${SYMBOL}:
    242  1.1  christos  .int 12
    243  1.1  christos  .int __speelf__
    244  1.1  christos  .int __spetoe__
    245  1.1  christos #endif
    246  1.1  christos  .size ${SYMBOL}, . - ${SYMBOL}
    247  1.1  christos 
    248  1.1  christos `${READELF} -s -W ${INFILE} | grep ' _SPUEAR_' | sort -k 2 | awk \
    249  1.1  christos '{ \
    250  1.1  christos 	print " .globl '${SYMBOL}'_" substr($8, 9); \
    251  1.1  christos 	print " .type '${SYMBOL}'_" substr($8, 9) ", @object"; \
    252  1.1  christos 	print " .size '${SYMBOL}'_" substr($8, 9) ", 4"; \
    253  1.1  christos 	print "'${SYMBOL}'_" substr($8, 9) ":"; \
    254  1.1  christos 	print " .int 0x" $2; \
    255  1.1  christos } \
    256  1.1  christos '`
    257  1.1  christos EOF
    258  1.1  christos }
    259  1.1  christos 
    260  1.1  christos main "$@"
    261