Objdump.pm 9.7 KB

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