1#! /bin/sh 
2# Embed an SPU ELF executable into a PowerPC object file.
3#
4# Copyright 2006 Free Software Foundation, Inc.
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
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}"
137
138 .section .data.spetoe,"aw",@progbits
139 .p2align 7
140__spetoe__:
141`${READELF} -s ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
142'BEGIN { \
143  last_addr = 0; \
144  last_sym = ""; \
145} \
146last_addr != 0 && strtonum("0x" $2) != last_addr + 16 { \
147	print "#error Symbols " last_sym " and " $8 " are not 16 bytes apart!"; \
148} \
149{ last_addr = strtonum("0x" $2); \
150  last_sym = $8; \
151} \
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"; \
158} \
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"; \
165}'`
166
167 .section .data,"aw",@progbits
168 .globl ${SYMBOL}
169# fill in a struct spe_program_handle
170#ifdef _LP64
171 .p2align 3
172${SYMBOL}:
173 .int 24
174 .int 0
175 .quad __speelf__
176 .quad __spetoe__
177#else
178 .p2align 2
179${SYMBOL}:
180 .int 12
181 .int __speelf__
182 .int __spetoe__
183#endif
184EOF
185}
186
187main "$@"
188