Debian Codegraph @

libstdc++-v3/scripts/extract_symvers.pl

raw
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2010-2025 Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU ISO C++ Library. This library is free
6 # software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this library; see the file COPYING3. If not see
18 # <http://www.gnu.org/licenses/>.
19
20 # Extract symbol version information on Solaris 2.
21 #
22 # Sun ld doesn't record symbol versions in .dynsym entries and they cannot
23 # easily be extracted from readelf --versions output, so use pvs instead.
24 # This way, we don't require GNU binutils in the native case. Also ensures
25 # that baseline_symbols.txt is identical between native (pvs, elfdump) and
26 # cross (readelf) cases.
27
28 my $lib = shift;
29
30 open PVS, "pvs -dsvo $lib |" or die $!;
31 while (<PVS>) {
32 chomp;
33
34 # Remove trailing semicolon.
35 s/;$//;
36
37 if (/\[WEAK\]/) {
38 # Allow for weak versions like
39 # libstdc++.so.6.0.34 - CXXABI_1.3.16 [WEAK]: {CXXABI_1.3.15};
40 #
41 # shared object, dash, version "[WEAK]", symbol, [size]
42 (undef, undef, $version, undef, $symbol, $size) = split;
43 } else {
44 # libstdc++.so.6.0.34 - CXXABI_1.3.16: {CXXABI_1.3.15};
45 #
46 # shared object, dash, version, symbol, [size]
47 (undef, undef, $version, $symbol, $size) = split;
48 }
49
50 # Remove colon separator from version field.
51 $version =~ s/:$//;
52
53 # Record base version. The [BASE] field was only added in Solaris 11,
54 # so simply use the first record instead.
55 if ($. == 1) {
56 $basever = $version;
57 next;
58 }
59
60 # Skip version declarations.
61 next unless defined ($symbol);
62
63 # Ignore version dependencies.
64 next if ($symbol =~ /\{.*\}/);
65
66 # Emit objects.
67 if (defined ($size)) {
68 # Strip parens from object size.
69 $size =~ s/\((\d+)\)/$1/;
70
71 $type{$symbol} = "OBJECT";
72 $version{$symbol} = $version;
73 $size{$symbol} = $size;
74 next;
75 }
76
77 if ($version eq $symbol or $version eq $basever) {
78 # Emit versions or symbols bound to base versions as objects.
79 $type{$symbol} = "OBJECT";
80 if ($version eq $basever) {
81 $version{$symbol} = $version;
82 } else {
83 $version{$symbol} = $symbol;
84 }
85 $size{$symbol} = 0;
86 } else {
87 # Everything else without a size field is a function.
88 $type{$symbol} = "FUNC";
89 $version{$symbol} = $version;
90 }
91 }
92 close PVS or die "pvs error";
93
94 # Only look at .dynsym table, like readelf in extract_symvers.
95 # Ignore error output to avoid getting confused by
96 # .gnu.version_r: zero sh_entsize information, expected 0x1
97 # warning with Solaris 11 elfdump on gld-produced shared objects.
98 open ELFDUMP, "/usr/ccs/bin/elfdump -s -N .dynsym $lib 2>/dev/null |" or die $!;
99 while (<ELFDUMP>) {
100 chomp;
101
102 # Ignore empty lines.
103 next if (/^$/);
104
105 # Ignore object name header.
106 next if (/:$/);
107
108 # Ignore table header lines.
109 next if (/^Symbol Table Section:/);
110 next if (/index.*value.*size/);
111
112 # Split table.
113 (undef, undef, undef, $type, $bind, $oth, undef, $shndx, $name) = split;
114
115 # Error out for unknown input.
116 die "unknown input line:\n$_" unless defined($bind);
117
118 # Ignore local symbols.
119 next if ($bind eq "LOCL");
120 # Ignore hidden symbols.
121 next if ($oth eq "H");
122 # Ignore undefined symbols.
123 next if ($shndx eq "UNDEF");
124 # Error out for unhandled cases. _GLOBAL_OFFSET_TABLE_ is P (protected).
125 die "unhandled symbol:\n$_" if ($bind !~ /^(GLOB|WEAK)/ or $oth !~ /[DP]/);
126
127 # Adapt to readelf type naming convention.
128 $type = "NOTYPE" if ($type eq "NOTY");
129 $type = "OBJECT" if ($type eq "OBJT");
130
131 # Use correct symbol type.
132 $type{$name} = $type if ($type{$name} ne $type);
133 }
134 close ELFDUMP or die "elfdump error";
135
136 foreach $symbol (keys %type) {
137 if ($type{$symbol} eq "FUNC" || $type{$symbol} eq "NOTYPE") {
138 push @lines, "$type{$symbol}:$symbol\@\@$version{$symbol}\n";
139 } elsif ($type{$symbol} eq "OBJECT" and $size{$symbol} == 0) {
140 # Omit symbols bound to base version; details can differ depending
141 # on the toolchain used.
142 next if $version{$symbol} eq $basever;
143
144 push @lines, "$type{$symbol}:$size{$symbol}:$version{$symbol}\n";
145 } else {
146 push @lines, "$type{$symbol}:$size{$symbol}:$symbol\@\@$version{$symbol}\n";
147 }
148 }
149 print sort @lines;