dis.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #reference implementation: https://riscvasm.lucasteske.dev/
  2. import elf
  3. import mem
  4. import opcode
  5. import sys
  6. def hexstr(num,size):
  7. if num < 0:
  8. num += 2**32
  9. h = hex(num)[2:]
  10. os = '0' * (size - len(h)) + h
  11. return os
  12. def binstr(number):
  13. binary = bin(number)[2:]
  14. while len(binary) <32:
  15. binary = '0' + binary
  16. return binary
  17. def decode_r(instr):
  18. rd = opcode.get_rd(instr)
  19. rs1 = opcode.get_rs1(instr)
  20. rs2 = opcode.get_rs2(instr)
  21. return '{}, {}, {}'.format(opcode.name_register(rd),opcode.name_register(rs1),opcode.name_register(rs2))
  22. def decode_i(instr):
  23. rd = opcode.get_rd(instr)
  24. rs1 = opcode.get_rs1(instr)
  25. rs2 = opcode.get_rs2(instr)
  26. imm = opcode.get_imm12(instr)
  27. return '{}, {}, {}'.format(opcode.name_register(rd),opcode.name_register(rs1),imm)
  28. def decode_s(instr):
  29. rd = opcode.get_rd(instr)
  30. rs1 = opcode.get_rs1(instr)
  31. rs2 = opcode.get_rs2(instr)
  32. imm = opcode.get_simm12(instr)
  33. return '{}, {}({})'.format(opcode.name_register(rs1),imm,opcode.name_register(rs2))
  34. def decode_u(instr):
  35. rd = opcode.get_rd(instr)
  36. rs1 = opcode.get_rs1(instr)
  37. rs2 = opcode.get_rs2(instr)
  38. imm = opcode.get_imm20(instr)
  39. return '{}, {}'.format(opcode.name_register(rd),imm)
  40. def decode_j(instr):
  41. rd = opcode.get_rd(instr)
  42. rs1 = opcode.get_rs1(instr)
  43. rs2 = opcode.get_rs2(instr)
  44. imm = opcode.get_jimm20(instr)
  45. return '{}, {}'.format(opcode.name_register(rd),hexstr(imm,8))
  46. def decode_b(instr):
  47. rd = opcode.get_rd(instr)
  48. rs1 = opcode.get_rs1(instr)
  49. rs2 = opcode.get_rs2(instr)
  50. imm = opcode.get_bimm12(instr)
  51. return '{}, {}'.format(opcode.name_register(rd),hexstr(imm,8))
  52. def decode_l(instr):
  53. rd = opcode.get_rd(instr)
  54. rs1 = opcode.get_rs1(instr)
  55. rs2 = opcode.get_rs2(instr)
  56. imm = opcode.get_imm12(instr)
  57. return '{}, {}({})'.format(opcode.name_register(rd),imm,opcode.name_register(rs1))
  58. def instr_to_string(instr):
  59. os = ''
  60. op = opcode.match_opcode(instr,'ic')
  61. if op:
  62. os += op[0]+ ' '
  63. if op[1] == 'i':
  64. os += decode_i(instr)
  65. if op[1] == 'l':
  66. os += decode_l(instr)
  67. if op[1] == 'j':
  68. os += decode_j(instr)
  69. if op[1] == 'r':
  70. os += decode_r(instr)
  71. if op[1] == 'u':
  72. os += decode_u(instr)
  73. if op[1] == 's':
  74. os += decode_s(instr)
  75. if op[1] == 'b':
  76. os += decode_b(instr)
  77. return os
  78. return 'Unknown'
  79. def fetch(pc,mem):
  80. ir = mem.read(pc) + mem.read(pc+1) * 256
  81. size = 2
  82. if ir & 3 == 3:
  83. ir += mem.read(pc+2) * 65536 + mem.read(pc+3) * 16777216
  84. size += 2
  85. return size,ir
  86. if __name__ == '__main__':
  87. code = elf.elf_file(sys.argv[1])
  88. code.load()
  89. m = mem.mem()
  90. for i in range(len(code.sheaders)):
  91. section = code.sheaders[i]
  92. if section.sh_type == 1:
  93. if (section.sh_flags & 6)==6:
  94. m.create_region(section.sh_addr,section.sh_size)
  95. buff = code.read_segment(i)
  96. for j in range(len(buff)):
  97. m.write(section.sh_addr+j,buff[j])
  98. print('{}'.format(section.name_text))
  99. pc = section.sh_addr
  100. while pc - section.sh_addr < section.sh_size:
  101. size,ir = fetch(pc,m)
  102. if size == 2:
  103. sys.stdout.write('{}: {} {}\n'.format(hexstr(pc,8),hexstr(ir,size*2),instr_to_string(ir)))
  104. if size == 4:
  105. sys.stdout.write('{}: {} {}\n'.format(hexstr(pc,8),hexstr(ir,size*2),instr_to_string(ir)))
  106. pc += size