distill.awk 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. # Symbol entry
  18. printf("%s%s\n", $2, $1)
  19. }
  20. /^ *[0-9a-f]+:/ {
  21. if (split($0, field, "\t") < 3) {
  22. # This is a continuation of the same insn.
  23. prev_hex = prev_hex field[2]
  24. } else {
  25. # Skip bad instructions
  26. if (match(prev_mnemonic, bad_expr))
  27. prev_addr = ""
  28. # Split fwait from other f* instructions
  29. if (match(prev_hex, fwait_expr) && prev_mnemonic != "fwait") {
  30. printf "%s\t%s\n", prev_addr, fwait_str
  31. sub(fwait_expr, "", prev_hex)
  32. }
  33. if (prev_addr != "")
  34. printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
  35. prev_addr = field[1]
  36. prev_hex = field[2]
  37. prev_mnemonic = field[3]
  38. }
  39. }
  40. END {
  41. if (prev_addr != "")
  42. printf "%s\t%s\t%s\n", prev_addr, prev_hex, prev_mnemonic
  43. }