Objdump.pm 11 KB

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