SymbolFile.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2009 Modestas Vainius <modestas@vainius.eu>
  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 exists $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 clear {
  85. my ($self) = @_;
  86. $self->{objects} = {};
  87. $self->{used_wildcards} = 0;
  88. }
  89. sub clear_except {
  90. my ($self, @ids) = @_;
  91. my %has;
  92. $has{$_} = 1 foreach (@ids);
  93. foreach my $objid (keys %{$self->{objects}}) {
  94. delete $self->{objects}{$objid} unless exists $has{$objid};
  95. }
  96. }
  97. sub add_symbol {
  98. my ($self, $soname, $symbol) = @_;
  99. my $object = (ref $soname) ? $soname : $self->{objects}{$soname};
  100. if (!$symbol->{deprecated} && (my $ver = $symbol->get_wildcard_version())) {
  101. error(_g("you can't use wildcards on unversioned symbols: %s"), $_) if $ver eq "Base";
  102. $object->{wildcards}{$ver} = $symbol;
  103. return 'wildcards';
  104. } else {
  105. # invalidate the minimum version cache
  106. $object->{minver_cache} = [];
  107. $object->{syms}{$symbol->get_symbolname()} = $symbol;
  108. return 'syms';
  109. }
  110. }
  111. # Parameter seen is only used for recursive calls
  112. sub load {
  113. my ($self, $file, $seen, $obj_ref, $base_symbol) = @_;
  114. sub new_symbol {
  115. my $base = shift || 'Dpkg::Shlibs::Symbol';
  116. return (ref $base) ? $base->clone(@_) : $base->new(@_);
  117. }
  118. if (defined($seen)) {
  119. return if exists $seen->{$file}; # Avoid include loops
  120. } else {
  121. $self->{file} = $file;
  122. $seen = {};
  123. }
  124. $seen->{$file} = 1;
  125. if (not ref($obj_ref)) { # Init ref to name of current object/lib
  126. $$obj_ref = undef;
  127. }
  128. open(my $sym_file, "<", $file)
  129. || syserr(_g("cannot open %s"), $file);
  130. while (defined($_ = <$sym_file>)) {
  131. chomp($_);
  132. if (/^(?:\s+|#(?:DEPRECATED|MISSING): ([^#]+)#\s*)(.*)/) {
  133. if (not defined ($$obj_ref)) {
  134. error(_g("Symbol information must be preceded by a header (file %s, line %s)."), $file, $.);
  135. }
  136. # Symbol specification
  137. my $deprecated = ($1) ? $1 : 0;
  138. my $sym = new_symbol($base_symbol, deprecated => $deprecated);
  139. if ($sym->parse($2)) {
  140. $sym->process_tags(arch => $self->{arch});
  141. $self->add_symbol($$obj_ref, $sym);
  142. } else {
  143. warning(_g("Failed to parse line in %s: %s"), $file, $_);
  144. }
  145. } elsif (/^(\(.*\))?#include\s+"([^"]+)"/) {
  146. my $tagspec = $1;
  147. my $filename = $2;
  148. my $dir = $file;
  149. my $new_base_symbol;
  150. if (defined $tagspec) {
  151. $new_base_symbol = new_symbol($base_symbol);
  152. $new_base_symbol->parse_tagspec($tagspec);
  153. }
  154. $dir =~ s{[^/]+$}{}; # Strip filename
  155. $self->load("$dir$filename", $seen, $obj_ref, $new_base_symbol);
  156. } elsif (/^#/) {
  157. # Skip possible comments
  158. } elsif (/^\|\s*(.*)$/) {
  159. # Alternative dependency template
  160. push @{$self->{objects}{$$obj_ref}{deps}}, "$1";
  161. } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
  162. # Add meta-fields
  163. $self->{objects}{$$obj_ref}{fields}{field_capitalize($1)} = $2;
  164. } elsif (/^(\S+)\s+(.*)$/) {
  165. # New object and dependency template
  166. $$obj_ref = $1;
  167. if (exists $self->{objects}{$$obj_ref}) {
  168. # Update/override infos only
  169. $self->{objects}{$$obj_ref}{deps} = [ "$2" ];
  170. } else {
  171. # Create a new object
  172. $self->create_object($$obj_ref, "$2");
  173. }
  174. } else {
  175. warning(_g("Failed to parse a line in %s: %s"), $file, $_);
  176. }
  177. }
  178. close($sym_file);
  179. delete $seen->{$file};
  180. }
  181. # Beware: we reuse the data structure of the provided symfile so make
  182. # sure to not modify them after having called this function
  183. sub merge_object_from_symfile {
  184. my ($self, $src, $objid) = @_;
  185. if (not $self->has_object($objid)) {
  186. $self->{objects}{$objid} = $src->{objects}{$objid};
  187. } else {
  188. warning(_g("Tried to merge the same object (%s) twice in a symfile."), $objid);
  189. }
  190. }
  191. sub save {
  192. my ($self, $file, %opts) = @_;
  193. $file = $self->{file} unless defined($file);
  194. my $fh;
  195. if ($file eq "-") {
  196. $fh = \*STDOUT;
  197. } else {
  198. open($fh, ">", $file)
  199. || syserr(_g("cannot write %s"), $file);
  200. }
  201. $self->dump($fh, %opts);
  202. close($fh) if ($file ne "-");
  203. }
  204. sub dump {
  205. my ($self, $fh, %opts) = @_;
  206. $opts{template_mode} = 0 unless exists $opts{template_mode};
  207. $opts{with_deprecated} = 1 unless exists $opts{with_deprecated};
  208. foreach my $soname (sort keys %{$self->{objects}}) {
  209. my @deps = @{$self->{objects}{$soname}{deps}};
  210. my $dep = shift @deps;
  211. $dep =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  212. print $fh "$soname $dep\n";
  213. foreach $dep (@deps) {
  214. $dep =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  215. print $fh "| $dep\n";
  216. }
  217. my $f = $self->{objects}{$soname}{fields};
  218. foreach my $field (sort keys %{$f}) {
  219. my $value = $f->{$field};
  220. $value =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  221. print $fh "* $field: $value\n";
  222. }
  223. my $syms = $self->{objects}{$soname}{syms};
  224. foreach my $name (sort { $syms->{$a}->get_symboltempl() cmp
  225. $syms->{$b}->get_symboltempl() } keys %$syms) {
  226. my $sym = $self->{objects}{$soname}{syms}{$name};
  227. next if $sym->{deprecated} and not $opts{with_deprecated};
  228. # Do not dump symbols from foreign arch unless dumping a template.
  229. next if not $opts{template_mode} and
  230. not $sym->arch_is_concerned($self->{arch});
  231. # Dump symbol specification. Dump symbol tags only in template mode.
  232. print $fh $sym->get_symbolspec($opts{template_mode}) . "\n";
  233. }
  234. }
  235. }
  236. # merge_symbols($object, $minver)
  237. # Needs $Objdump->get_object($soname) as parameter
  238. # Don't merge blacklisted symbols related to the internal (arch-specific)
  239. # machinery
  240. sub merge_symbols {
  241. my ($self, $object, $minver) = @_;
  242. my $soname = $object->{SONAME} || error(_g("Can't merge symbols from objects without SONAME."));
  243. my %dynsyms;
  244. foreach my $sym ($object->get_exported_dynamic_symbols()) {
  245. my $name = $sym->{name} . '@' .
  246. ($sym->{version} ? $sym->{version} : "Base");
  247. my $symobj = $self->lookup_symbol($name, [ $soname ]);
  248. if (exists $blacklist{$sym->{name}}) {
  249. next unless (defined $symobj and $symobj->has_tag("ignore-blacklist"));
  250. }
  251. $dynsyms{$name} = $sym;
  252. }
  253. unless (exists $self->{objects}{$soname}) {
  254. $self->create_object($soname, '');
  255. }
  256. # Scan all symbols provided by the objects
  257. my $obj = $self->{objects}{$soname};
  258. # invalidate the minimum version cache - it is not sufficient to
  259. # invalidate in add_symbol, since we might change a minimum
  260. # version for a particular symbol without adding it
  261. $obj->{minver_cache} = [];
  262. foreach my $name (keys %dynsyms) {
  263. my $sym;
  264. if (exists $obj->{syms}{$name}) {
  265. # If the symbol is already listed in the file
  266. $sym = $obj->{syms}{$name};
  267. if ($sym->{deprecated}) {
  268. # Symbol reappeared somehow
  269. $sym->{deprecated} = 0;
  270. $sym->{minver} = $minver if (not $sym->is_optional());
  271. } else {
  272. # We assume that the right dependency information is already
  273. # there.
  274. if (version_compare($minver, $sym->{minver}) < 0) {
  275. $sym->{minver} = $minver;
  276. }
  277. }
  278. if (not $sym->arch_is_concerned($self->{arch})) {
  279. # Remove arch tag because it is incorrect.
  280. $sym->delete_tag('arch');
  281. }
  282. } else {
  283. # The symbol is new and not present in the file
  284. my $symobj = $dynsyms{$name};
  285. $sym = $self->symbol_match_wildcard($soname, $name, $symobj->{version});
  286. if (not defined $sym) {
  287. # Symbol without any special info as no wildcard did match
  288. $sym = Dpkg::Shlibs::Symbol->new(symbol => $name,
  289. minver => $minver);
  290. }
  291. $self->add_symbol($obj, $sym);
  292. }
  293. }
  294. # Scan all symbols in the file and mark as deprecated those that are
  295. # no more provided (only if the minver is bigger than the version where
  296. # the symbol was introduced)
  297. foreach my $name (keys %{$self->{objects}{$soname}{syms}}) {
  298. if (not exists $dynsyms{$name}) {
  299. my $sym = $self->{objects}{$soname}{syms}{$name};
  300. # Ignore symbols from foreign arch
  301. next if not $sym->arch_is_concerned($self->{arch});
  302. if ($sym->{deprecated}) {
  303. # Bump deprecated if the symbol is optional so that it
  304. # keeps reappering in the diff while it's missing
  305. $sym->{deprecated} = $minver if $sym->is_optional();
  306. } elsif (version_compare($minver, $sym->{minver}) > 0) {
  307. $sym->{deprecated} = $minver;
  308. }
  309. }
  310. }
  311. }
  312. sub symbol_match_wildcard {
  313. my ($self, $soname, $name, $version) = @_;
  314. my $obj = $self->{objects}{$soname};
  315. if ($version and exists $obj->{wildcards}{$version}) {
  316. my $w_sym = $obj->{wildcards}{$version};
  317. return undef unless $w_sym->arch_is_concerned($self->{arch});
  318. $self->{used_wildcards}++;
  319. return $w_sym->clone(symbol => $name);
  320. }
  321. return undef;
  322. }
  323. sub is_empty {
  324. my ($self) = @_;
  325. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  326. }
  327. sub has_object {
  328. my ($self, $soname) = @_;
  329. return exists $self->{objects}{$soname};
  330. }
  331. sub create_object {
  332. my ($self, $soname, @deps) = @_;
  333. $self->{objects}{$soname} = {
  334. syms => {},
  335. fields => {},
  336. wildcards => {},
  337. deps => [ @deps ],
  338. minver_cache => []
  339. };
  340. }
  341. sub get_dependency {
  342. my ($self, $soname, $dep_id) = @_;
  343. $dep_id = 0 unless defined($dep_id);
  344. return $self->{objects}{$soname}{deps}[$dep_id];
  345. }
  346. sub get_smallest_version {
  347. my ($self, $soname, $dep_id) = @_;
  348. $dep_id = 0 unless defined($dep_id);
  349. my $so_object = $self->{objects}{$soname};
  350. return $so_object->{minver_cache}[$dep_id] if(defined($so_object->{minver_cache}[$dep_id]));
  351. my $minver;
  352. foreach my $sym (values %{$so_object->{syms}}) {
  353. next if $dep_id != $sym->{dep_id};
  354. $minver = $sym->{minver} unless defined($minver);
  355. if (version_compare($minver, $sym->{minver}) > 0) {
  356. $minver = $sym->{minver};
  357. }
  358. }
  359. $so_object->{minver_cache}[$dep_id] = $minver;
  360. return $minver;
  361. }
  362. sub get_dependencies {
  363. my ($self, $soname) = @_;
  364. return @{$self->{objects}{$soname}{deps}};
  365. }
  366. sub get_field {
  367. my ($self, $soname, $name) = @_;
  368. if (exists $self->{objects}{$soname}{fields}{$name}) {
  369. return $self->{objects}{$soname}{fields}{$name};
  370. }
  371. return undef;
  372. }
  373. sub contains_wildcards {
  374. my ($self) = @_;
  375. my $res = 0;
  376. foreach my $soname (sort keys %{$self->{objects}}) {
  377. if (scalar keys %{$self->{objects}{$soname}{wildcards}}) {
  378. $res = 1;
  379. }
  380. }
  381. return $res;
  382. }
  383. sub used_wildcards {
  384. my ($self) = @_;
  385. return $self->{used_wildcards};
  386. }
  387. sub lookup_symbol {
  388. my ($self, $name, $sonames, $inc_deprecated) = @_;
  389. $inc_deprecated = 0 unless defined($inc_deprecated);
  390. foreach my $so (@{$sonames}) {
  391. next if (! exists $self->{objects}{$so});
  392. if (exists $self->{objects}{$so}{syms}{$name} and
  393. ($inc_deprecated or not
  394. $self->{objects}{$so}{syms}{$name}{deprecated}))
  395. {
  396. my $dep_id = $self->{objects}{$so}{syms}{$name}{dep_id};
  397. my $clone = $self->{objects}{$so}{syms}{$name}->clone();
  398. $clone->{depends} = $self->{objects}{$so}{deps}[$dep_id];
  399. $clone->{soname} = $so;
  400. return $clone;
  401. }
  402. }
  403. return undef;
  404. }
  405. sub get_new_symbols {
  406. my ($self, $ref) = @_;
  407. my @res;
  408. foreach my $soname (keys %{$self->{objects}}) {
  409. my $mysyms = $self->{objects}{$soname}{syms};
  410. next if not exists $ref->{objects}{$soname};
  411. my $refsyms = $ref->{objects}{$soname}{syms};
  412. foreach my $sym (grep { not $mysyms->{$_}{deprecated} and
  413. not $mysyms->{$_}->is_optional() and
  414. $mysyms->{$_}->arch_is_concerned($self->{arch})
  415. } keys %{$mysyms})
  416. {
  417. if ((not exists $refsyms->{$sym}) or
  418. $refsyms->{$sym}{deprecated} or
  419. not $refsyms->{$sym}->arch_is_concerned($self->{arch}) )
  420. {
  421. push @res, $mysyms->{$sym}->clone(soname => $soname);
  422. }
  423. }
  424. }
  425. return @res;
  426. }
  427. sub get_lost_symbols {
  428. my ($self, $ref) = @_;
  429. return $ref->get_new_symbols($self);
  430. }
  431. sub get_new_libs {
  432. my ($self, $ref) = @_;
  433. my @res;
  434. foreach my $soname (keys %{$self->{objects}}) {
  435. push @res, $soname if not exists $ref->{objects}{$soname};
  436. }
  437. return @res;
  438. }
  439. sub get_lost_libs {
  440. my ($self, $ref) = @_;
  441. return $ref->get_new_libs($self);
  442. }
  443. 1;