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::SymbolFile;
22 our $VERSION = '0.01';
25 use Dpkg::ErrorHandling;
27 use Dpkg::Control::Fields;
28 use Dpkg::Shlibs::Symbol;
29 use Dpkg::Arch qw(get_host_arch);
31 use parent qw(Dpkg::Interface::Storable);
34 __bss_end__ => 1, # arm
36 _bss_end__ => 1, # arm
37 __bss_start => 1, # ALL
38 __bss_start__ => 1, # arm
39 __data_start => 1, # arm
40 __do_global_ctors_aux => 1, # ia64
41 __do_global_dtors_aux => 1, # ia64
42 __do_jv_register_classes => 1, # ia64
47 __exidx_end => 1, # armel
48 __exidx_start => 1, # armel
49 _fbss => 1, # mips, mipsel
50 _fdata => 1, # mips, mipsel
52 _ftext => 1, # mips, mipsel
53 _GLOBAL_OFFSET_TABLE_ => 1, # hppa, mips, mipsel
54 __gmon_start__ => 1, # hppa
55 __gnu_local_gp => 1, # mips, mipsel
56 _gp => 1, # mips, mipsel
58 _PROCEDURE_LINKAGE_TABLE_ => 1, # sparc, alpha
59 _SDA2_BASE_ => 1, # powerpc
60 _SDA_BASE_ => 1, # powerpc
63 for my $i (14 .. 31) {
64 # Many powerpc specific symbols
65 $blacklist{"_restfpr_$i"} = 1;
66 $blacklist{"_restfpr_$i\_x"} = 1;
67 $blacklist{"_restgpr_$i"} = 1;
68 $blacklist{"_restgpr_$i\_x"} = 1;
69 $blacklist{"_savefpr_$i"} = 1;
70 $blacklist{"_savegpr_$i"} = 1;
73 sub symbol_is_blacklisted {
74 my ($symbol, $include_groups) = @_;
76 return 1 if exists $blacklist{$symbol};
78 # The ARM Embedded ABI spec states symbols under this namespace as
79 # possibly appearing in output objects.
80 return 1 if not ${$include_groups}{aeabi} and $symbol =~ /^__aeabi_/;
82 # The GNU implementation of the OpenMP spec, specifies symbols under
83 # this namespace as possibly appearing in output objects.
84 return 1 if not ${$include_groups}{gomp}
85 and $symbol =~ /^\.gomp_critical_user_/;
91 my ($this, %opts) = @_;
92 my $class = ref($this) || $this;
95 $self->{arch} //= get_host_arch();
97 if (exists $self->{file}) {
98 $self->load($self->{file}) if -e $self->{file};
105 return $self->{arch};
110 $self->{objects} = {};
114 my ($self, @ids) = @_;
116 my %has = map { $_ => 1 } @ids;
117 foreach my $objid (keys %{$self->{objects}}) {
118 delete $self->{objects}{$objid} unless exists $has{$objid};
124 return keys %{$self->{objects}};
128 my ($self, $soname) = @_;
129 if (defined $soname) {
130 my $obj = $self->get_object($soname);
131 return (defined $obj) ? values %{$obj->{syms}} : ();
134 foreach my $soname ($self->get_sonames()) {
135 push @syms, $self->get_symbols($soname);
142 my ($self, $soname) = @_;
144 if (defined $soname) {
145 my $obj = $self->get_object($soname);
146 foreach my $alias (values %{$obj->{patterns}{aliases}}) {
147 push @patterns, values %$alias;
149 return (@patterns, @{$obj->{patterns}{generic}});
151 foreach my $soname ($self->get_sonames()) {
152 push @patterns, $self->get_patterns($soname);
158 # Create a symbol from the supplied string specification.
160 my ($self, $spec, %opts) = @_;
161 my $symbol = (exists $opts{base}) ? $opts{base} :
162 Dpkg::Shlibs::Symbol->new();
164 my $ret = $opts{dummy} ? $symbol->parse_symbolspec($spec, default_minver => 0) :
165 $symbol->parse_symbolspec($spec);
167 $symbol->initialize(arch => $self->get_arch());
174 my ($self, $symbol, $soname) = @_;
175 my $object = $self->get_object($soname);
177 if ($symbol->is_pattern()) {
178 if (my $alias_type = $symbol->get_alias_type()) {
179 $object->{patterns}{aliases}{$alias_type} //= {};
180 # Alias hash for matching.
181 my $aliases = $object->{patterns}{aliases}{$alias_type};
182 $aliases->{$symbol->get_symbolname()} = $symbol;
184 # Otherwise assume this is a generic sequential pattern. This
185 # should be always safe.
186 push @{$object->{patterns}{generic}}, $symbol;
190 # invalidate the minimum version cache
191 $object->{minver_cache} = [];
192 $object->{syms}{$symbol->get_symbolname()} = $symbol;
198 my $base = shift || 'Dpkg::Shlibs::Symbol';
199 return (ref $base) ? $base->clone(@_) : $base->new(@_);
202 # Parameter seen is only used for recursive calls
204 my ($self, $fh, $file, $seen, $obj_ref, $base_symbol) = @_;
206 if (defined($seen)) {
207 return if exists $seen->{$file}; # Avoid include loops
209 $self->{file} = $file;
214 if (not ref($obj_ref)) { # Init ref to name of current object/lib
221 if (/^(?:\s+|#(?:DEPRECATED|MISSING): ([^#]+)#\s*)(.*)/) {
222 if (not defined ($$obj_ref)) {
223 error(g_('symbol information must be preceded by a header (file %s, line %s)'), $file, $.);
225 # Symbol specification
226 my $deprecated = ($1) ? $1 : 0;
227 my $sym = _new_symbol($base_symbol, deprecated => $deprecated);
228 if ($self->create_symbol($2, base => $sym)) {
229 $self->add_symbol($sym, $$obj_ref);
231 warning(g_('failed to parse line in %s: %s'), $file, $_);
233 } elsif (/^(\(.*\))?#include\s+"([^"]+)"/) {
238 if (defined $tagspec) {
239 $new_base_symbol = _new_symbol($base_symbol);
240 $new_base_symbol->parse_tagspec($tagspec);
242 $dir =~ s{[^/]+$}{}; # Strip filename
243 $self->load("$dir$filename", $seen, $obj_ref, $new_base_symbol);
245 # Skip possible comments and empty lines
246 } elsif (/^\|\s*(.*)$/) {
247 # Alternative dependency template
248 push @{$self->{objects}{$$obj_ref}{deps}}, "$1";
249 } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
251 $self->{objects}{$$obj_ref}{fields}{field_capitalize($1)} = $2;
252 } elsif (/^(\S+)\s+(.*)$/) {
253 # New object and dependency template
255 if (exists $self->{objects}{$$obj_ref}) {
256 # Update/override infos only
257 $self->{objects}{$$obj_ref}{deps} = [ "$2" ];
259 # Create a new object
260 $self->create_object($$obj_ref, "$2");
263 warning(g_('failed to parse a line in %s: %s'), $file, $_);
266 delete $seen->{$file};
269 # Beware: we reuse the data structure of the provided symfile so make
270 # sure to not modify them after having called this function
271 sub merge_object_from_symfile {
272 my ($self, $src, $objid) = @_;
273 if (not $self->has_object($objid)) {
274 $self->{objects}{$objid} = $src->get_object($objid);
276 warning(g_('tried to merge the same object (%s) twice in a symfile'), $objid);
281 my ($self, $fh, %opts) = @_;
282 $opts{template_mode} //= 0;
283 $opts{with_deprecated} //= 1;
284 $opts{with_pattern_matches} //= 0;
286 foreach my $soname (sort $self->get_sonames()) {
287 my @deps = $self->get_dependencies($soname);
288 my $dep_first = shift @deps;
289 if (exists $opts{package} and not $opts{template_mode}) {
290 $dep_first =~ s/#PACKAGE#/$opts{package}/g;
292 print { $fh } "$soname $dep_first\n" if defined $fh;
293 $res .= "$soname $dep_first\n" if defined wantarray;
295 foreach my $dep_next (@deps) {
296 if (exists $opts{package} and not $opts{template_mode}) {
297 $dep_next =~ s/#PACKAGE#/$opts{package}/g;
299 print { $fh } "| $dep_next\n" if defined $fh;
300 $res .= "| $dep_next\n" if defined wantarray;
302 my $f = $self->{objects}{$soname}{fields};
303 foreach my $field (sort keys %{$f}) {
304 my $value = $f->{$field};
305 if (exists $opts{package} and not $opts{template_mode}) {
306 $value =~ s/#PACKAGE#/$opts{package}/g;
308 print { $fh } "* $field: $value\n" if defined $fh;
309 $res .= "* $field: $value\n" if defined wantarray;
313 if ($opts{template_mode}) {
314 # Exclude symbols matching a pattern, but include patterns themselves
315 @symbols = grep { not $_->get_pattern() } $self->get_symbols($soname);
316 push @symbols, $self->get_patterns($soname);
318 @symbols = $self->get_symbols($soname);
320 foreach my $sym (sort { $a->get_symboltempl() cmp
321 $b->get_symboltempl() } @symbols) {
322 next if $sym->{deprecated} and not $opts{with_deprecated};
323 # Do not dump symbols from foreign arch unless dumping a template.
324 next if not $opts{template_mode} and
325 not $sym->arch_is_concerned($self->get_arch());
326 # Dump symbol specification. Dump symbol tags only in template mode.
327 print { $fh } $sym->get_symbolspec($opts{template_mode}), "\n" if defined $fh;
328 $res .= $sym->get_symbolspec($opts{template_mode}) . "\n" if defined wantarray;
329 # Dump pattern matches as comments (if requested)
330 if ($opts{with_pattern_matches} && $sym->is_pattern()) {
331 for my $match (sort { $a->get_symboltempl() cmp
332 $b->get_symboltempl() } $sym->get_pattern_matches())
334 print { $fh } '#MATCH:', $match->get_symbolspec(0), "\n" if defined $fh;
335 $res .= '#MATCH:' . $match->get_symbolspec(0) . "\n" if defined wantarray;
343 # Tries to match a symbol name and/or version against the patterns defined.
344 # Returns a pattern which matches (if any).
345 sub find_matching_pattern {
346 my ($self, $refsym, $sonames, $inc_deprecated) = @_;
347 $inc_deprecated //= 0;
348 my $name = (ref $refsym) ? $refsym->get_symbolname() : $refsym;
350 my $pattern_ok = sub {
352 return defined $p && ($inc_deprecated || !$p->{deprecated}) &&
353 $p->arch_is_concerned($self->get_arch());
356 foreach my $soname ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
357 my $obj = $self->get_object($soname);
358 my ($type, $pattern);
359 next unless defined $obj;
361 my $all_aliases = $obj->{patterns}{aliases};
362 for my $type (Dpkg::Shlibs::Symbol::ALIAS_TYPES) {
363 if (exists $all_aliases->{$type} && keys(%{$all_aliases->{$type}})) {
364 my $aliases = $all_aliases->{$type};
365 my $converter = $aliases->{(keys %$aliases)[0]};
366 if (my $alias = $converter->convert_to_alias($name)) {
367 if ($alias && exists $aliases->{$alias}) {
368 $pattern = $aliases->{$alias};
369 last if &$pattern_ok($pattern);
370 $pattern = undef; # otherwise not found yet
376 # Now try generic patterns and use the first that matches
377 if (not defined $pattern) {
378 for my $p (@{$obj->{patterns}{generic}}) {
379 if (&$pattern_ok($p) && $p->matches_rawname($name)) {
385 if (defined $pattern) {
387 ( symbol => $pattern, soname => $soname ) : $pattern;
393 # merge_symbols($object, $minver)
394 # Needs $Objdump->get_object($soname) as parameter
395 # Don't merge blacklisted symbols related to the internal (arch-specific)
398 my ($self, $object, $minver) = @_;
400 my $soname = $object->{SONAME};
401 error(g_('cannot merge symbols from objects without SONAME'))
404 my %include_groups = ();
405 my $groups = $self->get_field($soname, 'Ignore-Blacklist-Groups');
406 if (defined $groups) {
407 $include_groups{$_} = 1 foreach (split /\s+/, $groups);
411 foreach my $sym ($object->get_exported_dynamic_symbols()) {
412 my $name = $sym->{name} . '@' .
413 ($sym->{version} ? $sym->{version} : 'Base');
414 my $symobj = $self->lookup_symbol($name, $soname);
415 if (symbol_is_blacklisted($sym->{name}, \%include_groups)) {
416 next unless (defined $symobj and $symobj->has_tag('ignore-blacklist'));
418 $dynsyms{$name} = $sym;
421 unless ($self->has_object($soname)) {
422 $self->create_object($soname, '');
424 # Scan all symbols provided by the objects
425 my $obj = $self->get_object($soname);
426 # invalidate the minimum version cache - it is not sufficient to
427 # invalidate in add_symbol, since we might change a minimum
428 # version for a particular symbol without adding it
429 $obj->{minver_cache} = [];
430 foreach my $name (keys %dynsyms) {
432 if ($sym = $self->lookup_symbol($name, $obj, 1)) {
433 # If the symbol is already listed in the file
434 $sym->mark_found_in_library($minver, $self->get_arch());
436 # The exact symbol is not present in the file, but it might match a
438 my $pattern = $self->find_matching_pattern($name, $obj, 1);
439 if (defined $pattern) {
440 $pattern->mark_found_in_library($minver, $self->get_arch());
441 $sym = $pattern->create_pattern_match(symbol => $name);
443 # Symbol without any special info as no pattern matched
444 $sym = Dpkg::Shlibs::Symbol->new(symbol => $name,
447 $self->add_symbol($sym, $obj);
451 # Process all symbols which could not be found in the library.
452 foreach my $sym ($self->get_symbols($soname)) {
453 if (not exists $dynsyms{$sym->get_symbolname()}) {
454 $sym->mark_not_found_in_library($minver, $self->get_arch());
458 # Deprecate patterns which didn't match anything
459 for my $pattern (grep { $_->get_pattern_matches() == 0 }
460 $self->get_patterns($soname)) {
461 $pattern->mark_not_found_in_library($minver, $self->get_arch());
467 return scalar(keys %{$self->{objects}}) ? 0 : 1;
471 my ($self, $soname) = @_;
472 return exists $self->{objects}{$soname};
476 my ($self, $soname) = @_;
477 return ref($soname) ? $soname : $self->{objects}{$soname};
481 my ($self, $soname, @deps) = @_;
482 $self->{objects}{$soname} = {
495 my ($self, $soname, $dep_id) = @_;
497 return $self->get_object($soname)->{deps}[$dep_id];
500 sub get_smallest_version {
501 my ($self, $soname, $dep_id) = @_;
503 my $so_object = $self->get_object($soname);
504 return $so_object->{minver_cache}[$dep_id]
505 if defined $so_object->{minver_cache}[$dep_id];
507 foreach my $sym ($self->get_symbols($so_object)) {
508 next if $dep_id != $sym->{dep_id};
509 $minver //= $sym->{minver};
510 if (version_compare($minver, $sym->{minver}) > 0) {
511 $minver = $sym->{minver};
514 $so_object->{minver_cache}[$dep_id] = $minver;
518 sub get_dependencies {
519 my ($self, $soname) = @_;
520 return @{$self->get_object($soname)->{deps}};
524 my ($self, $soname, $name) = @_;
525 if (my $obj = $self->get_object($soname)) {
526 if (exists $obj->{fields}{$name}) {
527 return $obj->{fields}{$name};
533 # Tries to find a symbol like the $refsym and returns its descriptor.
534 # $refsym may also be a symbol name.
536 my ($self, $refsym, $sonames, $inc_deprecated) = @_;
537 $inc_deprecated //= 0;
538 my $name = (ref $refsym) ? $refsym->get_symbolname() : $refsym;
540 foreach my $so ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
541 if (my $obj = $self->get_object($so)) {
542 my $sym = $obj->{syms}{$name};
543 if ($sym and ($inc_deprecated or not $sym->{deprecated}))
546 ( symbol => $sym, soname => $so ) : $sym;
553 # Tries to find a pattern like the $refpat and returns its descriptor.
554 # $refpat may also be a pattern spec.
556 my ($self, $refpat, $sonames, $inc_deprecated) = @_;
557 $inc_deprecated //= 0;
558 # If $refsym is a string, we need to create a dummy ref symbol.
559 $refpat = $self->create_symbol($refpat, dummy => 1) if ! ref($refpat);
561 if ($refpat && $refpat->is_pattern()) {
562 foreach my $soname ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
563 if (my $obj = $self->get_object($soname)) {
565 if (my $type = $refpat->get_alias_type()) {
566 if (exists $obj->{patterns}{aliases}{$type}) {
567 $pat = $obj->{patterns}{aliases}{$type}{$refpat->get_symbolname()};
569 } elsif ($refpat->get_pattern_type() eq 'generic') {
570 for my $p (@{$obj->{patterns}{generic}}) {
571 if (($inc_deprecated || !$p->{deprecated}) &&
572 $p->equals($refpat, versioning => 0))
579 if ($pat && ($inc_deprecated || !$pat->{deprecated})) {
581 (symbol => $pat, soname => $soname) : $pat;
589 # Get symbol object reference either by symbol name or by a reference object.
590 sub get_symbol_object {
591 my ($self, $refsym, $soname) = @_;
592 my $sym = $self->lookup_symbol($refsym, $soname, 1);
593 if (! defined $sym) {
594 $sym = $self->lookup_pattern($refsym, $soname, 1);
599 sub get_new_symbols {
600 my ($self, $ref, %opts) = @_;
601 my $with_optional = (exists $opts{with_optional}) ?
602 $opts{with_optional} : 0;
604 foreach my $soname ($self->get_sonames()) {
605 next if not $ref->has_object($soname);
607 # Scan raw symbols first.
608 foreach my $sym (grep { ($with_optional || ! $_->is_optional())
609 && $_->is_legitimate($self->get_arch()) }
610 $self->get_symbols($soname))
612 my $refsym = $ref->lookup_symbol($sym, $soname, 1);
614 if (defined $refsym) {
615 # If the symbol exists in the $ref symbol file, it might
616 # still be new if $refsym is not legitimate.
617 $isnew = not $refsym->is_legitimate($self->get_arch());
619 # If the symbol does not exist in the $ref symbol file, it does
620 # not mean that it's new. It might still match a pattern in the
621 # symbol file. However, due to performance reasons, first check
622 # if the pattern that the symbol matches (if any) exists in the
623 # ref symbol file as well.
625 ($sym->get_pattern() and $ref->lookup_pattern($sym->get_pattern(), $soname, 1)) or
626 $ref->find_matching_pattern($sym, $soname, 1)
629 push @res, { symbol => $sym, soname => $soname } if $isnew;
633 foreach my $p (grep { ($with_optional || ! $_->is_optional())
634 && $_->is_legitimate($self->get_arch()) }
635 $self->get_patterns($soname))
637 my $refpat = $ref->lookup_pattern($p, $soname, 0);
638 # If reference pattern was not found or it is not legitimate,
639 # considering current one as new.
640 if (not defined $refpat or
641 not $refpat->is_legitimate($self->get_arch()))
643 push @res, { symbol => $p , soname => $soname };
650 sub get_lost_symbols {
651 my ($self, $ref, %opts) = @_;
652 return $ref->get_new_symbols($self, %opts);
657 my ($self, $ref) = @_;
659 foreach my $soname ($self->get_sonames()) {
660 push @res, $soname if not $ref->get_object($soname);
666 my ($self, $ref) = @_;
667 return $ref->get_new_libs($self);