| 1 |
|
| 2 |
|
| 3 |
import sys |
| 4 |
import re |
| 5 |
import argparse |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
unique_set = set() |
| 14 |
|
| 15 |
|
| 16 |
def num2prefix_hex(x, prefix="#"): |
| 17 |
if x.startswith("0x") or x.startswith("-0x") or x == "0": |
| 18 |
x = prefix + x |
| 19 |
if x.isdigit() or (x.startswith("-") and x[1:].isdigit()): |
| 20 |
x = prefix + hex(int(x)) |
| 21 |
return x |
| 22 |
|
| 23 |
def op2prefix_hex(x): |
| 24 |
x = num2prefix_hex(x) |
| 25 |
if "]" in x: |
| 26 |
xs = x.split("]") |
| 27 |
if xs[1].isdigit() or xs[1].startswith('-'): |
| 28 |
x = xs[0] + "]" + num2prefix_hex(xs[1]) |
| 29 |
return x |
| 30 |
|
| 31 |
def gen(filename): |
| 32 |
with open(filename, "r") as f: |
| 33 |
for line in f: |
| 34 |
caps = re.findall( |
| 35 |
r"([0-9a-f]+):\s+([0-9a-f]+) ([0-9a-f]+) ([0-9a-f]+)? ([0-9a-f]+)?\s+" |
| 36 |
r"(\S+) (\S+)", |
| 37 |
line, |
| 38 |
) |
| 39 |
if not caps: |
| 40 |
continue |
| 41 |
caps = caps[0] |
| 42 |
addr = int(caps[0], 16) |
| 43 |
hexstr = caps[1:5] |
| 44 |
mnemonic: str = caps[5] |
| 45 |
operands = caps[6] |
| 46 |
|
| 47 |
def try_dedisp(x): |
| 48 |
try: |
| 49 |
disp = int(x, 16) |
| 50 |
if disp > 0x80000000: |
| 51 |
x = hex(disp - addr) |
| 52 |
return x |
| 53 |
except ValueError: |
| 54 |
pass |
| 55 |
return x |
| 56 |
|
| 57 |
def is_hex_string(s: str) -> bool: |
| 58 |
if not s.isalnum(): |
| 59 |
return False |
| 60 |
return all(c.isdigit() or c.lower() in "abcdef" for c in s) and any( |
| 61 |
c.lower() in "abcdef" for c in s |
| 62 |
) |
| 63 |
|
| 64 |
hexstr = ",".join(f"0x{x}" for x in hexstr if x) |
| 65 |
fun = re.match(r"\s*<.+>\s*", operands) |
| 66 |
|
| 67 |
if any( |
| 68 |
[ |
| 69 |
mnemonic.startswith(pre) |
| 70 |
for pre in [ |
| 71 |
"mtcr", |
| 72 |
"mfcr", |
| 73 |
"st.a", |
| 74 |
"st.b", |
| 75 |
"st.d", |
| 76 |
"st.w", |
| 77 |
"ld.a", |
| 78 |
"ld.b", |
| 79 |
"ld.d", |
| 80 |
"ld.w", |
| 81 |
] |
| 82 |
] |
| 83 |
): |
| 84 |
|
| 85 |
continue |
| 86 |
|
| 87 |
ops = operands.split(",") |
| 88 |
if ( |
| 89 |
any( |
| 90 |
[mnemonic.startswith(pre) for pre in ["j", "call", "loop", "fcall"]] |
| 91 |
) |
| 92 |
or fun |
| 93 |
): |
| 94 |
re.sub(r"\s*<.+>\s*", "", operands) |
| 95 |
|
| 96 |
ops = list(map(try_dedisp, ops)) |
| 97 |
|
| 98 |
for i, x in enumerate(ops): |
| 99 |
if is_hex_string(x) and not x.startswith("0x"): |
| 100 |
x = "#0x" + x |
| 101 |
x = op2prefix_hex(x) |
| 102 |
ops[i] = x |
| 103 |
|
| 104 |
operands = ", ".join(ops) |
| 105 |
operands = operands.replace("%", "") |
| 106 |
unique_set.add(f"{hexstr.ljust(19)} = {mnemonic}\t{operands}") |
| 107 |
|
| 108 |
print("# CS_ARCH_TRICORE, CS_MODE_TRICORE_162, None") |
| 109 |
print("\n".join(unique_set)) |
| 110 |
|
| 111 |
def att2intel(filename): |
| 112 |
with open(filename, "r") as fp: |
| 113 |
lines = [] |
| 114 |
for line in fp.readlines(): |
| 115 |
if not '=' in line: |
| 116 |
lines.append(line) |
| 117 |
continue |
| 118 |
insn = line.split('=') |
| 119 |
hexstr = insn[0] |
| 120 |
insn = insn[1] |
| 121 |
ops = insn.strip().split(', ') |
| 122 |
ops = ops[0].split('\t') + ops[1:] |
| 123 |
mnemonic = ops[0] |
| 124 |
ops = ops[1:] |
| 125 |
for i,op in enumerate(ops): |
| 126 |
op = op.strip() |
| 127 |
op = op2prefix_hex(op) |
| 128 |
ops[i] = op |
| 129 |
operands = ", ".join(ops) |
| 130 |
lines.append(f"{hexstr.ljust(19)} = {mnemonic}\t{operands}") |
| 131 |
print('\n'.join(lines)) |
| 132 |
|
| 133 |
def main(): |
| 134 |
parser = argparse.ArgumentParser(description="Convert objdump's output to .s.cs test file") |
| 135 |
parser.add_argument('input', type=Path, help='input file path') |
| 136 |
parser.add_argument('--intel', action='store_true', help='convert .s.cs file to intel syntax') |
| 137 |
args = parser.parse_args() |
| 138 |
if not args.intel: |
| 139 |
gen(args.input) |
| 140 |
else: |
| 141 |
att2intel(args.input) |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == "__main__": |
| 145 |
main() |