Objdump.pm 8.3 KB

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