SymbolFile.pm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 <http://www.gnu.org/licenses/>.
  16. package Dpkg::Shlibs::SymbolFile;
  17. use strict;
  18. use warnings;
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Version;
  22. use Dpkg::Control::Fields;
  23. use Dpkg::Shlibs::Symbol;
  24. use Dpkg::Arch qw(get_host_arch);
  25. my %blacklist = (
  26. '__bss_end__' => 1, # arm
  27. '__bss_end' => 1, # arm
  28. '_bss_end__' => 1, # arm
  29. '__bss_start' => 1, # ALL
  30. '__bss_start__' => 1, # arm
  31. '__data_start' => 1, # arm
  32. '__do_global_ctors_aux' => 1, # ia64
  33. '__do_global_dtors_aux' => 1, # ia64
  34. '__do_jv_register_classes' => 1,# ia64
  35. '_DYNAMIC' => 1, # ALL
  36. '_edata' => 1, # ALL
  37. '_end' => 1, # ALL
  38. '__end__' => 1, # arm
  39. '__exidx_end' => 1, # armel
  40. '__exidx_start' => 1, # armel
  41. '_fbss' => 1, # mips, mipsel
  42. '_fdata' => 1, # mips, mipsel
  43. '_fini' => 1, # ALL
  44. '_ftext' => 1, # mips, mipsel
  45. '_GLOBAL_OFFSET_TABLE_' => 1, # hppa, mips, mipsel
  46. '__gmon_start__' => 1, # hppa
  47. '__gnu_local_gp' => 1, # mips, mipsel
  48. '_gp' => 1, # mips, mipsel
  49. '_init' => 1, # ALL
  50. '_PROCEDURE_LINKAGE_TABLE_' => 1, # sparc, alpha
  51. '_SDA2_BASE_' => 1, # powerpc
  52. '_SDA_BASE_' => 1, # powerpc
  53. );
  54. for (my $i = 14; $i <= 31; $i++) {
  55. # Many powerpc specific symbols
  56. $blacklist{"_restfpr_$i"} = 1;
  57. $blacklist{"_restfpr_$i\_x"} = 1;
  58. $blacklist{"_restgpr_$i"} = 1;
  59. $blacklist{"_restgpr_$i\_x"} = 1;
  60. $blacklist{"_savefpr_$i"} = 1;
  61. $blacklist{"_savegpr_$i"} = 1;
  62. }
  63. # Many armel-specific symbols
  64. $blacklist{"__aeabi_$_"} = 1 foreach (qw(cdcmpeq cdcmple cdrcmple cfcmpeq
  65. cfcmple cfrcmple d2f d2iz d2lz d2uiz d2ulz dadd dcmpeq dcmpge dcmpgt
  66. dcmple dcmplt dcmpun ddiv dmul dneg drsub dsub f2d f2iz f2lz f2uiz f2ulz
  67. fadd fcmpeq fcmpge fcmpgt fcmple fcmplt fcmpun fdiv fmul fneg frsub fsub
  68. i2d i2f idiv idivmod l2d l2f lasr lcmp ldivmod llsl llsr lmul ui2d ui2f
  69. uidiv uidivmod ul2d ul2f ulcmp uldivmod unwind_cpp_pr0 unwind_cpp_pr1
  70. unwind_cpp_pr2 uread4 uread8 uwrite4 uwrite8));
  71. sub new {
  72. my $this = shift;
  73. my %opts=@_;
  74. my $class = ref($this) || $this;
  75. my $self = \%opts;
  76. bless $self, $class;
  77. $self->{arch} = get_host_arch() unless defined $self->{arch};
  78. $self->clear();
  79. if (exists $self->{file}) {
  80. $self->load($self->{file}) if -e $self->{file};
  81. }
  82. return $self;
  83. }
  84. sub get_arch {
  85. my ($self) = @_;
  86. return $self->{arch};
  87. }
  88. sub clear {
  89. my ($self) = @_;
  90. $self->{objects} = {};
  91. }
  92. sub clear_except {
  93. my ($self, @ids) = @_;
  94. my %has;
  95. $has{$_} = 1 foreach (@ids);
  96. foreach my $objid (keys %{$self->{objects}}) {
  97. delete $self->{objects}{$objid} unless exists $has{$objid};
  98. }
  99. }
  100. sub get_sonames {
  101. my ($self) = @_;
  102. return keys %{$self->{objects}};
  103. }
  104. sub get_symbols {
  105. my ($self, $soname) = @_;
  106. if (defined $soname) {
  107. my $obj = $self->get_object($soname);
  108. return (defined $obj) ? values %{$obj->{syms}} : ();
  109. } else {
  110. my @syms;
  111. foreach my $soname ($self->get_sonames()) {
  112. push @syms, $self->get_symbols($soname);
  113. }
  114. return @syms;
  115. }
  116. }
  117. sub get_patterns {
  118. my ($self, $soname) = @_;
  119. my @patterns;
  120. if (defined $soname) {
  121. my $obj = $self->get_object($soname);
  122. foreach my $alias (values %{$obj->{patterns}{aliases}}) {
  123. push @patterns, values %$alias;
  124. }
  125. return (@patterns, @{$obj->{patterns}{generic}});
  126. } else {
  127. foreach my $soname ($self->get_sonames()) {
  128. push @patterns, $self->get_patterns($soname);
  129. }
  130. return @patterns;
  131. }
  132. }
  133. # Create a symbol from the supplied string specification.
  134. sub create_symbol {
  135. my ($self, $spec, %opts) = @_;
  136. my $symbol = (exists $opts{base}) ? $opts{base} :
  137. Dpkg::Shlibs::Symbol->new();
  138. my $ret = ($opts{dummy}) ? $symbol->parse($spec, default_minver => 0) :
  139. $symbol->parse($spec);
  140. if ($ret) {
  141. $symbol->initialize(arch => $self->get_arch());
  142. return $symbol;
  143. }
  144. return undef;
  145. }
  146. sub add_symbol {
  147. my ($self, $symbol, $soname) = @_;
  148. my $object = $self->get_object($soname);
  149. if ($symbol->is_pattern()) {
  150. if (my $alias_type = $symbol->get_alias_type()) {
  151. unless (exists $object->{patterns}{aliases}{$alias_type}) {
  152. $object->{patterns}{aliases}{$alias_type} = {};
  153. }
  154. # Alias hash for matching.
  155. my $aliases = $object->{patterns}{aliases}{$alias_type};
  156. $aliases->{$symbol->get_symbolname()} = $symbol;
  157. } else {
  158. # Otherwise assume this is a generic sequential pattern. This
  159. # should be always safe.
  160. push @{$object->{patterns}{generic}}, $symbol;
  161. }
  162. return 'pattern';
  163. } else {
  164. # invalidate the minimum version cache
  165. $object->{minver_cache} = [];
  166. $object->{syms}{$symbol->get_symbolname()} = $symbol;
  167. return 'sym';
  168. }
  169. }
  170. # Parameter seen is only used for recursive calls
  171. sub load {
  172. my ($self, $file, $seen, $obj_ref, $base_symbol) = @_;
  173. sub new_symbol {
  174. my $base = shift || 'Dpkg::Shlibs::Symbol';
  175. return (ref $base) ? $base->clone(@_) : $base->new(@_);
  176. }
  177. if (defined($seen)) {
  178. return if exists $seen->{$file}; # Avoid include loops
  179. } else {
  180. $self->{file} = $file;
  181. $seen = {};
  182. }
  183. $seen->{$file} = 1;
  184. if (not ref($obj_ref)) { # Init ref to name of current object/lib
  185. $$obj_ref = undef;
  186. }
  187. open(my $sym_file, "<", $file)
  188. || syserr(_g("cannot open %s"), $file);
  189. while (defined($_ = <$sym_file>)) {
  190. chomp($_);
  191. if (/^(?:\s+|#(?:DEPRECATED|MISSING): ([^#]+)#\s*)(.*)/) {
  192. if (not defined ($$obj_ref)) {
  193. error(_g("Symbol information must be preceded by a header (file %s, line %s)."), $file, $.);
  194. }
  195. # Symbol specification
  196. my $deprecated = ($1) ? $1 : 0;
  197. my $sym = new_symbol($base_symbol, deprecated => $deprecated);
  198. if ($self->create_symbol($2, base => $sym)) {
  199. $self->add_symbol($sym, $$obj_ref);
  200. } else {
  201. warning(_g("Failed to parse line in %s: %s"), $file, $_);
  202. }
  203. } elsif (/^(\(.*\))?#include\s+"([^"]+)"/) {
  204. my $tagspec = $1;
  205. my $filename = $2;
  206. my $dir = $file;
  207. my $new_base_symbol;
  208. if (defined $tagspec) {
  209. $new_base_symbol = new_symbol($base_symbol);
  210. $new_base_symbol->parse_tagspec($tagspec);
  211. }
  212. $dir =~ s{[^/]+$}{}; # Strip filename
  213. $self->load("$dir$filename", $seen, $obj_ref, $new_base_symbol);
  214. } elsif (/^#/) {
  215. # Skip possible comments
  216. } elsif (/^\|\s*(.*)$/) {
  217. # Alternative dependency template
  218. push @{$self->{objects}{$$obj_ref}{deps}}, "$1";
  219. } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
  220. # Add meta-fields
  221. $self->{objects}{$$obj_ref}{fields}{field_capitalize($1)} = $2;
  222. } elsif (/^(\S+)\s+(.*)$/) {
  223. # New object and dependency template
  224. $$obj_ref = $1;
  225. if (exists $self->{objects}{$$obj_ref}) {
  226. # Update/override infos only
  227. $self->{objects}{$$obj_ref}{deps} = [ "$2" ];
  228. } else {
  229. # Create a new object
  230. $self->create_object($$obj_ref, "$2");
  231. }
  232. } else {
  233. warning(_g("Failed to parse a line in %s: %s"), $file, $_);
  234. }
  235. }
  236. close($sym_file);
  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 save {
  250. my ($self, $file, %opts) = @_;
  251. $file = $self->{file} unless defined($file);
  252. my $fh;
  253. if ($file eq "-") {
  254. $fh = \*STDOUT;
  255. } else {
  256. open($fh, ">", $file)
  257. || syserr(_g("cannot write %s"), $file);
  258. }
  259. $self->dump($fh, %opts);
  260. close($fh) if ($file ne "-");
  261. }
  262. sub dump {
  263. my ($self, $fh, %opts) = @_;
  264. $opts{template_mode} = 0 unless exists $opts{template_mode};
  265. $opts{with_deprecated} = 1 unless exists $opts{with_deprecated};
  266. $opts{with_pattern_matches} = 0 unless exists $opts{with_pattern_matches};
  267. foreach my $soname (sort $self->get_sonames()) {
  268. my @deps = $self->get_dependencies($soname);
  269. my $dep = shift @deps;
  270. $dep =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  271. print $fh "$soname $dep\n";
  272. foreach $dep (@deps) {
  273. $dep =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  274. print $fh "| $dep\n";
  275. }
  276. my $f = $self->{objects}{$soname}{fields};
  277. foreach my $field (sort keys %{$f}) {
  278. my $value = $f->{$field};
  279. $value =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  280. print $fh "* $field: $value\n";
  281. }
  282. my @symbols;
  283. if ($opts{template_mode}) {
  284. # Exclude symbols matching a pattern, but include patterns themselves
  285. @symbols = grep { not $_->get_pattern() } $self->get_symbols($soname);
  286. push @symbols, $self->get_patterns($soname);
  287. } else {
  288. @symbols = $self->get_symbols($soname);
  289. }
  290. foreach my $sym (sort { $a->get_symboltempl() cmp
  291. $b->get_symboltempl() } @symbols) {
  292. next if $sym->{deprecated} and not $opts{with_deprecated};
  293. # Do not dump symbols from foreign arch unless dumping a template.
  294. next if not $opts{template_mode} and
  295. not $sym->arch_is_concerned($self->get_arch());
  296. # Dump symbol specification. Dump symbol tags only in template mode.
  297. print $fh $sym->get_symbolspec($opts{template_mode}), "\n";
  298. # Dump pattern matches as comments (if requested)
  299. if ($opts{with_pattern_matches} && $sym->is_pattern()) {
  300. for my $match (sort { $a->get_symboltempl() cmp
  301. $b->get_symboltempl() } $sym->get_pattern_matches())
  302. {
  303. print $fh "#MATCH:", $match->get_symbolspec(0), "\n";
  304. }
  305. }
  306. }
  307. }
  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 unless defined $inc_deprecated;
  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 (wantarray) ? () : undef;
  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} || error(_g("Can't merge symbols from objects without SONAME."));
  361. my %dynsyms;
  362. foreach my $sym ($object->get_exported_dynamic_symbols()) {
  363. my $name = $sym->{name} . '@' .
  364. ($sym->{version} ? $sym->{version} : "Base");
  365. my $symobj = $self->lookup_symbol($name, $soname);
  366. if (exists $blacklist{$sym->{name}}) {
  367. next unless (defined $symobj and $symobj->has_tag("ignore-blacklist"));
  368. }
  369. $dynsyms{$name} = $sym;
  370. }
  371. unless ($self->has_object($soname)) {
  372. $self->create_object($soname, '');
  373. }
  374. # Scan all symbols provided by the objects
  375. my $obj = $self->get_object($soname);
  376. # invalidate the minimum version cache - it is not sufficient to
  377. # invalidate in add_symbol, since we might change a minimum
  378. # version for a particular symbol without adding it
  379. $obj->{minver_cache} = [];
  380. foreach my $name (keys %dynsyms) {
  381. my $sym;
  382. if ($sym = $self->lookup_symbol($name, $obj, 1)) {
  383. # If the symbol is already listed in the file
  384. $sym->mark_found_in_library($minver, $self->get_arch());
  385. } else {
  386. # The exact symbol is not present in the file, but it might match a
  387. # pattern.
  388. my $pattern = $self->find_matching_pattern($name, $obj, 1);
  389. if (defined $pattern) {
  390. $pattern->mark_found_in_library($minver, $self->get_arch());
  391. $sym = $pattern->create_pattern_match(symbol => $name);
  392. } else {
  393. # Symbol without any special info as no pattern matched
  394. $sym = Dpkg::Shlibs::Symbol->new(symbol => $name,
  395. minver => $minver);
  396. }
  397. $self->add_symbol($sym, $obj);
  398. }
  399. }
  400. # Process all symbols which could not be found in the library.
  401. foreach my $sym ($self->get_symbols($soname)) {
  402. if (not exists $dynsyms{$sym->get_symbolname()}) {
  403. $sym->mark_not_found_in_library($minver, $self->get_arch());
  404. }
  405. }
  406. # Deprecate patterns which didn't match anything
  407. for my $pattern (grep { $_->get_pattern_matches() == 0 }
  408. $self->get_patterns($soname)) {
  409. $pattern->mark_not_found_in_library($minver, $self->get_arch());
  410. }
  411. }
  412. sub is_empty {
  413. my ($self) = @_;
  414. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  415. }
  416. sub has_object {
  417. my ($self, $soname) = @_;
  418. return exists $self->{objects}{$soname};
  419. }
  420. sub get_object {
  421. my ($self, $soname) = @_;
  422. return ref($soname) ? $soname : $self->{objects}{$soname};
  423. }
  424. sub create_object {
  425. my ($self, $soname, @deps) = @_;
  426. $self->{objects}{$soname} = {
  427. syms => {},
  428. fields => {},
  429. patterns => {
  430. aliases => {},
  431. generic => [],
  432. },
  433. deps => [ @deps ],
  434. minver_cache => []
  435. };
  436. }
  437. sub get_dependency {
  438. my ($self, $soname, $dep_id) = @_;
  439. $dep_id = 0 unless defined($dep_id);
  440. return $self->get_object($soname)->{deps}[$dep_id];
  441. }
  442. sub get_smallest_version {
  443. my ($self, $soname, $dep_id) = @_;
  444. $dep_id = 0 unless defined($dep_id);
  445. my $so_object = $self->get_object($soname);
  446. return $so_object->{minver_cache}[$dep_id] if(defined($so_object->{minver_cache}[$dep_id]));
  447. my $minver;
  448. foreach my $sym ($self->get_symbols($so_object)) {
  449. next if $dep_id != $sym->{dep_id};
  450. $minver = $sym->{minver} unless defined($minver);
  451. if (version_compare($minver, $sym->{minver}) > 0) {
  452. $minver = $sym->{minver};
  453. }
  454. }
  455. $so_object->{minver_cache}[$dep_id] = $minver;
  456. return $minver;
  457. }
  458. sub get_dependencies {
  459. my ($self, $soname) = @_;
  460. return @{$self->get_object($soname)->{deps}};
  461. }
  462. sub get_field {
  463. my ($self, $soname, $name) = @_;
  464. if (my $obj = $self->get_object($soname)) {
  465. if (exists $obj->{fields}{$name}) {
  466. return $obj->{fields}{$name};
  467. }
  468. }
  469. return undef;
  470. }
  471. # Tries to find a symbol like the $refsym and returns its descriptor.
  472. # $refsym may also be a symbol name.
  473. sub lookup_symbol {
  474. my ($self, $refsym, $sonames, $inc_deprecated) = @_;
  475. $inc_deprecated = 0 unless defined($inc_deprecated);
  476. my $name = (ref $refsym) ? $refsym->get_symbolname() : $refsym;
  477. foreach my $so ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
  478. if (my $obj = $self->get_object($so)) {
  479. my $sym = $obj->{syms}{$name};
  480. if ($sym and ($inc_deprecated or not $sym->{deprecated}))
  481. {
  482. return (wantarray) ?
  483. ( symbol => $sym, soname => $so ) : $sym;
  484. }
  485. }
  486. }
  487. return (wantarray) ? () : undef;
  488. }
  489. # Tries to find a pattern like the $refpat and returns its descriptor.
  490. # $refpat may also be a pattern spec.
  491. sub lookup_pattern {
  492. my ($self, $refpat, $sonames, $inc_deprecated) = @_;
  493. $inc_deprecated = 0 unless defined($inc_deprecated);
  494. # If $refsym is a string, we need to create a dummy ref symbol.
  495. $refpat = $self->create_symbol($refpat, dummy => 1) if ! ref($refpat);
  496. if ($refpat && $refpat->is_pattern()) {
  497. foreach my $soname ((ref($sonames) eq 'ARRAY') ? @$sonames : $sonames) {
  498. if (my $obj = $self->get_object($soname)) {
  499. my $pat;
  500. if (my $type = $refpat->get_alias_type()) {
  501. if (exists $obj->{patterns}{aliases}{$type}) {
  502. $pat = $obj->{patterns}{aliases}{$type}{$refpat->get_symbolname()};
  503. }
  504. } elsif ($refpat->get_pattern_type() eq "generic") {
  505. for my $p (@{$obj->{patterns}{generic}}) {
  506. if (($inc_deprecated || !$p->{deprecated}) &&
  507. $p->equals($refpat, versioning => 0))
  508. {
  509. $pat = $p;
  510. last;
  511. }
  512. }
  513. }
  514. if ($pat && ($inc_deprecated || !$pat->{deprecated})) {
  515. return (wantarray) ?
  516. (symbol => $pat, soname => $soname) : $pat;
  517. }
  518. }
  519. }
  520. }
  521. return (wantarray) ? () : undef;
  522. }
  523. # Get symbol object reference either by symbol name or by a reference object.
  524. sub get_symbol_object {
  525. my ($self, $refsym, $soname) = @_;
  526. my $sym = $self->lookup_symbol($refsym, $soname, 1);
  527. if (! defined $sym) {
  528. $sym = $self->lookup_pattern($refsym, $soname, 1);
  529. }
  530. return $sym;
  531. }
  532. sub get_new_symbols {
  533. my ($self, $ref, %opts) = @_;
  534. my $with_optional = (exists $opts{with_optional}) ?
  535. $opts{with_optional} : 0;
  536. my @res;
  537. foreach my $soname ($self->get_sonames()) {
  538. next if not $ref->has_object($soname);
  539. # Scan raw symbols first.
  540. foreach my $sym (grep { ($with_optional || ! $_->is_optional())
  541. && $_->is_legitimate($self->get_arch()) }
  542. $self->get_symbols($soname))
  543. {
  544. my $refsym = $ref->lookup_symbol($sym, $soname, 1);
  545. my $isnew;
  546. if (defined $refsym) {
  547. # If the symbol exists in the $ref symbol file, it might
  548. # still be new if $refsym is not legitimate.
  549. $isnew = not $refsym->is_legitimate($self->get_arch());
  550. } else {
  551. # If the symbol does not exist in the $ref symbol file, it does
  552. # not mean that it's new. It might still match a pattern in the
  553. # symbol file. However, due to performance reasons, first check
  554. # if the pattern that the symbol matches (if any) exists in the
  555. # ref symbol file as well.
  556. $isnew = not (
  557. ($sym->get_pattern() and $ref->lookup_pattern($sym->get_pattern(), $soname, 1)) or
  558. $ref->find_matching_pattern($sym, $soname, 1)
  559. );
  560. }
  561. push @res, { symbol => $sym, soname => $soname } if $isnew;
  562. }
  563. # Now scan patterns
  564. foreach my $p (grep { ($with_optional || ! $_->is_optional())
  565. && $_->is_legitimate($self->get_arch()) }
  566. $self->get_patterns($soname))
  567. {
  568. my $refpat = $ref->lookup_pattern($p, $soname, 0);
  569. # If reference pattern was not found or it is not legitimate,
  570. # considering current one as new.
  571. if (not defined $refpat or
  572. not $refpat->is_legitimate($self->get_arch()))
  573. {
  574. push @res, { symbol => $p , soname => $soname };
  575. }
  576. }
  577. }
  578. return @res;
  579. }
  580. sub get_lost_symbols {
  581. my ($self, $ref, %opts) = @_;
  582. return $ref->get_new_symbols($self, %opts);
  583. }
  584. sub get_new_libs {
  585. my ($self, $ref) = @_;
  586. my @res;
  587. foreach my $soname ($self->get_sonames()) {
  588. push @res, $soname if not $ref->get_object($soname);
  589. }
  590. return @res;
  591. }
  592. sub get_lost_libs {
  593. my ($self, $ref) = @_;
  594. return $ref->get_new_libs($self);
  595. }
  596. 1;