3 # Try opening libperl.a with nm, and verifying it has the kind of symbols
4 # we expected. Fail softly, expect things only on known platforms.
6 # Also, if the rarely-used builds options -DPERL_GLOBAL_STRUCT or
7 # -DPERL_GLOBAL_STRUCT_PRIVATE are used, verify that they did what
8 # they were meant to do, hide the global variables (see perlguts for
21 if ($Config{cc} =~ /g\+\+/) {
22 # XXX Could use c++filt, maybe.
28 for my $f (qw(../libperl.a libperl.a)) {
35 unless (defined $libperl_a) {
36 skip_all "no libperl.a";
39 print "# libperl = $libperl_a\n";
48 } elsif ($^O eq 'darwin') {
51 # With the -m option we get better information than the BSD-like
52 # default: with the default, a lot of symbols get dumped into 'S'
53 # or 's', for example one cannot tell the difference between const
54 # and non-const symbols.
58 unless (defined $nm) {
62 unless (defined $nm_style) {
63 skip_all "no nm style";
67 print "# nm_style = $nm_style\n";
68 print "# nm_opt = $nm_opt\n";
71 skip_all "no executable nm $nm";
74 if ($nm_style eq 'gnu') {
75 open(my $nm_fh, "$nm --version|") or
76 skip_all "nm failed: $!";
84 unless ($gnu_verified) {
85 plan skil_all => "no GNU nm";
89 open(my $nm_fh, "$nm $nm_opt $libperl_a |") or
90 skip_all "$nm $nm_opt $libperl_a failed: $!";
95 $symbols->{obj}{$1}++;
98 die "$0: undefined current object: $_" unless defined $symbols->{o};
99 if (/^[0-9a-f]{16} [Rr] (\w+)$/) {
100 $symbols->{data}{const}{$1}{$symbols->{o}}++;
101 } elsif (/^[0-9a-f]{16} r .+$/) {
103 } elsif (/^[0-9a-f]{16} [Tti] (\w+)(\..+)?$/) {
104 $symbols->{text}{$1}{$symbols->{o}}++;
105 } elsif (/^[0-9a-f]{16} C (\w+)$/) {
106 $symbols->{data}{common}{$1}{$symbols->{o}}++;
107 } elsif (/^[0-9a-f]{16} [BbSs] (\w+)(\.\d+)?$/) {
108 $symbols->{data}{bss}{$1}{$symbols->{o}}++;
109 } elsif (/^0{16} D _LIB_VERSION$/) {
110 # Skip the _LIB_VERSION (not ours).
111 } elsif (/^[0-9a-f]{16} [DdGg] (\w+)$/) {
112 $symbols->{data}{data}{$1}{$symbols->{o}}++;
113 } elsif (/^ {16} U (\w+)$/) {
114 # Skip the undefined.
115 } elsif (/^[0-9a-f]{16} . \.?(\w+)$/) {
116 # Skip the unknown types.
117 print "# Unknown type: $_ ($symbols->{o})\n";
119 print "# Unexpected nm output '$_' ($symbols->{o})\n";
124 sub nm_parse_darwin {
126 if (m{^(?:\.\./)?libperl\.a\((\w+\.o)\):$}) {
127 $symbols->{obj}{$1}++;
130 die "$0: undefined current object: $_" unless defined $symbols->{o};
131 if (/^[0-9a-f]{16} \(__TEXT,__(?:eh_frame|cstring)\) /) {
132 # Skip the eh_frame and cstring.
133 } elsif (/^[0-9a-f]{16} \(__TEXT,__(?:const|literal\d+)\) (?:non-)?external _?(\w+)(\.\w+)?$/) {
134 my ($symbol, $suffix) = ($1, $2);
135 # Ignore function-local constants like
136 # _Perl_av_extend_guts.oom_array_extend
137 return if defined $suffix && /__TEXT,__const/;
138 $symbols->{data}{const}{$symbol}{$symbols->{o}}++;
139 } elsif (/^[0-9a-f]{16} \(__TEXT,__text\) (?:non-)?external _(\w+)$/) {
140 $symbols->{text}{$1}{$symbols->{o}}++;
141 } elsif (/^[0-9a-f]{16} \(__DATA,__(const|data|bss|common)\) (?:non-)?external _(\w+)(\.\w+)?$/) {
142 my ($dtype, $symbol, $suffix) = ($1, $2, $3);
143 # Ignore function-local constants like
144 # _Perl_pp_gmtime.dayname
145 return if defined $suffix;
146 $symbols->{data}{$dtype}{$symbol}{$symbols->{o}}++;
147 } elsif (/^[0-9a-f]{16} \(__DATA,__const\) non-external _\.memset_pattern\d*$/) {
148 # Skip this, whatever it is (some inlined leakage from darwin libc?)
149 } elsif (/^ {16} \(undefined\) /) {
150 # Skip the undefined.
151 } elsif (/^[0-9a-f]{16} \(__\w+,__\w+\) /) {
152 # Skip the unknown types.
153 print "# Unknown type: $_ ($symbols->{o})\n";
155 print "# Unexpected nm output '$_' ($symbols->{o})\n";
162 if ($nm_style eq 'gnu') {
163 $nm_parse = \&nm_parse_gnu;
164 } elsif ($nm_style eq 'darwin') {
165 $nm_parse = \&nm_parse_darwin;
168 unless (defined $nm_parse) {
169 skip_all "no nm parser";
177 $nm_parse->(\%symbols);
180 # use Data::Dumper; print Dumper(\%symbols);
182 if (keys %symbols == 0) {
183 skip_all "no symbols\n";
186 # These should always be true for everyone.
188 ok($symbols{obj}{'pp.o'}, "has object pp.o");
189 ok($symbols{text}{'Perl_peep'}, "has text Perl_peep");
190 ok($symbols{text}{'Perl_pp_uc'}{'pp.o'}, "has text Perl_pp_uc in pp.o");
191 ok(exists $symbols{data}{const}, "has data const symbols");
192 ok($symbols{data}{const}{PL_no_mem}{'globals.o'}, "has PL_no_mem");
194 my $DEBUGGING = $Config{ccflags} =~ /-DDEBUGGING/ ? 1 : 0;
196 my $GS = $Config{ccflags} =~ /-DPERL_GLOBAL_STRUCT\b/ ? 1 : 0;
197 my $GSP = $Config{ccflags} =~ /-DPERL_GLOBAL_STRUCT_PRIVATE/ ? 1 : 0;
199 print "# GS = $GS\n";
200 print "# GSP = $GSP\n";
204 for my $dtype (sort keys %{$symbols{data}}) {
205 for my $symbol (sort keys %{$symbols{data}{$dtype}}) {
206 $data_symbols{$symbol}++;
210 # Since we are deprived of Test::More.
213 if (ref $a eq 'ARRAY' && ref $b eq 'ARRAY') {
215 for my $i (0..$#$a) {
216 unless ($a->[$i] eq $b->[$i]) {
217 printf("# LHS elem #%d '%s' ne RHS elem #%d '%s'\n",
224 printf("# LHS length %d, RHS length %d\n",
229 die "$0: Unexpcted: is_deeply $a $b\n";
233 # The following tests differ between vanilla vs $GSP or $GS.
236 # - "text" symbols are code
237 # - "data" symbols are data (duh), with subdivisions:
238 # - "bss": (Block-Started-by-Symbol: originally from IBM assembler...),
239 # uninitialized data, which often even doesn't exist in the object
240 # file as such, only its size does, which is then created on demand
242 # - "const": initialized read-only data, like string literals
243 # - "common": uninitialized data unless initialized...
244 # (the full story is too long for here, see "man nm")
245 # - "data": initialized read-write data
246 # (somewhat confusingly below: "data data", but it makes code simpler)
249 print "# -DPERL_GLOBAL_STRUCT_PRIVATE\n";
250 ok(!exists $data_symbols{PL_hash_seed}, "has no PL_hash_seed");
251 ok(!exists $data_symbols{PL_ppaddr}, "has no PL_ppaddr");
253 ok(! exists $symbols{data}{bss}, "has no data bss symbols");
254 ok(! exists $symbols{data}{data}, "has no data data symbols");
255 ok(! exists $symbols{data}{common}, "has no data common symbols");
257 # -DPERL_GLOBAL_STRUCT_PRIVATE should NOT have
258 # the extra text symbol for accessing the vars
259 # (as opposed to "just" -DPERL_GLOBAL_STRUCT)
260 ok(! exists $symbols{text}{Perl_GetVars}, "has no Perl_GetVars");
262 print "# -DPERL_GLOBAL_STRUCT\n";
263 ok(!exists $data_symbols{PL_hash_seed}, "has no PL_hash_seed");
264 ok(!exists $data_symbols{PL_ppaddr}, "has no PL_ppaddr");
266 ok(! exists $symbols{data}{bss}, "has no data bss symbols");
268 # These PerlIO data symbols are left visible with
269 # -DPERL_GLOBAL_STRUCT (as opposed to -DPERL_GLOBAL_STRUCT_PRIVATE)
283 # PL_magic_vtables is const with -DPERL_GLOBAL_STRUCT_PRIVATE but
284 # otherwise not const -- because of SWIG which wants to modify
285 # the table. Evil SWIG, eeevil.
287 # my_cxt_index is used with PERL_IMPLICIT_CONTEXT, which
288 # -DPERL_GLOBAL_STRUCT has turned on.
289 is_deeply([sort keys %{$symbols{data}{data}}],
294 "data data symbols");
296 # Only one data common symbol, our "supervariable".
297 is_deeply([sort keys %{$symbols{data}{common}}],
299 "data common symbols");
301 ok($symbols{data}{data}{PL_VarsPtr}{'globals.o'}, "has PL_VarsPtr");
302 ok($symbols{data}{common}{PL_Vars}{'globals.o'}, "has PL_Vars");
304 # -DPERL_GLOBAL_STRUCT has extra text symbol for accessing the vars.
305 ok($symbols{text}{Perl_GetVars}{'util.o'}, "has Perl_GetVars");
307 print "# neither -DPERL_GLOBAL_STRUCT nor -DPERL_GLOBAL_STRUCT_PRIVATE\n";
309 ok($symbols{data}{common}{PL_hash_seed}{'globals.o'}, "has PL_hash_seed");
310 ok($symbols{data}{data}{PL_ppaddr}{'globals.o'}, "has PL_ppaddr");
312 # None of the GLOBAL_STRUCT* business here.
313 ok(! exists $symbols{data}{data}{PL_VarsPtr}, "has no PL_VarsPtr");
314 ok(! exists $symbols{data}{common}{PL_Vars}, "has no PL_Vars");
315 ok(! exists $symbols{text}{Perl_GetVars}, "has no Perl_GetVars");