2 # Embed an SPU ELF executable into a PowerPC object file.
4 # Copyright 2006, 2007 Free Software Foundation, Inc.
6 # This file is part of GNU Binutils.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 echo "Usage: embedspu [flags] symbol_name input_filename output_filename"
27 echo " input_filename: SPU ELF executable to be embedded"
28 echo " output_filename: Resulting PowerPC object file"
29 echo " symbol_name: Name of program handle struct to be defined"
30 echo " flags: GCC flags defining PowerPC object file format"
31 echo " (e.g. -m32 or -m64)"
35 program_transform_name
=
40 prog
=`echo $1 | sed "$program_transform_name"`
41 which $prog > /dev
/null
2> /dev
/null
&& return 0
43 test -x "$prog" && return 0
45 test -x "$prog" && return 0
46 prog
=`echo $1 | sed "$program_transform_name"`
57 while test -n "$1"; do
59 -*) FLAGS
="${FLAGS} $1" ;;
60 *) if test -z "$SYMBOL"; then
62 elif test -z "$INFILE"; then
64 elif test -z "$OUTFILE"; then
67 echo "Too many arguments!"
73 if test -z "$OUTFILE"; then
76 if test ! -r "$INFILE"; then
77 echo "${INFILE}: File not found"
86 # Find a powerpc gcc. Support running from a combined tree build.
87 if test -x "$mydir/../gcc/xgcc"; then
88 CC
="$mydir/../gcc/xgcc -B$mydir/../gcc/"
91 if test $?
-ne 0; then
92 echo "Cannot find $prog"
98 # Find readelf. Any old readelf should do. We only want to read syms.
100 if test $?
-ne 0; then
101 if which readelf
> /dev
/null
2> /dev
/null
; then
104 echo "Cannot find $prog"
110 # Sanity check the input file
111 if ! ${READELF} -h ${INFILE} |
grep 'Class:.*ELF32' >/dev
/null
2>/dev
/null \
112 ||
! ${READELF} -h ${INFILE} |
grep 'Type:.*EXEC' >/dev
/null
2>/dev
/null \
113 ||
! ${READELF} -h ${INFILE} |
egrep 'Machine:.*(SPU|17)' >/dev
/null
2>/dev
/null
115 echo "${INFILE}: Does not appear to be an SPU executable"
119 # Build embedded SPU image.
120 # 1. The whole SPU ELF file is written to .rodata.speelf
121 # 2. Symbols starting with the string "_EAR_" in the SPU ELF image are
122 # special. They allow an SPU program to access corresponding symbols
123 # (ie. minus the _EAR_ prefix), in the PowerPC program. _EAR_ without
124 # a suffix is used to refer to the addrress of the SPU image in
125 # PowerPC address space. _EAR_* symbols must all be defined in one
126 # section at 16 byte intervals.
127 # Find all _EAR_ symbols using readelf, sort by address, and write
128 # the address of the corresponding PowerPC symbol in a table built
130 # 3. Write a struct spe_program_handle to .data.
131 ${CC} ${FLAGS} -x assembler-with-cpp
-nostartfiles -nostdlib \
132 -Wa,-mbig -Wl,-r -Wl,-x -o ${OUTFILE} - <<EOF
133 .section .rodata.speelf,"a",@progbits
138 .section .data.spetoe,"aw",@progbits
141 `${READELF} -s -W ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
146 last_addr != 0 && strtonum("0x" $2) != last_addr + 16 { \
147 print "#error Symbols " last_sym " and " $8 " are not 16 bytes apart!"; \
149 { last_addr = strtonum("0x" $2); \
153 print "#ifdef _LP64"; \
154 print " .quad __speelf__, 0"; \
156 print " .int 0, __speelf__, 0, 0"; \
160 print "#ifdef _LP64"; \
161 print " .quad " substr($8, 6) ", 0"; \
163 print " .int 0, " substr($8, 6) ", 0, 0"; \
167 .section .data,"aw",@progbits
169 .type ${SYMBOL}, @object
170 # fill in a struct spe_program_handle
185 .size ${SYMBOL}, . - ${SYMBOL}