Objdump.pm 9.8 KB

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