Objdump.pm 9.9 KB

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