disassemble 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #! perl
  2. use B::Disassembler qw(disassemble_fh);
  3. use FileHandle;
  4. =pod
  5. =head1 NAME
  6. disassemble
  7. =head1 SYNOPSIS
  8. disassemble [--bare] [bytecode.plc] > bytecode.asm
  9. assemble bytecode.asm > bytecode.plc
  10. =head1 DESCRIPTION
  11. Decompiles binary bytecode to readable and recompilable
  12. bytecode assembler.
  13. byteocde is a binary file wih either the magic 4 bytes 'PLBC'
  14. at the start, or something like "#! /usr/bin/perl\n
  15. use ByteLoader '0.07'"
  16. Without the filename uses STDIN.
  17. =head1 OPTION --bare
  18. Without the option --bare the output will be commented.
  19. Note that older assembler, before B::Assembler version 0.07, i.e.
  20. up to perl-5.8.x, will not be able to parse this commented
  21. assembler.
  22. But --bare is only optional, so the default is not backwards
  23. compatible. Rationale: Disassembling is primarily done to make
  24. binary bytecode readable, and not necessarily recompilable with
  25. older assemblers.
  26. =cut
  27. BEGIN {
  28. if ($B::Disassembler::VERSION > '1.05') {
  29. B::Disassembler->import('print_insn');
  30. } else {
  31. sub print_insn {
  32. my ($insn, $arg) = @_;
  33. if (defined($arg)) {
  34. printf "%s %s\n", $insn, $arg;
  35. } else {
  36. print $insn, "\n";
  37. }
  38. }
  39. }
  40. }
  41. my $verbose = '1';
  42. if ($ARGV[0] eq "--bare") {
  43. shift;
  44. $verbose = 0;
  45. *print_insn = *B::Disassembler::print_insn_bare;
  46. }
  47. my $fh;
  48. if (@ARGV == 0) {
  49. $fh = \*STDIN;
  50. } elsif (@ARGV == 1) {
  51. $fh = new FileHandle "<$ARGV[0]";
  52. } else {
  53. die "Usage: disassemble [--bare] [filename]\n";
  54. }
  55. disassemble_fh($fh, \&print_insn, $verbose);