#! /bin/sh # Embed an SPU ELF executable into a PowerPC object file. # # Copyright 2006, 2007, 2008 Free Software Foundation, Inc. # # This file is part of GNU Binutils. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA # 02110-1301, USA. usage () { echo "Usage: embedspu [flags] symbol_name input_filename output_filename" echo echo " input_filename: SPU ELF executable to be embedded" echo " output_filename: Resulting PowerPC object file" echo " symbol_name: Name of program handle struct to be defined" echo " flags: GCC flags defining PowerPC object file format" echo " (e.g. -m32 or -m64)" exit 1 } program_transform_name= mydir=`dirname "$0"` find_prog () { prog=`echo $1 | sed "$program_transform_name"` prog="$mydir/$prog" test -x "$prog" && return 0 prog="$mydir/$1" test -x "$prog" && return 0 prog=`echo $1 | sed "$program_transform_name"` which $prog > /dev/null 2> /dev/null && return 0 return 1 } SYMBOL= INFILE= OUTFILE= FLAGS= parse_args () { while test -n "$1"; do case "$1" in -*) FLAGS="${FLAGS} $1" ;; *) if test -z "$SYMBOL"; then SYMBOL="$1" elif test -z "$INFILE"; then INFILE="$1" elif test -z "$OUTFILE"; then OUTFILE="$1" else echo "Too many arguments!" usage fi ;; esac shift done if test -z "$OUTFILE"; then usage fi if test ! -r "$INFILE"; then echo "${INFILE}: File not found" usage fi } main () { parse_args "$@" # Find a powerpc gcc. Support running from a combined tree build. if test -x "$mydir/../gcc/xgcc"; then CC="$mydir/../gcc/xgcc -B$mydir/../gcc/" else find_prog gcc if test $? -ne 0; then echo "Cannot find $prog" exit 1 fi CC="$prog" fi # Find readelf. Any old readelf should do. find_prog readelf if test $? -ne 0; then if which readelf > /dev/null 2> /dev/null; then prog=readelf else echo "Cannot find $prog" exit 1 fi fi READELF="$prog" # Sanity check the input file if ! ${READELF} -h ${INFILE} | grep 'Class:.*ELF32' >/dev/null 2>/dev/null \ || ! ${READELF} -h ${INFILE} | grep 'Type:.*EXEC' >/dev/null 2>/dev/null \ || ! ${READELF} -h ${INFILE} | egrep 'Machine:.*(SPU|17)' >/dev/null 2>/dev/null then echo "${INFILE}: Does not appear to be an SPU executable" exit 1 fi toe=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\.toe *[PROGN]*BITS *\([0-9a-f]*\).*,\1 \2,p'` toe_addr=`echo $toe | sed -n -e 's,.* ,,p'` toe=`echo $toe | sed -n -e 's, .*,,p'` has_ea=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\._ea *PROGBITS.*,\1,p'` # For loaded sections, pick off section number, address, and file offset sections=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *[^ ]* *PROGBITS *\([0-9a-f]*\) *\([0-9a-f]*\).*,\1 \2 \3,p'` sections=`echo ${sections}` # For relocation sections, pick off file offset and info (points to # section where relocs apply) 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'` relas=`echo ${relas}` # Build embedded SPU image. # 1. The whole SPU ELF file is written to .rodata.speelf # 2. Symbols starting with the string "_EAR_" in the SPU ELF image are # special. They allow an SPU program to access corresponding symbols # (ie. minus the _EAR_ prefix), in the PowerPC program. _EAR_ without # a suffix is used to refer to the addrress of the SPU image in # PowerPC address space. _EAR_* symbols must all be defined in .toe # at 16 byte intervals, or they must be defined in other non-bss # sections. # Find all _EAR_ symbols in .toe using readelf, sort by address, and # write the address of the corresponding PowerPC symbol in a table # built in .data.spetoe. For _EAR_ symbols not in .toe, create # .reloc commands to relocate their location directly. # 3. Look for R_SPU_PPU32 and R_SPU_PPU64 relocations in the SPU ELF image # and create .reloc commands for them. # 4. Write a struct spe_program_handle to .data. # 5. Write a table of _SPUEAR_ symbols. ${CC} ${FLAGS} -x assembler-with-cpp -nostartfiles -nostdlib \ -Wa,-mbig -Wl,-r -Wl,-x -o ${OUTFILE} - <