forked from freemint/mintlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecklib
executable file
·173 lines (148 loc) · 4.3 KB
/
checklib
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
#! /usr/bin/perl
#
# checklib - do some consistency checkings for libraries and object files.
# Copyright (C) 1999, 2000 by Guido Flohr <[email protected]>.
#
# The effect of this script is about the same as an invocation of the
# linker with "--whole-archive":
#
# checklib [ FILE ... ]
#
# The script throws together all FILES (stand-alone object files or
# libraries), extracts all symbols via nm and then outputs a list of
# multiple definitions for a symbol and references
# that cannot be satisfied from FILES.
#
# You can specify two lists of regular expressions, one that should match
# symbol names that are ok to remain undefined (for example the reference
# to the main() function from your startup code) and another list that
# defines symbols that are ok to be multiply defined (if your library
# is a hack). The MiNTLib uses the latter to for some dummy definitions
# that allow to link against libc.a without crt0.o.
#
# Common symbols should be handled alright, weak symbols aren't (but that
# shouldn't normally matter).
# Name of a BSD compatible nm.
# GNU binutils.
$nm = "nm -B";
# HP-UX
# $nm = "nm -p";
# Don't complain about undefined references to symbols that match this
# list of perl REs.
my @undefined_symbols = ( "_e(text|data)", # Linker.
"main", # Up to you.
"__V8_regerror", # Supplied by the user.
# Defined by libgcc and referenced by libc.
"___muldi3",
"___lshrdi3",
"___ashldi3",
"___udivdi3",
"___umoddi3",
);
# Don't complain about multiple definitions for symbols that match this
# list of perl REs.
my @duplicates = ();
##################### Main program ###################
# Exit status.
$status = 0;
if ($#ARGV == -1) {
print STDERR "Usage: $0 [ FILENAMES ]\n";
exit 1;
}
# Slurp in global symbols for each file.
ARGUMENT: foreach $file (@ARGV) {
open (NM, "$nm $file |") or die "cannot exec nm: $!";
my $lineno = 0;
my $archive = "";
my $last_empty = false;
my $module = $file;
LINE: while (<NM>) {
$lineno++;
chomp;
# Simple assumption: if first line is empty, we have
# a library archive, an object file otherwise.
if ($lineno == 1 && /^[:space:]*$/) {
$archive = $file;
$module = "???";
$last_empty = true;
next LINE;
}
if ($archive ne "") {
if ($last_empty eq true) {
# HP-SUX nm lists like
# /usr/lib/libc.a[foobar.o]:
# whereas GNU nm lists like
# foobar.o:
# These regex' always win.
s/.*\[//;
s/]+:$//;
$module = $_;
$last_empty = false;
next LINE;
}
if (/^\s*$/) {
$last_empty = true;
next LINE;
} else {
$last_empty = false;
}
}
# If we get here we have a line listing a symbol.
# Cast away the address.
s/^\S*//;
my ($type, $name) = split;
# Ignore additional type characters.
$type = substr ($type, 0, 1);
# Ignore local symbols.
next LINE unless ($type =~ /^[A-Z]/);
my $first_def = $symbols{$name}{"def"};
if ($type eq "U") {
# Add another reference if still undefined.
if ($first_def ne "y") {
$symbols{$name}{"refs"}
.= "$archive\[$module\]\n";
}
next LINE;
}
# We simply assume that all uppercase letters that are
# not `U' can satisfy a reference.
if ($first_def eq "y") {
my $first_ref = $symbols{$name}{"refs"};
foreach my $omit (@duplicates) {
next LINE if ($name =~ /$omit/);
}
# Handle common symbols.
next LINE if ($type eq "C");
# This is to make sure that we notice if a common
# symbol has turned into a real definition.
if ($symbols{$name}{"type"} eq "C") {
$symbols{$name}{"type"} = $type;
next LINE; # First time is still ok.
}
print STDERR
"$archive\[$module\]: redefinition of symbol \`$name'\
first defined in $first_ref\n";
next LINE;
$status = 1;
}
# This is a valid definition of the symbol.
$symbols{$name}{"def"} = "y";
$symbols{$name}{"type"} = $type;
$symbols{$name}{"refs"} = "$archive\[$module\]";
}
unless (close NM) {
$status = 1;
}
}
SYMBOL: foreach my $symbol (keys %symbols) {
if ($symbols{$symbol}{"def"} ne "y") {
foreach my $omit (@undefined_symbols) {
next SYMBOL if ($symbol =~ /$omit/);
}
$symbols{$symbol}{"refs"}
=~ s,\n,: unresolved reference to \`$symbol'\n,g;
print $symbols{$symbol}{"refs"};
$status = 1;
}
}
exit $status;