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