Objdump.pm 6.6 KB

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