SymbolFile.pm 20 KB

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