Objdump.pm 11 KB

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