summaryrefslogtreecommitdiffstats
path: root/data/sysrom/genrel.pl
blob: 435f5edc1ed19fdfa882163ffe687357a615b2c3 (plain)
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
#!/usr/bin/perl

use bytes;

($binfile, $shrfile, $out) = @ARGV;

if (!defined($out)) {
    die "Usage: $0 binfile Shrfile output\n";
}

open(BINFILE, '<', $binfile)
    or die "$0: $binfile: $!\n";
open(SHRFILE, '<', $shrfile)
    or die "$0: $shrfile: $!\n";

$binsize = (stat(BINFILE))[7];

#
# First, read the section header information
#
$entrypoint = 0;
while (defined($line = <SHRFILE>)) {
    if ($line =~ /^\s*Entry point address:\s*0x([0-9a-f]+)"/) {
	$entrypoint = hex $1;
    }
    last if ($line =~ /^Section Headers/);
}

scalar <SHRFILE>;		# Skip header

$totalsize = 0;
while (defined($line = <SHRFILE>)) {
    ($nr, $name, $type, $addr, $off, $size, $es, $flg, $lk, $inf, $al) =
	($line =~ /^\s*\[\s*([0-9]+)\]\s(\S*)\s+(\S+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([A-Z]*)\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)/);
    last unless (defined($al));

    $addr = hex $addr;
    $off  = hex $off;
    $size = hex $size;

    $nr += 0;
    $lk += 0;
    $inf += 0;
    $al += 0;

    $sec_nr{$name} = $nr;
    $sec[$nr] = [$name, $type, $addr, $off, $size, $es, $flg, $lk, $inf, $al];

    if ($flg =~ /A/) {
	if ($addr + $size > $totalsize) {
	    $totalsize = $addr + $size;
	}
    }
}

if ($binsize > $totalsize) {
    die "$0: binary size > total size\n";
}

#
# Now actually parse relocations
#
undef $base;
@relocs = ();
while (defined($line = <SHRFILE>)) {
    chomp $line;
    if ($line =~ /^Relocation section '([^\']+)' /) {
	$rs = $1;		# Relocation section name
	$as = ${$sec[$sec_nr{$rs}]}[8]; # Associated section (= real section)
	if (!$as || !defined($sec[$as])) {
	    die "$0: cannot figure out base section for '$rs'\n";
	}
	if (${$sec[$as]}[6] =~ /A/) {
	    $base = ${$sec[$as]}[2];	# Base address of this section
	} else {
	    undef $base;	# Non-manifest section, ignore
	}
    } elsif (defined($base) &&
	     $line =~ /^([0-9a-f]+)\s+([0-9a-f]+)\s+(\S+)\s+([0-9a-f]+)/) {
	$offset = hex $1;
	$info   = hex $2;
	$type   = $3;
	$symval = hex $4;

	if ($type ne 'R_68K_32') {
	    die "$0: unknown relocation: $line\n";
	}
	$addr = $base + $offset;

	if ($addr + 3 > $totalsize) {
	    die "$0: relocation outside the file\n";
	} elsif ($addr & 1) {
	    die "$0: relocation at an odd address\n";
	}

	push(@relocs, $base + $offset);
    }
}
close(SHRFILE);

#
# Write executable header
#

open(OUT, '>' , $out)
    or die "$0: $out: $!\n";

# Pad the sizes to a multiple of 32
$binsize   = ($binsize + 31) & ~31;
$totalsize = ($totalsize + 31) & ~31;
$bsssize   = $totalsize - $binsize;

print OUT pack("N*",
	       0x1ae475e6,	# Magic number
	       512,		# Header length
	       0,		# Flags
	       $entrypoint,	# Entry point
	       $totalsize,	# Memory allocation size
	       $binsize,	# Binary data size
	       $bsssize,	# Size to be zeroed
	       scalar @relocs);	# Relocation count
print OUT "\0" x (512 - 8*4);

#
# Copy the binary data
#
$raw_binsize = 0;
while (read(BINFILE, $bindata, 4096) > 0) {
    print OUT $bindata;
    $raw_binsize += length($bindata);
}
close(BINFILE);

#
# Add padding if necessary
#
print OUT "\0" x ($binsize - $raw_binsize);

#
# Output the relocations
#
@relocs = sort { $a <=> $b } @relocs;
print OUT pack("N*", @relocs);

exit 0;