disas-objdump.pl 2.6 KB

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