SymbolFile.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2009-2010 Modestas Vainius <modax@debian.org>
  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 <https://www.gnu.org/licenses/>.
  16. package Dpkg::Shlibs::SymbolFile;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '0.01';
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Version;
  23. use Dpkg::Control::Fields;
  24. use Dpkg::Shlibs::Symbol;
  25. use Dpkg::Arch qw(get_host_arch);
  26. use parent qw(Dpkg::Interface::Storable);
  27. my %blacklist = (
  28. __bss_end__ => 1, # arm
  29. __bss_end => 1, # arm
  30. _bss_end__ => 1, # arm
  31. __bss_start => 1, # ALL
  32. __bss_start__ => 1, # arm
  33. __data_start => 1, # arm
  34. __do_global_ctors_aux => 1, # ia64
  35. __do_global_dtors_aux => 1, # ia64
  36. __do_jv_register_classes => 1, # ia64
  37. _DYNAMIC => 1, # ALL
  38. _edata => 1, # ALL
  39. _end => 1, # ALL
  40. __end__ => 1, # arm
  41. __exidx_end => 1, # armel
  42. __exidx_start => 1, # armel
  43. _fbss => 1, # mips, mipsel
  44. _fdata => 1, # mips, mipsel
  45. _fini => 1, # ALL
  46. _ftext => 1, # mips, mipsel
  47. _GLOBAL_OFFSET_TABLE_ => 1, # hppa, mips, mipsel
  48. __gmon_start__ => 1, # hppa
  49. __gnu_local_gp => 1, # mips, mipsel
  50. _gp => 1, # mips, mipsel
  51. _init => 1, # ALL
  52. _PROCEDURE_LINKAGE_TABLE_ => 1, # sparc, alpha
  53. _SDA2_BASE_ => 1, # powerpc
  54. _SDA_BASE_ => 1, # powerpc
  55. );
  56. for my $i (14 .. 31) {
  57. # Many powerpc specific symbols
  58. $blacklist{"_restfpr_$i"} = 1;
  59. $blacklist{"_restfpr_$i\_x"} = 1;
  60. $blacklist{"_restgpr_$i"} = 1;
  61. $blacklist{"_restgpr_$i\_x"} = 1;
  62. $blacklist{"_savefpr_$i"} = 1;
  63. $blacklist{"_savegpr_$i"} = 1;
  64. }
  65. sub symbol_is_blacklisted {
  66. my ($symbol, $include_groups) = @_;
  67. return 1 if exists $blacklist{$symbol};
  68. # The ARM Embedded ABI spec states symbols under this namespace as
  69. # possibly appearing in output objects.
  70. return 1 if not ${$include_groups}{aeabi} and $symbol =~ /^__aeabi_/;
  71. # The GNU implementation of the OpenMP spec, specifies symbols under
  72. # this namespace as possibly appearing in output objects.
  73. return 1 if not ${$include_groups}{gomp}
  74. and $symbol =~ /^\.gomp_critical_user_/;
  75. return 0;
  76. }
  77. sub new {
  78. my ($this, %opts) = @_;
  79. my $class = ref($this) || $this;
  80. my $self = \%opts;
  81. bless $self, $class;
  82. $self->{arch} //= get_host_arch();
  83. $self->clear();
  84. if (exists $self->{file}) {
  85. $self->load($self->{file}) if -e $self->{file};
  86. }
  87. return $self;
  88. }
  89. sub get_arch {
  90. my $self = shift;
  91. return $self->{arch};
  92. }
  93. sub clear {
  94. my $self = shift;
  95. $self->{objects} = {};
  96. }
  97. sub clear_except {
  98. my ($self, @ids) = @_;
  99. my %has;
  100. $has{$_} = 1 foreach (@ids);
  101. foreach my $objid (keys %{$self->{objects}}) {
  102. delete $self->{objects}{$objid} unless exists $has{$objid};
  103. }
  104. }
  105. sub get_sonames {
  106. my $self = shift;
  107. return keys %{$self->{objects}};
  108. }
  109. sub get_symbols {
  110. my ($self, $soname) = @_;
  111. if (defined $soname) {
  112. my $obj = $self->get_object($soname);
  113. return (defined $obj) ? values %{$obj->{syms}} : ();
  114. } else {
  115. my @syms;
  116. foreach my $soname ($self->get_sonames()) {
  117. push @syms, $self->get_symbols($soname);
  118. }
  119. return @syms;
  120. }
  121. }
  122. sub get_patterns {
  123. my ($self, $soname) = @_;
  124. my @patterns;
  125. if (defined $soname) {
  126. my $obj = $self->get_object($soname);
  127. foreach my $alias (values %{$obj->{patterns}{aliases}}) {
  128. push @patterns, values %$alias;
  129. }
  130. return (@patterns, @{$obj->{patterns}{generic}});
  131. } else {
  132. foreach my $soname ($self->get_sonames()) {
  133. push @patterns, $self->get_patterns($soname);
  134. }
  135. return @patterns;
  136. }
  137. }
  138. # Create a symbol from the supplied string specification.
  139. sub create_symbol {
  140. my ($self, $spec, %opts) = @_;
  141. my $symbol = (exists $opts{base}) ? $opts{base} :
  142. Dpkg::Shlibs::Symbol->new();
  143. my $ret = $opts{dummy} ? $symbol->parse_symbolspec($spec, default_minver => 0) :
  144. $symbol->parse_symbolspec($spec);
  145. if ($ret) {
  146. $symbol->initialize(arch => $self->get_arch());
  147. return $symbol;
  148. }
  149. return;
  150. }
  151. sub add_symbol {
  152. my ($self, $symbol, $soname) = @_;
  153. my $object = $self->get_object($soname);
  154. if ($symbol->is_pattern()) {
  155. if (my $alias_type = $symbol->get_alias_type()) {
  156. $object->{patterns}{aliases}{$alias_type} //= {};
  157. # Alias hash for matching.
  158. my $aliases = $object->{patterns}{aliases}{$alias_type};
  159. $aliases->{$symbol->get_symbolname()} = $symbol;
  160. } else {
  161. # Otherwise assume this is a generic sequential pattern. This
  162. # should be always safe.
  163. push @{$object->{patterns}{generic}}, $symbol;
  164. }
  165. return 'pattern';
  166. } else {
  167. # invalidate the minimum version cache
  168. $object->{minver_cache} = [];
  169. $object->{syms}{$symbol->get_symbolname()} = $symbol;
  170. return 'sym';
  171. }
  172. }
  173. sub _new_symbol {
  174. my $base = shift || 'Dpkg::Shlibs::Symbol';
  175. return (ref $base) ? $base->clone(@_) : $base->new(@_);
  176. }
  177. # Parameter seen is only used for recursive calls
  178. sub parse {
  179. my ($self, $fh, $file, $seen, $obj_ref, $base_symbol) = @_;
  180. if (defined($seen)) {
  181. return if exists $seen->{$file}; # Avoid include loops
  182. } else {
  183. $self->{file} = $file;
  184. $seen = {};
  185. }
  186. $seen->{$file} = 1;
  187. if (not ref($obj_ref)) { # Init ref to name of current object/lib
  188. $$obj_ref = undef;
  189. }
  190. while (<$fh>) {
  191. chomp;
  192. if (/^(?:\s+|#(?:DEPRECATED|MISSING): ([^#]+)#\s*)(.*)/) {
  193. if (not defined ($$obj_ref)) {
  194. error(g_('symbol information must be preceded by a header (file %s, line %s)'), $file, $.);
  195. }
  196. # Symbol specification
  197. my $deprecated = ($1) ? $1 : 0;
  198. my $sym = _new_symbol($base_symbol, deprecated => $deprecated);
  199. if ($self->create_symbol($2, base => $sym)) {
  200. $self->add_symbol($sym, $$obj_ref);
  201. } else {
  202. warning(g_('failed to parse line in %s: %s'), $file, $_);
  203. }
  204. } elsif (/^(\(.*\))?#include\s+"([^"]+)"/) {
  205. my $tagspec = $1;
  206. my $filename = $2;
  207. my $dir = $file;
  208. my $new_base_symbol;
  209. if (defined $tagspec) {
  210. $new_base_symbol = _new_symbol($base_symbol);
  211. $new_base_symbol->parse_tagspec($tagspec);
  212. }
  213. $dir =~ s{[^/]+$}{}; # Strip filename
  214. $self->load("$dir$filename", $seen, $obj_ref, $new_base_symbol);
  215. } elsif (/^#|^$/) {
  216. # Skip possible comments and empty lines
  217. } elsif (/^\|\s*(.*)$/) {
  218. # Alternative dependency template
  219. push @{$self->{objects}{$$obj_ref}{deps}}, "$1";
  220. } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
  221. # Add meta-fields
  222. $self->{objects}{$$obj_ref}{fields}{field_capitalize($1)} = $2;
  223. } elsif (/^(\S+)\s+(.*)$/) {
  224. # New object and dependency template
  225. $$obj_ref = $1;
  226. if (exists $self->{objects}{$$obj_ref}) {
  227. # Update/override infos only
  228. $self->{objects}{$$obj_ref}{deps} = [ "$2" ];
  229. } else {
  230. # Create a new object
  231. $self->create_object($$obj_ref, "$2");
  232. }
  233. } else {
  234. warning(g_('failed to parse a line in %s: %s'), $file, $_);
  235. }
  236. }
  237. delete $seen->{$file};
  238. }
  239. # Beware: we reuse the data structure of the provided symfile so make
  240. # sure to not modify them after having called this function
  241. sub merge_object_from_symfile {
  242. my ($self, $src, $objid) = @_;
  243. if (not $self->has_object($objid)) {
  244. $self->{objects}{$objid} = $src->get_object($objid);
  245. } else {
  246. warning(g_('tried to merge the same object (%s) twice in a symfile'), $objid);
  247. }
  248. }
  249. sub output {
  250. my ($self, $fh, %opts) = @_;
  251. $opts{template_mode} //= 0;
  252. $opts{with_deprecated} //= 1;
  253. $opts{with_pattern_matches} //= 0;
  254. my $res = '';
  255. foreach my $soname (sort $self->get_sonames()) {
  256. my @deps = $self->get_dependencies($soname);
  257. my $dep_first = shift @deps;
  258. if (exists $opts{package} and not $opts{template_mode}) {
  259. $dep_first =~ s/#PACKAGE#/$opts{package}/g;
  260. }
  261. print { $fh } "$soname $dep_first\n" if defined $fh;
  262. $res .= "$soname $dep_first\n" if defined wantarray;
  263. foreach my $dep_next (@deps) {
  264. if (exists $opts{package} and not $opts{template_mode}) {
  265. $dep_next =~ s/#PACKAGE#/$opts{package}/g;
  266. }
  267. print { $fh } "| $dep_next\n" if defined $fh;
  268. $res .= "| $dep_next\n" if defined wantarray;
  269. }
  270. my $f = $self->{objects}{$soname}{fields};
  271. foreach my $field (sort keys %{$f}) {
  272. my $value = $f->{$field};
  273. if (exists $opts{package} and not $opts{template_mode}) {
  274. $value =~ s/#PACKAGE#/$opts{package}/g;
  275. }
  276. print { $fh } "* $field: $value\n" if defined $fh;
  277. $res .= "* $field: $value\n" if defined wantarray;
  278. }
  279. my @symbols;
  280. if ($opts{template_mode}) {
  281. # Exclude symbols matching a pattern, but include patterns themselves
  282. @symbols = grep { not $_->get_pattern() } $self->get_symbols($soname);
  283. push @symbols, $self->get_patterns($soname);
  284. } else {
  285. @symbols = $self->get_symbols($soname);
  286. }
  287. foreach my $sym (sort { $a->get_symboltempl() cmp
  288. $b->get_symboltempl() } @symbols) {
  289. next if $sym->{deprecated} and not $opts{with_deprecated};
  290. # Do not dump symbols from foreign arch unless dumping a template.
  291. next if not $opts{template_mode} and
  292. not $sym->arch_is_concerned($self->get_arch());
  293. # Dump symbol specification. Dump symbol tags only in template mode.
  294. print { $fh } $sym->get_symbolspec($opts{template_mode}), "\n" if defined $fh;
  295. $res .= $sym->get_symbolspec($opts{template_mode}) . "\n" if defined wantarray;
  296. # Dump pattern matches as comments (if requested)
  297. if ($opts{with_pattern_matches} && $sym->is_pattern()) {
  298. for my $match (sort { $a->get_symboltempl() cmp
  299. $b->get_symboltempl() } $sym->get_pattern_matches())
  300. {
  301. print { $fh } '#MATCH:', $match->get_symbolspec(0), "\n" if defined $fh;
  302. $res .= '#MATCH:' . $match->get_symbolspec(0) . "\n" if defined wantarray;
  303. }
  304. }
  305. }
  306. }
  307. return $res;
  308. }
  309. # Tries to match a symbol name and/or version against the patterns defined.
  310. # Returns a pattern which matches (if any).
  311. sub find_matching_pattern {
  312. my ($self, $refsym, $sonames, $inc_deprecated) = @_;
  313. $inc_deprecated //= 0;
  314. my $name = (ref $refsym) ? $refsym->get_symbolname() : $refsym;
  315. my $pattern_ok = sub {
  316. my $p = shift;
  317. return defined $p && ($inc_deprecated || !$p->{deprecated}) &&
  318. $p->arch_is_concerned($self->get_arch());
  319. };
  320. foreach my $soname ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
  321. my $obj = $self->get_object($soname);
  322. my ($type, $pattern);
  323. next unless defined $obj;
  324. my $all_aliases = $obj->{patterns}{aliases};
  325. for my $type (Dpkg::Shlibs::Symbol::ALIAS_TYPES) {
  326. if (exists $all_aliases->{$type} && keys(%{$all_aliases->{$type}})) {
  327. my $aliases = $all_aliases->{$type};
  328. my $converter = $aliases->{(keys %$aliases)[0]};
  329. if (my $alias = $converter->convert_to_alias($name)) {
  330. if ($alias && exists $aliases->{$alias}) {
  331. $pattern = $aliases->{$alias};
  332. last if &$pattern_ok($pattern);
  333. $pattern = undef; # otherwise not found yet
  334. }
  335. }
  336. }
  337. }
  338. # Now try generic patterns and use the first that matches
  339. if (not defined $pattern) {
  340. for my $p (@{$obj->{patterns}{generic}}) {
  341. if (&$pattern_ok($p) && $p->matches_rawname($name)) {
  342. $pattern = $p;
  343. last;
  344. }
  345. }
  346. }
  347. if (defined $pattern) {
  348. return (wantarray) ?
  349. ( symbol => $pattern, soname => $soname ) : $pattern;
  350. }
  351. }
  352. return;
  353. }
  354. # merge_symbols($object, $minver)
  355. # Needs $Objdump->get_object($soname) as parameter
  356. # Don't merge blacklisted symbols related to the internal (arch-specific)
  357. # machinery
  358. sub merge_symbols {
  359. my ($self, $object, $minver) = @_;
  360. my $soname = $object->{SONAME};
  361. error(g_('cannot merge symbols from objects without SONAME'))
  362. unless $soname;
  363. my %include_groups = ();
  364. my $groups = $self->get_field($soname, 'Ignore-Blacklist-Groups');
  365. if (defined $groups) {
  366. $include_groups{$_} = 1 foreach (split /\s+/, $groups);
  367. }
  368. my %dynsyms;
  369. foreach my $sym ($object->get_exported_dynamic_symbols()) {
  370. my $name = $sym->{name} . '@' .
  371. ($sym->{version} ? $sym->{version} : 'Base');
  372. my $symobj = $self->lookup_symbol($name, $soname);
  373. if (symbol_is_blacklisted($sym->{name}, \%include_groups)) {
  374. next unless (defined $symobj and $symobj->has_tag('ignore-blacklist'));
  375. }
  376. $dynsyms{$name} = $sym;
  377. }
  378. unless ($self->has_object($soname)) {
  379. $self->create_object($soname, '');
  380. }
  381. # Scan all symbols provided by the objects
  382. my $obj = $self->get_object($soname);
  383. # invalidate the minimum version cache - it is not sufficient to
  384. # invalidate in add_symbol, since we might change a minimum
  385. # version for a particular symbol without adding it
  386. $obj->{minver_cache} = [];
  387. foreach my $name (keys %dynsyms) {
  388. my $sym;
  389. if ($sym = $self->lookup_symbol($name, $obj, 1)) {
  390. # If the symbol is already listed in the file
  391. $sym->mark_found_in_library($minver, $self->get_arch());
  392. } else {
  393. # The exact symbol is not present in the file, but it might match a
  394. # pattern.
  395. my $pattern = $self->find_matching_pattern($name, $obj, 1);
  396. if (defined $pattern) {
  397. $pattern->mark_found_in_library($minver, $self->get_arch());
  398. $sym = $pattern->create_pattern_match(symbol => $name);
  399. } else {
  400. # Symbol without any special info as no pattern matched
  401. $sym = Dpkg::Shlibs::Symbol->new(symbol => $name,
  402. minver => $minver);
  403. }
  404. $self->add_symbol($sym, $obj);
  405. }
  406. }
  407. # Process all symbols which could not be found in the library.
  408. foreach my $sym ($self->get_symbols($soname)) {
  409. if (not exists $dynsyms{$sym->get_symbolname()}) {
  410. $sym->mark_not_found_in_library($minver, $self->get_arch());
  411. }
  412. }
  413. # Deprecate patterns which didn't match anything
  414. for my $pattern (grep { $_->get_pattern_matches() == 0 }
  415. $self->get_patterns($soname)) {
  416. $pattern->mark_not_found_in_library($minver, $self->get_arch());
  417. }
  418. }
  419. sub is_empty {
  420. my $self = shift;
  421. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  422. }
  423. sub has_object {
  424. my ($self, $soname) = @_;
  425. return exists $self->{objects}{$soname};
  426. }
  427. sub get_object {
  428. my ($self, $soname) = @_;
  429. return ref($soname) ? $soname : $self->{objects}{$soname};
  430. }
  431. sub create_object {
  432. my ($self, $soname, @deps) = @_;
  433. $self->{objects}{$soname} = {
  434. syms => {},
  435. fields => {},
  436. patterns => {
  437. aliases => {},
  438. generic => [],
  439. },
  440. deps => [ @deps ],
  441. minver_cache => []
  442. };
  443. }
  444. sub get_dependency {
  445. my ($self, $soname, $dep_id) = @_;
  446. $dep_id //= 0;
  447. return $self->get_object($soname)->{deps}[$dep_id];
  448. }
  449. sub get_smallest_version {
  450. my ($self, $soname, $dep_id) = @_;
  451. $dep_id //= 0;
  452. my $so_object = $self->get_object($soname);
  453. return $so_object->{minver_cache}[$dep_id]
  454. if defined $so_object->{minver_cache}[$dep_id];
  455. my $minver;
  456. foreach my $sym ($self->get_symbols($so_object)) {
  457. next if $dep_id != $sym->{dep_id};
  458. $minver //= $sym->{minver};
  459. if (version_compare($minver, $sym->{minver}) > 0) {
  460. $minver = $sym->{minver};
  461. }
  462. }
  463. $so_object->{minver_cache}[$dep_id] = $minver;
  464. return $minver;
  465. }
  466. sub get_dependencies {
  467. my ($self, $soname) = @_;
  468. return @{$self->get_object($soname)->{deps}};
  469. }
  470. sub get_field {
  471. my ($self, $soname, $name) = @_;
  472. if (my $obj = $self->get_object($soname)) {
  473. if (exists $obj->{fields}{$name}) {
  474. return $obj->{fields}{$name};
  475. }
  476. }
  477. return;
  478. }
  479. # Tries to find a symbol like the $refsym and returns its descriptor.
  480. # $refsym may also be a symbol name.
  481. sub lookup_symbol {
  482. my ($self, $refsym, $sonames, $inc_deprecated) = @_;
  483. $inc_deprecated //= 0;
  484. my $name = (ref $refsym) ? $refsym->get_symbolname() : $refsym;
  485. foreach my $so ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
  486. if (my $obj = $self->get_object($so)) {
  487. my $sym = $obj->{syms}{$name};
  488. if ($sym and ($inc_deprecated or not $sym->{deprecated}))
  489. {
  490. return (wantarray) ?
  491. ( symbol => $sym, soname => $so ) : $sym;
  492. }
  493. }
  494. }
  495. return;
  496. }
  497. # Tries to find a pattern like the $refpat and returns its descriptor.
  498. # $refpat may also be a pattern spec.
  499. sub lookup_pattern {
  500. my ($self, $refpat, $sonames, $inc_deprecated) = @_;
  501. $inc_deprecated //= 0;
  502. # If $refsym is a string, we need to create a dummy ref symbol.
  503. $refpat = $self->create_symbol($refpat, dummy => 1) if ! ref($refpat);
  504. if ($refpat && $refpat->is_pattern()) {
  505. foreach my $soname ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
  506. if (my $obj = $self->get_object($soname)) {
  507. my $pat;
  508. if (my $type = $refpat->get_alias_type()) {
  509. if (exists $obj->{patterns}{aliases}{$type}) {
  510. $pat = $obj->{patterns}{aliases}{$type}{$refpat->get_symbolname()};
  511. }
  512. } elsif ($refpat->get_pattern_type() eq 'generic') {
  513. for my $p (@{$obj->{patterns}{generic}}) {
  514. if (($inc_deprecated || !$p->{deprecated}) &&
  515. $p->equals($refpat, versioning => 0))
  516. {
  517. $pat = $p;
  518. last;
  519. }
  520. }
  521. }
  522. if ($pat && ($inc_deprecated || !$pat->{deprecated})) {
  523. return (wantarray) ?
  524. (symbol => $pat, soname => $soname) : $pat;
  525. }
  526. }
  527. }
  528. }
  529. return;
  530. }
  531. # Get symbol object reference either by symbol name or by a reference object.
  532. sub get_symbol_object {
  533. my ($self, $refsym, $soname) = @_;
  534. my $sym = $self->lookup_symbol($refsym, $soname, 1);
  535. if (! defined $sym) {
  536. $sym = $self->lookup_pattern($refsym, $soname, 1);
  537. }
  538. return $sym;
  539. }
  540. sub get_new_symbols {
  541. my ($self, $ref, %opts) = @_;
  542. my $with_optional = (exists $opts{with_optional}) ?
  543. $opts{with_optional} : 0;
  544. my @res;
  545. foreach my $soname ($self->get_sonames()) {
  546. next if not $ref->has_object($soname);
  547. # Scan raw symbols first.
  548. foreach my $sym (grep { ($with_optional || ! $_->is_optional())
  549. && $_->is_legitimate($self->get_arch()) }
  550. $self->get_symbols($soname))
  551. {
  552. my $refsym = $ref->lookup_symbol($sym, $soname, 1);
  553. my $isnew;
  554. if (defined $refsym) {
  555. # If the symbol exists in the $ref symbol file, it might
  556. # still be new if $refsym is not legitimate.
  557. $isnew = not $refsym->is_legitimate($self->get_arch());
  558. } else {
  559. # If the symbol does not exist in the $ref symbol file, it does
  560. # not mean that it's new. It might still match a pattern in the
  561. # symbol file. However, due to performance reasons, first check
  562. # if the pattern that the symbol matches (if any) exists in the
  563. # ref symbol file as well.
  564. $isnew = not (
  565. ($sym->get_pattern() and $ref->lookup_pattern($sym->get_pattern(), $soname, 1)) or
  566. $ref->find_matching_pattern($sym, $soname, 1)
  567. );
  568. }
  569. push @res, { symbol => $sym, soname => $soname } if $isnew;
  570. }
  571. # Now scan patterns
  572. foreach my $p (grep { ($with_optional || ! $_->is_optional())
  573. && $_->is_legitimate($self->get_arch()) }
  574. $self->get_patterns($soname))
  575. {
  576. my $refpat = $ref->lookup_pattern($p, $soname, 0);
  577. # If reference pattern was not found or it is not legitimate,
  578. # considering current one as new.
  579. if (not defined $refpat or
  580. not $refpat->is_legitimate($self->get_arch()))
  581. {
  582. push @res, { symbol => $p , soname => $soname };
  583. }
  584. }
  585. }
  586. return @res;
  587. }
  588. sub get_lost_symbols {
  589. my ($self, $ref, %opts) = @_;
  590. return $ref->get_new_symbols($self, %opts);
  591. }
  592. sub get_new_libs {
  593. my ($self, $ref) = @_;
  594. my @res;
  595. foreach my $soname ($self->get_sonames()) {
  596. push @res, $soname if not $ref->get_object($soname);
  597. }
  598. return @res;
  599. }
  600. sub get_lost_libs {
  601. my ($self, $ref) = @_;
  602. return $ref->get_new_libs($self);
  603. }
  604. 1;