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>
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.
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,
25 # Usage: Run this anywhere inside your source tree. It will read
26 # include/elf/mep.h and scan the comments therein. It will renumber
27 # the relocs to be sequential (this is needed so that bfd/elf32-mep.h
28 # works) if needed. It will then update the reloc list in bfd/reloc.c
29 # and the howto, mapping, and apply routines in bfd/elf32-mep.c. You
30 # can then regenerate bfd-in2.h and check everything in.
32 # FIXME: After the relocation list is finalized, change this to
33 # *verify* the reloc list, rather than resequence it.
35 while (! -f
"include/elf/mep.h" && ! -f
"bfd/reloc.c") {
38 if ($pwd !~ m@
/.*/@
) {
39 print STDERR
"Cannot find include/elf/mep.h or bfd/reloc.h\n";
44 print "srctop is $pwd";
46 printf "Reading include/elf/mep.h ...\n";
47 open(MEPH
, "include/elf/mep.h");
48 open(MEPHO
, "> include/elf/mep.h.new") || die("mep.h.new create: $!");
51 if (($pre,$rel,$rest) = /(.*RELOC_NUMBER \()([^,]+), *\d+(.*)/) {
52 $rest =~ s/[\r\n]+$//;
53 print (MEPHO
"$pre$rel, $val$rest\n") || die("mep.h.new write: $!");
59 ($pattern, $sign, $attrs) = $rest =~ m@
(.*) ([US
]) (.*)\
*/@
;
61 push(@pattern, $pattern);
65 printf "%4d $rel p=`$pattern' s=`$sign' a=`$attrs'\n", $#pattern;
68 print(MEPHO
) || die("mep.h.new write: $!");
72 close(MEPHO
) || die("mep.h.new close: $!");
74 &swapfile
("include/elf/mep.h");
76 redo_file
("bfd/reloc.c",
78 "ENUMDOC\n Toshiba Media Processor Relocations.\n\nCOMMENT\n",
79 "ENUM\n BFD_RELOC_MEP_%s\n",
82 $autogen = " /* This section generated from bfd/mep-relocs.pl from include/elf/mep.h. */\n";
84 redo_file
("bfd/elf32-mep.c",
104 my ($bits, $left, $right, $ci, $c, $cv);
108 for ($ci=0; $ci<length($mask); $ci++) {
109 $c = substr($mask, $ci, 1);
113 $cv = ord($c) - ord('0');
114 $cv -= ord('a') - ord('9') - 1 if $cv > 9;
115 $right = $cv unless $right < $cv;
116 $bits = $cv+1 unless $bits > $cv+1;
120 ($rmask = $mask) =~ tr/01/10/;
121 $mask = unpack("H*", pack("B*", $mask));
122 $rmask = unpack("H*", pack("B*", $rmask));
123 return ($bits, $left, $right, $mask, $rmask);
127 for ($i=2; $i<=$#relocs; $i++) {
128 $mask = $pattern[$i];
130 if (length($mask) == 8) { $bytesize = 0; }
131 elsif (length($mask) == 16) { $bytesize = 1; }
132 elsif (length($mask) == 32) { $bytesize = 2; }
134 ($bits, $left, $right, $mask) = mask2shifts
($mask);
137 $pcrel = 1 if $attrs[$i] =~ /pc-rel/i;
138 $overflow = $sign[$i];
139 $overflow = 'N' if $attrs[$i] =~ /no-overflow/;
142 printf(NEW
" MEPREL (R_MEP_%-10s%d,%3d,%2d,%2d,%2d,%2s, 0x%s),\n",
143 $c, $bytesize, $bits, $left, $right, $pcrel, $overflow, $mask);
148 for ($i=2; $i<=$#relocs; $i++) {
150 $v = "s" if $sign[$i] =~ /S/;
151 if (length($pattern[$i]) == 8) {
152 $e = ''; # no endian swap for bytes
153 } elsif ($pattern[$i] =~ /-/ || length($pattern[$i]) == 16) {
154 $e = '^e2'; # endian swap - 2byte words only
156 $e = '^e4' # endian swap for data
158 print NEW
" case R_MEP_$relocs[$i]: /* $pattern[$i] */\n";
159 if ($relocs[$i] =~ /HI16S/) {
160 print NEW
" u += 0x8000;\n"
162 if ($attrs[$i] =~ /tp-rel/i) {
163 print NEW
" u -= mep_tpoff_base(rel->r_offset);\n";
165 if ($attrs[$i] =~ /gp-rel/i) {
166 print NEW
" u -= mep_sdaoff_base(rel->r_offset);\n";
168 if ($attrs[$i] !~ /no-overflow/ && $bits[$i] < 32) {
170 $max = (1 << $bits[$i]) - 1;
171 print NEW
" if (u > $max) r = bfd_reloc_overflow;\n";
173 $min = (1 << ($bits[$i]-1));
174 $max = (1 << ($bits[$i])) - 1;
175 print NEW
" if (u + $min > $max) r = bfd_reloc_overflow;\n";
178 for ($b=0; $b<length($pattern[$i]); $b += 8) {
179 $mask = substr($pattern[$i], $b, 8);
180 ($bits, $left, $right, $mask, $rmask) = mask2shifts
($mask);
181 if ($left > $right) { $left -= $right; $right = 0; }
182 else { $right -= $left; $left = 0; }
186 print NEW
" byte[$bb$e] = ";
187 print NEW
"(byte[$bb$e] & 0x$rmask) | " if $rmask ne "00";
189 print NEW
"((u << $left) & 0x$mask)";
191 print NEW
"((u >> $right) & 0x$mask)";
193 print NEW
"(u & 0x$mask)";
198 print NEW
" break;\n";
203 #-----------------------------------------------------------------------------
206 my ($file, @control) = @_;
208 open(NEW
, "> $file.new") || die("$file.new create: $!");
210 print "Scanning file $file ...\n";
213 $start = shift @control;
214 $prefix = shift @control;
215 $end = shift @control;
216 $suffix = shift @control;
217 $pattern = shift @control;
220 print NEW
while <OLD
>;
224 print " looking for $start\n";
227 last if /\Q$start\E/;
229 print "can't find $start\n" unless $_;
233 if ($pattern =~ /^\&/) {
235 die("$pattern: $@") if $@
;
237 for $i (2..$#relocs) {
238 printf (NEW
"$pattern", $relocs[$i]) || die("$file.new write: $!");
239 $pattern =~ s/^ENUM\n/ENUMX\n/;
250 close(NEW
) || die("$file.new close: $!");
254 #-----------------------------------------------------------------------------
258 if ( ! -f
"$f.save") {
259 system "cp $f $f.save";
268 print " Updating $f\n";
269 rename "$f", "$f.old";
270 rename "$f.new", "$f";
276 print " No change to $f\n";