Tips and tricks
First of all, read the troubleshooting page. It explains how to deal with most decompilation problems. Below is a mix of other useful information that did not fit into any other page:
Volatile memory
Sometimes the decompiler can be overly aggressive and optimize references to volatile memory completely away. A typical situation like the following:
can be decompiled into
because the decompiler assumes that a variable cannot change its value by itself and it can prove that r0 continues to point to the same location during the loop.
To prevent such optimization, we need to mark the variable as volatile. Currently the decompiler considers memory to be volatile if it belongs to a segment with one of the following names: IO, IOPORTS, PORTS, VOLATILE. The character case is not important.
Constant memory
Sometimes the decompiler does not optimize the code enough because it assumes that variables may change their values. For example, the following code:
can be decompiled into
but this code is much better:
because
is a pointer that resides in constant memory and will never change its value.
The decompiler considers memory to be constant if one of the following conditions hold:
the segment has access permissions defined but the write permission is not in the list (to change the segment permissions use the "Edit, Segments, Edit Segment" menu item or the built-in function)
the segment name is one of the following (the list may change in the future): .text, .rdata, .got, .got.plt, .rodata, __text, __const, __const_coal, __cstring, __cfstring, __literal4, __literal8, __pointers, __nl_symbol_ptr, __la_symbol_ptr, __objc_catlist, __objc_classlist, __objc_classname, __objc_classrefs, __objc_const, __objc_data, __objc_imageinfo, __objc_ivar, __objc_methname, __objc_methtype, __objc_protolist, __objc_protorefs, __objc_selrefs, __objc_superrefs, __message_refs, __cls_refs, __inst_meth, __cat_inst_meth, __cat_cls_meth, __OBJC_RO
It is possible to override the constness of an individual item by specifying its type with the volatile or const modifiers.
CONTAINING_RECORD macro
The decompiler knows about the macro and tries to use it in the output. However, in most cases it is impossible to create this macro automatically, because the information about the containing record is not available. The decompiler uses three sources of information to determine if CONTAINING_RECORD should be used:
If there is an assignment like this:
it can be converted into
by simply confirming the types of v1 and v2.
NOTE: the variables types must be specified explicitly. Even if the types are displayed as correct, the user should press Y followed by Enter to confirm the variable type.
applied to numbers in the disassembly listing are used as a hint to create CONTAINING_RECORD. For example, applying structure offset to 0x41C in
In most cases the CONTAING_RECORD macro can be replaced by a shorter and nicer expression if a used. In this case it is enough to declare the pointer as a shifted pointer and the decompiler will transform all expressions where it is used.
Since the arguments of indirect calls are collected before defining variables, specifying the type of the variable that holds the function pointer may not be enough. The user have to specify the function type using other methods in this case. The following methods exist (in the order of preference):
For indirect calls of this form:
If funcptr is initialized statically and points to a valid function, just ensure a correct function prototype. The decompiler will use it.
For indirect calls of this form:
If reg points to a structure with a member that is a function pointer, just convert the operand into a structure offset (hotkey T):
and ensure that the type of mystruct::funcptr is a pointer to a function of the desired type.