about summary refs log tree commit diff stats
path: root/miasm2
diff options
context:
space:
mode:
Diffstat (limited to 'miasm2')
-rw-r--r--miasm2/arch/arm/sem.py23
-rw-r--r--miasm2/arch/x86/sem.py11
-rw-r--r--miasm2/jitter/jitload.py52
3 files changed, 47 insertions, 39 deletions
diff --git a/miasm2/arch/arm/sem.py b/miasm2/arch/arm/sem.py
index 8f176947..72625eab 100644
--- a/miasm2/arch/arm/sem.py
+++ b/miasm2/arch/arm/sem.py
@@ -12,22 +12,13 @@ EXCEPT_PRIV_INSN = (1 << 17)
 
 
 def update_flag_zf(a):
-    return [ExprAff(zf, ExprCond(a, ExprInt_from(zf, 0), ExprInt_from(zf, 1)))]
+    return [ExprAff(zf, ExprCond(a, ExprInt1(0), ExprInt1(1)))]
 
 
 def update_flag_nf(a):
     return [ExprAff(nf, a.msb())]
 
 
-def update_flag_pf(a):
-    return [ExprAff(pf, ExprOp('parity', a))]
-
-
-def update_flag_af(a):
-    return [ExprAff(af, ExprCond(a & ExprInt_from(a, 0x10),
-                                 ExprInt_from(af, 1), ExprInt_from(af, 0)))]
-
-
 def update_flag_zn(a):
     e = []
     e += update_flag_zf(a)
@@ -61,14 +52,14 @@ def arith_flag(a, b, c):
 
 # checked: ok for adc add because b & c before +cf
 
-
-def update_flag_add_cf(a, b, c):
-    return ExprAff(cf,
-        ((((a ^ b) ^ c) ^ ((a ^ c) & (~(a ^ b)))).msb()) ^ ExprInt1(1))
+def update_flag_add_cf(op1, op2, res):
+    "Compute cf in @res = @op1 + @op2"
+    return ExprAff(cf, (((op1 ^ op2) ^ res) ^ ((op1 ^ res) & (~(op1 ^ op2)))).msb())
 
 
-def update_flag_add_of(a, b, c):
-    return ExprAff(of, (((a ^ c) & (~(a ^ b)))).msb())
+def update_flag_add_of(op1, op2, res):
+    "Compute of in @res = @op1 + @op2"
+    return ExprAff(of, (((op1 ^ res) & (~(op1 ^ op2)))).msb())
 
 
 # checked: ok for sbb add because b & c before +cf
diff --git a/miasm2/arch/x86/sem.py b/miasm2/arch/x86/sem.py
index 781b3321..b192ee2c 100644
--- a/miasm2/arch/x86/sem.py
+++ b/miasm2/arch/x86/sem.py
@@ -123,13 +123,14 @@ def arith_flag(a, b, c):
 
 # checked: ok for adc add because b & c before +cf
 
+def update_flag_add_cf(op1, op2, res):
+    "Compute cf in @res = @op1 + @op2"
+    return ExprAff(cf, (((op1 ^ op2) ^ res) ^ ((op1 ^ res) & (~(op1 ^ op2)))).msb())
 
-def update_flag_add_cf(a, b, c):
-    return ExprAff(cf, (((a ^ b) ^ c) ^ ((a ^ c) & (~(a ^ b)))).msb())
 
-
-def update_flag_add_of(a, b, c):
-    return ExprAff(of, (((a ^ c) & (~(a ^ b)))).msb())
+def update_flag_add_of(op1, op2, res):
+    "Compute of in @res = @op1 + @op2"
+    return ExprAff(of, (((op1 ^ res) & (~(op1 ^ op2)))).msb())
 
 
 # checked: ok for sbb add because b & c before +cf
diff --git a/miasm2/jitter/jitload.py b/miasm2/jitter/jitload.py
index 42b7fe87..f79b5f79 100644
--- a/miasm2/jitter/jitload.py
+++ b/miasm2/jitter/jitload.py
@@ -298,41 +298,57 @@ class libimp:
                 self.fad2cname[ad] = c_name
                 self.fad2info[ad] = libad, imp_ord_or_name
 
-    def gen_new_lib(self, e, filter=lambda x: True):
+    def gen_new_lib(self, target_pe, filter=lambda _: True):
+        """Gen a new DirImport description
+        @target_pe: PE instance
+        @filter: (boolean f(address)) restrict addresses to keep
+        """
+
         new_lib = []
-        for n, ad in self.name2off.items():
-            out_ads = dict()
-            for k, vs in self.lib_imp2dstad[ad].items():
-                for v in vs:
-                    out_ads[v] = k
-            all_ads = self.lib_imp2dstad[ad].values()
-            all_ads = reduce(lambda x, y: x + list(y), all_ads, [])
-            all_ads = [x for x in all_ads if filter(x)]
-            log.debug('ads: %s' % [hex(x) for x in all_ads])
-            all_ads.sort()
-            # first, drop None
+        for lib_name, ad in self.name2off.items():
+            # Build an IMAGE_IMPORT_DESCRIPTOR
+
+            # Get fixed addresses
+            out_ads = dict() # addr -> func_name
+            for func_name, dst_addresses in self.lib_imp2dstad[ad].items():
+                out_ads.update({addr:func_name for addr in dst_addresses})
+
+            # Filter available addresses according to @filter
+            all_ads = [addr for addr in out_ads.keys() if filter(addr)]
+            log.debug('ads: %s' % map(hex, all_ads))
             if not all_ads:
                 continue
+
+            # Keep non-NULL elements
+            all_ads.sort()
             for i, x in enumerate(all_ads):
-                if not x in [0,  None]:
+                if x not in [0,  None]:
                     break
             all_ads = all_ads[i:]
+
             while all_ads:
+                # Find libname's Import Address Table
                 othunk = all_ads[0]
                 i = 0
                 while i + 1 < len(all_ads) and all_ads[i] + 4 == all_ads[i + 1]:
                     i += 1
-                funcs = [out_ads[x] for x in all_ads[:i + 1]]
+                # 'i + 1' is IAT's length
+
+                # Effectively build an IMAGE_IMPORT_DESCRIPTOR
+                funcs = [out_ads[addr] for addr in all_ads[:i + 1]]
                 try:
-                    rva = e.virt2rva(othunk)
+                    rva = target_pe.virt2rva(othunk)
                 except pe.InvalidOffset:
-                    rva = None
-                if rva is not None:  # e.is_in_virt_address(othunk):
-                    new_lib.append(({"name": n,
+                    pass
+                else:
+                    new_lib.append(({"name": lib_name,
                                      "firstthunk": rva},
                                     funcs)
                                    )
+
+                # Update elements to handle
                 all_ads = all_ads[i + 1:]
+
         return new_lib