Objdump.pm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. sub new {
  19. my $this = shift;
  20. my $class = ref($this) || $this;
  21. my $self = { 'objects' => {} };
  22. bless $self, $class;
  23. return $self;
  24. }
  25. sub parse {
  26. my ($self, $file) = @_;
  27. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  28. my $id = $obj->get_id;
  29. if ($id) {
  30. $self->{objects}{$id} = $obj;
  31. }
  32. return $id;
  33. }
  34. sub locate_symbol {
  35. my ($self, $name) = @_;
  36. foreach my $obj (values %{$self->{objects}}) {
  37. my $sym = $obj->get_symbol($name);
  38. if (defined($sym) && $sym->{defined}) {
  39. return $sym;
  40. }
  41. }
  42. return undef;
  43. }
  44. sub get_object {
  45. my ($self, $objid) = @_;
  46. if (exists $self->{objects}{$objid}) {
  47. return $self->{objects}{$objid};
  48. }
  49. return undef;
  50. }
  51. {
  52. my %format; # Cache of result
  53. sub get_format {
  54. my ($file) = @_;
  55. if (exists $format{$file}) {
  56. return $format{$file};
  57. } else {
  58. local $ENV{LC_ALL} = "C";
  59. open(P, "-|", "objdump", "-a", "--", $file)
  60. || syserr(_g("cannot fork for objdump"));
  61. while (<P>) {
  62. chomp;
  63. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  64. $format{$file} = $1;
  65. return $format{$file};
  66. }
  67. }
  68. close(P) or subprocerr(_g("objdump on \`%s'"), $file);
  69. }
  70. }
  71. }
  72. sub is_elf {
  73. my ($file) = @_;
  74. open(FILE, "<", $file) || syserr(_g("cannot read %s"), $file);
  75. my ($header, $result) = ("", 0);
  76. if (read(FILE, $header, 4) == 4) {
  77. $result = 1 if ($header =~ /^\177ELF$/);
  78. }
  79. close(FILE);
  80. return $result;
  81. }
  82. package Dpkg::Shlibs::Objdump::Object;
  83. use Dpkg::Gettext;
  84. use Dpkg::ErrorHandling qw(syserr warning);
  85. sub new {
  86. my $this = shift;
  87. my $file = shift || '';
  88. my $class = ref($this) || $this;
  89. my $self = {};
  90. bless $self, $class;
  91. $self->reset;
  92. if ($file) {
  93. $self->_read($file);
  94. }
  95. return $self;
  96. }
  97. sub reset {
  98. my ($self) = @_;
  99. $self->{file} = '';
  100. $self->{id} = '';
  101. $self->{SONAME} = '';
  102. $self->{HASH} = '';
  103. $self->{GNU_HASH} = '';
  104. $self->{SONAME} = '';
  105. $self->{NEEDED} = [];
  106. $self->{RPATH} = [];
  107. $self->{dynsyms} = {};
  108. $self->{flags} = {};
  109. $self->{dynrelocs} = {};
  110. return $self;
  111. }
  112. sub _read {
  113. my ($self, $file) = @_;
  114. $file ||= $self->{file};
  115. return unless $file;
  116. $self->reset;
  117. $self->{file} = $file;
  118. local $ENV{LC_ALL} = 'C';
  119. open(my $objdump, "-|", "objdump", "-w", "-f", "-p", "-T", "-R", $file)
  120. || syserr(_g("cannot fork %s"), "objdump");
  121. my $ret = $self->_parse($objdump);
  122. close($objdump);
  123. return $ret;
  124. }
  125. sub _parse {
  126. my ($self, $fh) = @_;
  127. my $section = "none";
  128. while (defined($_ = <$fh>)) {
  129. chomp;
  130. next if /^\s*$/;
  131. if (/^DYNAMIC SYMBOL TABLE:/) {
  132. $section = "dynsym";
  133. next;
  134. } elsif (/^DYNAMIC RELOCATION RECORDS/) {
  135. $section = "dynreloc";
  136. $_ = <$fh>; # Skip header
  137. next;
  138. } elsif (/^Dynamic Section:/) {
  139. $section = "dyninfo";
  140. next;
  141. } elsif (/^Program Header:/) {
  142. $section = "header";
  143. next;
  144. } elsif (/^Version definitions:/) {
  145. $section = "verdef";
  146. next;
  147. } elsif (/^Version References:/) {
  148. $section = "verref";
  149. next;
  150. }
  151. if ($section eq "dynsym") {
  152. $self->parse_dynamic_symbol($_);
  153. } elsif ($section eq "dynreloc") {
  154. if (/^\S+\s+(\S+)\s+(\S+)\s*$/) {
  155. $self->{dynrelocs}{$2} = $1;
  156. } else {
  157. warning(_g("Couldn't parse dynamic relocation record: %s"), $_);
  158. }
  159. } elsif ($section eq "dyninfo") {
  160. if (/^\s*NEEDED\s+(\S+)/) {
  161. push @{$self->{NEEDED}}, $1;
  162. } elsif (/^\s*SONAME\s+(\S+)/) {
  163. $self->{SONAME} = $1;
  164. } elsif (/^\s*HASH\s+(\S+)/) {
  165. $self->{HASH} = $1;
  166. } elsif (/^\s*GNU_HASH\s+(\S+)/) {
  167. $self->{GNU_HASH} = $1;
  168. } elsif (/^\s*RPATH\s+(\S+)/) {
  169. push @{$self->{RPATH}}, split (/:/, $1);
  170. }
  171. } elsif ($section eq "none") {
  172. if (/^\s*\S+:\s*file\s+format\s+(\S+)\s*$/) {
  173. $self->{format} = $1;
  174. } elsif (/^architecture:\s*\S+,\s*flags\s*\S+:\s*$/) {
  175. # Parse 2 lines of "-f"
  176. # architecture: i386, flags 0x00000112:
  177. # EXEC_P, HAS_SYMS, D_PAGED
  178. # start address 0x08049b50
  179. $_ = <$fh>;
  180. chomp;
  181. $self->{flags}{$_} = 1 foreach (split(/,\s*/));
  182. }
  183. }
  184. }
  185. # Update status of dynamic symbols given the relocations that have
  186. # been parsed after the symbols...
  187. $self->apply_relocations();
  188. return $section ne "none";
  189. }
  190. # Output format of objdump -w -T
  191. #
  192. # /lib/libc.so.6: file format elf32-i386
  193. #
  194. # DYNAMIC SYMBOL TABLE:
  195. # 00056ef0 g DF .text 000000db GLIBC_2.2 getwchar
  196. # 00000000 g DO *ABS* 00000000 GCC_3.0 GCC_3.0
  197. # 00069960 w DF .text 0000001e GLIBC_2.0 bcmp
  198. # 00000000 w D *UND* 00000000 _pthread_cleanup_pop_restore
  199. # 0000b788 g DF .text 0000008e Base .protected xine_close
  200. # 0000b788 g DF .text 0000008e .hidden IA__g_free
  201. # | ||||||| | | | |
  202. # | ||||||| | | Version str (.visibility) + Symbol name
  203. # | ||||||| | Alignment
  204. # | ||||||| Section name (or *UND* for an undefined symbol)
  205. # | ||||||F=Function,f=file,O=object
  206. # | |||||d=debugging,D=dynamic
  207. # | ||||I=Indirect
  208. # | |||W=warning
  209. # | ||C=constructor
  210. # | |w=weak
  211. # | g=global,l=local,!=both global/local
  212. # Size of the symbol
  213. #
  214. # GLIBC_2.2 is the version string associated to the symbol
  215. # (GLIBC_2.2) is the same but the symbol is hidden, a newer version of the
  216. # symbol exist
  217. sub parse_dynamic_symbol {
  218. my ($self, $line) = @_;
  219. my $vis_re = '(\.protected|\.hidden|\.internal|0x\S+)';
  220. if ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+\s+(\S+)?(?:(?:\s+$vis_re)?\s+(\S+))/) {
  221. my ($flags, $sect, $ver, $vis, $name) = ($1, $2, $3, $4, $5);
  222. # Special case if version is missing but extra visibility
  223. # attribute replaces it in the match
  224. if (defined($ver) and $ver =~ /^$vis_re$/) {
  225. $vis = $ver;
  226. $ver = '';
  227. }
  228. # Cleanup visibility field
  229. $vis =~ s/^\.// if defined($vis);
  230. my $symbol = {
  231. name => $name,
  232. version => defined($ver) ? $ver : '',
  233. section => $sect,
  234. dynamic => substr($flags, 5, 1) eq "D",
  235. debug => substr($flags, 5, 1) eq "d",
  236. type => substr($flags, 6, 1),
  237. weak => substr($flags, 1, 1) eq "w",
  238. local => substr($flags, 0, 1) eq "l",
  239. global => substr($flags, 0, 1) eq "g",
  240. visibility => defined($vis) ? $vis : '',
  241. hidden => '',
  242. defined => $sect ne '*UND*'
  243. };
  244. # Handle hidden symbols
  245. if (defined($ver) and $ver =~ /^\((.*)\)$/) {
  246. $ver = $1;
  247. $symbol->{version} = $1;
  248. $symbol->{hidden} = 1;
  249. }
  250. # Register symbol
  251. $self->add_dynamic_symbol($symbol);
  252. } elsif ($line =~ /^[0-9a-f]+ (.{7})\s+(\S+)\s+[0-9a-f]+/) {
  253. # Same start but no version and no symbol ... just ignore
  254. } elsif ($line =~ /^REG_G\d+\s+/) {
  255. # Ignore some s390-specific output like
  256. # REG_G6 g R *UND* 0000000000000000 #scratch
  257. } else {
  258. warning(_g("Couldn't parse dynamic symbol definition: %s"), $line);
  259. }
  260. }
  261. sub apply_relocations {
  262. my ($self) = @_;
  263. foreach my $sym (values %{$self->{dynsyms}}) {
  264. # We want to mark as undefined symbols those which are currently
  265. # defined but that depend on a copy relocation
  266. next if not $sym->{'defined'};
  267. next if not exists $self->{dynrelocs}{$sym->{name}};
  268. if ($self->{dynrelocs}{$sym->{name}} =~ /^R_.*_COPY$/) {
  269. $sym->{'defined'} = 0;
  270. }
  271. }
  272. }
  273. sub add_dynamic_symbol {
  274. my ($self, $symbol) = @_;
  275. $symbol->{objid} = $symbol->{soname} = $self->get_id();
  276. $symbol->{soname} =~ s{^.*/}{} unless $self->{SONAME};
  277. if ($symbol->{version}) {
  278. $self->{dynsyms}{$symbol->{name} . '@' . $symbol->{version}} = $symbol;
  279. } else {
  280. $self->{dynsyms}{$symbol->{name}} = $symbol;
  281. }
  282. }
  283. sub get_id {
  284. my $self = shift;
  285. return $self->{SONAME} || $self->{file};
  286. }
  287. sub get_symbol {
  288. my ($self, $name) = @_;
  289. if (exists $self->{dynsyms}{$name}) {
  290. return $self->{dynsyms}{$name};
  291. }
  292. return undef;
  293. }
  294. sub get_exported_dynamic_symbols {
  295. my ($self) = @_;
  296. return grep { $_->{defined} && $_->{dynamic} && !$_->{local} }
  297. values %{$self->{dynsyms}};
  298. }
  299. sub get_undefined_dynamic_symbols {
  300. my ($self) = @_;
  301. return grep { (!$_->{defined}) && $_->{dynamic} }
  302. values %{$self->{dynsyms}};
  303. }
  304. sub get_needed_libraries {
  305. my $self = shift;
  306. return @{$self->{NEEDED}};
  307. }
  308. sub is_executable {
  309. my $self = shift;
  310. return exists $self->{flags}{EXEC_P} && $self->{flags}{EXEC_P};
  311. }
  312. sub is_public_library {
  313. my $self = shift;
  314. return exists $self->{flags}{DYNAMIC} && $self->{flags}{DYNAMIC}
  315. && exists $self->{SONAME} && $self->{SONAME};
  316. }
  317. 1;