Skip to content

Instantly share code, notes, and snippets.

@zerotypic
Created February 16, 2022 15:25
Show Gist options
  • Select an option

  • Save zerotypic/193fffe0e7be66453f82784b7a40b670 to your computer and use it in GitHub Desktop.

Select an option

Save zerotypic/193fffe0e7be66453f82784b7a40b670 to your computer and use it in GitHub Desktop.
#
# tinfo_calc_udt_aligns.py : Generate a struct type given a list of members with offsets
#
# First, create a simple uint32 tinfo.
uint32 = idaapi.tinfo_t()
uint32.create_simple_type(idaapi.BTF_UINT32)
# The udt_type_data_t struct holds information about struct types.
udt_data = idaapi.udt_type_data_t()
# This is the list of elements/members of the struct.
elems = [
(0, "a", uint32),
(4*8, "b", uint32),
(12*8, "c", uint32)
]
for (offset, name, ty) in elems:
udt_memb = udt_data.push_back()
udt_memb.offset = offset
udt_memb.type = ty
udt_memb.size = udt_memb.type.get_size() << 3
udt_memb.name = name
#endfor
# Set the size of the struct.
udt_data.total_size = 20
# Create the tinfo_t object.
tinfo = idaapi.tinfo_t()
# Turn it into a UDT using udt_data.
tinfo.create_udt(udt_data, idaapi.BTF_STRUCT)
# Ask IDA to use the provided offsets to generate the struct.
# If this returns True, then it means it succeeded.
tinfo.calc_udt_aligns()
str(tinfo)
# Output (formatted):
#
# struct __attribute__((aligned(4))) {
# unsigned __int32 a;
# unsigned __int32 b;
# _BYTE gap8[4];
# unsigned __int32 c;
# _BYTE gap10[4];
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment