SymbolFile.pm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # Copyright (C) 2007 Raphael Hertzog
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Shlibs::SymbolFile;
  14. use Dpkg::Gettext;
  15. use Dpkg::ErrorHandling qw(syserr warning error);
  16. use Dpkg::Version qw(vercmp);
  17. use Dpkg::Fields qw(capit);
  18. my %blacklist = (
  19. '__bss_end__' => 1, # arm
  20. '__bss_end' => 1, # arm
  21. '_bss_end__' => 1, # arm
  22. '__bss_start' => 1, # ALL
  23. '__bss_start__' => 1, # arm
  24. '__data_start' => 1, # arm
  25. '__do_global_ctors_aux' => 1, # ia64
  26. '__do_global_dtors_aux' => 1, # ia64
  27. '__do_jv_register_classes' => 1,# ia64
  28. '_DYNAMIC' => 1, # ALL
  29. '_edata' => 1, # ALL
  30. '_end' => 1, # ALL
  31. '__end__' => 1, # arm
  32. '__exidx_end' => 1, # armel
  33. '__exidx_start' => 1, # armel
  34. '_fbss' => 1, # mips, mipsel
  35. '_fdata' => 1, # mips, mipsel
  36. '_fini' => 1, # ALL
  37. '_ftext' => 1, # mips, mipsel
  38. '_GLOBAL_OFFSET_TABLE_' => 1, # hppa, mips, mipsel
  39. '__gmon_start__' => 1, # hppa
  40. '_gp' => 1, # mips, mipsel
  41. '_init' => 1, # ALL
  42. '_PROCEDURE_LINKAGE_TABLE_' => 1, # sparc, alpha
  43. '_SDA2_BASE_' => 1, # powerpc
  44. '_SDA_BASE_' => 1, # powerpc
  45. );
  46. for (my $i = 14; $i <= 31; $i++) {
  47. # Many powerpc specific symbols
  48. $blacklist{"_restfpr_$i"} = 1;
  49. $blacklist{"_restfpr_$i\_x"} = 1;
  50. $blacklist{"_restgpr_$i"} = 1;
  51. $blacklist{"_restgpr_$i\_x"} = 1;
  52. $blacklist{"_savefpr_$i"} = 1;
  53. $blacklist{"_savegpr_$i"} = 1;
  54. }
  55. sub new {
  56. my $this = shift;
  57. my $file = shift;
  58. my $class = ref($this) || $this;
  59. my $self = { };
  60. bless $self, $class;
  61. if (defined($file) ) {
  62. $self->{file} = $file;
  63. $self->load($file) if -e $file;
  64. }
  65. return $self;
  66. }
  67. sub clear {
  68. my ($self) = @_;
  69. $self->{objects} = {};
  70. }
  71. sub clear_except {
  72. my ($self, @ids) = @_;
  73. my %has;
  74. $has{$_} = 1 foreach (@ids);
  75. foreach my $objid (keys %{$self->{objects}}) {
  76. delete $self->{objects}{$objid} unless exists $has{$objid};
  77. }
  78. }
  79. # Parameter seen is only used for recursive calls
  80. sub load {
  81. my ($self, $file, $seen, $current_object) = @_;
  82. if (defined($seen)) {
  83. return if exists $seen->{$file}; # Avoid include loops
  84. } else {
  85. $self->{file} = $file;
  86. $seen = {};
  87. }
  88. $seen->{$file} = 1;
  89. open(my $sym_file, "<", $file)
  90. || syserr(sprintf(_g("Can't open %s: %s"), $file));
  91. my $object = $current_object;
  92. while (defined($_ = <$sym_file>)) {
  93. chomp($_);
  94. if (/^\s+(\S+)\s(\S+)(?:\s(\d+))?/) {
  95. if (not defined ($object)) {
  96. error(sprintf(_g("Symbol information must be preceded by a header (file %s, line %s).", $file, $.)));
  97. }
  98. # New symbol
  99. my $sym = {
  100. minver => $2,
  101. dep_id => defined($3) ? $3 : 0,
  102. deprecated => 0
  103. };
  104. $self->{objects}{$object}{syms}{$1} = $sym;
  105. } elsif (/^#include\s+"([^"]+)"/) {
  106. my $filename = $1;
  107. my $dir = $file;
  108. $dir =~ s{[^/]+$}{}; # Strip filename
  109. $self->load("$dir$filename", $seen, $object);
  110. } elsif (/^#(?:DEPRECATED|MISSING): ([^#]+)#\s*(\S+)\s(\S+)(?:\s(\d+))?/) {
  111. my $sym = {
  112. minver => $3,
  113. dep_id => defined($4) ? $4 : 0,
  114. deprecated => $1
  115. };
  116. $self->{objects}{$object}{syms}{$2} = $sym;
  117. } elsif (/^#/) {
  118. # Skip possible comments
  119. } elsif (/^\|\s*(.*)$/) {
  120. # Alternative dependency template
  121. push @{$self->{objects}{$object}{deps}}, "$1";
  122. } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
  123. # Add meta-fields
  124. $self->{objects}{$object}{fields}{capit($1)} = $2;
  125. } elsif (/^(\S+)\s+(.*)$/) {
  126. # New object and dependency template
  127. $object = $1;
  128. if (exists $self->{objects}{$object}) {
  129. # Update/override infos only
  130. $self->{objects}{$object}{deps} = [ "$2" ];
  131. } else {
  132. # Create a new object
  133. $self->create_object($object, "$2");
  134. }
  135. } else {
  136. warning(sprintf(_g("Failed to parse a line in %s: %s"), $file, $_));
  137. }
  138. }
  139. close($sym_file);
  140. }
  141. sub save {
  142. my ($self, $file, $with_deprecated) = @_;
  143. $file = $self->{file} unless defined($file);
  144. my $fh;
  145. if ($file eq "-") {
  146. $fh = \*STDOUT;
  147. } else {
  148. open($fh, ">", $file)
  149. || syserr(sprintf(_g("Can't open %s for writing: %s"), $file, $!));
  150. }
  151. $self->dump($fh, $with_deprecated);
  152. close($fh) if ($file ne "-");
  153. }
  154. sub dump {
  155. my ($self, $fh, $with_deprecated) = @_;
  156. $with_deprecated = 1 unless defined($with_deprecated);
  157. foreach my $soname (sort keys %{$self->{objects}}) {
  158. my @deps = @{$self->{objects}{$soname}{deps}};
  159. print $fh "$soname $deps[0]\n";
  160. shift @deps;
  161. print $fh "| $_\n" foreach (@deps);
  162. my $f = $self->{objects}{$soname}{fields};
  163. print $fh "* $_: $f->{$_}\n" foreach (sort keys %{$f});
  164. foreach my $sym (sort keys %{$self->{objects}{$soname}{syms}}) {
  165. my $info = $self->{objects}{$soname}{syms}{$sym};
  166. next if $info->{deprecated} and not $with_deprecated;
  167. print $fh "#MISSING: $info->{deprecated}#" if $info->{deprecated};
  168. print $fh " $sym $info->{minver}";
  169. print $fh " $info->{dep_id}" if $info->{dep_id};
  170. print $fh "\n";
  171. }
  172. }
  173. }
  174. # merge_symbols($object, $minver)
  175. # Needs $Objdump->get_object($soname) as parameter
  176. # Don't merge blacklisted symbols related to the internal (arch-specific)
  177. # machinery
  178. sub merge_symbols {
  179. my ($self, $object, $minver) = @_;
  180. my $soname = $object->{SONAME} || error(_g("Can't merge symbols from objects without SONAME."));
  181. my %dynsyms;
  182. foreach my $sym ($object->get_exported_dynamic_symbols()) {
  183. next if exists $blacklist{$sym->{name}};
  184. if ($sym->{version}) {
  185. $dynsyms{$sym->{name} . '@' . $sym->{version}} = $sym;
  186. } else {
  187. $dynsyms{$sym->{name}} = $sym;
  188. }
  189. }
  190. unless ($self->{objects}{$soname}) {
  191. $self->create_object($soname, '');
  192. }
  193. # Scan all symbols provided by the objects
  194. foreach my $sym (keys %dynsyms) {
  195. if (exists $self->{objects}{$soname}{syms}{$sym}) {
  196. # If the symbol is already listed in the file
  197. my $info = $self->{objects}{$soname}{syms}{$sym};
  198. if ($info->{deprecated}) {
  199. # Symbol reappeared somehow
  200. $info->{deprecated} = 0;
  201. $info->{minver} = $minver;
  202. next;
  203. }
  204. # We assume that the right dependency information is already
  205. # there.
  206. if (vercmp($minver, $info->{minver}) < 0) {
  207. $info->{minver} = $minver;
  208. }
  209. } else {
  210. # The symbol is new and not present in the file
  211. my $info = {
  212. minver => $minver,
  213. deprecated => 0,
  214. dep_id => 0
  215. };
  216. $self->{objects}{$soname}{syms}{$sym} = $info;
  217. }
  218. }
  219. # Scan all symbols in the file and mark as deprecated those that are
  220. # no more provided (only if the minver is bigger than the version where
  221. # the symbol was introduced)
  222. foreach my $sym (keys %{$self->{objects}{$soname}{syms}}) {
  223. if (! exists $dynsyms{$sym}) {
  224. # Do nothing if already deprecated
  225. next if $self->{objects}{$soname}{syms}{$sym}{deprecated};
  226. my $info = $self->{objects}{$soname}{syms}{$sym};
  227. if (vercmp($minver, $info->{minver}) > 0) {
  228. $self->{objects}{$soname}{syms}{$sym}{deprecated} = $minver;
  229. }
  230. }
  231. }
  232. }
  233. sub is_empty {
  234. my ($self) = @_;
  235. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  236. }
  237. sub has_object {
  238. my ($self, $soname) = @_;
  239. return exists $self->{objects}{$soname};
  240. }
  241. sub create_object {
  242. my ($self, $soname, @deps) = @_;
  243. $self->{objects}{$soname} = {
  244. syms => {},
  245. fields => {},
  246. deps => [ @deps ]
  247. };
  248. }
  249. sub get_dependency {
  250. my ($self, $soname, $dep_id) = @_;
  251. $dep_id = 0 unless defined($dep_id);
  252. return $self->{objects}{$soname}{deps}[$dep_id];
  253. }
  254. sub get_dependencies {
  255. my ($self, $soname) = @_;
  256. return @{$self->{objects}{$soname}{deps}};
  257. }
  258. sub get_field {
  259. my ($self, $soname, $name) = @_;
  260. if (exists $self->{objects}{$soname}{fields}{$name}) {
  261. return $self->{objects}{$soname}{fields}{$name};
  262. }
  263. return undef;
  264. }
  265. sub lookup_symbol {
  266. my ($self, $name, $sonames, $inc_deprecated) = @_;
  267. $inc_deprecated = 0 unless defined($inc_deprecated);
  268. foreach my $so (@{$sonames}) {
  269. next if (! exists $self->{objects}{$so});
  270. if (exists $self->{objects}{$so}{syms}{$name} and
  271. ($inc_deprecated or not
  272. $self->{objects}{$so}{syms}{$name}{deprecated}))
  273. {
  274. my $dep_id = $self->{objects}{$so}{syms}{$name}{dep_id};
  275. return {
  276. 'depends' => $self->{objects}{$so}{deps}[$dep_id],
  277. 'soname' => $so,
  278. %{$self->{objects}{$so}{syms}{$name}}
  279. };
  280. }
  281. }
  282. return undef;
  283. }
  284. sub has_new_symbols {
  285. my ($self, $ref) = @_;
  286. foreach my $soname (keys %{$self->{objects}}) {
  287. my $mysyms = $self->{objects}{$soname}{syms};
  288. next if not exists $ref->{objects}{$soname};
  289. my $refsyms = $ref->{objects}{$soname}{syms};
  290. foreach my $sym (grep { not $mysyms->{$_}{deprecated} }
  291. keys %{$mysyms})
  292. {
  293. if ((not exists $refsyms->{$sym}) or
  294. $refsyms->{$sym}{deprecated})
  295. {
  296. return 1;
  297. }
  298. }
  299. }
  300. return 0;
  301. }
  302. sub has_lost_symbols {
  303. my ($self, $ref) = @_;
  304. return $ref->has_new_symbols($self);
  305. }
  306. sub has_new_libs {
  307. my ($self, $ref) = @_;
  308. foreach my $soname (keys %{$self->{objects}}) {
  309. return 1 if not exists $ref->{objects}{$soname};
  310. }
  311. return 0;
  312. }
  313. sub has_lost_libs {
  314. my ($self, $ref) = @_;
  315. return $ref->has_new_libs($self);
  316. }
  317. 1;