3

I'd like to be able to create and change global variables at addresses with IDA Python: if variable doesn't exist, create it, if it exists, change it, set its name, undefine it.

For structs, there's idc.add_struc, idc.add_struc_member, idc.set_member_type, idc.set_member_name, idc.del_struc, idc.del_struc_member.

There's idc.set_name to set a name at address and ida_bytes.get_flags function to get flags, but I can't find a respective function such as set_flags or set_type to change global variable type at address. So to set variable type to dword, I'd do something like ida_bytes.set_flags(0x14001D01C, ida_bytes.dword_flag()), and to set it to structure type, I'd like to do something like ida_bytes.set_flags(0x14001D01C, ida_bytes.stru_flag(), ida_struct.get_struc_id("MyStructName"))

I scrolled through idaapi, ida_bytes, idc, ida_struct and ida_typeinf documentation. I also searched for set_, _flag, _type and other in the search box on the hex-rays documentation website, and also manually with Ctrl+F in the pages for namespaces above, but couldn't find functions to set flags at address.

Ray
  • 1,083
  • 9
  • 25
KulaGGin
  • 341
  • 1
  • 13

1 Answers1

3

The methods I was looking for are:

To get the name of the variable at address, use idc.get_name.

To get the type of the variable at address, first retreive the flags with ida_bytes.get_flags. Then to get its actual type you can use functions, such as idc.is_byte to determine if it's a byte, or idc.is_struct to determine if it's a struct. Then, if it's a struct, to determine specific type of a struct, use idaapi.get_opinfo(struct_id_buffer, ea, 0, flags) (example).

For structs, there's idc.add_struc, idc.add_struc_member, idc.set_member_type, idc.set_member_name, idc.del_struc, idc.del_struc_member.

Ray
  • 1,083
  • 9
  • 25
KulaGGin
  • 341
  • 1
  • 13