4

I was not able to find an API function to get the CPU architecture of the loaded binary. In idaapi.py, there is a function def set_processor_type(*args) but no equivalent such as get_processor_type.

I don't want to fall back to running some additional (python) tool such as file, readelf or a python ELF parser. I also don't like the idea of parsing the strings displayed at the beginning of the IDA assembly listing.

There must be a way to use idc.py, idaapi.py or idautils.py for this.

7

You can get the name of the processor from the idainfo struct:

import idaapi

info = idaapi.get_inf_structure()

if info.is_64bit():
    bits = 64
elif info.is_32bit():
    bits = 32
else:
    bits = 16

try:
    is_be = info.is_be()
except:
    is_be = info.mf
endian = "big" if is_be else "little"

print 'Processor: {}, {}bit, {} endian'.format(info.procName, bits, endian)
# Result: Processor: mipsr, 32bit, big endian

Still haven't found the endian, though.

EDIT: Endian can be determined by processor name. For example, mipsr is big endian, and mipsrl is little endian.

EDIT:
Added a proper endian check (mf == MSB First). Based on this answer.

EDIT:
Since IDA7 beta 3 (170724), idainfo.mf was removed, and idainfo.is_be() must be used. Based on keypatch

2

Not all processor name has endian information. Generally it can be determinded as:

BIG_ENDIAN if _idaapi.cvar.inf.mf else LITTLE_ENDIAN
1

This is definitely not exact answer, but probably you could make use of

idaapi.ph_get_regnames() 

Returns the list of register names as defined in the processor module

Different platforms has different register sets, and if you will not find your solution about exact cpu you can give it a try. This function exists in IDA 6.8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.