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/>.
21 Dpkg::Shlibs::Symbol - represent an object file symbol
25 This module provides a class to handle symbols from an executable or
28 B<Note>: This is a private module, its API can change at any time.
32 package Dpkg
::Shlibs
::Symbol
0.01;
38 use List
::Util
qw(any);
41 use Dpkg
::ErrorHandling
;
42 use Dpkg
::Arch
qw(debarch_is_concerned debarch_to_abiattrs);
44 use Dpkg
::Shlibs
::Cppfilt
;
46 # Supported alias types in the order of matching preference
47 use constant ALIAS_TYPES
=> qw(
53 my ($this, %args) = @_;
54 my $class = ref($this) || $this;
57 symbol_templ
=> undef,
64 $self->{$_} = $args{$_} foreach keys %args;
70 my ($self, %args) = @_;
71 my $clone = Storable
::dclone
($self);
72 $clone->{$_} = $args{$_} foreach keys %args;
77 my ($self, $tagspec) = @_;
79 if ($tagspec =~ /^\s*\((.*?)\)(.*)$/ && $1) {
80 # (tag1=t1 value|tag2|...|tagN=tNp)
81 # Symbols ()|= cannot appear in the tag names and values
83 my $rest = ($2) ?
$2 : '';
84 my @tags = split(/\|/, $tagspec);
88 if ($tag =~ /^(.*)=(.*)$/) {
90 $self->add_tag($1, $2);
93 $self->add_tag($tag, undef);
101 sub parse_symbolspec
{
102 my ($self, $symbolspec, %opts) = @_;
108 if (defined($symbol = $self->parse_tagspec($symbolspec))) {
109 # (tag1=t1 value|tag2|...|tagN=tNp)"Foo::Bar::foobar()"@Base 1.0 1
110 # Symbols ()|= cannot appear in the tag names and values
112 # If the tag specification exists symbol name template might be quoted too
113 if ($symbol =~ /^(['"])/ && $symbol =~ /^($1)(.*?)$1(.*)$/) {
119 if ($symbol =~ m/^(\S+)(.*)$/) {
125 error
(g_
('symbol name unspecified: %s'), $symbolspec) if (!$symbol);
127 # No tag specification. Symbol name is up to the first space
128 # foobarsymbol@Base 1.0 1
129 if ($symbolspec =~ m/^(\S+)(.*)$/) {
136 $self->{symbol
} = $symbol;
137 $self->{symbol_templ
} = $symbol_templ;
138 $self->{symbol_quoted
} = $symbol_quoted if ($symbol_quoted);
140 # Now parse "the rest" (minver and dep_id)
141 if ($rest =~ /^\s(\S+)(?:\s(\d+))?/) {
142 $self->{minver
} = $1;
143 $self->{dep_id
} = $2 // 0;
144 } elsif (defined $opts{default_minver
}) {
145 $self->{minver
} = $opts{default_minver
};
153 # A hook for symbol initialization (typically processing of tags). The code
154 # here may even change symbol name. Called from
155 # Dpkg::Shlibs::SymbolFile::create_symbol().
159 # Look for tags marking symbol patterns. The pattern may match multiple
162 if ($self->has_tag('c++')) {
163 # Raw symbol name is always demangled to the same alias while demangled
164 # symbol name cannot be reliably converted back to raw symbol name.
165 # Therefore, we can use hash for mapping.
169 # Support old style wildcard syntax. That's basically a symver
170 # with an optional tag.
171 if ($self->get_symbolname() =~ /^\*@(.*)$/) {
172 $self->add_tag('symver') unless $self->has_tag('symver');
173 $self->add_tag('optional') unless $self->has_tag('optional');
174 $self->{symbol
} = $1;
177 if ($self->has_tag('symver')) {
178 # Each symbol is matched against its version rather than full
179 # name@version string.
180 $type = (defined $type) ?
'generic' : 'alias-symver';
181 if ($self->get_symbolname() =~ /@/) {
182 warning
(g_
('symver tag with versioned symbol will not match: %s'),
183 $self->get_symbolspec(1));
185 if ($self->get_symbolname() eq 'Base') {
186 error
(g_
("you can't use symver tag to catch unversioned symbols: %s"),
187 $self->get_symbolspec(1));
191 # As soon as regex is involved, we need to match each real
192 # symbol against each pattern (aka 'generic' pattern).
193 if ($self->has_tag('regex')) {
195 # Pre-compile regular expression for better performance.
196 my $regex = $self->get_symbolname();
197 $self->{pattern
}{regex
} = qr/$regex/;
200 $self->init_pattern($type);
207 return $self->{symbol
};
210 sub get_symboltempl
{
213 return $self->{symbol_templ
} || $self->{symbol
};
217 my ($self, $name, $templ, $quoted) = @_;
219 $name //= $self->{symbol
};
220 if (!defined $templ && $name =~ /\s/) {
223 if (!defined $quoted && defined $templ && $templ =~ /\s/) {
226 $self->{symbol
} = $name;
227 $self->{symbol_templ
} = $templ;
229 $self->{symbol_quoted
} = $quoted;
231 delete $self->{symbol_quoted
};
237 return scalar (@
{$self->{tagorder
}});
241 my ($self, $tagname, $tagval) = @_;
242 if (exists $self->{tags
}{$tagname}) {
243 $self->{tags
}{$tagname} = $tagval;
246 $self->{tags
}{$tagname} = $tagval;
247 push @
{$self->{tagorder
}}, $tagname;
253 my ($self, $tagname) = @_;
254 if (exists $self->{tags
}{$tagname}) {
255 delete $self->{tags
}{$tagname};
256 $self->{tagorder
} = [ grep { $_ ne $tagname } @
{$self->{tagorder
}} ];
263 my ($self, $tag) = @_;
264 return exists $self->{tags
}{$tag};
268 my ($self, $tag) = @_;
269 return $self->{tags
}{$tag};
272 # Checks if the symbol is equal to another one (by name and optionally,
273 # tag sets, versioning info (minver and depid))
275 my ($self, $other, %opts) = @_;
276 $opts{versioning
} //= 1;
279 return 0 if $self->{symbol
} ne $other->{symbol
};
281 if ($opts{versioning
}) {
282 return 0 if $self->{minver
} ne $other->{minver
};
283 return 0 if $self->{dep_id
} ne $other->{dep_id
};
287 return 0 if scalar(@
{$self->{tagorder
}}) != scalar(@
{$other->{tagorder
}});
289 for my $i (0 .. scalar(@
{$self->{tagorder
}}) - 1) {
290 my $tag = $self->{tagorder
}->[$i];
291 return 0 if $tag ne $other->{tagorder
}->[$i];
292 if (defined $self->{tags
}{$tag} && defined $other->{tags
}{$tag}) {
293 return 0 if $self->{tags
}{$tag} ne $other->{tags
}{$tag};
294 } elsif (defined $self->{tags
}{$tag} || defined $other->{tags
}{$tag}) {
306 return $self->has_tag('optional');
309 sub is_arch_specific
{
311 return $self->has_tag('arch');
314 sub arch_is_concerned
{
315 my ($self, $arch) = @_;
316 my $arches = $self->{tags
}{arch
};
318 return 0 if defined $arch && defined $arches &&
319 !debarch_is_concerned
($arch, split /[\s,]+/, $arches);
321 my ($bits, $endian) = debarch_to_abiattrs
($arch);
322 return 0 if defined $bits && defined $self->{tags
}{'arch-bits'} &&
323 $bits ne $self->{tags
}{'arch-bits'};
324 return 0 if defined $endian && defined $self->{tags
}{'arch-endian'} &&
325 $endian ne $self->{tags
}{'arch-endian'};
330 # Get reference to the pattern the symbol matches (if any)
334 return $self->{matching_pattern
};
337 ### NOTE: subroutines below require (or initialize) $self to be a pattern ###
339 # Initializes this symbol as a pattern of the specified type.
341 my ($self, $type) = @_;
343 $self->{pattern
}{type
} = $type;
344 # To be filled with references to symbols matching this pattern.
345 $self->{pattern
}{matches
} = [];
348 # Is this symbol a pattern or not?
352 return exists $self->{pattern
};
355 # Get pattern type if this symbol is a pattern.
356 sub get_pattern_type
{
359 return $self->{pattern
}{type
} // '';
362 # Get (sub)type of the alias pattern. Returns empty string if current
363 # pattern is not alias.
367 return ($self->get_pattern_type() =~ /^alias-(.+)/ && $1) || '';
370 # Get a list of symbols matching this pattern if this symbol is a pattern
371 sub get_pattern_matches
{
374 return @
{$self->{pattern
}{matches
}};
377 # Create a new symbol based on the pattern (i.e. $self)
378 # and add it to the pattern matches list.
379 sub create_pattern_match
{
381 return unless $self->is_pattern();
383 # Leave out 'pattern' subfield while deep-cloning
384 my $pattern_stuff = $self->{pattern
};
385 delete $self->{pattern
};
386 my $newsym = $self->clone(@_);
387 $self->{pattern
} = $pattern_stuff;
389 # Clean up symbol name related internal fields
390 $newsym->set_symbolname();
392 # Set newsym pattern reference, add to pattern matches list
393 $newsym->{matching_pattern
} = $self;
394 push @
{$self->{pattern
}{matches
}}, $newsym;
398 ### END of pattern subroutines ###
400 # Given a raw symbol name the call returns its alias according to the rules of
401 # the current pattern ($self). Returns undef if the supplied raw name is not
402 # transformable to alias.
403 sub convert_to_alias
{
404 my ($self, $rawname, $type) = @_;
405 $type = $self->get_alias_type() unless $type;
408 if ($type eq 'symver') {
409 # In case of symver, alias is symbol version. Extract it from the
411 return "$1" if ($rawname =~ /\@([^@]+)$/);
412 } elsif ($rawname =~ /^_Z/ && $type eq 'c++') {
413 return cppfilt_demangle_cpp
($rawname);
421 if ($self->has_tags()) {
423 for my $tagname (@
{$self->{tagorder
}}) {
424 my $tagval = $self->{tags
}{$tagname};
425 if (defined $tagval) {
426 push @tags, $tagname . '=' . $tagval;
428 push @tags, $tagname;
431 return '(' . join('|', @tags) . ')';
438 my $template_mode = shift;
440 $spec .= "#MISSING: $self->{deprecated}#" if $self->{deprecated
};
442 if ($template_mode) {
443 if ($self->has_tags()) {
444 $spec .= sprintf('%s%3$s%s%3$s', $self->get_tagspec(),
445 $self->get_symboltempl(), $self->{symbol_quoted
} // '');
447 $spec .= $self->get_symboltempl();
450 $spec .= $self->get_symbolname();
452 $spec .= " $self->{minver}";
453 $spec .= " $self->{dep_id}" if $self->{dep_id
};
457 # Sanitize the symbol when it is confirmed to be found in
458 # the respective library.
459 sub mark_found_in_library
{
460 my ($self, $minver, $arch) = @_;
462 if ($self->{deprecated
}) {
463 # Symbol reappeared somehow
464 $self->{deprecated
} = 0;
465 $self->{minver
} = $minver if (not $self->is_optional());
467 # We assume that the right dependency information is already
469 if (version_compare
($minver, $self->{minver
}) < 0) {
470 $self->{minver
} = $minver;
473 # Never remove arch tags from patterns
474 if (not $self->is_pattern()) {
475 if (not $self->arch_is_concerned($arch)) {
476 # Remove arch tags because they are incorrect.
477 $self->delete_tag('arch');
478 $self->delete_tag('arch-bits');
479 $self->delete_tag('arch-endian');
484 # Sanitize the symbol when it is confirmed to be NOT found in
485 # the respective library.
486 # Mark as deprecated those that are no more provided (only if the
487 # minver is later than the version where the symbol was introduced)
488 sub mark_not_found_in_library
{
489 my ($self, $minver, $arch) = @_;
491 # Ignore symbols from foreign arch
492 return if not $self->arch_is_concerned($arch);
494 if ($self->{deprecated
}) {
495 # Bump deprecated if the symbol is optional so that it
496 # keeps reappearing in the diff while it's missing
497 $self->{deprecated
} = $minver if $self->is_optional();
498 } elsif (version_compare
($minver, $self->{minver
}) > 0) {
499 $self->{deprecated
} = $minver;
503 # Checks if the symbol (or pattern) is legitimate as a real symbol for the
504 # specified architecture.
506 my ($self, $arch) = @_;
507 return ! $self->{deprecated
} &&
508 $self->arch_is_concerned($arch);
511 # Determine whether a supplied raw symbol name matches against current ($self)
513 sub matches_rawname
{
514 my ($self, $rawname) = @_;
515 my $target = $rawname;
519 if ($self->is_pattern()) {
520 # Process pattern tags in the order they were specified.
521 for my $tag (@
{$self->{tagorder
}}) {
522 if (any
{ $tag eq $_ } ALIAS_TYPES
) {
523 $ok = not not ($target = $self->convert_to_alias($target, $tag));
524 } elsif ($tag eq 'regex') {
525 # Symbol name is a regex. Match it against the target
527 $ok = ($target =~ $self->{pattern
}{regex
});
533 # Equality match by default
534 if ($ok && $do_eq_match) {
535 $ok = $target eq $self->get_symbolname();
544 This is a private module.