distill.awk 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/awk -f
  2. # Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
  3. # Distills the disassembly as follows:
  4. # - Removes all lines except the disassembled instructions.
  5. # - For instructions that exceed 1 line (7 bytes), crams all the hex bytes
  6. # into a single line.
  7. # - Remove bad(or prefix only) instructions
  8. BEGIN {
  9. prev_addr = ""
  10. prev_hex = ""
  11. prev_mnemonic = ""
  12. bad_expr = "(\\(bad\\)|^rex|^.byte|^rep(z|nz)$|^lock$|^es$|^cs$|^ss$|^ds$|^fs$|^gs$|^data(16|32)$|^addr(16|32|64))"
  13. fwait_expr = "^9b "
  14. fwait_str="9b\tfwait"
  15. }
  16. /^ *[0-9a-f]+:/ {
  17. if (split($0, field, "\t") < 3) {
  18. # This is a continuation of the same insn.
  19. prev_hex = prev_hex field[2]
  20. } else {
  21. # Skip bad instructions
  22. if (match(prev_mnemonic, bad_expr))
  23. prev_addr = ""
  24. # Split fwait from other f* instructions
  25. if (match(prev_hex, fwait_expr) && prev_mnemonic != "fwait") {
  26. printf "%s\t%s\n", prev_addr, fwait_str
  27. sub(fwait_expr, "", prev_hex)
  28. }
  29. if (prev_addr != "")
  30. printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
  31. prev_addr = field[1]
  32. prev_hex = field[2]
  33. prev_mnemonic = field[3]
  34. }
  35. }
  36. END {
  37. if (prev_addr != "")
  38. printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
  39. }