Objdump.pm 6.7 KB

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