Objdump.pm 8.3 KB

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