Objdump.pm 10 KB

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