Objdump.pm 6.0 KB

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