about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--miasm2/arch/x86/ctype.py10
-rw-r--r--miasm2/core/objc.py17
2 files changed, 25 insertions, 2 deletions
diff --git a/miasm2/arch/x86/ctype.py b/miasm2/arch/x86/ctype.py
index f1f61a96..5e16f945 100644
--- a/miasm2/arch/x86/ctype.py
+++ b/miasm2/arch/x86/ctype.py
@@ -1,10 +1,12 @@
-from miasm2.core.objc import CLeafTypes, ObjCDecl
+from miasm2.core.objc import CLeafTypes, ObjCDecl, PADDING_TYPE_NAME
 from miasm2.core.ctypesmngr import CTypeId, CTypePtr
 
 
 class CTypeAMD64_unk(CLeafTypes):
     """Define C types sizes/alignement for x86_64 architecture"""
 
+    obj_pad = ObjCDecl(PADDING_TYPE_NAME, 1, 1) # __padding__ is size 1/align 1
+
     obj_char = ObjCDecl("char", 1, 1)
     obj_short = ObjCDecl("short", 2, 2)
     obj_int = ObjCDecl("int", 4, 4)
@@ -25,6 +27,8 @@ class CTypeAMD64_unk(CLeafTypes):
 
     def __init__(self):
         self.types = {
+            CTypeId(PADDING_TYPE_NAME): self.obj_pad,
+
             CTypeId('char'): self.obj_char,
             CTypeId('short'): self.obj_short,
             CTypeId('int'): self.obj_int,
@@ -70,6 +74,8 @@ class CTypeAMD64_unk(CLeafTypes):
 class CTypeX86_unk(CLeafTypes):
     """Define C types sizes/alignement for x86_32 architecture"""
 
+    obj_pad = ObjCDecl(PADDING_TYPE_NAME, 1, 1) # __padding__ is size 1/align 1
+
     obj_char = ObjCDecl("char", 1, 1)
     obj_short = ObjCDecl("short", 2, 2)
     obj_int = ObjCDecl("int", 4, 4)
@@ -90,6 +96,8 @@ class CTypeX86_unk(CLeafTypes):
 
     def __init__(self):
         self.types = {
+            CTypeId(PADDING_TYPE_NAME): self.obj_pad,
+
             CTypeId('char'): self.obj_char,
             CTypeId('short'): self.obj_short,
             CTypeId('int'): self.obj_int,
diff --git a/miasm2/core/objc.py b/miasm2/core/objc.py
index 9ae16291..06719096 100644
--- a/miasm2/core/objc.py
+++ b/miasm2/core/objc.py
@@ -15,6 +15,8 @@ from miasm2.core.ctypesmngr import CTypeUnion, CTypeStruct, CTypeId, CTypePtr,\
     CTypeArray, CTypeOp, CTypeSizeof, CTypeEnum, CTypeFunc, CTypeEllipsis
 
 
+PADDING_TYPE_NAME = "___padding___"
+
 class ObjC(object):
     """Generic ObjC"""
 
@@ -1348,6 +1350,11 @@ class CTypesManager(object):
         """Retrieve a void* objc"""
         return self.leaf_types.types.get(CTypePtr(CTypeId('void')))
 
+    @property
+    def padding(self):
+        """Retrieve a padding ctype"""
+        return CTypeId(PADDING_TYPE_NAME)
+
     def _get_objc(self, type_id, resolved=None, to_fix=None, lvl=0):
         if resolved is None:
             resolved = {}
@@ -1378,11 +1385,19 @@ class CTypesManager(object):
             align_max, size_max = 0, 0
 
             offset, align_max = 0, 1
+            pad_index = 0
             for name, field in type_id.fields:
                 objc = self._get_objc(field, resolved, to_fix, lvl + 1)
                 resolved[field] = objc
                 align_max = max(align_max, objc.align)
-                offset = self.struct_compute_field_offset(objc, offset)
+                new_offset = self.struct_compute_field_offset(objc, offset)
+                if new_offset - offset:
+                    pad_name = "__PAD__%d__" % pad_index
+                    pad_index += 1
+                    size = new_offset - offset
+                    pad_objc = self._get_objc(CTypeArray(self.padding, size), resolved, to_fix, lvl + 1)
+                    out.add_field(pad_name, pad_objc, offset, pad_objc.size)
+                offset = new_offset
                 out.add_field(name, objc, offset, objc.size)
                 offset += objc.size