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