Home | History | Annotate | Line # | Download | only in binutils
      1   1.3  christos #! /bin/sh
      2   1.1  christos # Embed an SPU ELF executable into a PowerPC object file.
      3   1.1  christos #
      4  1.10  christos # Copyright (C) 2006-2025 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.9  christos   if test `${READELF} -h ${INFILE} | sed -n -e '/Class:.*ELF32/p' -e '/Type:.*EXEC/p' -e '/Machine:.*SPU/p' -e '/Machine:.*17/p' | sed -n '$='` != 3
    112   1.1  christos   then
    113   1.1  christos     echo "${INFILE}: Does not appear to be an SPU executable"
    114   1.1  christos     exit 1
    115   1.1  christos   fi
    116   1.1  christos 
    117   1.1  christos   toe=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\.toe *[PROGN]*BITS *\([0-9a-f]*\).*,\1 \2,p'`
    118   1.1  christos   toe_addr=`echo $toe | sed -n -e 's,.* ,,p'`
    119   1.1  christos   toe=`echo $toe | sed -n -e 's, .*,,p'`
    120   1.1  christos   has_ea=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\._ea *PROGBITS.*,\1,p'`
    121   1.1  christos   # For loaded sections, pick off section number, address, and file offset
    122   1.1  christos   sections=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *[^ ]* *PROGBITS *\([0-9a-f]*\) *\([0-9a-f]*\).*,\1 \2 \3,p'`
    123   1.1  christos   sections=`echo ${sections}`
    124   1.1  christos   # For relocation sections, pick off file offset and info (points to
    125   1.1  christos   # section where relocs apply)
    126   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'`
    127   1.1  christos   relas=`echo ${relas}`
    128   1.1  christos 
    129   1.1  christos   # Build embedded SPU image.
    130   1.1  christos   # 1. The whole SPU ELF file is written to .rodata.speelf
    131   1.1  christos   # 2. Symbols starting with the string "_EAR_" in the SPU ELF image are
    132   1.1  christos   #    special.  They allow an SPU program to access corresponding symbols
    133   1.1  christos   #    (ie. minus the _EAR_ prefix), in the PowerPC program.  _EAR_ without
    134   1.1  christos   #    a suffix is used to refer to the addrress of the SPU image in
    135   1.1  christos   #    PowerPC address space.  _EAR_* symbols must all be defined in .toe
    136   1.1  christos   #    at 16 byte intervals, or they must be defined in other non-bss
    137   1.1  christos   #    sections.
    138   1.1  christos   #    Find all _EAR_ symbols in .toe using readelf, sort by address, and
    139   1.1  christos   #    write the address of the corresponding PowerPC symbol in a table
    140   1.1  christos   #    built in .data.spetoe.  For _EAR_ symbols not in .toe, create
    141   1.1  christos   #    .reloc commands to relocate their location directly.
    142   1.1  christos   # 3. Look for R_SPU_PPU32 and R_SPU_PPU64 relocations in the SPU ELF image
    143   1.1  christos   #    and create .reloc commands for them.
    144   1.1  christos   # 4. Write a struct spe_program_handle to .data.
    145   1.1  christos   # 5. Write a table of _SPUEAR_ symbols.
    146   1.1  christos   ${CC} ${FLAGS} -x assembler-with-cpp -nostartfiles -nostdlib \
    147   1.1  christos 	-Wa,-mbig -Wa,-noexecstack -Wl,-r -Wl,-x -o ${OUTFILE} - <<EOF
    148   1.1  christos  .section .data.spetoe,"aw",@progbits
    149   1.1  christos  .p2align 7
    150   1.1  christos __spetoe__:
    151   1.1  christos `${READELF} -s -W ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
    152   1.1  christos 'BEGIN { \
    153   1.1  christos 	addr = strtonum ("0x" "'${toe_addr:-0}'"); \
    154   1.1  christos 	split ("'"${sections}"'", s, " "); \
    155   1.1  christos 	for (i = 1; i in s; i += 3) { \
    156   1.1  christos 	    sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
    157   1.1  christos 	} \
    158   1.1  christos } \
    159   1.1  christos $7 == "'${toe}'" && strtonum ("0x" $2) != addr { \
    160   1.1  christos 	print "#error Symbol " $8 " not in 16 byte element toe array!"; \
    161   1.1  christos } \
    162   1.1  christos $7 == "'${toe}'" { \
    163   1.1  christos 	addr = addr + 16; \
    164   1.1  christos } \
    165   1.1  christos $7 == "'${toe}'" { \
    166   1.1  christos 	print "#ifdef _LP64"; \
    167   1.1  christos 	print " .quad " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0"; \
    168   1.1  christos 	print "#else"; \
    169   1.1  christos 	print " .int 0, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0, 0"; \
    170   1.1  christos 	print "#endif"; \
    171   1.1  christos } \
    172   1.1  christos $7 != "'${toe}'" && $7 in sec_off { \
    173   1.1  christos 	print "#ifdef _LP64"; \
    174   1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] ", R_PPC64_ADDR64, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
    175   1.1  christos 	print "#else"; \
    176   1.1  christos 	print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] + 4 ", R_PPC_ADDR32, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
    177   1.1  christos 	print "#endif"; \
    178   1.1  christos 	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
    179   1.1  christos } \
    180   1.1  christos $7 != "'${toe}'" && ! $7 in sec_off { \
    181   1.1  christos 	print "#error Section not found for " $8; \
    182   1.1  christos } \
    183   1.1  christos '`
    184   1.1  christos `test -z "${relas}" || ${READELF} -r -W ${INFILE} | awk \
    185   1.1  christos 'BEGIN { \
    186   1.1  christos 	split ("'"${sections}"'", s, " "); \
    187   1.1  christos 	for (i = 1; i in s; i += 3) { \
    188   1.1  christos 	    sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
    189   1.1  christos 	} \
    190   1.1  christos 	split ("'"${relas}"'", s, " "); \
    191   1.1  christos 	for (i = 1; i in s; i += 2) { \
    192   1.1  christos 	    rela[s[i]] = strtonum (s[i+1]); \
    193   1.1  christos 	} \
    194   1.1  christos } \
    195   1.1  christos /^Relocation section/ { \
    196   1.1  christos 	sec = substr($6, 3); \
    197   1.1  christos } \
    198   1.1  christos $3 ~ /R_SPU_PPU/ { \
    199   1.1  christos 	print "#ifdef _LP64"; \
    200   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); \
    201   1.1  christos 	print "#else"; \
    202   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); \
    203   1.1  christos 	print "#endif"; \
    204   1.1  christos 	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
    205   1.1  christos } \
    206   1.1  christos $3 ~ /unrecognized:/ { \
    207   1.1  christos 	print "#ifdef _LP64"; \
    208   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); \
    209   1.1  christos 	print "#else"; \
    210   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); \
    211   1.1  christos 	print "#endif"; \
    212   1.1  christos 	if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
    213   1.1  christos } \
    214   1.1  christos '`
    215   1.1  christos #if ${has_ea:-0}
    216   1.1  christos  .section .data.speelf,"aw",@progbits
    217   1.1  christos #elif defined (HAS_RELOCS) && (defined (__PIC__) || defined (__PIE__))
    218   1.1  christos  .section .data.rel.ro.speelf,"a",@progbits
    219   1.1  christos #else
    220   1.1  christos  .section .rodata.speelf,"a",@progbits
    221   1.1  christos #endif
    222   1.1  christos  .p2align 7
    223   1.1  christos __speelf__:
    224   1.1  christos  .incbin "${INFILE}"
    225   1.1  christos 
    226   1.1  christos  .section .data.spehandle,"aw",@progbits
    227   1.1  christos  .globl ${SYMBOL}
    228   1.1  christos  .type ${SYMBOL}, @object
    229   1.1  christos # fill in a struct spe_program_handle
    230   1.1  christos #ifdef _LP64
    231   1.1  christos  .p2align 3
    232   1.1  christos ${SYMBOL}:
    233   1.1  christos  .int 24
    234   1.1  christos  .int 0
    235   1.1  christos  .quad __speelf__
    236   1.1  christos  .quad __spetoe__
    237   1.1  christos #else
    238   1.1  christos  .p2align 2
    239   1.1  christos ${SYMBOL}:
    240   1.1  christos  .int 12
    241   1.1  christos  .int __speelf__
    242   1.1  christos  .int __spetoe__
    243   1.1  christos #endif
    244   1.1  christos  .size ${SYMBOL}, . - ${SYMBOL}
    245   1.1  christos 
    246   1.1  christos `${READELF} -s -W ${INFILE} | grep ' _SPUEAR_' | sort -k 2 | awk \
    247   1.1  christos '{ \
    248   1.1  christos 	print " .globl '${SYMBOL}'_" substr($8, 9); \
    249   1.1  christos 	print " .type '${SYMBOL}'_" substr($8, 9) ", @object"; \
    250   1.1  christos 	print " .size '${SYMBOL}'_" substr($8, 9) ", 4"; \
    251   1.1  christos 	print "'${SYMBOL}'_" substr($8, 9) ":"; \
    252   1.1  christos 	print " .int 0x" $2; \
    253   1.1  christos } \
    254   1.1  christos '`
    255   1.1  christos EOF
    256   1.1  christos }
    257   1.1  christos 
    258   1.1  christos main "$@"
    259