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