A decompiler is a computer program that translates an executable file back into high-level source code. Unlike a compiler, which converts high-level code into machine code, a decompiler performs the reverse process. While disassemblers translate executables into assembly language, decompilers reconstruct higher level representations resembling source languages such as C. Because compilation generally discards source-level information such as variable names, comments, types, and aspects of the original program structure, a decompiler usually cannot reproduce the original source code exactly.
Introduction Decompilation is the process of transforming executable code into a high-level, human-readable format using a decompiler. This process is commonly used for tasks that involve reverse-engineering the logic behind executable code, such as recovering lost or unavailable source code. Decompilers face inherent challenges due to the loss of information during compilation, such as variable names, comments, and aspects of the original source-level structure. Certain factors can impact the success of decompilation. Executables containing detailed metadata, such as those used by Java and .NET, are easier to reverse-engineer because they often retain class structures, method signatures, and debugging information. Executable files stripped of such context are more challenging to translate into meaningful source code. Some software developers may obfuscate, pack, or encrypt parts of their executable programs, making the decompiled code harder to interpret. These techniques may be used to deter reverse engineering by making analysis more difficult and time-consuming.
Design Decompilers can be thought of as composed of a series of phases, each of which contributes specific aspects of the overall decompilation process.
Loader The first decompilation phase loads and parses the input machine code or intermediate language program's binary file format. It should be able to discover basic facts about the input program, such as the architecture (Pentium, PowerPC, etc.) and the entry point. In many cases, it should be able to find the equivalent of the main function of a C program, which is the start of the user written code. This excludes the runtime initialization code, which should not be decompiled if possible. If available, the symbol tables and debug data are also loaded. The front end may be able to identify the libraries used even if they are linked with the code; this can provide library interfaces. If it can determine the compiler or compilers used, this may provide useful information in identifying code idioms.
Disassembly The next logical phase is the disassembly of machine code instructions into a machine independent intermediate representation (IR). For example, the Pentium machine instruction
might be translated to the IR
Idioms Idiomatic machine code sequences are sequences of code whose combined semantics are not immediately apparent from the instructions' individual semantics. Either as part of the disassembly phase, or as part of later analyses, these idiomatic sequences need to be translated into known equivalent IR. For example, the x86 assembly code:
computes the absolute value of eax for values other than the most negative representable signed integer, and could be translated into an equivalent higher-level operation such as: Some idiomatic sequences are machine independent; some involve only one instruction. For example, xor eax, eax clears the eax register (sets it to zero). This can be implemented with a machine independent simplification rule, such as a = 0. In general, it is best to delay detection of idiomatic sequences if possible, to later stages that are less affected by instruction ordering. For example, the instruction scheduling phase of a compiler may insert other instructions into an idiomatic sequence, or change the ordering of instructions in the sequence. A pattern matching process in the disassembly phase would probably not recognize the altered pattern. Later phases group instruction expressions into more complex expressions, and modify them into a canonical (standardized) form, making it more likely that even the altered idiom will match a higher level pattern later in the decompilation. It is particularly important to recognize compiler idioms for subroutine calls, exception handling, and switch statements. Some languages also have extensive support for strings or long integers.
Program analysis Various program analyses can be applied to the IR. In particular, expression propagation combines the semantics of several instructions into more complex expressions. For example,
could result in the following IR after expression propagation: The resulting expression more closely resembles a high level language expression and has eliminated the explicit use of the machine register eax. Later analyses may also eliminate the explicit use of ebx.
Data flow analysis The places where register contents are defined and used can be traced using data flow analysis. The same analysis can be applied to memory locations used for temporary and local data. A different name can then be formed for each connected set of value definitions and uses. The same local storage location may have been reused for more than one source level variable in different parts of the original program. Conversely, an imprecise analysis may infer data-flow relationships between uses that could not occur in an actual execution. These ambiguities can make recovery of source level variables and types more difficult.
Type analysis A machine code decompiler may perform type analysis. Here, the ways in which registers or memory locations are used impose constraints on their possible types. For example, an and instruction generally suggests an integer-like operand, while arithmetic involving a memory address may indicate a pointer. Type analysis attempts to combine such constraints to recover higher level type information. Various high level expressions can be recognized which trigger recognition of structures or arrays. However, it is difficult to distinguish many of the possibilities because of the freedom that machine code, or even some high level languages such as C, allow with casts and pointer arithmetic. The example from the previous section could result in the following high level code:
… excerpt ends here. Continue reading the full article.