disas-objdump.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env perl
  2. use warnings;
  3. use File::Temp qw/ tempfile /;
  4. use Getopt::Long;
  5. # Default to the system objdump if a cross-compiler edition not given.
  6. my $aobjdump = "objdump";
  7. my $hobjdump = "";
  8. my $tobjdump = "";
  9. my $hmachine = "";
  10. my $tmachine = "";
  11. GetOptions ('O|objdump=s' => \$aobjdump,
  12. 'host-objdump=s' => \$hobjdump,
  13. 'target-objdump=s' => \$tobjdump,
  14. 'h|host-machine=s' => \$hmachine,
  15. 't|target-machine=s' => \$tmachine);
  16. # But we can't default the machines. Sanity check that we've at least one.
  17. die "No host or target machine type" if !$hmachine && !$tmachine;
  18. # Reuse one temp file for all of the hunks.
  19. my ($outh, $outname) = tempfile();
  20. binmode($outh);
  21. END { unlink $outname; }
  22. # Pre-construct the command-lines for executing the dump.
  23. sub mkobjcommand ($$) {
  24. my ($cmd, $mach) = @_;
  25. return 0 if !$mach;
  26. $cmd = $aobjdump if !$cmd;
  27. return "$cmd -m $mach --disassemble-all -b binary";
  28. }
  29. $objdump[1] = mkobjcommand($hobjdump, $hmachine);
  30. $objdump[2] = mkobjcommand($tobjdump, $tmachine);
  31. # Zero-initialize current dumping state.
  32. my $mem = "";
  33. my $inobjd = 0;
  34. my $vma = 0;
  35. sub objcommand {
  36. my $ret = $objdump[$inobjd];
  37. if (!$ret) {
  38. die "Host machine type not specified" if $inobjd == 1;
  39. die "Target machine type not specified" if $inobjd == 2;
  40. die "Internal error";
  41. }
  42. return $ret;
  43. }
  44. while (<>) {
  45. # Collect the data from the relevant OBJD-* lines ...
  46. if (/^OBJD-H: /) {
  47. die "Internal error" if $inobjd == 2;
  48. $mem = $mem . pack("H*", substr($_, 8, -1));
  49. $inobjd = 1;
  50. } elsif (/^OBJD-T: /) {
  51. die "Internal error" if $inobjd == 1;
  52. $mem = $mem . pack("H*", substr($_, 8, -1));
  53. $inobjd = 2;
  54. }
  55. # ... which will always be followed by a blank line,
  56. # at which point we should produce our dump.
  57. elsif ($inobjd) {
  58. # Rewrite the temp file in one go; it will usually be small.
  59. sysseek $outh, 0, 0;
  60. truncate $outh, 0;
  61. syswrite $outh, $mem;
  62. my $cmd = objcommand();
  63. $cmd = $cmd . " --adjust-vma=" . $vma if $vma;
  64. $cmd = $cmd . " " . $outname;
  65. # Pipe from objdump...
  66. open IN, "-|", $cmd;
  67. # ... copying all but the first 7 lines of boilerplate to our stdout.
  68. my $i = 0;
  69. while (<IN>) {
  70. print if (++$i > 7);
  71. }
  72. close IN;
  73. print "\n";
  74. $mem = "";
  75. $inobjd = 0;
  76. $vma = 0;
  77. }
  78. # The line before "OBJD-*" will be of the form "0x<hex>+: +\n".
  79. # Extract the value for passing to --adjust-vma.
  80. elsif (/^(0x[0-9a-fA-F]+):\s*$/) {
  81. $vma = $1;
  82. print;
  83. } else {
  84. print;
  85. }
  86. }