SymbolFile.pm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 = map { $_ => $object->{dynsyms}{$_} }
  170. grep {
  171. local $a = $object->{dynsyms}{$_};
  172. $a->{dynamic} && $a->{defined} && not exists $blacklist{$a->{name}}
  173. }
  174. keys %{$object->{dynsyms}};
  175. unless ($self->{objects}{$soname}) {
  176. $self->create_object($soname, '');
  177. }
  178. # Scan all symbols provided by the objects
  179. foreach my $sym (keys %dynsyms) {
  180. if (exists $self->{objects}{$soname}{syms}{$sym}) {
  181. # If the symbol is already listed in the file
  182. my $info = $self->{objects}{$soname}{syms}{$sym};
  183. if ($info->{deprecated}) {
  184. # Symbol reappeared somehow
  185. $info->{deprecated} = 0;
  186. $info->{minver} = $minver;
  187. next;
  188. }
  189. # We assume that the right dependency information is already
  190. # there.
  191. if (vercmp($minver, $info->{minver}) < 0) {
  192. $info->{minver} = $minver;
  193. }
  194. } else {
  195. # The symbol is new and not present in the file
  196. my $info = {
  197. minver => $minver,
  198. deprecated => 0,
  199. dep_id => 0
  200. };
  201. $self->{objects}{$soname}{syms}{$sym} = $info;
  202. }
  203. }
  204. # Scan all symbols in the file and mark as deprecated those that are
  205. # no more provided
  206. foreach my $sym (keys %{$self->{objects}{$soname}{syms}}) {
  207. if (! exists $dynsyms{$sym}) {
  208. $self->{objects}{$soname}{syms}{$sym}{deprecated} = $minver;
  209. }
  210. }
  211. }
  212. sub is_empty {
  213. my ($self) = @_;
  214. return scalar(keys %{$self->{objects}}) ? 0 : 1;
  215. }
  216. sub has_object {
  217. my ($self, $soname) = @_;
  218. return exists $self->{objects}{$soname};
  219. }
  220. sub create_object {
  221. my ($self, $soname, @deps) = @_;
  222. $self->{objects}{$soname} = {
  223. syms => {},
  224. deps => [ @deps ]
  225. };
  226. }
  227. sub get_dependency {
  228. my ($self, $soname, $dep_id) = @_;
  229. $dep_id = 0 unless defined($dep_id);
  230. return $self->{objects}{$soname}{deps}[$dep_id];
  231. }
  232. sub lookup_symbol {
  233. my ($self, $name, $sonames) = @_;
  234. foreach my $so (@{$sonames}) {
  235. next if (! exists $self->{objects}{$so});
  236. if (exists $self->{objects}{$so}{syms}{$name} and
  237. not $self->{objects}{$so}{syms}{$name}{deprecated})
  238. {
  239. my $dep_id = $self->{objects}{$so}{syms}{$name}{dep_id};
  240. return {
  241. 'depends' => $self->{objects}{$so}{deps}[$dep_id],
  242. 'soname' => $so,
  243. %{$self->{objects}{$so}{syms}{$name}}
  244. };
  245. }
  246. }
  247. return undef;
  248. }
  249. sub has_new_symbols {
  250. my ($self, $ref) = @_;
  251. foreach my $soname (keys %{$self->{objects}}) {
  252. my $mysyms = $self->{objects}{$soname}{syms};
  253. next if not exists $ref->{objects}{$soname};
  254. my $refsyms = $ref->{objects}{$soname}{syms};
  255. foreach my $sym (grep { not $mysyms->{$_}{deprecated} }
  256. keys %{$mysyms})
  257. {
  258. if ((not exists $refsyms->{$sym}) or
  259. $refsyms->{$sym}{deprecated})
  260. {
  261. return 1;
  262. }
  263. }
  264. }
  265. return 0;
  266. }
  267. sub has_lost_symbols {
  268. my ($self, $ref) = @_;
  269. return $ref->has_new_symbols($self);
  270. }
  271. sub has_new_libs {
  272. my ($self, $ref) = @_;
  273. foreach my $soname (keys %{$self->{objects}}) {
  274. return 1 if not exists $ref->{objects}{$soname};
  275. }
  276. return 0;
  277. }
  278. sub has_lost_libs {
  279. my ($self, $ref) = @_;
  280. return $ref->has_new_libs($self);
  281. }
  282. 1;