* elf32-xtensa.c (analyze_relocations): Zero src_count if not relaxing.
[binutils.git] / binutils / embedspu.sh
blob0d41865ba5bf83a8402ae44f152cd9bc47f95ea8
1 #! /bin/sh
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
21 # 02110-1301, USA.
23 usage ()
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
35 program_transform_name=
36 mydir=`dirname "$0"`
38 find_prog ()
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
50 SYMBOL=
51 INFILE=
52 OUTFILE=
53 FLAGS=
55 parse_args ()
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
76 if test ! -r "$INFILE"; then
77 echo "${INFILE}: File not found"
78 usage
82 main ()
84 parse_args "$@"
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
95 CC="$prog"
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
108 READELF="$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
114 then
115 echo "${INFILE}: Does not appear to be an SPU executable"
116 exit 1
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
129 # in .data.spetoe.
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
134 .p2align 7
135 __speelf__:
136 .incbin "${INFILE}"
138 .section .data.spetoe,"aw",@progbits
139 .p2align 7
140 __spetoe__:
141 `${READELF} -s -W ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
142 'BEGIN { \
143 last_addr = 0; \
144 last_sym = ""; \
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); \
150 last_sym = $8; \
152 $8 == "_EAR_" { \
153 print "#ifdef _LP64"; \
154 print " .quad __speelf__, 0"; \
155 print "#else"; \
156 print " .int 0, __speelf__, 0, 0"; \
157 print "#endif"; \
159 $8 != "_EAR_" { \
160 print "#ifdef _LP64"; \
161 print " .quad " substr($8, 6) ", 0"; \
162 print "#else"; \
163 print " .int 0, " substr($8, 6) ", 0, 0"; \
164 print "#endif"; \
167 .section .data,"aw",@progbits
168 .globl ${SYMBOL}
169 .type ${SYMBOL}, @object
170 # fill in a struct spe_program_handle
171 #ifdef _LP64
172 .p2align 3
173 ${SYMBOL}:
174 .int 24
175 .int 0
176 .quad __speelf__
177 .quad __spetoe__
178 #else
179 .p2align 2
180 ${SYMBOL}:
181 .int 12
182 .int __speelf__
183 .int __spetoe__
184 #endif
185 .size ${SYMBOL}, . - ${SYMBOL}
189 main "$@"