4 # Toshiba MeP Media Engine Relocation Generator
5 # Copyright (C) 2001, 2007 Free Software Foundation, Inc.
6 # This file is part of BFD.
7 # Originally written by DJ Delorie <dj@redhat.com>
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 2 of the License, or
12 # (at your option) any later version.
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.
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, MA 02110-1301, USA. */
24 # Usage: Run this anywhere inside your source tree. It will read
25 # include/elf/mep.h and scan the comments therein. It will renumber
26 # the relocs to be sequential (this is needed so that bfd/elf32-mep.h
27 # works) if needed. It will then update the reloc list in bfd/reloc.c
28 # and the howto, mapping, and apply routines in bfd/elf32-mep.c. You
29 # can then regenerate bfd-in2.h and check everything in.
31 # FIXME: After the relocation list is finalized, change this to
32 # *verify* the reloc list, rather than resequence it.
34 while (! -f
"include/elf/mep.h" && ! -f
"bfd/reloc.c") {
37 if ($pwd !~ m@
/.*/@
) {
38 print STDERR
"Cannot find include/elf/mep.h or bfd/reloc.h\n";
43 print "srctop is $pwd";
45 printf "Reading include/elf/mep.h ...\n";
46 open(MEPH
, "include/elf/mep.h");
47 open(MEPHO
, "> include/elf/mep.h.new") || die("mep.h.new create: $!");
50 if (($pre,$rel,$rest) = /(.*RELOC_NUMBER \()([^,]+), *\d+(.*)/) {
51 $rest =~ s/[\r\n]+$//;
52 print (MEPHO
"$pre$rel, $val$rest\n") || die("mep.h.new write: $!");
58 ($pattern, $sign, $attrs) = $rest =~ m@
(.*) ([US
]) (.*)\
*/@
;
60 push(@pattern, $pattern);
64 printf "%4d $rel p=`$pattern' s=`$sign' a=`$attrs'\n", $#pattern;
67 print(MEPHO
) || die("mep.h.new write: $!");
71 close(MEPHO
) || die("mep.h.new close: $!");
73 &swapfile
("include/elf/mep.h");
75 redo_file
("bfd/reloc.c",
77 "ENUMDOC\n Toshiba Media Processor Relocations.\n\nCOMMENT\n",
78 "ENUM\n BFD_RELOC_MEP_%s\n",
81 $autogen = " /* This section generated from bfd/mep-relocs.pl from include/elf/mep.h. */\n";
83 redo_file
("bfd/elf32-mep.c",
103 my ($bits, $left, $right, $ci, $c, $cv);
107 for ($ci=0; $ci<length($mask); $ci++) {
108 $c = substr($mask, $ci, 1);
112 $cv = ord($c) - ord('0');
113 $cv -= ord('a') - ord('9') - 1 if $cv > 9;
114 $right = $cv unless $right < $cv;
115 $bits = $cv+1 unless $bits > $cv+1;
119 ($rmask = $mask) =~ tr/01/10/;
120 $mask = unpack("H*", pack("B*", $mask));
121 $rmask = unpack("H*", pack("B*", $rmask));
122 return ($bits, $left, $right, $mask, $rmask);
126 for ($i=2; $i<=$#relocs; $i++) {
127 $mask = $pattern[$i];
129 if (length($mask) == 8) { $bytesize = 0; }
130 elsif (length($mask) == 16) { $bytesize = 1; }
131 elsif (length($mask) == 32) { $bytesize = 2; }
133 ($bits, $left, $right, $mask) = mask2shifts
($mask);
136 $pcrel = 1 if $attrs[$i] =~ /pc-rel/i;
137 $overflow = $sign[$i];
138 $overflow = 'N' if $attrs[$i] =~ /no-overflow/;
141 printf(NEW
" MEPREL (R_MEP_%-10s%d,%3d,%2d,%2d,%2d,%2s, 0x%s),\n",
142 $c, $bytesize, $bits, $left, $right, $pcrel, $overflow, $mask);
147 for ($i=2; $i<=$#relocs; $i++) {
149 $v = "s" if $sign[$i] =~ /S/;
150 if (length($pattern[$i]) == 8) {
151 $e = ''; # no endian swap for bytes
152 } elsif ($pattern[$i] =~ /-/ || length($pattern[$i]) == 16) {
153 $e = '^e2'; # endian swap - 2byte words only
155 $e = '^e4' # endian swap for data
157 print NEW
" case R_MEP_$relocs[$i]: /* $pattern[$i] */\n";
158 if ($attrs[$i] =~ /tp-rel/i) {
159 print NEW
" $v -= mep_tpoff_base(rel->r_offset);\n";
161 if ($attrs[$i] =~ /gp-rel/i) {
162 print NEW
" $v -= mep_sdaoff_base(rel->r_offset);\n";
164 if ($attrs[$i] !~ /no-overflow/ && $bits[$i] < 32) {
166 $max = (1 << $bits[$i]) - 1;
167 print NEW
" if (u > $max) r = bfd_reloc_overflow;\n";
169 $min = -(1 << ($bits[$i]-1));
170 $max = (1 << ($bits[$i]-1)) - 1;
171 print NEW
" if ($min > s || s > $max) r = bfd_reloc_overflow;\n";
174 for ($b=0; $b<length($pattern[$i]); $b += 8) {
175 $mask = substr($pattern[$i], $b, 8);
176 ($bits, $left, $right, $mask, $rmask) = mask2shifts
($mask);
177 if ($left > $right) { $left -= $right; $right = 0; }
178 else { $right -= $left; $left = 0; }
182 print NEW
" byte[$bb$e] = ";
183 print NEW
"(byte[$bb$e] & 0x$rmask) | " if $rmask ne "00";
185 print NEW
"(($v << $left) & 0x$mask)";
187 print NEW
"(($v >> $right) & 0x$mask)";
189 print NEW
"($v & 0x$mask)";
194 print NEW
" break;\n";
199 #-----------------------------------------------------------------------------
202 my ($file, @control) = @_;
204 open(NEW
, "> $file.new") || die("$file.new create: $!");
206 print "Scanning file $file ...\n";
209 $start = shift @control;
210 $prefix = shift @control;
211 $end = shift @control;
212 $suffix = shift @control;
213 $pattern = shift @control;
216 print NEW
while <OLD
>;
220 print " looking for $start\n";
223 last if /\Q$start\E/;
225 print "can't find $start\n" unless $_;
229 if ($pattern =~ /^\&/) {
231 die("$pattern: $@") if $@
;
233 for $i (2..$#relocs) {
234 printf (NEW
"$pattern", $relocs[$i]) || die("$file.new write: $!");
235 $pattern =~ s/^ENUM\n/ENUMX\n/;
246 close(NEW
) || die("$file.new close: $!");
250 #-----------------------------------------------------------------------------
254 if ( ! -f
"$f.save") {
255 system "cp $f $f.save";
264 print " Updating $f\n";
265 rename "$f", "$f.old";
266 rename "$f.new", "$f";
272 print " No change to $f\n";