4 # Toshiba MeP Media Engine Relocation Generator
5 # Copyright (C) 2001-2017 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 ($attrs[$i] =~ /tp-rel/i) {
160 print NEW " $v -= mep_tpoff_base(rel->r_offset);\n";
162 if ($attrs[$i] =~ /gp-rel/i) {
163 print NEW " $v -= mep_sdaoff_base(rel->r_offset);\n";
165 if ($attrs[$i] !~ /no-overflow/ && $bits[$i] < 32) {
167 $max = (1 << $bits[$i]) - 1;
168 print NEW " if (u > $max) r = bfd_reloc_overflow;\n";
170 $min = -(1 << ($bits[$i]-1));
171 $max = (1 << ($bits[$i]-1)) - 1;
172 print NEW " if ($min > s || s > $max) r = bfd_reloc_overflow;\n";
175 for ($b=0; $b<length($pattern[$i]); $b += 8) {
176 $mask = substr($pattern[$i], $b, 8);
177 ($bits, $left, $right, $mask, $rmask) = mask2shifts ($mask);
178 if ($left > $right) { $left -= $right; $right = 0; }
179 else { $right -= $left; $left = 0; }
183 print NEW " byte[$bb$e] = ";
184 print NEW "(byte[$bb$e] & 0x$rmask) | " if $rmask ne "00";
186 print NEW "(($v << $left) & 0x$mask)";
188 print NEW "(($v >> $right) & 0x$mask)";
190 print NEW "($v & 0x$mask)";
195 print NEW " break;\n";
200 #-----------------------------------------------------------------------------
203 my ($file, @control) = @_;
205 open(NEW, "> $file.new") || die("$file.new create: $!");
207 print "Scanning file $file ...\n";
210 $start = shift @control;
211 $prefix = shift @control;
212 $end = shift @control;
213 $suffix = shift @control;
214 $pattern = shift @control;
217 print NEW while <OLD>;
221 print " looking for $start\n";
224 last if /\Q$start\E/;
226 print "can't find $start\n" unless $_;
230 if ($pattern =~ /^\&/) {
232 die("$pattern: $@") if $@;
234 for $i (2..$#relocs) {
235 printf (NEW "$pattern", $relocs[$i]) || die("$file.new write: $!");
236 $pattern =~ s/^ENUM\n/ENUMX\n/;
247 close(NEW) || die("$file.new close: $!");
251 #-----------------------------------------------------------------------------
255 if ( ! -f "$f.save") {
256 system "cp $f $f.save";
265 print " Updating $f\n";
266 rename "$f", "$f.old";
267 rename "$f.new", "$f";
273 print " No change to $f\n";