objdump_reformat.awk 1.3 KB

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