mep-relocs.pl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/usr/bin/perl
  2. # -*- perl -*-
  3. #
  4. # Toshiba MeP Media Engine Relocation Generator
  5. # Copyright (C) 2001-2022 Free Software Foundation, Inc.
  6. # This file is part of BFD.
  7. # Originally written by DJ Delorie <dj@redhat.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. # MA 02110-1301, USA.
  23. # Usage: Run this anywhere inside your source tree. It will read
  24. # include/elf/mep.h and scan the comments therein. It will renumber
  25. # the relocs to be sequential (this is needed so that bfd/elf32-mep.h
  26. # works) if needed. It will then update the reloc list in bfd/reloc.c
  27. # and the howto, mapping, and apply routines in bfd/elf32-mep.c. You
  28. # can then regenerate bfd-in2.h and check everything in.
  29. # FIXME: After the relocation list is finalized, change this to
  30. # *verify* the reloc list, rather than resequence it.
  31. while (! -f "include/elf/mep.h" && ! -f "bfd/reloc.c") {
  32. chdir "..";
  33. $pwd = `pwd`;
  34. if ($pwd !~ m@/.*/@) {
  35. print STDERR "Cannot find include/elf/mep.h or bfd/reloc.h\n";
  36. exit 1;
  37. }
  38. }
  39. $pwd = `pwd`;
  40. print "srctop is $pwd";
  41. printf "Reading include/elf/mep.h ...\n";
  42. open(MEPH, "include/elf/mep.h");
  43. open(MEPHO, "> include/elf/mep.h.new") || die("mep.h.new create: $!");
  44. $val = 0;
  45. while (<MEPH>) {
  46. if (($pre,$rel,$rest) = /(.*RELOC_NUMBER \()([^,]+), *\d+(.*)/) {
  47. $rest =~ s/[\r\n]+$//;
  48. print (MEPHO "$pre$rel, $val$rest\n") || die("mep.h.new write: $!");
  49. $val ++;
  50. $rel =~ s/R_MEP_//;
  51. push(@relocs, $rel);
  52. $rest =~ s@.*/\* @@;
  53. ($pattern, $sign, $attrs) = $rest =~ m@(.*) ([US]) (.*)\*/@;
  54. $pattern =~ s/ //g;
  55. push(@pattern, $pattern);
  56. push(@sign, $sign);
  57. push(@attrs, $attrs);
  58. printf "%4d $rel p=`$pattern' s=`$sign' a=`$attrs'\n", $#pattern;
  59. } else {
  60. print(MEPHO) || die("mep.h.new write: $!");
  61. }
  62. }
  63. close(MEPH);
  64. close(MEPHO) || die("mep.h.new close: $!");
  65. &swapfile("include/elf/mep.h");
  66. redo_file ("bfd/reloc.c",
  67. "",
  68. "ENUMDOC\n Toshiba Media Processor Relocations.\n\nCOMMENT\n",
  69. "ENUM\n BFD_RELOC_MEP_%s\n",
  70. "");
  71. $autogen = " /* This section generated from bfd/mep-relocs.pl from include/elf/mep.h. */\n";
  72. redo_file ("bfd/elf32-mep.c",
  73. "MEPRELOC:HOWTO",
  74. $autogen,
  75. "MEPRELOC:END",
  76. "",
  77. "&emit_howto();",
  78. "MEPRELOC:MAP",
  79. $autogen,
  80. "MEPRELOC:END",
  81. "",
  82. " MAP(%s);\n",
  83. "MEPRELOC:APPLY",
  84. $autogen,
  85. "MEPRELOC:END",
  86. "",
  87. "&emit_apply();",
  88. );
  89. sub mask2shifts {
  90. my ($mask) = @_;
  91. my ($bits, $left, $right, $ci, $c, $cv);
  92. $bits = 0;
  93. $left = 0;
  94. $right = 32;
  95. for ($ci=0; $ci<length($mask); $ci++) {
  96. $c = substr($mask, $ci, 1);
  97. $left++;
  98. next if $c eq '-';
  99. $left = 0;
  100. $cv = ord($c) - ord('0');
  101. $cv -= ord('a') - ord('9') - 1 if $cv > 9;
  102. $right = $cv unless $right < $cv;
  103. $bits = $cv+1 unless $bits > $cv+1;
  104. }
  105. $mask =~ tr/-/1/c;
  106. $mask =~ tr/-/0/;
  107. ($rmask = $mask) =~ tr/01/10/;
  108. $mask = unpack("H*", pack("B*", $mask));
  109. $rmask = unpack("H*", pack("B*", $rmask));
  110. return ($bits, $left, $right, $mask, $rmask);
  111. }
  112. sub emit_howto {
  113. for ($i=2; $i<=$#relocs; $i++) {
  114. $mask = $pattern[$i];
  115. if (length($mask) == 8) { $bytesize = 0; }
  116. elsif (length($mask) == 16) { $bytesize = 1; }
  117. elsif (length($mask) == 32) { $bytesize = 2; }
  118. ($bits, $left, $right, $mask) = mask2shifts ($mask);
  119. $bits[$i] = $bits;
  120. $pcrel = 0;
  121. $pcrel = 1 if $attrs[$i] =~ /pc-rel/i;
  122. $overflow = $sign[$i];
  123. $overflow = 'N' if $attrs[$i] =~ /no-overflow/;
  124. $c = "$relocs[$i],";
  125. printf(NEW " MEPREL (R_MEP_%-10s%d,%3d,%2d,%2d,%2d,%2s, 0x%s),\n",
  126. $c, $bytesize, $bits, $left, $right, $pcrel, $overflow, $mask);
  127. }
  128. }
  129. sub emit_apply {
  130. for ($i=2; $i<=$#relocs; $i++) {
  131. $v = "u";
  132. $v = "s" if $sign[$i] =~ /S/;
  133. if (length($pattern[$i]) == 8) {
  134. $e = ''; # no endian swap for bytes
  135. } elsif ($pattern[$i] =~ /-/ || length($pattern[$i]) == 16) {
  136. $e = '^e2'; # endian swap - 2byte words only
  137. } else {
  138. $e = '^e4' # endian swap for data
  139. }
  140. print NEW " case R_MEP_$relocs[$i]: /* $pattern[$i] */\n";
  141. if ($relocs[$i] =~ /HI16S/) {
  142. print NEW " u += 0x8000;\n"
  143. }
  144. if ($attrs[$i] =~ /tp-rel/i) {
  145. print NEW " u -= mep_tpoff_base(rel->r_offset);\n";
  146. }
  147. if ($attrs[$i] =~ /gp-rel/i) {
  148. print NEW " u -= mep_sdaoff_base(rel->r_offset);\n";
  149. }
  150. if ($attrs[$i] !~ /no-overflow/ && $bits[$i] < 32) {
  151. if ($v eq "u") {
  152. $max = (1 << $bits[$i]) - 1;
  153. print NEW " if (u > $max) r = bfd_reloc_overflow;\n";
  154. } else {
  155. $min = (1 << ($bits[$i]-1));
  156. $max = (1 << ($bits[$i])) - 1;
  157. print NEW " if (u + $min > $max) r = bfd_reloc_overflow;\n";
  158. }
  159. }
  160. for ($b=0; $b<length($pattern[$i]); $b += 8) {
  161. $mask = substr($pattern[$i], $b, 8);
  162. ($bits, $left, $right, $mask, $rmask) = mask2shifts ($mask);
  163. if ($left > $right) { $left -= $right; $right = 0; }
  164. else { $right -= $left; $left = 0; }
  165. if ($mask ne "00") {
  166. $bb = $b / 8;
  167. print NEW " byte[$bb$e] = ";
  168. print NEW "(byte[$bb$e] & 0x$rmask) | " if $rmask ne "00";
  169. if ($left) {
  170. print NEW "((u << $left) & 0x$mask)";
  171. } elsif ($right) {
  172. print NEW "((u >> $right) & 0x$mask)";
  173. } else {
  174. print NEW "(u & 0x$mask)";
  175. }
  176. print NEW ";\n";
  177. }
  178. }
  179. print NEW " break;\n";
  180. }
  181. }
  182. #-----------------------------------------------------------------------------
  183. sub redo_file {
  184. my ($file, @control) = @_;
  185. open(OLD, $file);
  186. open(NEW, "> $file.new") || die("$file.new create: $!");
  187. print "Scanning file $file ...\n";
  188. while (1) {
  189. $start = shift @control;
  190. $prefix = shift @control;
  191. $end = shift @control;
  192. $suffix = shift @control;
  193. $pattern = shift @control;
  194. if (!$start) {
  195. print NEW while <OLD>;
  196. last;
  197. }
  198. print " looking for $start\n";
  199. while (<OLD>) {
  200. print NEW;
  201. last if /\Q$start\E/;
  202. }
  203. print "can't find $start\n" unless $_;
  204. last unless $_;
  205. print NEW $prefix;
  206. if ($pattern =~ /^\&/) {
  207. eval $pattern;
  208. die("$pattern: $@") if $@;
  209. } else {
  210. for $i (2..$#relocs) {
  211. printf (NEW "$pattern", $relocs[$i]) || die("$file.new write: $!");
  212. $pattern =~ s/^ENUM\n/ENUMX\n/;
  213. }
  214. }
  215. print NEW $suffix;
  216. while (<OLD>) {
  217. last if /\Q$end\E/;
  218. }
  219. print NEW;
  220. }
  221. close(OLD);
  222. close(NEW) || die("$file.new close: $!");
  223. &swapfile($file);
  224. }
  225. #-----------------------------------------------------------------------------
  226. sub swapfile {
  227. my ($f) = @_;
  228. if ( ! -f "$f.save") {
  229. system "cp $f $f.save";
  230. }
  231. open(ORIG, $f);
  232. open(NEW, "$f.new");
  233. while (<ORIG>) {
  234. $n = <NEW>;
  235. if ($n ne $_) {
  236. close(ORIG);
  237. close(NEW);
  238. print " Updating $f\n";
  239. rename "$f", "$f.old";
  240. rename "$f.new", "$f";
  241. return;
  242. }
  243. }
  244. close(ORIG);
  245. close(NEW);
  246. print " No change to $f\n";
  247. unlink "$f.new";
  248. }