disassembler

term

programminghacking

Kept in this browser · My desk

A program that turns machine code back into assembly language, mnemonic by mnemonic; the reading half of reverse engineering, used by malware analysts, vulnerability researchers and anyone checking what a compiler produced.

A disassembler is only as right as its assumptions. On x86, where instructions have no fixed length, starting one byte off produces a different but plausible listing, and the same byte means different things in 32-bit and 64-bit mode. Free tools such as GNU objdump and the NSA's Ghidra made disassembly routine; the law treats it differently by country.

What it is. A disassembler is the assembler run backwards: it takes machine code and prints it as assembly language. David Salomon's Assemblers and Loaders puts it in one line, "in a sense, the opposite of an assembler", one that "translates machine code into a source program in assembler language". What it cannot restore is what the assembler threw away: comments, the programmer's names unless a symbol table survives, the distinction between code and data, and the macros that generated the instructions.

How it works. A disassembler reads bytes, decodes each instruction with the architecture's encoding rules and prints the mnemonic and operands, replacing addresses with symbol names where the file still has them. On fixed-length architectures such as AArch64 and RISC-V it knows where every instruction starts; on x86 it must follow the code. Disassemblers print the canonical mnemonic: GNU's tools print je for the byte 74 that a programmer wrote as jz, both names the Intel manual lists for that opcode.

Two algorithms. Benjamin Schwarz, Saumya Debray and Gregory Andrews of the University of Arizona, in Disassembly of Executable Code Revisited, describe the two classic designs. Linear sweep decodes "everything appearing in sections of the executable that are typically reserved for machine code", the method of GNU objdump; it breaks when data sits in the code, and their example is three NULL bytes inserted for alignment after a jmp, which linear sweep decoded as add instructions because on x86 "a NULL byte can be a valid opcode", throwing off the instructions that followed. Recursive traversal follows the control flow from known entry points and so skips that padding, but indirect jumps, such as position-independent jump tables, hide where control goes. Their warning about existing tools is the important one: they "fail silently", producing incorrect disassemblies "without any indication that the results they are producing are incorrect".

Why it can disagree with itself. Hovav Shacham's 2007 paper compared x86 code to English written without spaces and showed two instructions, f7 c7 07 00 00 00 and 0f 95 45 c3, that decode, one byte later, as a move, an exchange, an increment and a ret. That example is 32-bit code: in 64-bit mode the bytes 40H to 4FH are REX prefixes, not INC and DEC, so the mode must be known as well as the start.

Tools. GNU objdump disassembles from the command line (objdump -d). LLVM's llvm-objdump offers -d, -D or --disassemble-all for every section, and --disassemble-symbols to limit the output to named functions. Inside a running program, the GNU debugger (GDB) uses its disassemble command "to display a range of addresses as machine instructions", and set disassemble-next-line shows the instructions at each stop. Capstone, "a lightweight multi-platform, multi-architecture disassembly framework", is the engine many security tools embed, covering ARM, ARM64, BPF, the Ethereum VM, M68K, MIPS, MOS65XX, PowerPC, RISC-V, SPARC, SystemZ, WebAssembly and x86 in 16, 32 and 64 bits, among others. Ghidra, "a software reverse engineering (SRE) framework created and maintained by the National Security Agency Research Directorate", lists disassembly, assembly, decompilation, graphing and scripting among its capabilities; its public repository dates from 1 March 2019, Coordinated Universal Time (UTC).

The law. In the United States, 17 U.S. Code 1201(f) permits circumventing a protection measure for the sole purpose of the analysis needed for interoperability of an independently created program. The leading case is older: in Sega Enterprises v. Accolade (Ninth Circuit, decided 20 October 1992, amended 6 January 1993), Accolade had disassembled Sega Genesis games to learn the console's interface, and the court held that "where disassembly is the only way to gain access to the ideas and functional elements embodied in a copyrighted computer program and where there is a legitimate reason for seeking such access, disassembly is a fair use of the copyrighted work, as a matter of law", while leaving Sega free to claim infringement in the finished games. Brazil's software law, Lei 9.609/1998, does not mention disassembly; its article 6 lists acts that do not offend the owner's rights, among them partial citation for teaching and integration technically indispensable to the user.

What goes wrong. A listing is an interpretation, not the program: data decoded as code, the wrong processor mode, the wrong starting byte, or deliberately confusing code produces output that looks reasonable and is false. A decompiler built on top inherits every error and adds its own guesses about types and structure. Analysts therefore cross-check: entry points from the file header, symbols, cross-references and, where possible, running the code under a debugger.

Neighbours. The disassembler reverses the assembler, reads machine code through the opcode tables of an instruction set architecture, and is the reading half of reverse engineering, beside the debugger. Brazilian practice says desmontador or, more often, disassembler.

Also known as: disassembly, desmontador, desmontagem, objdump, Ghidra

Sources

← Back to D Back to the glossary