1 # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2009-2010 Modestas Vainius <modax@debian.org>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 package Dpkg::Shlibs::Symbol;
22 our $VERSION = '0.01';
27 use Dpkg::ErrorHandling;
28 use Dpkg::Util qw(:list);
29 use Dpkg::Arch qw(debarch_is_concerned debarch_to_cpuattrs);
31 use Dpkg::Shlibs::Cppfilt;
33 # Supported alias types in the order of matching preference
34 use constant ALIAS_TYPES => qw(c++ symver);
37 my ($this, %args) = @_;
38 my $class = ref($this) || $this;
41 symbol_templ => undef,
48 $self->{$_} = $args{$_} foreach keys %args;
54 my ($self, %args) = @_;
55 my $clone = Storable::dclone($self);
56 $clone->{$_} = $args{$_} foreach keys %args;
61 my ($self, $tagspec) = @_;
63 if ($tagspec =~ /^\s*\((.*?)\)(.*)$/ && $1) {
64 # (tag1=t1 value|tag2|...|tagN=tNp)
65 # Symbols ()|= cannot appear in the tag names and values
67 my $rest = ($2) ? $2 : '';
68 my @tags = split(/\|/, $tagspec);
72 if ($tag =~ /^(.*)=(.*)$/) {
74 $self->add_tag($1, $2);
77 $self->add_tag($tag, undef);
85 sub parse_symbolspec {
86 my ($self, $symbolspec, %opts) = @_;
92 if (defined($symbol = $self->parse_tagspec($symbolspec))) {
93 # (tag1=t1 value|tag2|...|tagN=tNp)"Foo::Bar::foobar()"@Base 1.0 1
94 # Symbols ()|= cannot appear in the tag names and values
96 # If the tag specification exists symbol name template might be quoted too
97 if ($symbol =~ /^(['"])/ && $symbol =~ /^($1)(.*?)$1(.*)$/) {
103 if ($symbol =~ m/^(\S+)(.*)$/) {
109 error(g_('symbol name unspecified: %s'), $symbolspec) if (!$symbol);
111 # No tag specification. Symbol name is up to the first space
112 # foobarsymbol@Base 1.0 1
113 if ($symbolspec =~ m/^(\S+)(.*)$/) {
120 $self->{symbol} = $symbol;
121 $self->{symbol_templ} = $symbol_templ;
122 $self->{symbol_quoted} = $symbol_quoted if ($symbol_quoted);
124 # Now parse "the rest" (minver and dep_id)
125 if ($rest =~ /^\s(\S+)(?:\s(\d+))?/) {
126 $self->{minver} = $1;
127 $self->{dep_id} = $2 // 0;
128 } elsif (defined $opts{default_minver}) {
129 $self->{minver} = $opts{default_minver};
137 # A hook for symbol initialization (typically processing of tags). The code
138 # here may even change symbol name. Called from
139 # Dpkg::Shlibs::SymbolFile::create_symbol().
143 # Look for tags marking symbol patterns. The pattern may match multiple
146 if ($self->has_tag('c++')) {
147 # Raw symbol name is always demangled to the same alias while demangled
148 # symbol name cannot be reliably converted back to raw symbol name.
149 # Therefore, we can use hash for mapping.
153 # Support old style wildcard syntax. That's basically a symver
154 # with an optional tag.
155 if ($self->get_symbolname() =~ /^\*@(.*)$/) {
156 $self->add_tag('symver') unless $self->has_tag('symver');
157 $self->add_tag('optional') unless $self->has_tag('optional');
158 $self->{symbol} = $1;
161 if ($self->has_tag('symver')) {
162 # Each symbol is matched against its version rather than full
163 # name@version string.
164 $type = (defined $type) ? 'generic' : 'alias-symver';
165 if ($self->get_symbolname() eq 'Base') {
166 error(g_("you can't use symver tag to catch unversioned symbols: %s"),
167 $self->get_symbolspec(1));
171 # As soon as regex is involved, we need to match each real
172 # symbol against each pattern (aka 'generic' pattern).
173 if ($self->has_tag('regex')) {
175 # Pre-compile regular expression for better performance.
176 my $regex = $self->get_symbolname();
177 $self->{pattern}{regex} = qr/$regex/;
180 $self->init_pattern($type);
187 return $self->{symbol};
190 sub get_symboltempl {
193 return $self->{symbol_templ} || $self->{symbol};
197 my ($self, $name, $templ, $quoted) = @_;
199 $name //= $self->{symbol};
200 if (!defined $templ && $name =~ /\s/) {
203 if (!defined $quoted && defined $templ && $templ =~ /\s/) {
206 $self->{symbol} = $name;
207 $self->{symbol_templ} = $templ;
209 $self->{symbol_quoted} = $quoted;
211 delete $self->{symbol_quoted};
217 return scalar (@{$self->{tagorder}});
221 my ($self, $tagname, $tagval) = @_;
222 if (exists $self->{tags}{$tagname}) {
223 $self->{tags}{$tagname} = $tagval;
226 $self->{tags}{$tagname} = $tagval;
227 push @{$self->{tagorder}}, $tagname;
233 my ($self, $tagname) = @_;
234 if (exists $self->{tags}{$tagname}) {
235 delete $self->{tags}{$tagname};
236 $self->{tagorder} = [ grep { $_ ne $tagname } @{$self->{tagorder}} ];
243 my ($self, $tag) = @_;
244 return exists $self->{tags}{$tag};
248 my ($self, $tag) = @_;
249 return $self->{tags}{$tag};
252 # Checks if the symbol is equal to another one (by name and optionally,
253 # tag sets, versioning info (minver and depid))
255 my ($self, $other, %opts) = @_;
256 $opts{versioning} //= 1;
259 return 0 if $self->{symbol} ne $other->{symbol};
261 if ($opts{versioning}) {
262 return 0 if $self->{minver} ne $other->{minver};
263 return 0 if $self->{dep_id} ne $other->{dep_id};
267 return 0 if scalar(@{$self->{tagorder}}) != scalar(@{$other->{tagorder}});
269 for my $i (0 .. scalar(@{$self->{tagorder}}) - 1) {
270 my $tag = $self->{tagorder}->[$i];
271 return 0 if $tag ne $other->{tagorder}->[$i];
272 if (defined $self->{tags}{$tag} && defined $other->{tags}{$tag}) {
273 return 0 if $self->{tags}{$tag} ne $other->{tags}{$tag};
274 } elsif (defined $self->{tags}{$tag} || defined $other->{tags}{$tag}) {
286 return $self->has_tag('optional');
289 sub is_arch_specific {
291 return $self->has_tag('arch');
294 sub arch_is_concerned {
295 my ($self, $arch) = @_;
296 my $arches = $self->{tags}{arch};
298 return 0 if defined $arch && defined $arches &&
299 !debarch_is_concerned($arch, split /[\s,]+/, $arches);
301 my ($bits, $endian) = debarch_to_cpuattrs($arch);
302 return 0 if defined $bits && defined $self->{tags}{'arch-bits'} &&
303 $bits ne $self->{tags}{'arch-bits'};
304 return 0 if defined $endian && defined $self->{tags}{'arch-endian'} &&
305 $endian ne $self->{tags}{'arch-endian'};
310 # Get reference to the pattern the symbol matches (if any)
314 return $self->{matching_pattern};
317 ### NOTE: subroutines below require (or initialize) $self to be a pattern ###
319 # Initializes this symbol as a pattern of the specified type.
321 my ($self, $type) = @_;
323 $self->{pattern}{type} = $type;
324 # To be filled with references to symbols matching this pattern.
325 $self->{pattern}{matches} = [];
328 # Is this symbol a pattern or not?
332 return exists $self->{pattern};
335 # Get pattern type if this symbol is a pattern.
336 sub get_pattern_type {
339 return $self->{pattern}{type} // '';
342 # Get (sub)type of the alias pattern. Returns empty string if current
343 # pattern is not alias.
347 return ($self->get_pattern_type() =~ /^alias-(.+)/ && $1) || '';
350 # Get a list of symbols matching this pattern if this symbol is a pattern
351 sub get_pattern_matches {
354 return @{$self->{pattern}{matches}};
357 # Create a new symbol based on the pattern (i.e. $self)
358 # and add it to the pattern matches list.
359 sub create_pattern_match {
361 return unless $self->is_pattern();
363 # Leave out 'pattern' subfield while deep-cloning
364 my $pattern_stuff = $self->{pattern};
365 delete $self->{pattern};
366 my $newsym = $self->clone(@_);
367 $self->{pattern} = $pattern_stuff;
369 # Clean up symbol name related internal fields
370 $newsym->set_symbolname();
372 # Set newsym pattern reference, add to pattern matches list
373 $newsym->{matching_pattern} = $self;
374 push @{$self->{pattern}{matches}}, $newsym;
378 ### END of pattern subroutines ###
380 # Given a raw symbol name the call returns its alias according to the rules of
381 # the current pattern ($self). Returns undef if the supplied raw name is not
382 # transformable to alias.
383 sub convert_to_alias {
384 my ($self, $rawname, $type) = @_;
385 $type = $self->get_alias_type() unless $type;
388 if ($type eq 'symver') {
389 # In case of symver, alias is symbol version. Extract it from the
391 return "$1" if ($rawname =~ /\@([^@]+)$/);
392 } elsif ($rawname =~ /^_Z/ && $type eq 'c++') {
393 return cppfilt_demangle_cpp($rawname);
401 if ($self->has_tags()) {
403 for my $tagname (@{$self->{tagorder}}) {
404 my $tagval = $self->{tags}{$tagname};
405 if (defined $tagval) {
406 push @tags, $tagname . '=' . $tagval;
408 push @tags, $tagname;
411 return '(' . join('|', @tags) . ')';
418 my $template_mode = shift;
420 $spec .= "#MISSING: $self->{deprecated}#" if $self->{deprecated};
422 if ($template_mode) {
423 if ($self->has_tags()) {
424 $spec .= sprintf('%s%3$s%s%3$s', $self->get_tagspec(),
425 $self->get_symboltempl(), $self->{symbol_quoted} // '');
427 $spec .= $self->get_symboltempl();
430 $spec .= $self->get_symbolname();
432 $spec .= " $self->{minver}";
433 $spec .= " $self->{dep_id}" if $self->{dep_id};
437 # Sanitize the symbol when it is confirmed to be found in
438 # the respective library.
439 sub mark_found_in_library {
440 my ($self, $minver, $arch) = @_;
442 if ($self->{deprecated}) {
443 # Symbol reappeared somehow
444 $self->{deprecated} = 0;
445 $self->{minver} = $minver if (not $self->is_optional());
447 # We assume that the right dependency information is already
449 if (version_compare($minver, $self->{minver}) < 0) {
450 $self->{minver} = $minver;
453 # Never remove arch tags from patterns
454 if (not $self->is_pattern()) {
455 if (not $self->arch_is_concerned($arch)) {
456 # Remove arch tags because they are incorrect.
457 $self->delete_tag('arch');
458 $self->delete_tag('arch-bits');
459 $self->delete_tag('arch-endian');
464 # Sanitize the symbol when it is confirmed to be NOT found in
465 # the respective library.
466 # Mark as deprecated those that are no more provided (only if the
467 # minver is later than the version where the symbol was introduced)
468 sub mark_not_found_in_library {
469 my ($self, $minver, $arch) = @_;
471 # Ignore symbols from foreign arch
472 return if not $self->arch_is_concerned($arch);
474 if ($self->{deprecated}) {
475 # Bump deprecated if the symbol is optional so that it
476 # keeps reappearing in the diff while it's missing
477 $self->{deprecated} = $minver if $self->is_optional();
478 } elsif (version_compare($minver, $self->{minver}) > 0) {
479 $self->{deprecated} = $minver;
483 # Checks if the symbol (or pattern) is legitimate as a real symbol for the
484 # specified architecture.
486 my ($self, $arch) = @_;
487 return ! $self->{deprecated} &&
488 $self->arch_is_concerned($arch);
491 # Determine whether a supplied raw symbol name matches against current ($self)
493 sub matches_rawname {
494 my ($self, $rawname) = @_;
495 my $target = $rawname;
499 if ($self->is_pattern()) {
500 # Process pattern tags in the order they were specified.
501 for my $tag (@{$self->{tagorder}}) {
502 if (any { $tag eq $_ } ALIAS_TYPES) {
503 $ok = not not ($target = $self->convert_to_alias($target, $tag));
504 } elsif ($tag eq 'regex') {
505 # Symbol name is a regex. Match it against the target
507 $ok = ($target =~ $self->{pattern}{regex});
513 # Equality match by default
514 if ($ok && $do_eq_match) {
515 $ok = $target eq $self->get_symbolname();