Objdump.pm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. use strict;
  14. use warnings;
  15. package Dpkg::Shlibs::Objdump;
  16. use Dpkg::Gettext;
  17. use Dpkg::ErrorHandling qw(syserr subprocerr warning);
  18. textdomain("dpkg-dev");
  19. sub new {
  20. my $this = shift;
  21. my $class = ref($this) || $this;
  22. my $self = { 'objects' => {} };
  23. bless $self, $class;
  24. return $self;
  25. }
  26. sub parse {
  27. my ($self, $file) = @_;
  28. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  29. my $id = $obj->get_id;
  30. if ($id) {
  31. $self->{objects}{$id} = $obj;
  32. }
  33. return $id;
  34. }
  35. sub locate_symbol {
  36. my ($self, $name) = @_;
  37. foreach my $obj (values %{$self->{objects}}) {
  38. my $sym = $obj->get_symbol($name);
  39. if (defined($sym) && $sym->{defined}) {
  40. return $sym;
  41. }
  42. }
  43. return undef;
  44. }
  45. sub get_object {
  46. my ($self, $objid) = @_;
  47. if (exists $self->{objects}{$objid}) {
  48. return $self->{objects}{$objid};
  49. }
  50. return undef;
  51. }
  52. {
  53. my %format; # Cache of result
  54. sub get_format {
  55. my ($file) = @_;
  56. if (exists $format{$file}) {
  57. return $format{$file};
  58. } else {
  59. local $ENV{LC_ALL} = "C";
  60. open(P, "-|", "objdump", "-a", "--", $file)
  61. || syserr(_g("cannot fork for objdump"));
  62. while (<P>) {
  63. chomp;
  64. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  65. $format{$file} = $1;
  66. return $format{$file};
  67. }
  68. }
  69. close(P) or subprocerr(sprintf(_g("objdump on \`%s'"), $file));
  70. }
  71. }
  72. }
  73. sub is_elf {
  74. my ($file) = @_;
  75. open(FILE, "<", $file) ||
  76. syserr(sprintf(_g("Can't open %s for test: %s"), $file, $!));
  77. my ($header, $result) = ("", 0);
  78. if (read(FILE, $header, 4) == 4) {
  79. $result = 1 if ($header =~ /^\177ELF$/);
  80. }
  81. close(FILE);
  82. return $result;
  83. }
  84. package Dpkg::Shlibs::Objdump::Object;
  85. use Dpkg::Gettext;
  86. use Dpkg::ErrorHandling qw(syserr warning);
  87. sub new {
  88. my $this = shift;
  89. my $file = shift || '';
  90. my $class = ref($this) || $this;
  91. my $self = {};
  92. bless $self, $class;
  93. $self->reset;
  94. if ($file) {
  95. $self->_read($file);
  96. }
  97. return $self;
  98. }
  99. sub reset {
  100. my ($self) = @_;
  101. $self->{file} = '';
  102. $self->{id} = '';
  103. $self->{SONAME} = '';
  104. $self->{NEEDED} = [];
  105. $self->{RPATH} = [];
  106. $self->{dynsyms} = {};
  107. return $self;
  108. }
  109. sub _read {
  110. my ($self, $file) = @_;
  111. $file ||= $self->{file};
  112. return unless $file;
  113. $self->reset;
  114. $self->{file} = $file;
  115. local $ENV{LC_ALL} = 'C';
  116. open(my $objdump, "-|", "objdump", "-w", "-p", "-T", $file)
  117. || syserr(sprintf(_g("Can't execute objdump: %s"), $!));
  118. my $ret = $self->_parse($objdump);
  119. close($objdump);
  120. return $ret;
  121. }
  122. sub _parse {
  123. my ($self, $fh) = @_;
  124. my $section = "none";
  125. while (defined($_ = <$fh>)) {
  126. chomp;
  127. next if /^\s*$/;
  128. if (/^DYNAMIC SYMBOL TABLE:/) {
  129. $section = "dynsym";
  130. next;
  131. } elsif (/^Dynamic Section:/) {
  132. $section = "dyninfo";
  133. next;
  134. } elsif (/^Program Header:/) {
  135. $section = "header";
  136. next;
  137. } elsif (/^Version definitions:/) {
  138. $section = "verdef";
  139. next;
  140. } elsif (/^Version References:/) {
  141. $section = "verref";
  142. next;
  143. }
  144. if ($section eq "dynsym") {
  145. $self->parse_dynamic_symbol($_);
  146. } elsif ($section eq "dyninfo") {
  147. if (/^\s*NEEDED\s+(\S+)/) {
  148. push @{$self->{NEEDED}}, $1;
  149. } elsif (/^\s*SONAME\s+(\S+)/) {
  150. $self->{SONAME} = $1;
  151. } elsif (/^\s*HASH\s+(\S+)/) {
  152. $self->{HASH} = $1;
  153. } elsif (/^\s*GNU_HASH\s+(\S+)/) {
  154. $self->{GNU_HASH} = $1;
  155. } elsif (/^\s*RPATH\s+(\S+)/) {
  156. push @{$self->{RPATH}}, split (/:/, $1);
  157. }
  158. } elsif ($section eq "none") {
  159. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  160. $self->{format} = $1;
  161. }
  162. }
  163. }
  164. return $section ne "none";
  165. }
  166. # Output format of objdump -w -T
  167. #
  168. # /lib/libc.so.6: file format elf32-i386
  169. #
  170. # DYNAMIC SYMBOL TABLE:
  171. # 00056ef0 g DF .text 000000db GLIBC_2.2 getwchar
  172. # 00000000 g DO *ABS* 00000000 GCC_3.0 GCC_3.0
  173. # 00069960 w DF .text 0000001e GLIBC_2.0 bcmp
  174. # 00000000 w D *UND* 00000000 _pthread_cleanup_pop_restore
  175. # 0000b788 g DF .text 0000008e Base .protected xine_close
  176. # 0000b788 g DF .text 0000008e .hidden IA__g_free
  177. # | ||||||| | | | |
  178. # | ||||||| | | Version str (.visibility) + Symbol name
  179. # | ||||||| | Alignment
  180. # | ||||||| Section name (or *UND* for an undefined symbol)
  181. # | ||||||F=Function,f=file,O=object
  182. # | |||||d=debugging,D=dynamic
  183. # | ||||I=Indirect
  184. # | |||W=warning
  185. # | ||C=constructor
  186. # | |w=weak
  187. # | g=global,l=local,!=both global/local
  188. # Size of the symbol
  189. #
  190. # GLIBC_2.2 is the version string associated to the symbol
  191. # (GLIBC_2.2) is the same but the symbol is hidden, a newer version of the
  192. # symbol exist
  193. sub parse_dynamic_symbol {
  194. my ($self, $line) = @_;
  195. my $vis_re = '(\.protected|\.hidden|\.internal|0x\S+)';
  196. if ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+\s+(\S+)?(?:(?:\s+$vis_re)?\s+(\S+))/) {
  197. my ($flags, $sect, $ver, $vis, $name) = ($1, $2, $3, $4, $5);
  198. # Special case if version is missing but extra visibility
  199. # attribute replaces it in the match
  200. if (defined($ver) and $ver =~ /^$vis_re$/) {
  201. $vis = $ver;
  202. $ver = '';
  203. }
  204. # Cleanup visibility field
  205. $vis =~ s/^\.// if defined($vis);
  206. my $symbol = {
  207. name => $name,
  208. version => defined($ver) ? $ver : '',
  209. section => $sect,
  210. dynamic => substr($flags, 5, 1) eq "D",
  211. debug => substr($flags, 5, 1) eq "d",
  212. type => substr($flags, 6, 1),
  213. weak => substr($flags, 1, 1) eq "w",
  214. local => substr($flags, 0, 1) eq "l",
  215. global => substr($flags, 0, 1) eq "g",
  216. visibility => defined($vis) ? $vis : '',
  217. hidden => '',
  218. defined => $sect ne '*UND*'
  219. };
  220. # Handle hidden symbols
  221. if (defined($ver) and $ver =~ /^\((.*)\)$/) {
  222. $ver = $1;
  223. $symbol->{version} = $1;
  224. $symbol->{hidden} = 1;
  225. }
  226. # Register symbol
  227. $self->add_dynamic_symbol($symbol);
  228. } elsif ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+/) {
  229. # Same start but no version and no symbol ... just ignore
  230. } else {
  231. warning(sprintf(_g("Couldn't parse dynamic symbol definition: %s"), $line));
  232. }
  233. }
  234. sub add_dynamic_symbol {
  235. my ($self, $symbol) = @_;
  236. $symbol->{soname} = $self->{SONAME};
  237. if ($symbol->{version}) {
  238. $self->{dynsyms}{$symbol->{name} . '@' . $symbol->{version}} = $symbol;
  239. } else {
  240. $self->{dynsyms}{$symbol->{name}} = $symbol;
  241. }
  242. }
  243. sub get_id {
  244. my $self = shift;
  245. return $self->{SONAME} || $self->{file};
  246. }
  247. sub get_symbol {
  248. my ($self, $name) = @_;
  249. if (exists $self->{dynsyms}{$name}) {
  250. return $self->{dynsyms}{$name};
  251. }
  252. return undef;
  253. }
  254. sub get_exported_dynamic_symbols {
  255. my ($self) = @_;
  256. return grep { $_->{defined} && $_->{dynamic} && !$_->{local} }
  257. values %{$self->{dynsyms}};
  258. }
  259. sub get_undefined_dynamic_symbols {
  260. my ($self) = @_;
  261. return grep { (!$_->{defined}) && $_->{dynamic} }
  262. values %{$self->{dynsyms}};
  263. }
  264. sub get_needed_libraries {
  265. my $self = shift;
  266. return @{$self->{NEEDED}};
  267. }
  268. 1;