*** empty log message ***
[deliverable/binutils-gdb.git] / binutils / embedspu.sh
CommitLineData
669a9a2a
AM
1#! /bin/sh
2# Embed an SPU ELF executable into a PowerPC object file.
3#
41160f02 4# Copyright 2006, 2007 Free Software Foundation, Inc.
669a9a2a
AM
5#
6# This file is part of GNU Binutils.
7#
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.
12#
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.
17#
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
21# 02110-1301, USA.
22
23usage ()
24{
25 echo "Usage: embedspu [flags] symbol_name input_filename output_filename"
26 echo
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)"
32 exit 1
33}
34
35program_transform_name=
36mydir=`dirname "$0"`
37
38find_prog ()
39{
40 prog=`echo $1 | sed "$program_transform_name"`
41 which $prog > /dev/null 2> /dev/null && return 0
42 prog="$mydir/$prog"
43 test -x "$prog" && return 0
44 prog="$mydir/$1"
45 test -x "$prog" && return 0
46 prog=`echo $1 | sed "$program_transform_name"`
47 return 1
48}
49
50SYMBOL=
51INFILE=
52OUTFILE=
53FLAGS=
54
55parse_args ()
56{
57 while test -n "$1"; do
58 case "$1" in
59 -*) FLAGS="${FLAGS} $1" ;;
60 *) if test -z "$SYMBOL"; then
61 SYMBOL="$1"
62 elif test -z "$INFILE"; then
63 INFILE="$1"
64 elif test -z "$OUTFILE"; then
65 OUTFILE="$1"
66 else
67 echo "Too many arguments!"
68 usage
69 fi ;;
70 esac
71 shift
72 done
73 if test -z "$OUTFILE"; then
74 usage
75 fi
76 if test ! -r "$INFILE"; then
77 echo "${INFILE}: File not found"
78 usage
79 fi
80}
81
82main ()
83{
84 parse_args "$@"
85
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/"
89 else
90 find_prog gcc
91 if test $? -ne 0; then
92 echo "Cannot find $prog"
93 exit 1
94 fi
95 CC="$prog"
96 fi
97
98 # Find readelf. Any old readelf should do. We only want to read syms.
99 find_prog readelf
100 if test $? -ne 0; then
101 if which readelf > /dev/null 2> /dev/null; then
102 prog=readelf
103 else
104 echo "Cannot find $prog"
105 exit 1
106 fi
107 fi
108 READELF="$prog"
109
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
114 then
115 echo "${INFILE}: Does not appear to be an SPU executable"
116 exit 1
117 fi
118
88948ad0
AM
119 toe=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\.toe *[PROGN]*BITS *\([0-9a-f]*\).*,\1 \2,p'`
120 toe_addr=`echo $toe | sed -n -e 's,.* ,,p'`
121 toe=`echo $toe | sed -n -e 's, .*,,p'`
122 sections=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *[^ ]* *PROGBITS *\([0-9a-f]*\) *\([0-9a-f]*\).*,\1 \2 \3,p'`
123 sections=`echo ${sections}`
124
669a9a2a
AM
125 # Build embedded SPU image.
126 # 1. The whole SPU ELF file is written to .rodata.speelf
127 # 2. Symbols starting with the string "_EAR_" in the SPU ELF image are
128 # special. They allow an SPU program to access corresponding symbols
129 # (ie. minus the _EAR_ prefix), in the PowerPC program. _EAR_ without
130 # a suffix is used to refer to the addrress of the SPU image in
88948ad0
AM
131 # PowerPC address space. _EAR_* symbols must all be defined in .toe
132 # at 16 byte intervals, or they must be defined in other non-bss
133 # sections.
134 # Find all _EAR_ symbols in .toe using readelf, sort by address, and
135 # write the address of the corresponding PowerPC symbol in a table
136 # built in .data.spetoe. For _EAE_ symbols not in .toe, create
137 # .reloc commands to relocate their location directly.
669a9a2a 138 # 3. Write a struct spe_program_handle to .data.
88948ad0 139 # 4. Write a table of _SPUEAR_ symbols.
669a9a2a
AM
140 ${CC} ${FLAGS} -x assembler-with-cpp -nostartfiles -nostdlib \
141 -Wa,-mbig -Wl,-r -Wl,-x -o ${OUTFILE} - <<EOF
142 .section .rodata.speelf,"a",@progbits
143 .p2align 7
144__speelf__:
145 .incbin "${INFILE}"
146
147 .section .data.spetoe,"aw",@progbits
148 .p2align 7
149__spetoe__:
41160f02 150`${READELF} -s -W ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
669a9a2a 151'BEGIN { \
48d8dc64 152 addr = strtonum ("0x" "'${toe_addr-0}'"); \
88948ad0
AM
153 split ("'"${sections}"'", s, " "); \
154 for (i = 1; i in s; i += 3) { \
155 sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
156 } \
669a9a2a 157} \
88948ad0
AM
158$7 == "'${toe}'" && strtonum ("0x" $2) != addr { \
159 print "#error Symbol " $8 " not in 16 byte element toe array!"; \
669a9a2a 160} \
88948ad0
AM
161$7 == "'${toe}'" { \
162 addr = addr + 16; \
669a9a2a 163} \
88948ad0 164$7 == "'${toe}'" { \
669a9a2a 165 print "#ifdef _LP64"; \
88948ad0 166 print " .quad " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0"; \
669a9a2a 167 print "#else"; \
88948ad0 168 print " .int 0, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0, 0"; \
669a9a2a
AM
169 print "#endif"; \
170} \
88948ad0 171$7 != "'${toe}'" && $7 in sec_off { \
669a9a2a 172 print "#ifdef _LP64"; \
88948ad0 173 print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] ", R_PPC64_ADDR64, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
669a9a2a 174 print "#else"; \
88948ad0 175 print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] + 4 ", R_PPC_ADDR32, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
669a9a2a 176 print "#endif"; \
88948ad0
AM
177} \
178$7 != "'${toe}'" && ! $7 in sec_off { \
179 print "#error Section not found for " $8; \
180} \
181'`
669a9a2a
AM
182
183 .section .data,"aw",@progbits
184 .globl ${SYMBOL}
11d5fa89 185 .type ${SYMBOL}, @object
669a9a2a
AM
186# fill in a struct spe_program_handle
187#ifdef _LP64
188 .p2align 3
189${SYMBOL}:
190 .int 24
191 .int 0
192 .quad __speelf__
193 .quad __spetoe__
194#else
195 .p2align 2
196${SYMBOL}:
197 .int 12
198 .int __speelf__
199 .int __spetoe__
200#endif
11d5fa89 201 .size ${SYMBOL}, . - ${SYMBOL}
88948ad0
AM
202
203`${READELF} -s -W ${INFILE} | grep ' _SPUEAR_' | sort -k 2 | awk \
204'{ \
205 print " .globl '${SYMBOL}'_" substr($8, 9); \
206 print " .type '${SYMBOL}'_" substr($8, 9) ", @object"; \
207 print " .size '${SYMBOL}'_" substr($8, 9) ", 4"; \
208 print "'${SYMBOL}'_" substr($8, 9) ":"; \
209 print " .int " $2; \
210} \
211'`
669a9a2a
AM
212EOF
213}
214
215main "$@"
This page took 0.042558 seconds and 4 git commands to generate.