Objdump.pm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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", "-f", "-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. } elsif (/^architecture:\s*\S+,\s*flags:\s*\S+\s*$/) {
  162. # Parse 2 lines of "-f"
  163. # architecture: i386, flags 0x00000112:
  164. # EXEC_P, HAS_SYMS, D_PAGED
  165. # start address 0x08049b50
  166. $self->{flags}{$_} = 1 foreach (split(/,\s*/, <$fh>));
  167. }
  168. }
  169. }
  170. return $section ne "none";
  171. }
  172. # Output format of objdump -w -T
  173. #
  174. # /lib/libc.so.6: file format elf32-i386
  175. #
  176. # DYNAMIC SYMBOL TABLE:
  177. # 00056ef0 g DF .text 000000db GLIBC_2.2 getwchar
  178. # 00000000 g DO *ABS* 00000000 GCC_3.0 GCC_3.0
  179. # 00069960 w DF .text 0000001e GLIBC_2.0 bcmp
  180. # 00000000 w D *UND* 00000000 _pthread_cleanup_pop_restore
  181. # 0000b788 g DF .text 0000008e Base .protected xine_close
  182. # 0000b788 g DF .text 0000008e .hidden IA__g_free
  183. # | ||||||| | | | |
  184. # | ||||||| | | Version str (.visibility) + Symbol name
  185. # | ||||||| | Alignment
  186. # | ||||||| Section name (or *UND* for an undefined symbol)
  187. # | ||||||F=Function,f=file,O=object
  188. # | |||||d=debugging,D=dynamic
  189. # | ||||I=Indirect
  190. # | |||W=warning
  191. # | ||C=constructor
  192. # | |w=weak
  193. # | g=global,l=local,!=both global/local
  194. # Size of the symbol
  195. #
  196. # GLIBC_2.2 is the version string associated to the symbol
  197. # (GLIBC_2.2) is the same but the symbol is hidden, a newer version of the
  198. # symbol exist
  199. sub parse_dynamic_symbol {
  200. my ($self, $line) = @_;
  201. my $vis_re = '(\.protected|\.hidden|\.internal|0x\S+)';
  202. if ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+\s+(\S+)?(?:(?:\s+$vis_re)?\s+(\S+))/) {
  203. my ($flags, $sect, $ver, $vis, $name) = ($1, $2, $3, $4, $5);
  204. # Special case if version is missing but extra visibility
  205. # attribute replaces it in the match
  206. if (defined($ver) and $ver =~ /^$vis_re$/) {
  207. $vis = $ver;
  208. $ver = '';
  209. }
  210. # Cleanup visibility field
  211. $vis =~ s/^\.// if defined($vis);
  212. my $symbol = {
  213. name => $name,
  214. version => defined($ver) ? $ver : '',
  215. section => $sect,
  216. dynamic => substr($flags, 5, 1) eq "D",
  217. debug => substr($flags, 5, 1) eq "d",
  218. type => substr($flags, 6, 1),
  219. weak => substr($flags, 1, 1) eq "w",
  220. local => substr($flags, 0, 1) eq "l",
  221. global => substr($flags, 0, 1) eq "g",
  222. visibility => defined($vis) ? $vis : '',
  223. hidden => '',
  224. defined => $sect ne '*UND*'
  225. };
  226. # Handle hidden symbols
  227. if (defined($ver) and $ver =~ /^\((.*)\)$/) {
  228. $ver = $1;
  229. $symbol->{version} = $1;
  230. $symbol->{hidden} = 1;
  231. }
  232. # Register symbol
  233. $self->add_dynamic_symbol($symbol);
  234. } elsif ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+/) {
  235. # Same start but no version and no symbol ... just ignore
  236. } else {
  237. warning(sprintf(_g("Couldn't parse dynamic symbol definition: %s"), $line));
  238. }
  239. }
  240. sub add_dynamic_symbol {
  241. my ($self, $symbol) = @_;
  242. $symbol->{soname} = $self->{SONAME};
  243. if ($symbol->{version}) {
  244. $self->{dynsyms}{$symbol->{name} . '@' . $symbol->{version}} = $symbol;
  245. } else {
  246. $self->{dynsyms}{$symbol->{name}} = $symbol;
  247. }
  248. }
  249. sub get_id {
  250. my $self = shift;
  251. return $self->{SONAME} || $self->{file};
  252. }
  253. sub get_symbol {
  254. my ($self, $name) = @_;
  255. if (exists $self->{dynsyms}{$name}) {
  256. return $self->{dynsyms}{$name};
  257. }
  258. return undef;
  259. }
  260. sub get_exported_dynamic_symbols {
  261. my ($self) = @_;
  262. return grep { $_->{defined} && $_->{dynamic} && !$_->{local} }
  263. values %{$self->{dynsyms}};
  264. }
  265. sub get_undefined_dynamic_symbols {
  266. my ($self) = @_;
  267. return grep { (!$_->{defined}) && $_->{dynamic} }
  268. values %{$self->{dynsyms}};
  269. }
  270. sub get_needed_libraries {
  271. my $self = shift;
  272. return @{$self->{NEEDED}};
  273. }
  274. sub is_executable {
  275. my $self = shift;
  276. return exists $self->{flags}{EXEC_P} and $self->{flags}{EXEC_P};
  277. }
  278. sub is_public_library {
  279. my $self = shift;
  280. return exists $self->{flags}{DYNAMIC} and $self->{flags}{DYNAMIC}
  281. and exists $self->{SONAME} and $self->{SONAME};
  282. }
  283. 1;