Disassembly with objdump: Linear Sweep and Its Limits
A disassembler turns bytes back into instructions. That sounds mechanical, and for compiler output it nearly is, but there are two fundamentally different ways to do it, and the difference decides what you see when a file contains data mixed with code or was crafted to mislead. GNU objdump uses linear sweep; Ghidra, IDA, Binary Ninja and radare2's analysis use recursive descent. This lesson shows objdump's most useful options on a toy, then breaks linear sweep on purpose with two bytes.
Definitions
- Linear sweep: start at the beginning of a section and decode instruction after instruction until the end. Simple, complete (every byte is shown), and wrong when data sits between instructions, because one mis-decoded byte shifts every boundary after it.
- Recursive descent: start at known entry points (the entry point, exported functions, symbols), decode, and follow every jump and call target; bytes never reached are left as data. It handles data in code but misses code reached only by indirect jumps it can't resolve.
- Useful objdump options:
-d(disassemble code sections),-D(all sections),-M intel(Intel syntax),--no-show-raw-insn(hide bytes),--disassemble=sym(one function),-S(interleave source when built with-g),-r/-R(static/dynamic relocations),-s -j .rodata(hex dump of a section),--start-address=/--stop-address=(a range),-b binary -m i386:x86-64(raw bytes with no file format).
objdump on a toy
cat > greet.c <<'SRC'
#include <stdio.h>
static const char GREETING[] = "hello, analyst";
int shout(int n) {
for (int i = 0; i < n; i++) puts(GREETING);
return n;
}
int main(void) { return shout(2) == 2 ? 0 : 1; }
SRC
gcc -O1 -g greet.c -o greet
echo "== one function, Intel syntax, with source lines"
objdump -d -S -M intel --no-show-raw-insn --disassemble=shout greet | sed -n '/<shout>:/,$p' | grep -vE '^$|^Disassembly'
echo "== where the string lives"
objdump -s -j .rodata greet | grep -i hello== one function, Intel syntax, with source lines
0000000000001149 <shout>:
#include <stdio.h>
static const char GREETING[] = "hello, analyst";
int shout(int n) {
1149: endbr64
114d: push r12
114f: push rbp
1150: push rbx
1151: mov ebp,edi
for (int i = 0; i < n; i++) puts(GREETING);
1153: test edi,edi
1155: jle 1172 <shout+0x29>
1157: mov ebx,0x0
115c: lea r12,[rip+0xea5] # 2008 <GREETING>
1163: mov rdi,r12
1166: call 1050 <puts@plt>
116b: add ebx,0x1
116e: cmp ebp,ebx
1170: jne 1163 <shout+0x1a>
return n;
}
1172: mov eax,ebp
1174: pop rbx
1175: pop rbp
1176: pop r12
1178: ret
== where the string lives
2000 01000200 00000000 68656c6c 6f2c2061 ........hello, a
With -S and a -g build, C lines appear above the instructions generated for them; the whole optimised loop (entry test, call, increment, backward jne) sits under the single for line, because -O1 merged the statements. lea r12, [rip+0xea5] with the # 2008 <GREETING> comment is the address of the string (loaded once, before the loop), and the -s -j .rodata dump shows the bytes at that address. For stripped release builds there are no source lines and no <GREETING> labels, only the comment with the address.
Breaking linear sweep with one byte
The program below jumps over a single byte 0xE8 placed between two instructions. The CPU never executes that byte. But 0xE8 is the opcode of call rel32, so a linear-sweep disassembler decodes it plus the next four bytes as a call and loses the instruction boundaries that follow. This is the simplest anti-disassembly trick; the session assembles it, runs it, and shows both views.
cat > junk.asm <<'SRC'
bits 64
section .data
msg: db "the real code ran", 10
len equ $ - msg
section .text
global _start
_start:
jmp .real ; always jumps over the next byte
db 0xE8 ; junk byte: the opcode of 'call rel32'
.real:
mov eax, 1 ; write(1, msg, len)
mov edi, 1
lea rsi, [rel msg]
mov edx, len
syscall
mov eax, 60 ; exit(0)
xor edi, edi
syscall
SRC
nasm -f elf64 junk.asm -o junk.o && ld junk.o -o junk
./junk
start=$(nm junk | awk '/_start.real/{print $1}')
echo "== with symbols: objdump restarts decoding at the label"
objdump -d -M intel junk | grep -E '^\s+[0-9a-f]+:' | head -3
strip junk
echo "== stripped: pure linear sweep"
objdump -d -M intel junk | grep -E '^\s+[0-9a-f]+:' | head -5
echo "== decoding from the jump target 0x$start (what recursive descent does)"
objdump -d -M intel --start-address=0x$start junk | grep -E '^\s+[0-9a-f]+:' | head -4the real code ran
== with symbols: objdump restarts decoding at the label
401000: eb 01 jmp 401003 <_start.real>
401002: e8 .byte 0xe8
401003: b8 01 00 00 00 mov eax,0x1
== stripped: pure linear sweep
401000: eb 01 jmp 0x401003
401002: e8 b8 01 00 00 call 0x4011bf
401007: 00 bf 01 00 00 00 add BYTE PTR [rdi+0x1],bh
40100d: 48 8d 35 ec 0f 00 00 lea rsi,[rip+0xfec] # 0x402000
401014: ba 12 00 00 00 mov edx,0x12
== decoding from the jump target 0x0000000000401003 (what recursive descent does)
401003: b8 01 00 00 00 mov eax,0x1
401008: bf 01 00 00 00 mov edi,0x1
40100d: 48 8d 35 ec 0f 00 00 lea rsi,[rip+0xfec] # 0x402000
401014: ba 12 00 00 00 mov edx,0x12
The program runs normally. While the label .real is in the symbol table, objdump uses it as a synchronisation point: it prints the junk as .byte 0xe8 and restarts at the label. Symbols are a crutch that linear sweep leans on. Once the file is stripped, objdump's linear view shows a call with a nonsense target that has eaten the first four bytes of mov eax, 1, the next "instruction" is a fragment (add BYTE PTR [rdi+0x1], bh), and mov eax, 1 and mov edi, 1 have vanished from the listing. x86 decoding often falls back into step by itself after a few bytes (here at 0x40100d), which makes the damage easy to overlook: two missing instructions in a long listing. Starting at the jump's target, the same bytes decode correctly. A recursive-descent disassembler gets this simple case right automatically because it follows the jmp; tricks aimed at recursive descent use conditional jumps that always go one way (jz right after xor eax, eax), so the tool also decodes the never-taken fall-through path into junk. Tools let you fix this by hand: mark the byte as data, redefine the code, and continue.
How anti-disassembly is detected
- Jumps into the middle of a decoded instruction (a jump target that is not the start of any instruction in the listing) are the clearest sign. objdump doesn't warn about it; Ghidra and IDA mark such conflicts (overlapping instructions, red or highlighted addresses).
- Instructions that make no sense in context: privileged instructions (
in,out,hlt) in user code,calls to addresses outside any section, impossible prefixes. They usually mean data decoded as code. - Conditional jumps whose condition is fixed (
xor eax, eax; jzorstc; jc): the other path is junk. - Compare two disassemblers; where they disagree, the bytes deserve a closer look.
How analysts work around it
- Use a recursive-descent tool for real analysis and objdump for quick, scriptable views and as a second opinion.
- When you find a trick, undefine the bytes and redefine code at the true target (
UandCin IDA, "Clear Code Bytes" and "Disassemble" in Ghidra), and document the trick in your notes. - For automated triage, emulate (Unicorn, Qiling, Speakeasy) rather than disassemble the confusing region.
For developers, anti-disassembly protects nothing that matters: it costs an analyst minutes, and it makes the program look like malware to security products.
Pitfalls
- AT&T is objdump's default; always add
-M intelif you read Intel syntax. -dskips data sections; code in.data(self-modifying or unpacked code) needs-Dor-j .data.- Padding between functions (
nopvariants,int3, zero bytes) is not code that runs. - Addresses in a PIE are offsets until the program is loaded; gdb shows the run-time addresses (gdb in Batch Mode).
Practice
1. Which objdump command shows only the function shout, in Intel syntax, without raw bytes?
objdump -d -M intel --no-show-raw-insn --disassemble=shout greet.
2. In the stripped junk listing, why does the instruction after the fake call look wrong?
The fake call consumed the 0xE8 plus four bytes of the following mov eax, 1 (b8 01 00 00) as its displacement, so decoding restarted at the last byte of that instruction (00), which combined with the start of mov edi, 1 into add BYTE PTR [rdi+0x1], bh.
3. Replace jmp .real by xor eax, eax plus jz .real. Why would this also fool many recursive-descent tools?
The tool follows both successors of a conditional jump, including the fall-through, which starts at the junk byte. Only constant propagation (knowing that ZF is always set) reveals that the fall-through is dead.
Test yourself
How does linear sweep decide where the next instruction starts?
- It follows jumps
- Right after the end of the previous decoded instruction
- It uses the symbol table
- It asks the CPU
What did the single byte 0xE8 do to objdump's view of junk?
- Nothing
- It was decoded as the start of a 5-byte call, swallowing bytes of the next real instruction
- It crashed objdump
- It hid the strings
Which disassembly strategy follows jump and call targets from known entry points?
- Linear sweep
- Recursive descent
- Hex dumping
- Strings extraction
Which objdump option interleaves C source lines with the disassembly?
- -r
- -S (with a -g build)
- -s
- -M intel