about summary refs log tree commit diff stats
path: root/example/asm/arm_sc.py
diff options
context:
space:
mode:
Diffstat (limited to 'example/asm/arm_sc.py')
-rw-r--r--example/asm/arm_sc.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/example/asm/arm_sc.py b/example/asm/arm_sc.py
new file mode 100644
index 00000000..83787f02
--- /dev/null
+++ b/example/asm/arm_sc.py
@@ -0,0 +1,62 @@
+#! /usr/bin/env python
+
+from pdb import pm
+
+from elfesteem.strpatchwork import StrPatchwork
+
+from miasm2.core import asmbloc
+from miasm2.core.cpu import parse_ast
+from miasm2.arch.arm.arch import mn_arm, base_expr
+from miasm2.core import parse_asm
+import miasm2.expression.expression as m2_expr
+
+reg_and_id = dict(mn_arm.regs.all_regs_ids_byname)
+
+
+def my_ast_int2expr(a):
+    return m2_expr.ExprInt32(a)
+
+
+def my_ast_id2expr(t):
+    return reg_and_id.get(t, m2_expr.ExprId(t, size=32))
+
+my_var_parser = parse_ast(my_ast_id2expr, my_ast_int2expr)
+base_expr.setParseAction(my_var_parser)
+
+
+st = StrPatchwork()
+
+blocs, symbol_pool = parse_asm.parse_txt(mn_arm, 'l', '''
+main:
+    MOV R1, R0
+    MOV R2, 0x100
+    LDR R3, [PC, mykey1-$]
+loop:
+    ADD R2, R1, R2
+    ADD R1, R1, 1
+    LDR R3, [PC, mykey2-$]
+    CMP R1, R3
+    BEQ loop
+
+    ADD R0, R1, R2
+    BX LR
+mykey1:
+.long 0x1
+mykey2:
+.long 0x2
+''')
+
+# fix shellcode addr
+symbol_pool.set_offset(symbol_pool.getby_name("main"), 0)
+
+for b in blocs[0]:
+    print b
+
+resolved_b, patches = asmbloc.asm_resolve_final(
+    mn_arm, blocs[0], symbol_pool)
+print patches
+
+for offset, raw in patches.items():
+    st[offset] = raw
+
+open('arm_sc.bin', 'wb').write(str(st))