about summary refs log tree commit diff stats
path: root/miasm2/core/cpu.py
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2015-04-01 15:58:29 +0200
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2015-04-01 23:47:37 +0200
commit53d82c13f7da6851196e69c67841af24bcf218b2 (patch)
tree36a74eb31953b449544dfc6eedd8e61a1be7a5eb /miasm2/core/cpu.py
parent5a6145c5ea3a1df1e666224962dc3ba685327a12 (diff)
downloadmiasm-53d82c13f7da6851196e69c67841af24bcf218b2.tar.gz
miasm-53d82c13f7da6851196e69c67841af24bcf218b2.zip
Cpu: modify instructions' offset relative encoding
The assembler will automatically use instruction len in offset computation

In the following instruction:
0x10: EB 02   JMP 0x14

If we assemble this instruction, the requested instruction send to the assembler
engine will be:
JMP +0x4

And will be encoded to:
EB 02

Previously, the assembly of:
JMP +0x4
was:
EB 04
Diffstat (limited to 'miasm2/core/cpu.py')
-rw-r--r--miasm2/core/cpu.py15
1 files changed, 9 insertions, 6 deletions
diff --git a/miasm2/core/cpu.py b/miasm2/core/cpu.py
index ed124462..efb511ce 100644
--- a/miasm2/core/cpu.py
+++ b/miasm2/core/cpu.py
@@ -1151,6 +1151,7 @@ class cls_mn(object):
             if not getok:
                 continue
 
+            c.l = prefix_len + total_l / 8
             for i in c.to_decode:
                 f = c.fields_order[i]
                 if f.is_present:
@@ -1164,7 +1165,6 @@ class cls_mn(object):
             for a in c.args:
                 a.expr = expr_simp(a.expr)
 
-            c.l = prefix_len + total_l / 8
             c.b = cls.getbytes(bs, offset_o, c.l)
             c.offset = offset_o
             c = c.post_dis()
@@ -1335,14 +1335,14 @@ class cls_mn(object):
         return o
 
     def value(self, mode):
-        todo = [(0, [(x, self.fields_order[x]) for x in self.to_decode[::-1]])]
+        todo = [(0, 0, [(x, self.fields_order[x]) for x in self.to_decode[::-1]])]
 
         result = []
         done = []
         cpt = 0
 
         while todo:
-            index, to_decode = todo.pop()
+            index, cur_len, to_decode = todo.pop()
             # TEST XXX
             for i, f in to_decode:
                 setattr(self, f.fname, f)
@@ -1353,11 +1353,14 @@ class cls_mn(object):
             cpt += 1
             can_encode = True
             for i, f in to_decode[index:]:
+                f.parent.l = cur_len
                 ret = f.encode()
                 if not ret:
                     log.debug('cannot encode %r', f)
                     can_encode = False
                     break
+                if f.value is not None and f.l:
+                    cur_len += f.l
                 index += 1
                 if ret is True:
                     continue
@@ -1366,14 +1369,14 @@ class cls_mn(object):
                 for i in ret:
                     gcpt += 1
                     o = []
-                    if ((index, [xx[1].value for xx in to_decode]) in todo or
-                        (index, [xx[1].value for xx in to_decode]) in done):
+                    if ((index, cur_len, [xx[1].value for xx in to_decode]) in todo or
+                        (index, cur_len, [xx[1].value for xx in to_decode]) in done):
                         raise NotImplementedError('not fully functional')
 
                     for p, f in to_decode:
                         fnew = f.clone()
                         o.append((p, fnew))
-                    todo.append((index, o))
+                    todo.append((index, cur_len, o))
                 can_encode = False
 
                 break