Objdump.pm 6.1 KB

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