Objdump.pm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. sub new {
  86. my $this = shift;
  87. my $file = shift || '';
  88. my $class = ref($this) || $this;
  89. my $self = {};
  90. bless $self, $class;
  91. $self->reset;
  92. if ($file) {
  93. $self->_read($file);
  94. }
  95. return $self;
  96. }
  97. sub reset {
  98. my ($self) = @_;
  99. $self->{file} = '';
  100. $self->{id} = '';
  101. $self->{SONAME} = '';
  102. $self->{NEEDED} = [];
  103. $self->{RPATH} = [];
  104. $self->{dynsyms} = {};
  105. return $self;
  106. }
  107. sub _read {
  108. my ($self, $file) = @_;
  109. $file ||= $self->{file};
  110. return unless $file;
  111. $self->reset;
  112. $self->{file} = $file;
  113. local $ENV{LC_ALL} = 'C';
  114. open(my $objdump, "-|", "objdump", "-w", "-p", "-T", $file)
  115. || syserr(sprintf(_g("Can't execute objdump: %s"), $!));
  116. my $ret = $self->_parse($objdump);
  117. close($objdump);
  118. return $ret;
  119. }
  120. sub _parse {
  121. my ($self, $fh) = @_;
  122. my $section = "none";
  123. while (defined($_ = <$fh>)) {
  124. chomp;
  125. next if /^\s*$/;
  126. if (/^DYNAMIC SYMBOL TABLE:/) {
  127. $section = "dynsym";
  128. next;
  129. } elsif (/^Dynamic Section:/) {
  130. $section = "dyninfo";
  131. next;
  132. } elsif (/^Program Header:/) {
  133. $section = "header";
  134. next;
  135. } elsif (/^Version definitions:/) {
  136. $section = "verdef";
  137. next;
  138. } elsif (/^Version References:/) {
  139. $section = "verref";
  140. next;
  141. }
  142. if ($section eq "dynsym") {
  143. $self->parse_dynamic_symbol($_);
  144. } elsif ($section eq "dyninfo") {
  145. if (/^\s*NEEDED\s+(\S+)/) {
  146. push @{$self->{NEEDED}}, $1;
  147. } elsif (/^\s*SONAME\s+(\S+)/) {
  148. $self->{SONAME} = $1;
  149. } elsif (/^\s*HASH\s+(\S+)/) {
  150. $self->{HASH} = $1;
  151. } elsif (/^\s*GNU_HASH\s+(\S+)/) {
  152. $self->{GNU_HASH} = $1;
  153. } elsif (/^\s*RPATH\s+(\S+)/) {
  154. push @{$self->{RPATH}}, split (/:/, $1);
  155. }
  156. } elsif ($section eq "none") {
  157. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  158. $self->{format} = $1;
  159. }
  160. }
  161. }
  162. return $section ne "none";
  163. }
  164. # Output format of objdump -w -T
  165. #
  166. # /lib/libc.so.6: file format elf32-i386
  167. #
  168. # DYNAMIC SYMBOL TABLE:
  169. # 00056ef0 g DF .text 000000db GLIBC_2.2 getwchar
  170. # 00000000 g DO *ABS* 00000000 GCC_3.0 GCC_3.0
  171. # 00069960 w DF .text 0000001e GLIBC_2.0 bcmp
  172. # 00000000 w D *UND* 00000000 _pthread_cleanup_pop_restore
  173. # 0000b788 g DF .text 0000008e Base .protected xine_close
  174. # 0000b788 g DF .text 0000008e .hidden IA__g_free
  175. # | ||||||| | | | |
  176. # | ||||||| | | Version str (.visibility) + Symbol name
  177. # | ||||||| | Alignment
  178. # | ||||||| Section name (or *UND* for an undefined symbol)
  179. # | ||||||F=Function,f=file,O=object
  180. # | |||||d=debugging,D=dynamic
  181. # | ||||I=Indirect
  182. # | |||W=warning
  183. # | ||C=constructor
  184. # | |w=weak
  185. # | g=global,l=local,!=both global/local
  186. # Size of the symbol
  187. #
  188. # GLIBC_2.2 is the version string associated to the symbol
  189. # (GLIBC_2.2) is the same but the symbol is hidden, a newer version of the
  190. # symbol exist
  191. sub parse_dynamic_symbol {
  192. my ($self, $line) = @_;
  193. my $vis = '(?:\.protected|\.hidden|\.internal|0x\S+)';
  194. if ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+\s+(\S+)?(?:(?:\s+$vis)?\s+(\S+))/) {
  195. my ($flags, $sect, $ver, $name) = ($1, $2, $3, $4);
  196. # Special case if version is missing but extra visibility
  197. # attribute replaces it in the match
  198. $ver = '' if defined($ver) and $ver =~ /^$vis$/;
  199. my $symbol = {
  200. name => $name,
  201. version => defined($ver) ? $ver : '',
  202. section => $sect,
  203. dynamic => substr($flags, 5, 1) eq "D",
  204. debug => substr($flags, 5, 1) eq "d",
  205. type => substr($flags, 6, 1),
  206. weak => substr($flags, 1, 1) eq "w",
  207. hidden => '',
  208. defined => $sect ne '*UND*'
  209. };
  210. # Handle hidden symbols
  211. if (defined($ver) and $ver =~ /^\((.*)\)$/) {
  212. $ver = $1;
  213. $symbol->{version} = $1;
  214. $symbol->{hidden} = 1;
  215. }
  216. # Register symbol
  217. $self->add_dynamic_symbol($symbol);
  218. } elsif ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+/) {
  219. # Same start but no version and no symbol ... just ignore
  220. } else {
  221. warning(sprintf(_g("Couldn't parse dynamic symbol definition: %s"), $line));
  222. }
  223. }
  224. sub add_dynamic_symbol {
  225. my ($self, $symbol) = @_;
  226. $symbol->{soname} = $self->{SONAME};
  227. if ($symbol->{version}) {
  228. $self->{dynsyms}{$symbol->{name} . '@' . $symbol->{version}} = $symbol;
  229. } else {
  230. $self->{dynsyms}{$symbol->{name}} = $symbol;
  231. }
  232. }
  233. sub get_id {
  234. my $self = shift;
  235. return $self->{SONAME} || $self->{file};
  236. }
  237. sub get_symbol {
  238. my ($self, $name) = @_;
  239. if (exists $self->{dynsyms}{$name}) {
  240. return $self->{dynsyms}{$name};
  241. }
  242. return undef;
  243. }
  244. sub get_exported_dynamic_symbols {
  245. my ($self) = @_;
  246. return grep { $_->{defined} && $_->{dynamic} }
  247. values %{$self->{dynsyms}};
  248. }
  249. sub get_undefined_dynamic_symbols {
  250. my ($self) = @_;
  251. return grep { (!$_->{defined}) && $_->{dynamic} }
  252. values %{$self->{dynsyms}};
  253. }
  254. sub get_needed_libraries {
  255. my $self = shift;
  256. return @{$self->{NEEDED}};
  257. }
  258. 1;