SymbolFile.pm 8.6 KB

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