Objdump.pm 9.8 KB

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