SymbolFile.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. # Copyright (C) 2007 Raphael Hertzog
  2. # (C) 2009 Modestas Vainius
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License along
  12. # with this program; if not, write to the Free Software Foundation, Inc.,
  13. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. package Dpkg::Shlibs::SymbolFile;
  15. use Dpkg::Gettext;
  16. use Dpkg::ErrorHandling;
  17. use Dpkg::Version qw(vercmp);
  18. use Dpkg::Fields qw(capit);
  19. use Dpkg::Shlibs::Symbol;
  20. use Dpkg::Arch qw(get_host_arch);
  21. my %blacklist = (
  22. '__bss_end__' => 1, # arm
  23. '__bss_end' => 1, # arm
  24. '_bss_end__' => 1, # arm
  25. '__bss_start' => 1, # ALL
  26. '__bss_start__' => 1, # arm
  27. '__data_start' => 1, # arm
  28. '__do_global_ctors_aux' => 1, # ia64
  29. '__do_global_dtors_aux' => 1, # ia64
  30. '__do_jv_register_classes' => 1,# ia64
  31. '_DYNAMIC' => 1, # ALL
  32. '_edata' => 1, # ALL
  33. '_end' => 1, # ALL
  34. '__end__' => 1, # arm
  35. '__exidx_end' => 1, # armel
  36. '__exidx_start' => 1, # armel
  37. '_fbss' => 1, # mips, mipsel
  38. '_fdata' => 1, # mips, mipsel
  39. '_fini' => 1, # ALL
  40. '_ftext' => 1, # mips, mipsel
  41. '_GLOBAL_OFFSET_TABLE_' => 1, # hppa, mips, mipsel
  42. '__gmon_start__' => 1, # hppa
  43. '__gnu_local_gp' => 1, # mips, mipsel
  44. '_gp' => 1, # mips, mipsel
  45. '_init' => 1, # ALL
  46. '_PROCEDURE_LINKAGE_TABLE_' => 1, # sparc, alpha
  47. '_SDA2_BASE_' => 1, # powerpc
  48. '_SDA_BASE_' => 1, # powerpc
  49. );
  50. for (my $i = 14; $i <= 31; $i++) {
  51. # Many powerpc specific symbols
  52. $blacklist{"_restfpr_$i"} = 1;
  53. $blacklist{"_restfpr_$i\_x"} = 1;
  54. $blacklist{"_restgpr_$i"} = 1;
  55. $blacklist{"_restgpr_$i\_x"} = 1;
  56. $blacklist{"_savefpr_$i"} = 1;
  57. $blacklist{"_savegpr_$i"} = 1;
  58. }
  59. # Many armel-specific symbols
  60. $blacklist{"__aeabi_$_"} = 1 foreach (qw(cdcmpeq cdcmple cdrcmple cfcmpeq
  61. cfcmple cfrcmple d2f d2iz d2lz d2uiz d2ulz dadd dcmpeq dcmpge dcmpgt
  62. dcmple dcmplt dcmpun ddiv dmul dneg drsub dsub f2d f2iz f2lz f2uiz f2ulz
  63. fadd fcmpeq fcmpge fcmpgt fcmple fcmplt fcmpun fdiv fmul fneg frsub fsub
  64. i2d i2f idiv idivmod l2d l2f lasr lcmp ldivmod llsl llsr lmul ui2d ui2f
  65. uidiv uidivmod ul2d ul2f ulcmp uldivmod unwind_cpp_pr0 unwind_cpp_pr1
  66. unwind_cpp_pr2 uread4 uread8 uwrite4 uwrite8));
  67. sub new {
  68. my $this = shift;
  69. my %opts=@_;
  70. my $class = ref($this) || $this;
  71. my $self = \%opts;
  72. bless $self, $class;
  73. $self->{arch} = get_host_arch() unless exists $self->{arch};
  74. $self->clear();
  75. if (exists $self->{file}) {
  76. $self->load($self->{file}) if -e $self->{file};
  77. }
  78. return $self;
  79. }
  80. sub clear {
  81. my ($self) = @_;
  82. $self->{objects} = {};
  83. $self->{used_wildcards} = 0;
  84. }
  85. sub clear_except {
  86. my ($self, @ids) = @_;
  87. my %has;
  88. $has{$_} = 1 foreach (@ids);
  89. foreach my $objid (keys %{$self->{objects}}) {
  90. delete $self->{objects}{$objid} unless exists $has{$objid};
  91. }
  92. }
  93. sub add_symbol {
  94. my ($self, $soname, $symbol) = @_;
  95. my $object = (ref $soname) ? $soname : $self->{objects}{$soname};
  96. if (!$symbol->{deprecated} && (my $ver = $symbol->get_wildcard_version())) {
  97. error(_g("you can't use wildcards on unversioned symbols: %s"), $_) if $ver eq "Base";
  98. $object->{wildcards}{$ver} = $symbol;
  99. return 'wildcards';
  100. } else {
  101. $object->{syms}{$symbol->get_symbolname()} = $symbol;
  102. return 'syms';
  103. }
  104. }
  105. # Parameter seen is only used for recursive calls
  106. sub load {
  107. my ($self, $file, $seen, $current_object_ref, $base_symbol) = @_;
  108. sub new_symbol {
  109. my $base = shift || 'Dpkg::Shlibs::Symbol';
  110. return (ref $base) ? $base->clone(@_) : $base->new(@_);
  111. }
  112. if (defined($seen)) {
  113. return if exists $seen->{$file}; # Avoid include loops
  114. } else {
  115. $self->{file} = $file;
  116. $seen = {};
  117. }
  118. $seen->{$file} = 1;
  119. open(my $sym_file, "<", $file)
  120. || syserr(_g("cannot open %s"), $file);
  121. if (not ref($current_object_ref)) {
  122. my $obj;
  123. $current_object_ref = \$obj;
  124. }
  125. local *object = $current_object_ref;
  126. while (defined($_ = <$sym_file>)) {
  127. chomp($_);
  128. if (/^(?:\s+|#(?:DEPRECATED|MISSING): ([^#]+)#\s*)(.*)/) {
  129. if (not defined ($object)) {
  130. error(_g("Symbol information must be preceded by a header (file %s, line %s)."), $file, $.);
  131. }
  132. # Symbol specification
  133. my $deprecated = ($1) ? $1 : 0;
  134. my $sym = new_symbol($base_symbol, deprecated => $deprecated);
  135. if ($sym->parse($2)) {
  136. $sym->process_tags(arch => $self->{arch});
  137. $self->add_symbol($object, $sym);
  138. } else {
  139. warning(_g("Failed to parse line in %s: %s"), $file, $_);
  140. }
  141. } elsif (/^(\(.*\))?#include\s+"([^"]+)"/) {
  142. my $tagspec = $1;
  143. my $filename = $2;
  144. my $dir = $file;
  145. my $new_base_symbol;
  146. if (defined $tagspec) {
  147. $new_base_symbol = new_symbol($base_symbol);
  148. $new_base_symbol->parse_tagspec($tagspec);
  149. }
  150. $dir =~ s{[^/]+$}{}; # Strip filename
  151. $self->load("$dir$filename", $seen, $current_object_ref, $new_base_symbol);
  152. } elsif (/^#/) {
  153. # Skip possible comments
  154. } elsif (/^\|\s*(.*)$/) {
  155. # Alternative dependency template
  156. push @{$self->{objects}{$object}{deps}}, "$1";
  157. } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
  158. # Add meta-fields
  159. $self->{objects}{$object}{fields}{capit($1)} = $2;
  160. } elsif (/^(\S+)\s+(.*)$/) {
  161. # New object and dependency template
  162. $object = $1;
  163. if (exists $self->{objects}{$object}) {
  164. # Update/override infos only
  165. $self->{objects}{$object}{deps} = [ "$2" ];
  166. } else {
  167. # Create a new object
  168. $self->create_object($object, "$2");
  169. }
  170. } else {
  171. warning(_g("Failed to parse a line in %s: %s"), $file, $_);
  172. }
  173. }
  174. close($sym_file);
  175. delete $seen->{$file};
  176. }
  177. # Beware: we reuse the data structure of the provided symfile so make
  178. # sure to not modify them after having called this function
  179. sub merge_object_from_symfile {
  180. my ($self, $src, $objid) = @_;
  181. if (not $self->has_object($objid)) {
  182. $self->{objects}{$objid} = $src->{objects}{$objid};
  183. } else {
  184. warning(_g("Tried to merge the same object (%s) twice in a symfile."), $objid);
  185. }
  186. }
  187. sub save {
  188. my ($self, $file, %opts) = @_;
  189. $file = $self->{file} unless defined($file);
  190. my $fh;
  191. if ($file eq "-") {
  192. $fh = \*STDOUT;
  193. } else {
  194. open($fh, ">", $file)
  195. || syserr(_g("cannot write %s"), $file);
  196. }
  197. $self->dump($fh, %opts);
  198. close($fh) if ($file ne "-");
  199. }
  200. sub dump {
  201. my ($self, $fh, %opts) = @_;
  202. $opts{template_mode} = 0 unless exists $opts{template_mode};
  203. $opts{with_deprecated} = 1 unless exists $opts{with_deprecated};
  204. foreach my $soname (sort keys %{$self->{objects}}) {
  205. my @deps = @{$self->{objects}{$soname}{deps}};
  206. my $dep = shift @deps;
  207. $dep =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  208. print $fh "$soname $dep\n";
  209. foreach $dep (@deps) {
  210. $dep =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  211. print $fh "| $dep\n";
  212. }
  213. my $f = $self->{objects}{$soname}{fields};
  214. foreach my $field (sort keys %{$f}) {
  215. my $value = $f->{$field};
  216. $value =~ s/#PACKAGE#/$opts{package}/g if exists $opts{package};
  217. print $fh "* $field: $value\n";
  218. }
  219. my $syms = $self->{objects}{$soname}{syms};
  220. foreach my $name (sort { $syms->{$a}->get_symboltempl() cmp
  221. $syms->{$b}->get_symboltempl() } keys %$syms) {
  222. my $sym = $self->{objects}{$soname}{syms}{$name};
  223. next if $sym->{deprecated} and not $opts{with_deprecated};
  224. # Do not dump symbols from foreign arch unless dumping a template.
  225. next if not $opts{template_mode} and
  226. not $sym->arch_is_concerned($self->{arch});
  227. # Dump symbol specification. Dump symbol tags only in template mode.
  228. print $fh $sym->get_symbolspec($opts{template_mode}) . "\n";
  229. }
  230. }
  231. }
  232. # merge_symbols($object, $minver)
  233. # Needs $Objdump->get_object($soname) as parameter
  234. # Don't merge blacklisted symbols related to the internal (arch-specific)
  235. # machinery
  236. sub merge_symbols {
  237. my ($self, $object, $minver) = @_;
  238. my $soname = $object->{SONAME} || error(_g("Can't merge symbols from objects without SONAME."));
  239. my %dynsyms;
  240. foreach my $sym ($object->get_exported_dynamic_symbols()) {
  241. my $name = $sym->{name} . '@' .
  242. ($sym->{version} ? $sym->{version} : "Base");
  243. my $symobj = $self->lookup_symbol($name, [ $soname ]);
  244. if (exists $blacklist{$sym->{name}}) {
  245. next unless (defined $symobj and $symobj->has_tag("ignore-blacklist"));
  246. }
  247. $dynsyms{$name} = $sym;
  248. }
  249. unless (exists $self->{objects}{$soname}) {
  250. $self->create_object($soname, '');
  251. }
  252. # Scan all symbols provided by the objects
  253. foreach my $name (keys %dynsyms) {
  254. my $obj = $self->{objects}{$soname};
  255. my $sym;
  256. if (exists $obj->{syms}{$name}) {
  257. # If the symbol is already listed in the file
  258. $sym = $obj->{syms}{$name};
  259. if ($sym->{deprecated}) {
  260. # Symbol reappeared somehow
  261. $sym->{deprecated} = 0;
  262. $sym->{minver} = $minver if (not $sym->is_optional());
  263. } else {
  264. # We assume that the right dependency information is already
  265. # there.
  266. if (vercmp($minver, $sym->{minver}) < 0) {
  267. $sym->{minver} = $minver;
  268. }
  269. }
  270. if (not $sym->arch_is_concerned($self->{arch})) {
  271. # Remove arch tag because it is incorrect.
  272. $sym->delete_tag('arch');
  273. }
  274. } else {
  275. # The symbol is new and not present in the file
  276. my $symobj = $dynsyms{$name};
  277. $sym = $self->symbol_match_wildcard($soname, $name, $symobj->{version});
  278. if (not defined $sym) {
  279. # Symbol without any special info as no wildcard did match
  280. $sym = Dpkg::Shlibs::Symbol->new(symbol => $name,
  281. minver => $minver);
  282. }
  283. $self->add_symbol($obj, $sym);
  284. }
  285. }
  286. # Scan all symbols in the file and mark as deprecated those that are
  287. # no more provided (only if the minver is bigger than the version where
  288. # the symbol was introduced)
  289. foreach my $name (keys %{$self->{objects}{$soname}{syms}}) {
  290. if (not exists $dynsyms{$name}) {
  291. my $sym = $self->{objects}{$soname}{syms}{$name};
  292. # Ignore symbols from foreign arch
  293. next if not $sym->arch_is_concerned($self->{arch});
  294. if ($sym->{deprecated}) {
  295. # Bump deprecated if the symbol is optional so that it
  296. # keeps reappering in the diff while it's missing
  297. $sym->{deprecated} = $minver if $sym->is_optional();
  298. } elsif (vercmp($minver, $sym->{minver}) > 0) {
  299. $sym->{deprecated} = $minver;
  300. }
  301. }
  302. }
  303. }
  304. sub symbol_match_wildcard {
  305. my ($self, $soname, $name, $version) = @_;
  306. my $obj = $self->{objects}{$soname};
  307. if ($version and exists $obj->{wildcards}{$version}) {
  308. my $w_sym = $obj->{wildcards}{$version};
  309. return undef unless $w_sym->arch_is_concerned($self->{arch});
  310. $self->{used_wildcards}++;
  311. return $w_sym->clone(symbol => $name);
  312. }
  313. return undef;
  314. }
  315. sub is_empty {
  316. my ($self) = @_;
  317. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  318. }
  319. sub has_object {
  320. my ($self, $soname) = @_;
  321. return exists $self->{objects}{$soname};
  322. }
  323. sub create_object {
  324. my ($self, $soname, @deps) = @_;
  325. $self->{objects}{$soname} = {
  326. syms => {},
  327. fields => {},
  328. wildcards => {},
  329. deps => [ @deps ]
  330. };
  331. }
  332. sub get_dependency {
  333. my ($self, $soname, $dep_id) = @_;
  334. $dep_id = 0 unless defined($dep_id);
  335. return $self->{objects}{$soname}{deps}[$dep_id];
  336. }
  337. sub get_smallest_version {
  338. my ($self, $soname, $dep_id) = @_;
  339. $dep_id = 0 unless defined($dep_id);
  340. my $minver;
  341. foreach my $sym (values %{$self->{objects}{$soname}{syms}}) {
  342. next if $dep_id != $sym->{dep_id};
  343. $minver = $sym->{minver} unless defined($minver);
  344. if (vercmp($minver, $sym->{minver}) > 0) {
  345. $minver = $sym->{minver};
  346. }
  347. }
  348. return $minver;
  349. }
  350. sub get_dependencies {
  351. my ($self, $soname) = @_;
  352. return @{$self->{objects}{$soname}{deps}};
  353. }
  354. sub get_field {
  355. my ($self, $soname, $name) = @_;
  356. if (exists $self->{objects}{$soname}{fields}{$name}) {
  357. return $self->{objects}{$soname}{fields}{$name};
  358. }
  359. return undef;
  360. }
  361. sub contains_wildcards {
  362. my ($self) = @_;
  363. my $res = 0;
  364. foreach my $soname (sort keys %{$self->{objects}}) {
  365. if (scalar keys %{$self->{objects}{$soname}{wildcards}}) {
  366. $res = 1;
  367. }
  368. }
  369. return $res;
  370. }
  371. sub used_wildcards {
  372. my ($self) = @_;
  373. return $self->{used_wildcards};
  374. }
  375. sub lookup_symbol {
  376. my ($self, $name, $sonames, $inc_deprecated) = @_;
  377. $inc_deprecated = 0 unless defined($inc_deprecated);
  378. foreach my $so (@{$sonames}) {
  379. next if (! exists $self->{objects}{$so});
  380. if (exists $self->{objects}{$so}{syms}{$name} and
  381. ($inc_deprecated or not
  382. $self->{objects}{$so}{syms}{$name}{deprecated}))
  383. {
  384. my $dep_id = $self->{objects}{$so}{syms}{$name}{dep_id};
  385. my $clone = $self->{objects}{$so}{syms}{$name}->clone();
  386. $clone->{depends} = $self->{objects}{$so}{deps}[$dep_id];
  387. $clone->{soname} = $so;
  388. return $clone;
  389. }
  390. }
  391. return undef;
  392. }
  393. sub get_new_symbols {
  394. my ($self, $ref) = @_;
  395. my @res;
  396. foreach my $soname (keys %{$self->{objects}}) {
  397. my $mysyms = $self->{objects}{$soname}{syms};
  398. next if not exists $ref->{objects}{$soname};
  399. my $refsyms = $ref->{objects}{$soname}{syms};
  400. foreach my $sym (grep { not $mysyms->{$_}{deprecated} and
  401. not $mysyms->{$_}->is_optional() and
  402. $mysyms->{$_}->arch_is_concerned($self->{arch})
  403. } keys %{$mysyms})
  404. {
  405. if ((not exists $refsyms->{$sym}) or
  406. $refsyms->{$sym}{deprecated} or
  407. not $refsyms->{$sym}->arch_is_concerned($self->{arch}) )
  408. {
  409. push @res, $mysyms->{$sym}->clone(soname => $soname);
  410. }
  411. }
  412. }
  413. return @res;
  414. }
  415. sub get_lost_symbols {
  416. my ($self, $ref) = @_;
  417. return $ref->get_new_symbols($self);
  418. }
  419. sub get_new_libs {
  420. my ($self, $ref) = @_;
  421. my @res;
  422. foreach my $soname (keys %{$self->{objects}}) {
  423. push @res, $soname if not exists $ref->{objects}{$soname};
  424. }
  425. return @res;
  426. }
  427. sub get_lost_libs {
  428. my ($self, $ref) = @_;
  429. return $ref->get_new_libs($self);
  430. }
  431. 1;