SymbolFile.pm 8.9 KB

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