-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenlib.py
More file actions
executable file
·47 lines (33 loc) · 1.39 KB
/
genlib.py
File metadata and controls
executable file
·47 lines (33 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
from itertools import product
from typing import List
OUTFILE = "librop.c"
REG2REG_OP = ["xor", "and", "or", "mov", "add", "sub", "xchg", "cmova", "cmovae", "cmovb", "cmovbe", "cmovc", "cmove", "cmovg", "cmovge", "cmovl", "cmovle", "cmovna", "cmovnae",
"cmovnb", "cmovnbe", "cmovnc", "cmovne", "cmovng", "cmovnge", "cmovnl", "cmovnle", "cmovno", "cmovnp", "cmovns", "cmovnz", "cmovo", "cmovp", "cmovpe", "cmovpo", "cmovs", "cmovz"]
REGONLY_OP = ["push", "pop", "dec", "inc"]
X86_REGS = ["eax", "ebx", "ecx", "edx", "esi", "edi", "esp", "ebp"]
def create_function(name, mnemonic, req_op, opt_op = None):
return \
f"""__attribute__((naked))void {name}(){{
asm __volatile__ (
".intel_syntax noprefix;"
"{mnemonic} {','.join([x for x in [req_op, opt_op] if x])};"
"ret;"
);
}}
"""
def main():
functions: List[str] = []
for op in REG2REG_OP:
for dst, src in product(X86_REGS, X86_REGS):
symname = f"{op}_{dst}_{src}"
functions.append(create_function(symname, op, dst, src))
for op in REGONLY_OP:
for r in X86_REGS:
symname = f"{op}_{r}"
functions.append(create_function(symname, op, r))
with open(OUTFILE, "w") as f:
f.write("\n".join(functions))
return
if __name__ == "__main__":
main()