File Coverage

blib/lib/Dpkg/Shlibs/Symbol.pm
Criterion Covered Total %
statement 243 258 94.1
branch 99 128 77.3
condition 45 69 65.2
subroutine 41 43 95.3
pod 0 31 0.0
total 428 529 80.9


line stmt bran cond sub pod time code
1             # Copyright © 2007 Raphaël Hertzog
2             # Copyright © 2009-2010 Modestas Vainius
3             #
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.
8             #
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.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see .
16              
17             package Dpkg::Shlibs::Symbol;
18              
19 2     2   16 use strict;
  2         226  
  2         70  
20 2     2   12 use warnings;
  2         4  
  2         78  
21              
22             our $VERSION = '0.01';
23              
24 2     2   1582 use Storable ();
  2         7302  
  2         84  
25 2     2   16 use List::Util qw(any);
  2         154  
  2         198  
26              
27 2     2   16 use Dpkg::Gettext;
  2         4  
  2         108  
28 2     2   12 use Dpkg::ErrorHandling;
  2         4  
  2         138  
29 2     2   12 use Dpkg::Arch qw(debarch_is_concerned debarch_to_abiattrs);
  2         4  
  2         134  
30 2     2   14 use Dpkg::Version;
  2         6  
  2         136  
31 2     2   1514 use Dpkg::Shlibs::Cppfilt;
  2         4  
  2         116  
32              
33             # Supported alias types in the order of matching preference
34 2     2   14 use constant ALIAS_TYPES => qw(c++ symver);
  2         8  
  2         162  
35              
36             # Needed by the deprecated key, which is a correct use.
37 2         12 no if $Dpkg::Version::VERSION ge '1.02',
38 2     2   1588 warnings => qw(Dpkg::Version::semantic_change::overload::bool);
  2         28  
39              
40             sub new {
41 14271     14271 0 39589 my ($this, %args) = @_;
42 14271   33     42561 my $class = ref($this) || $this;
43 14271         73159 my $self = bless {
44             symbol => undef,
45             symbol_templ => undef,
46             minver => undef,
47             dep_id => 0,
48             deprecated => 0,
49             tags => {},
50             tagorder => [],
51             }, $class;
52 14271         53459 $self->{$_} = $args{$_} foreach keys %args;
53 14271         44223 return $self;
54             }
55              
56             # Deep clone
57             sub clone {
58 237     237 0 764 my ($self, %args) = @_;
59 237         13292 my $clone = Storable::dclone($self);
60 237         1289 $clone->{$_} = $args{$_} foreach keys %args;
61 237         750 return $clone;
62             }
63              
64             sub parse_tagspec {
65 5251     5251 0 9112 my ($self, $tagspec) = @_;
66              
67 5251 50 66     16171 if ($tagspec =~ /^\s*\((.*?)\)(.*)$/ && $1) {
68             # (tag1=t1 value|tag2|...|tagN=tNp)
69             # Symbols ()|= cannot appear in the tag names and values
70 233         539 my $tagspec = $1;
71 233 100       660 my $rest = ($2) ? $2 : '';
72 233         717 my @tags = split(/\|/, $tagspec);
73              
74             # Parse each tag
75 233         514 for my $tag (@tags) {
76 334 100       983 if ($tag =~ /^(.*)=(.*)$/) {
77             # Tag with value
78 174         398 $self->add_tag($1, $2);
79             } else {
80             # Tag without value
81 160         442 $self->add_tag($tag, undef);
82             }
83             }
84 233         1518 return $rest;
85             }
86 5018         12165 return;
87             }
88              
89             sub parse_symbolspec {
90 5245     5245 0 9775 my ($self, $symbolspec, %opts) = @_;
91 5245         15687 my $symbol;
92             my $symbol_templ;
93 5245         0 my $symbol_quoted;
94 5245         0 my $rest;
95              
96 5245 100       10463 if (defined($symbol = $self->parse_tagspec($symbolspec))) {
97             # (tag1=t1 value|tag2|...|tagN=tNp)"Foo::Bar::foobar()"@Base 1.0 1
98             # Symbols ()|= cannot appear in the tag names and values
99              
100             # If the tag specification exists symbol name template might be quoted too
101 227 100 66     1745 if ($symbol =~ /^(['"])/ && $symbol =~ /^($1)(.*?)$1(.*)$/) {
102 59         187 $symbol_quoted = $1;
103 59         136 $symbol_templ = $2;
104 59         115 $symbol = $2;
105 59         152 $rest = $3;
106             } else {
107 168 50       638 if ($symbol =~ m/^(\S+)(.*)$/) {
108 168         417 $symbol_templ = $1;
109 168         337 $symbol = $1;
110 168         318 $rest = $2;
111             }
112             }
113 227 50       505 error(g_('symbol name unspecified: %s'), $symbolspec) if (!$symbol);
114             } else {
115             # No tag specification. Symbol name is up to the first space
116             # foobarsymbol@Base 1.0 1
117 5018 50       19776 if ($symbolspec =~ m/^(\S+)(.*)$/) {
118 5018         11656 $symbol = $1;
119 5018         9288 $rest = $2;
120             } else {
121 0         0 return 0;
122             }
123             }
124 5245         9893 $self->{symbol} = $symbol;
125 5245         7876 $self->{symbol_templ} = $symbol_templ;
126 5245 100       10555 $self->{symbol_quoted} = $symbol_quoted if ($symbol_quoted);
127              
128             # Now parse "the rest" (minver and dep_id)
129 5245 100       17805 if ($rest =~ /^\s(\S+)(?:\s(\d+))?/) {
    50          
130 5244         11842 $self->{minver} = $1;
131 5244   100     18352 $self->{dep_id} = $2 // 0;
132             } elsif (defined $opts{default_minver}) {
133 1         3 $self->{minver} = $opts{default_minver};
134 1         3 $self->{dep_id} = 0;
135             } else {
136 0         0 return 0;
137             }
138 5245         12926 return 1;
139             }
140              
141             # A hook for symbol initialization (typically processing of tags). The code
142             # here may even change symbol name. Called from
143             # Dpkg::Shlibs::SymbolFile::create_symbol().
144             sub initialize {
145 5245     5245 0 8074 my $self = shift;
146              
147             # Look for tags marking symbol patterns. The pattern may match multiple
148             # real symbols.
149 5245         7610 my $type;
150 5245 100       11018 if ($self->has_tag('c++')) {
151             # Raw symbol name is always demangled to the same alias while demangled
152             # symbol name cannot be reliably converted back to raw symbol name.
153             # Therefore, we can use hash for mapping.
154 71         156 $type = 'alias-c++';
155             }
156              
157             # Support old style wildcard syntax. That's basically a symver
158             # with an optional tag.
159 5245 100       11607 if ($self->get_symbolname() =~ /^\*@(.*)$/) {
160 3 50       16 $self->add_tag('symver') unless $self->has_tag('symver');
161 3 50       13 $self->add_tag('optional') unless $self->has_tag('optional');
162 3         10 $self->{symbol} = $1;
163             }
164              
165 5245 100       10083 if ($self->has_tag('symver')) {
166             # Each symbol is matched against its version rather than full
167             # name@version string.
168 27 100       102 $type = (defined $type) ? 'generic' : 'alias-symver';
169 27 50       70 if ($self->get_symbolname() eq 'Base') {
170 0         0 error(g_("you can't use symver tag to catch unversioned symbols: %s"),
171             $self->get_symbolspec(1));
172             }
173             }
174              
175             # As soon as regex is involved, we need to match each real
176             # symbol against each pattern (aka 'generic' pattern).
177 5245 100       10139 if ($self->has_tag('regex')) {
178 25         42 $type = 'generic';
179             # Pre-compile regular expression for better performance.
180 25         60 my $regex = $self->get_symbolname();
181 25         793 $self->{pattern}{regex} = qr/$regex/;
182             }
183 5245 100       14080 if (defined $type) {
184 86         230 $self->init_pattern($type);
185             }
186             }
187              
188             sub get_symbolname {
189 61691     61691 0 95308 my $self = shift;
190              
191 61691         181610 return $self->{symbol};
192             }
193              
194             sub get_symboltempl {
195 271478     271478 0 363881 my $self = shift;
196              
197 271478   66     703219 return $self->{symbol_templ} || $self->{symbol};
198             }
199              
200             sub set_symbolname {
201 195     195 0 464 my ($self, $name, $templ, $quoted) = @_;
202              
203 195   33     1047 $name //= $self->{symbol};
204 195 50 33     973 if (!defined $templ && $name =~ /\s/) {
205 0         0 $templ = $name;
206             }
207 195 50 33     736 if (!defined $quoted && defined $templ && $templ =~ /\s/) {
      33        
208 0         0 $quoted = '"';
209             }
210 195         364 $self->{symbol} = $name;
211 195         319 $self->{symbol_templ} = $templ;
212 195 50       429 if ($quoted) {
213 0         0 $self->{symbol_quoted} = $quoted;
214             } else {
215 195         426 delete $self->{symbol_quoted};
216             }
217             }
218              
219             sub has_tags {
220 162     162 0 223 my $self = shift;
221 162         229 return scalar (@{$self->{tagorder}});
  162         392  
222             }
223              
224             sub add_tag {
225 340     340 0 1102 my ($self, $tagname, $tagval) = @_;
226 340 100       824 if (exists $self->{tags}{$tagname}) {
227 6         14 $self->{tags}{$tagname} = $tagval;
228 6         16 return 0;
229             } else {
230 334         972 $self->{tags}{$tagname} = $tagval;
231 334         509 push @{$self->{tagorder}}, $tagname;
  334         844  
232             }
233 334         778 return 1;
234             }
235              
236             sub delete_tag {
237 6     6 0 38 my ($self, $tagname) = @_;
238 6 100       56 if (exists $self->{tags}{$tagname}) {
239 2         10 delete $self->{tags}{$tagname};
240 2         22 $self->{tagorder} = [ grep { $_ ne $tagname } @{$self->{tagorder}} ];
  2         26  
  2         20  
241 2         8 return 1;
242             }
243 4         22 return 0;
244             }
245              
246             sub has_tag {
247 25306     25306 0 43316 my ($self, $tag) = @_;
248 25306         81583 return exists $self->{tags}{$tag};
249             }
250              
251             sub get_tag_value {
252 0     0 0 0 my ($self, $tag) = @_;
253 0         0 return $self->{tags}{$tag};
254             }
255              
256             # Checks if the symbol is equal to another one (by name and optionally,
257             # tag sets, versioning info (minver and depid))
258             sub equals {
259 116     116 0 289 my ($self, $other, %opts) = @_;
260 116   100     256 $opts{versioning} //= 1;
261 116   50     449 $opts{tags} //= 1;
262              
263 116 100       444 return 0 if $self->{symbol} ne $other->{symbol};
264              
265 48 100       123 if ($opts{versioning}) {
266 1 50       6 return 0 if $self->{minver} ne $other->{minver};
267 1 50       6 return 0 if $self->{dep_id} ne $other->{dep_id};
268             }
269              
270 48 50       115 if ($opts{tags}) {
271 48 50       79 return 0 if scalar(@{$self->{tagorder}}) != scalar(@{$other->{tagorder}});
  48         95  
  48         116  
272              
273 48         88 for my $i (0 .. scalar(@{$self->{tagorder}}) - 1) {
  48         131  
274 99         214 my $tag = $self->{tagorder}->[$i];
275 99 50       235 return 0 if $tag ne $other->{tagorder}->[$i];
276 99 50 33     529 if (defined $self->{tags}{$tag} && defined $other->{tags}{$tag}) {
    50 33        
277 0 0       0 return 0 if $self->{tags}{$tag} ne $other->{tags}{$tag};
278             } elsif (defined $self->{tags}{$tag} || defined $other->{tags}{$tag}) {
279 0         0 return 0;
280             }
281             }
282             }
283              
284 48         191 return 1;
285             }
286              
287              
288             sub is_optional {
289 9541     9541 0 14950 my $self = shift;
290 9541         16063 return $self->has_tag('optional');
291             }
292              
293             sub is_arch_specific {
294 0     0 0 0 my $self = shift;
295 0         0 return $self->has_tag('arch');
296             }
297              
298             sub arch_is_concerned {
299 41425     41425 0 76668 my ($self, $arch) = @_;
300 41425         67232 my $arches = $self->{tags}{arch};
301              
302 41425 100 66     130957 return 0 if defined $arch && defined $arches &&
      100        
303             !debarch_is_concerned($arch, split /[\s,]+/, $arches);
304              
305 41323         94657 my ($bits, $endian) = debarch_to_abiattrs($arch);
306             return 0 if defined $bits && defined $self->{tags}{'arch-bits'} &&
307 41323 100 66     158050 $bits ne $self->{tags}{'arch-bits'};
      100        
308             return 0 if defined $endian && defined $self->{tags}{'arch-endian'} &&
309 41281 100 66     135155 $endian ne $self->{tags}{'arch-endian'};
      100        
310              
311 41243         145507 return 1;
312             }
313              
314             # Get reference to the pattern the symbol matches (if any)
315             sub get_pattern {
316 626     626 0 2304 my $self = shift;
317              
318 626         2378 return $self->{matching_pattern};
319             }
320              
321             ### NOTE: subroutines below require (or initialize) $self to be a pattern ###
322              
323             # Initializes this symbol as a pattern of the specified type.
324             sub init_pattern {
325 86     86 0 179 my ($self, $type) = @_;
326              
327 86         287 $self->{pattern}{type} = $type;
328             # To be filled with references to symbols matching this pattern.
329 86         308 $self->{pattern}{matches} = [];
330             }
331              
332             # Is this symbol a pattern or not?
333             sub is_pattern {
334 24010     24010 0 40587 my $self = shift;
335              
336 24010         67045 return exists $self->{pattern};
337             }
338              
339             # Get pattern type if this symbol is a pattern.
340             sub get_pattern_type {
341 760     760 0 1097 my $self = shift;
342              
343 760   50     8310 return $self->{pattern}{type} // '';
344             }
345              
346             # Get (sub)type of the alias pattern. Returns empty string if current
347             # pattern is not alias.
348             sub get_alias_type {
349 713     713 0 1240 my $self = shift;
350              
351 713   100     1562 return ($self->get_pattern_type() =~ /^alias-(.+)/ && $1) || '';
352             }
353              
354             # Get a list of symbols matching this pattern if this symbol is a pattern
355             sub get_pattern_matches {
356 53     53 0 1076 my $self = shift;
357              
358 53         89 return @{$self->{pattern}{matches}};
  53         1894  
359             }
360              
361             # Create a new symbol based on the pattern (i.e. $self)
362             # and add it to the pattern matches list.
363             sub create_pattern_match {
364 195     195 0 304 my $self = shift;
365 195 50       416 return unless $self->is_pattern();
366              
367             # Leave out 'pattern' subfield while deep-cloning
368 195         380 my $pattern_stuff = $self->{pattern};
369 195         459 delete $self->{pattern};
370 195         574 my $newsym = $self->clone(@_);
371 195         405 $self->{pattern} = $pattern_stuff;
372              
373             # Clean up symbol name related internal fields
374 195         607 $newsym->set_symbolname();
375              
376             # Set newsym pattern reference, add to pattern matches list
377 195         611 $newsym->{matching_pattern} = $self;
378 195         338 push @{$self->{pattern}{matches}}, $newsym;
  195         634  
379 195         682 return $newsym;
380             }
381              
382             ### END of pattern subroutines ###
383              
384             # Given a raw symbol name the call returns its alias according to the rules of
385             # the current pattern ($self). Returns undef if the supplied raw name is not
386             # transformable to alias.
387             sub convert_to_alias {
388 896     896 0 1868 my ($self, $rawname, $type) = @_;
389 896 100       2306 $type = $self->get_alias_type() unless $type;
390              
391 896 50       2037 if ($type) {
392 896 100 33     3752 if ($type eq 'symver') {
    50          
393             # In case of symver, alias is symbol version. Extract it from the
394             # rawname.
395 445 50       3073 return "$1" if ($rawname =~ /\@([^@]+)$/);
396             } elsif ($rawname =~ /^_Z/ && $type eq 'c++') {
397 451         1446 return cppfilt_demangle_cpp($rawname);
398             }
399             }
400 0         0 return;
401             }
402              
403             sub get_tagspec {
404 40     40 0 60 my $self = shift;
405 40 50       79 if ($self->has_tags()) {
406 40         71 my @tags;
407 40         60 for my $tagname (@{$self->{tagorder}}) {
  40         106  
408 56         127 my $tagval = $self->{tags}{$tagname};
409 56 100       105 if (defined $tagval) {
410 28         74 push @tags, $tagname . '=' . $tagval;
411             } else {
412 28         75 push @tags, $tagname;
413             }
414             }
415 40         167 return '(' . join('|', @tags) . ')';
416             }
417 0         0 return '';
418             }
419              
420             sub get_symbolspec {
421 13900     13900 0 24406 my $self = shift;
422 13900         18344 my $template_mode = shift;
423 13900         20702 my $spec = '';
424 13900 100       27184 $spec .= "#MISSING: $self->{deprecated}#" if $self->{deprecated};
425 13900         25683 $spec .= ' ';
426 13900 100       23050 if ($template_mode) {
427 122 100       241 if ($self->has_tags()) {
428             $spec .= sprintf('%s%3$s%s%3$s', $self->get_tagspec(),
429 40   100     90 $self->get_symboltempl(), $self->{symbol_quoted} // '');
430             } else {
431 82         160 $spec .= $self->get_symboltempl();
432             }
433             } else {
434 13778         25936 $spec .= $self->get_symbolname();
435             }
436 13900         30301 $spec .= " $self->{minver}";
437 13900 100       27060 $spec .= " $self->{dep_id}" if $self->{dep_id};
438 13900         35885 return $spec;
439             }
440              
441             # Sanitize the symbol when it is confirmed to be found in
442             # the respective library.
443             sub mark_found_in_library {
444 9027     9027 0 18751 my ($self, $minver, $arch) = @_;
445              
446 9027 100       25176 if ($self->{deprecated}) {
447             # Symbol reappeared somehow
448 3         31 $self->{deprecated} = 0;
449 3 100       33 $self->{minver} = $minver if (not $self->is_optional());
450             } else {
451             # We assume that the right dependency information is already
452             # there.
453 9024 50       23498 if (version_compare($minver, $self->{minver}) < 0) {
454 0         0 $self->{minver} = $minver;
455             }
456             }
457             # Never remove arch tags from patterns
458 9027 100       22229 if (not $self->is_pattern()) {
459 8832 100       18505 if (not $self->arch_is_concerned($arch)) {
460             # Remove arch tags because they are incorrect.
461 2         38 $self->delete_tag('arch');
462 2         38 $self->delete_tag('arch-bits');
463 2         20 $self->delete_tag('arch-endian');
464             }
465             }
466             }
467              
468             # Sanitize the symbol when it is confirmed to be NOT found in
469             # the respective library.
470             # Mark as deprecated those that are no more provided (only if the
471             # minver is later than the version where the symbol was introduced)
472             sub mark_not_found_in_library {
473 170     170 0 374 my ($self, $minver, $arch) = @_;
474              
475             # Ignore symbols from foreign arch
476 170 100       337 return if not $self->arch_is_concerned($arch);
477              
478 114 100       413 if ($self->{deprecated}) {
    50          
479             # Bump deprecated if the symbol is optional so that it
480             # keeps reappearing in the diff while it's missing
481 2 50       34 $self->{deprecated} = $minver if $self->is_optional();
482             } elsif (version_compare($minver, $self->{minver}) > 0) {
483 112         527 $self->{deprecated} = $minver;
484             }
485             }
486              
487             # Checks if the symbol (or pattern) is legitimate as a real symbol for the
488             # specified architecture.
489             sub is_legitimate {
490 18318     18318 0 32692 my ($self, $arch) = @_;
491             return ! $self->{deprecated} &&
492 18318   100     53682 $self->arch_is_concerned($arch);
493             }
494              
495             # Determine whether a supplied raw symbol name matches against current ($self)
496             # symbol or pattern.
497             sub matches_rawname {
498 267     267 0 540 my ($self, $rawname) = @_;
499 267         433 my $target = $rawname;
500 267         443 my $ok = 1;
501 267         408 my $do_eq_match = 1;
502              
503 267 50       564 if ($self->is_pattern()) {
504             # Process pattern tags in the order they were specified.
505 267         483 for my $tag (@{$self->{tagorder}}) {
  267         651  
506 573 100   879   2527 if (any { $tag eq $_ } ALIAS_TYPES) {
  879 100       2043  
507 351         743 $ok = not not ($target = $self->convert_to_alias($target, $tag));
508             } elsif ($tag eq 'regex') {
509             # Symbol name is a regex. Match it against the target
510 183         298 $do_eq_match = 0;
511 183         1532 $ok = ($target =~ $self->{pattern}{regex});
512             }
513 573 100       2143 last if not $ok;
514             }
515             }
516              
517             # Equality match by default
518 267 100 100     939 if ($ok && $do_eq_match) {
519 84         197 $ok = $target eq $self->get_symbolname();
520             }
521 267         1326 return $ok;
522             }
523              
524             1;