embedspu.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #! /bin/sh
  2. # Embed an SPU ELF executable into a PowerPC object file.
  3. #
  4. # Copyright (C) 2006-2022 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 3 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. usage ()
  23. {
  24. echo "Usage: embedspu [flags] symbol_name input_filename output_filename"
  25. echo
  26. echo " input_filename: SPU ELF executable to be embedded"
  27. echo " output_filename: Resulting PowerPC object file"
  28. echo " symbol_name: Name of program handle struct to be defined"
  29. echo " flags: GCC flags defining PowerPC object file format"
  30. echo " (e.g. -m32 or -m64)"
  31. exit 1
  32. }
  33. program_transform_name=
  34. mydir=`dirname "$0"`
  35. find_prog ()
  36. {
  37. prog=`echo $1 | sed "$program_transform_name"`
  38. prog="$mydir/$prog"
  39. test -x "$prog" && return 0
  40. prog="$mydir/$1"
  41. test -x "$prog" && return 0
  42. prog=`echo $1 | sed "$program_transform_name"`
  43. which $prog > /dev/null 2> /dev/null && return 0
  44. return 1
  45. }
  46. SYMBOL=
  47. INFILE=
  48. OUTFILE=
  49. FLAGS=
  50. parse_args ()
  51. {
  52. while test -n "$1"; do
  53. case "$1" in
  54. -*) FLAGS="${FLAGS} $1" ;;
  55. *) if test -z "$SYMBOL"; then
  56. SYMBOL="$1"
  57. elif test -z "$INFILE"; then
  58. INFILE="$1"
  59. elif test -z "$OUTFILE"; then
  60. OUTFILE="$1"
  61. else
  62. echo "Too many arguments!"
  63. usage
  64. fi ;;
  65. esac
  66. shift
  67. done
  68. if test -z "$OUTFILE"; then
  69. usage
  70. fi
  71. if test ! -r "$INFILE"; then
  72. echo "${INFILE}: File not found"
  73. usage
  74. fi
  75. }
  76. main ()
  77. {
  78. parse_args "$@"
  79. # Find a powerpc gcc. Support running from a combined tree build.
  80. if test -x "$mydir/../gcc/xgcc"; then
  81. CC="$mydir/../gcc/xgcc -B$mydir/../gcc/"
  82. else
  83. find_prog gcc
  84. if test $? -ne 0; then
  85. echo "Cannot find $prog"
  86. exit 1
  87. fi
  88. CC="$prog"
  89. fi
  90. # Find readelf. Any old readelf should do.
  91. find_prog readelf
  92. if test $? -ne 0; then
  93. if which readelf > /dev/null 2> /dev/null; then
  94. prog=readelf
  95. else
  96. echo "Cannot find $prog"
  97. exit 1
  98. fi
  99. fi
  100. READELF="$prog"
  101. # Sanity check the input file
  102. if ! ${READELF} -h ${INFILE} | grep 'Class:.*ELF32' >/dev/null 2>/dev/null \
  103. || ! ${READELF} -h ${INFILE} | grep 'Type:.*EXEC' >/dev/null 2>/dev/null \
  104. || ! ${READELF} -h ${INFILE} | egrep 'Machine:.*(SPU|17)' >/dev/null 2>/dev/null
  105. then
  106. echo "${INFILE}: Does not appear to be an SPU executable"
  107. exit 1
  108. fi
  109. toe=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\.toe *[PROGN]*BITS *\([0-9a-f]*\).*,\1 \2,p'`
  110. toe_addr=`echo $toe | sed -n -e 's,.* ,,p'`
  111. toe=`echo $toe | sed -n -e 's, .*,,p'`
  112. has_ea=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *\._ea *PROGBITS.*,\1,p'`
  113. # For loaded sections, pick off section number, address, and file offset
  114. sections=`${READELF} -S ${INFILE} | sed -n -e 's, *\[ *\([0-9]*\)\] *[^ ]* *PROGBITS *\([0-9a-f]*\) *\([0-9a-f]*\).*,\1 \2 \3,p'`
  115. sections=`echo ${sections}`
  116. # For relocation sections, pick off file offset and info (points to
  117. # section where relocs apply)
  118. 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'`
  119. relas=`echo ${relas}`
  120. # Build embedded SPU image.
  121. # 1. The whole SPU ELF file is written to .rodata.speelf
  122. # 2. Symbols starting with the string "_EAR_" in the SPU ELF image are
  123. # special. They allow an SPU program to access corresponding symbols
  124. # (ie. minus the _EAR_ prefix), in the PowerPC program. _EAR_ without
  125. # a suffix is used to refer to the addrress of the SPU image in
  126. # PowerPC address space. _EAR_* symbols must all be defined in .toe
  127. # at 16 byte intervals, or they must be defined in other non-bss
  128. # sections.
  129. # Find all _EAR_ symbols in .toe using readelf, sort by address, and
  130. # write the address of the corresponding PowerPC symbol in a table
  131. # built in .data.spetoe. For _EAR_ symbols not in .toe, create
  132. # .reloc commands to relocate their location directly.
  133. # 3. Look for R_SPU_PPU32 and R_SPU_PPU64 relocations in the SPU ELF image
  134. # and create .reloc commands for them.
  135. # 4. Write a struct spe_program_handle to .data.
  136. # 5. Write a table of _SPUEAR_ symbols.
  137. ${CC} ${FLAGS} -x assembler-with-cpp -nostartfiles -nostdlib \
  138. -Wa,-mbig -Wa,-noexecstack -Wl,-r -Wl,-x -o ${OUTFILE} - <<EOF
  139. .section .data.spetoe,"aw",@progbits
  140. .p2align 7
  141. __spetoe__:
  142. `${READELF} -s -W ${INFILE} | grep ' _EAR_' | sort -k 2 | awk \
  143. 'BEGIN { \
  144. addr = strtonum ("0x" "'${toe_addr:-0}'"); \
  145. split ("'"${sections}"'", s, " "); \
  146. for (i = 1; i in s; i += 3) { \
  147. sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
  148. } \
  149. } \
  150. $7 == "'${toe}'" && strtonum ("0x" $2) != addr { \
  151. print "#error Symbol " $8 " not in 16 byte element toe array!"; \
  152. } \
  153. $7 == "'${toe}'" { \
  154. addr = addr + 16; \
  155. } \
  156. $7 == "'${toe}'" { \
  157. print "#ifdef _LP64"; \
  158. print " .quad " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0"; \
  159. print "#else"; \
  160. print " .int 0, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)) ", 0, 0"; \
  161. print "#endif"; \
  162. } \
  163. $7 != "'${toe}'" && $7 in sec_off { \
  164. print "#ifdef _LP64"; \
  165. print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] ", R_PPC64_ADDR64, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
  166. print "#else"; \
  167. print " .reloc __speelf__+" strtonum ("0x" $2) + sec_off[$7] + 4 ", R_PPC_ADDR32, " ($8 == "_EAR_" ? "__speelf__" : substr($8, 6)); \
  168. print "#endif"; \
  169. if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
  170. } \
  171. $7 != "'${toe}'" && ! $7 in sec_off { \
  172. print "#error Section not found for " $8; \
  173. } \
  174. '`
  175. `test -z "${relas}" || ${READELF} -r -W ${INFILE} | awk \
  176. 'BEGIN { \
  177. split ("'"${sections}"'", s, " "); \
  178. for (i = 1; i in s; i += 3) { \
  179. sec_off[s[i]] = strtonum ("0x" s[i+2]) - strtonum ("0x" s[i+1]); \
  180. } \
  181. split ("'"${relas}"'", s, " "); \
  182. for (i = 1; i in s; i += 2) { \
  183. rela[s[i]] = strtonum (s[i+1]); \
  184. } \
  185. } \
  186. /^Relocation section/ { \
  187. sec = substr($6, 3); \
  188. } \
  189. $3 ~ /R_SPU_PPU/ { \
  190. print "#ifdef _LP64"; \
  191. print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] ", R_PPC64_ADDR" substr($3, 10) ", " ($5 != "" ? $5 "+0x" $7 : "__speelf__ + 0x" $4); \
  192. print "#else"; \
  193. 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); \
  194. print "#endif"; \
  195. if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
  196. } \
  197. $3 ~ /unrecognized:/ { \
  198. print "#ifdef _LP64"; \
  199. print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] ", R_PPC64_ADDR" ($4 == "f" ? "64" : "32") ", " ($6 != "" ? $6 "+0x" $8 : "__speelf__ + 0x" $5); \
  200. print "#else"; \
  201. print " .reloc __speelf__+" strtonum ("0x" $1) + sec_off[rela[sec]] + ($4 == "f" ? 4 : 0)", R_PPC_ADDR32, " ($6 != "" ? $6 "+0x" $8 : "__speelf__ + 0x" $5); \
  202. print "#endif"; \
  203. if (!donedef) { print "#define HAS_RELOCS 1"; donedef = 1; }; \
  204. } \
  205. '`
  206. #if ${has_ea:-0}
  207. .section .data.speelf,"aw",@progbits
  208. #elif defined (HAS_RELOCS) && (defined (__PIC__) || defined (__PIE__))
  209. .section .data.rel.ro.speelf,"a",@progbits
  210. #else
  211. .section .rodata.speelf,"a",@progbits
  212. #endif
  213. .p2align 7
  214. __speelf__:
  215. .incbin "${INFILE}"
  216. .section .data.spehandle,"aw",@progbits
  217. .globl ${SYMBOL}
  218. .type ${SYMBOL}, @object
  219. # fill in a struct spe_program_handle
  220. #ifdef _LP64
  221. .p2align 3
  222. ${SYMBOL}:
  223. .int 24
  224. .int 0
  225. .quad __speelf__
  226. .quad __spetoe__
  227. #else
  228. .p2align 2
  229. ${SYMBOL}:
  230. .int 12
  231. .int __speelf__
  232. .int __spetoe__
  233. #endif
  234. .size ${SYMBOL}, . - ${SYMBOL}
  235. `${READELF} -s -W ${INFILE} | grep ' _SPUEAR_' | sort -k 2 | awk \
  236. '{ \
  237. print " .globl '${SYMBOL}'_" substr($8, 9); \
  238. print " .type '${SYMBOL}'_" substr($8, 9) ", @object"; \
  239. print " .size '${SYMBOL}'_" substr($8, 9) ", 4"; \
  240. print "'${SYMBOL}'_" substr($8, 9) ":"; \
  241. print " .int 0x" $2; \
  242. } \
  243. '`
  244. EOF
  245. }
  246. main "$@"