SymbolFile.pm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. '_fbss' => 1, # mips, mipsel
  33. '_fdata' => 1, # mips, mipsel
  34. '_fini' => 1, # ALL
  35. '_ftext' => 1, # mips, mipsel
  36. '_GLOBAL_OFFSET_TABLE_' => 1, # hppa, mips, mipsel
  37. '__gmon_start__' => 1, # hppa
  38. '_gp' => 1, # mips, mipsel
  39. '_init' => 1, # ALL
  40. '_PROCEDURE_LINKAGE_TABLE_' => 1, # sparc, alpha
  41. '_SDA2_BASE_' => 1, # powerpc
  42. '_SDA_BASE_' => 1, # powerpc
  43. );
  44. for (my $i = 14; $i <= 31; $i++) {
  45. # Many powerpc specific symbols
  46. $blacklist{"_restfpr_$i"} = 1;
  47. $blacklist{"_restfpr_$i\_x"} = 1;
  48. $blacklist{"_restgpr_$i"} = 1;
  49. $blacklist{"_restgpr_$i\_x"} = 1;
  50. $blacklist{"_savefpr_$i"} = 1;
  51. $blacklist{"_savegpr_$i"} = 1;
  52. }
  53. sub new {
  54. my $this = shift;
  55. my $file = shift;
  56. my $class = ref($this) || $this;
  57. my $self = { };
  58. bless $self, $class;
  59. if (defined($file) ) {
  60. $self->{file} = $file;
  61. $self->load($file) if -e $file;
  62. }
  63. return $self;
  64. }
  65. sub clear {
  66. my ($self) = @_;
  67. $self->{objects} = {};
  68. }
  69. sub clear_except {
  70. my ($self, @ids) = @_;
  71. my %has;
  72. $has{$_} = 1 foreach (@ids);
  73. foreach my $objid (keys %{$self->{objects}}) {
  74. delete $self->{objects}{$objid} unless exists $has{$objid};
  75. }
  76. }
  77. # Parameter seen is only used for recursive calls
  78. sub load {
  79. my ($self, $file, $seen, $current_object) = @_;
  80. if (defined($seen)) {
  81. return if exists $seen->{$file}; # Avoid include loops
  82. } else {
  83. $self->{file} = $file;
  84. $seen = {};
  85. }
  86. $seen->{$file} = 1;
  87. open(my $sym_file, "<", $file)
  88. || syserr(sprintf(_g("Can't open %s: %s"), $file));
  89. my $object = $current_object;
  90. while (defined($_ = <$sym_file>)) {
  91. chomp($_);
  92. if (/^\s+(\S+)\s(\S+)(?:\s(\d+))?/) {
  93. if (not defined ($object)) {
  94. error(sprintf(_g("Symbol information must be preceded by a header (file %s, line %s).", $file, $.)));
  95. }
  96. # New symbol
  97. my $sym = {
  98. minver => $2,
  99. dep_id => defined($3) ? $3 : 0,
  100. deprecated => 0
  101. };
  102. $self->{objects}{$object}{syms}{$1} = $sym;
  103. } elsif (/^#include\s+"([^"]+)"/) {
  104. my $filename = $1;
  105. my $dir = $file;
  106. $dir =~ s{[^/]+$}{}; # Strip filename
  107. $self->load("$dir$filename", $seen, $object);
  108. } elsif (/^#DEPRECATED: ([^#]+)#\s*(\S+)\s(\S+)(?:\s(\d+))?/) {
  109. my $sym = {
  110. minver => $3,
  111. dep_id => defined($4) ? $4 : 0,
  112. deprecated => $1
  113. };
  114. $self->{objects}{$object}{syms}{$2} = $sym;
  115. } elsif (/^#/) {
  116. # Skip possible comments
  117. } elsif (/^\|\s*(.*)$/) {
  118. # Alternative dependency template
  119. push @{$self->{objects}{$object}{deps}}, "$1";
  120. } elsif (/^\*\s*([^:]+):\s*(.*\S)\s*$/) {
  121. # Add meta-fields
  122. $self->{objects}{$object}{fields}{capit($1)} = $2;
  123. } elsif (/^(\S+)\s+(.*)$/) {
  124. # New object and dependency template
  125. $object = $1;
  126. if (exists $self->{objects}{$object}) {
  127. # Update/override infos only
  128. $self->{objects}{$object}{deps} = [ "$2" ];
  129. } else {
  130. # Create a new object
  131. $self->create_object($object, "$2");
  132. }
  133. } else {
  134. warning(sprintf(_g("Failed to parse a line in %s: %s"), $file, $_));
  135. }
  136. }
  137. close($sym_file);
  138. }
  139. sub save {
  140. my ($self, $file, $with_deprecated) = @_;
  141. $file = $self->{file} unless defined($file);
  142. my $fh;
  143. if ($file eq "-") {
  144. $fh = \*STDOUT;
  145. } else {
  146. open($fh, ">", $file)
  147. || syserr(sprintf(_g("Can't open %s for writing: %s"), $file, $!));
  148. }
  149. $self->dump($fh, $with_deprecated);
  150. close($fh) if ($file ne "-");
  151. }
  152. sub dump {
  153. my ($self, $fh, $with_deprecated) = @_;
  154. $with_deprecated = 1 unless defined($with_deprecated);
  155. foreach my $soname (sort keys %{$self->{objects}}) {
  156. my @deps = @{$self->{objects}{$soname}{deps}};
  157. print $fh "$soname $deps[0]\n";
  158. shift @deps;
  159. print $fh "| $_\n" foreach (@deps);
  160. my $f = $self->{objects}{$soname}{fields};
  161. print $fh "* $_: $f->{$_}\n" foreach (sort keys %{$f});
  162. foreach my $sym (sort keys %{$self->{objects}{$soname}{syms}}) {
  163. my $info = $self->{objects}{$soname}{syms}{$sym};
  164. next if $info->{deprecated} and not $with_deprecated;
  165. print $fh "#DEPRECATED: $info->{deprecated}#" if $info->{deprecated};
  166. print $fh " $sym $info->{minver}";
  167. print $fh " $info->{dep_id}" if $info->{dep_id};
  168. print $fh "\n";
  169. }
  170. }
  171. }
  172. # merge_symbols($object, $minver)
  173. # Needs $Objdump->get_object($soname) as parameter
  174. # Don't merge blacklisted symbols related to the internal (arch-specific)
  175. # machinery
  176. sub merge_symbols {
  177. my ($self, $object, $minver) = @_;
  178. my $soname = $object->{SONAME} || error(_g("Can't merge symbols from objects without SONAME."));
  179. my %dynsyms;
  180. foreach my $sym ($object->get_exported_dynamic_symbols()) {
  181. next if exists $blacklist{$sym->{name}};
  182. if ($sym->{version}) {
  183. $dynsyms{$sym->{name} . '@' . $sym->{version}} = $sym;
  184. } else {
  185. $dynsyms{$sym->{name}} = $sym;
  186. }
  187. }
  188. unless ($self->{objects}{$soname}) {
  189. $self->create_object($soname, '');
  190. }
  191. # Scan all symbols provided by the objects
  192. foreach my $sym (keys %dynsyms) {
  193. if (exists $self->{objects}{$soname}{syms}{$sym}) {
  194. # If the symbol is already listed in the file
  195. my $info = $self->{objects}{$soname}{syms}{$sym};
  196. if ($info->{deprecated}) {
  197. # Symbol reappeared somehow
  198. $info->{deprecated} = 0;
  199. $info->{minver} = $minver;
  200. next;
  201. }
  202. # We assume that the right dependency information is already
  203. # there.
  204. if (vercmp($minver, $info->{minver}) < 0) {
  205. $info->{minver} = $minver;
  206. }
  207. } else {
  208. # The symbol is new and not present in the file
  209. my $info = {
  210. minver => $minver,
  211. deprecated => 0,
  212. dep_id => 0
  213. };
  214. $self->{objects}{$soname}{syms}{$sym} = $info;
  215. }
  216. }
  217. # Scan all symbols in the file and mark as deprecated those that are
  218. # no more provided (only if the minver is bigger than the version where
  219. # the symbol was introduced)
  220. foreach my $sym (keys %{$self->{objects}{$soname}{syms}}) {
  221. if (! exists $dynsyms{$sym}) {
  222. # Do nothing if already deprecated
  223. next if $self->{objects}{$soname}{syms}{$sym}{deprecated};
  224. my $info = $self->{objects}{$soname}{syms}{$sym};
  225. if (vercmp($minver, $info->{minver}) > 0) {
  226. $self->{objects}{$soname}{syms}{$sym}{deprecated} = $minver;
  227. }
  228. }
  229. }
  230. }
  231. sub is_empty {
  232. my ($self) = @_;
  233. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  234. }
  235. sub has_object {
  236. my ($self, $soname) = @_;
  237. return exists $self->{objects}{$soname};
  238. }
  239. sub create_object {
  240. my ($self, $soname, @deps) = @_;
  241. $self->{objects}{$soname} = {
  242. syms => {},
  243. fields => {},
  244. deps => [ @deps ]
  245. };
  246. }
  247. sub get_dependency {
  248. my ($self, $soname, $dep_id) = @_;
  249. $dep_id = 0 unless defined($dep_id);
  250. return $self->{objects}{$soname}{deps}[$dep_id];
  251. }
  252. sub lookup_symbol {
  253. my ($self, $name, $sonames, $inc_deprecated) = @_;
  254. $inc_deprecated = 0 unless defined($inc_deprecated);
  255. foreach my $so (@{$sonames}) {
  256. next if (! exists $self->{objects}{$so});
  257. if (exists $self->{objects}{$so}{syms}{$name} and
  258. ($inc_deprecated or not
  259. $self->{objects}{$so}{syms}{$name}{deprecated}))
  260. {
  261. my $dep_id = $self->{objects}{$so}{syms}{$name}{dep_id};
  262. return {
  263. 'depends' => $self->{objects}{$so}{deps}[$dep_id],
  264. 'soname' => $so,
  265. %{$self->{objects}{$so}{syms}{$name}}
  266. };
  267. }
  268. }
  269. return undef;
  270. }
  271. sub has_new_symbols {
  272. my ($self, $ref) = @_;
  273. foreach my $soname (keys %{$self->{objects}}) {
  274. my $mysyms = $self->{objects}{$soname}{syms};
  275. next if not exists $ref->{objects}{$soname};
  276. my $refsyms = $ref->{objects}{$soname}{syms};
  277. foreach my $sym (grep { not $mysyms->{$_}{deprecated} }
  278. keys %{$mysyms})
  279. {
  280. if ((not exists $refsyms->{$sym}) or
  281. $refsyms->{$sym}{deprecated})
  282. {
  283. return 1;
  284. }
  285. }
  286. }
  287. return 0;
  288. }
  289. sub has_lost_symbols {
  290. my ($self, $ref) = @_;
  291. return $ref->has_new_symbols($self);
  292. }
  293. sub has_new_libs {
  294. my ($self, $ref) = @_;
  295. foreach my $soname (keys %{$self->{objects}}) {
  296. return 1 if not exists $ref->{objects}{$soname};
  297. }
  298. return 0;
  299. }
  300. sub has_lost_libs {
  301. my ($self, $ref) = @_;
  302. return $ref->has_new_libs($self);
  303. }
  304. 1;