forked from FDOS/freecom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkxref.pl
263 lines (220 loc) · 5.55 KB
/
mkxref.pl
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
#!perl
# Generate Xref from all local *.OBJ files and the C standard library
##### Environmental settings
# How to dump .OBJ/.LIB files
## '%f%' is substituted by the source filename
$dump = 'tdump %f%';
# Name of C standard library
$clib = 'd:\bc5\lib\cs.lib';
# Name of SUPPL library
$suppl = 'suppl_s.lib';
# Names of files to be ignored while gener‘ted the dependency
%ignoreFile = ('c0.asm' => 1);
##### End of user-configurable settings
open(LOG, ">mkxref.log")
or die "Cannot create mkxfer.log: $!";
@files = <*.dmp>;
# Read in all local source files
$mkextref = sub {
$ext{$name} .= ',' . $file;
$extf{$file} .= ',' . $name;
};
foreach $file (@files) {
print STDERR "Processing $file ...\n";
$file =~ tr/A-Z/a-z/;
next if $file =~ /^(suppl|clib)_.\.dmp$/;
&scan($file, sub { $file = $name; }
, sub {
if(defined $pub{$name}) {
&warning("$name multiply defined: $pub{$name} and $file");
} else {
$pub{$name} = $file;
$pubf{$file} .= ",$name";
}
}
, $mkextref);
}
print STDERR "Processing SUPPL library...\n";
&scan('suppl_s.dmp', sub { $file = "Suppl<$name>"; }
, sub {
if(defined $pub{$name}) {
&warning("$name in $file overridden by $pub{$name}");
--$warning;
} else {
$pub{$name} = $file;
$pubf{$file} .= ",$name";
}
}
, $mkextref);
print STDERR "Processing C library...\n";
&scan('clib_s.dmp', sub { $file = "Clib<$name>"; }
, sub {
if(defined $pub{$name}) {
&warning("$name in $file overridden by $pub{$name}");
--$warning;
} else {
$pub{$name} = $file;
$pubf{$file} .= ",$name";
}
}
, $mkextref);
die "Solve the warnings before proceed" if $warning;
print STDERR "Generating file dependency information...\n";
# "What files reference a given symbol"
foreach $symbol (keys %pubf) {
$reftbl{$symbol} = '';
%refby = ('' => -1, $symbol => -1);
# $refSyms: symbols exports the given file and can be
# referenced by other files
$refSyms = $pubf{$symbol};
$depth = 0;
while($refSyms) {
++$depth;
# Aquire where these symbols are located
$files = '';
foreach $name (split(',', $refSyms)) {
# The exported symbols of the files referenced at this depth are
# referenced are referenced themselves by these files
$files .= $ext{$name};
}
$refSyms = '';
# Aquire which symbols are externally referenced by the new files
$sym = '|';
foreach $file (split(',', $files)) {
if(!defined $refby{$file} && !defined $ignoreFile{$file}) {
$reftbl{$symbol} .= $sym . $file;
$sym = ',';
$refby{$file} = $depth;
# the $file references the current file, now determine
# which files reference symbols exported by this one
# to get a recursive list
$refSyms .= $pubf{$file};
}
}
}
}
# "What files a given symbol references"
foreach $symbol (keys %extf) {
$needtbl{$symbol} = '';
%needs = ('' => -1, $symbol => -1);
$needSyms = $extf{$symbol};
$depth = 0;
while($needSyms) {
++$depth;
# Aquire which files the imported symbols are located in
$newneedSyms = '';
$sym = '|';
foreach $name (split(',', $needSyms)) {
$file = $pub{$name};
if(!defined $needs{$file} && !defined $ignoreFile{$file}) {
$needs{$file} = $depth;
$needtbl{$symbol} .= $sym . $file;
$sym = ',';
# $file is referenced by the current file, now determine
# which symbols are referenced by this one
$newneedSyms .= $extf{$file};
}
}
$needSyms = $newneedSyms;
}
}
print STDERR "Processing MAP file...\n";
@ARGV = <*.map> unless @ARGV;
while(<>) {
last if /Publics by Value/;
}
$lastSegm = $lastAddr = '';
@names = ();
@files = ();
while(<>) {
next unless /^\s*([0-9a-fA-F]+):([0-9a-fA-F]+)\s+(\S+)\s*$/;
$segm = $1;
$addr = $2;
$name = $3;
push(@names, $name);
if(defined $pub{$name}) {
$file = $pub{$name};
$files{$file} = 1;
}
if($lastSegm eq $segm) {
$len{$lastName} = hex($addr) - hex($lastAddr);
}
$lastSegm = $segm;
$lastAddr = $addr;
$lastName = $name;
}
print "Imported symbols:\n\t" . join("\n\t", sort @names) . "\n";
print "Linked files:\n\t" . join("\n\t", sort keys %files) . "\n";
foreach $name (sort @names) {
print "\n$name: ";
if(defined $pub{$name}) {
$file = $pub{$name};
print "PUBDEF in $file\n";
} else {
print "no PUBDEF\n";
}
if(defined $len{$name}) {
print "\testimated length: $len{$name} byte\n";
}
&printRec("references", 'references nothing', $needtbl{$pub{$name}});
&printRec("referenced by", 'never referenced', $reftbl{$pub{$name}});
}
sub files {
local($files) = @_;
local($file);
local($h) = '';
foreach $file (split(',', $files)) {
if($file && defined $files{$file}) {
$h .= ", $file";
}
}
return substr($h, 2);
}
sub warning {
++$warning;
print LOG join("\n", @_) . "\n";
print STDERR join("\n", @_) . "\n";
}
sub scan {
local($file, $thecode, $pubcode, $extcode) = @_;
open(IN, "$file")
or die "Cannot open $file";
while(<IN>) {
$code = '' if /^\S/;
if(/\bTHEADR\s+(\S+)\s*$/) {
$name = $1;
$name =~ tr/A-Z/a-z/;
&$thecode;
} elsif(!$code && /\bPUBDEF\b/) {
$_ = $';
$code = $pubcode;
} elsif(!$code && /\bEXTDEF\b/) {
$_ = $';
$code = $extcode;
}
&processList($code) if $code;
}
close IN;
}
sub processList {
local($code) = @_;
if(/\'([^\']+)\'/) {
$name = $1;
&$code;
}
}
sub printRec {
local($yesStr, $noStr, $files) = @_;
local(@files, $depth, $prompt, $fil, $dep);
if(length($files) > 1) {
@files = split(/\|/, $files);
$prompt = "==$yesStr";
$dep = 0;
foreach $depth ($[..$#files) {
$fil = &files($files[$depth])
and (print "$prompt: $fil\n"), $prompt = " depth #" . ++$dep;
}
} else {
$noStr and print "==$noStr\n";
}
}