13.2 — From C++ to a program file
What's inside a program file?#
When you compile, you get an executable file: .exe on Windows, often no extension on Linux and Mac.
It isn't magic. It's a file with a clear layout, split into sections:
| Section | What's in it |
|---|---|
.text | the machine code — your functions, as CPU instructions |
.rodata / .rdata | read-only data, like your text in "quotes" |
.data | global variables that start with a value |
.bss | global variables that start at zero |
The layout rules for program files. Windows uses PE (Portable Executable). Linux uses ELF. Macs use Mach-O. The ideas are the same.
Machine code and assembly#
The CPU runs machine code: bytes like 8d 04 37 c3. Nobody wants to read that.
Assembly is the human-readable version. Each line is one CPU instruction:
8d 04 37 lea eax, [rdi+rsi]c3 retA tool that turns machine code back into assembly is called a disassembler.
Compiling throws away your variable names, comments and nice structure. Reverse engineering is the art of figuring them back out.
Text is easy to find#
Text in quotes is stored as-is in the program file. A tool called strings prints every readable piece of text in a file:
#include <iostream> int main(){ std::cout << "Welcome to my secret program!\n"; return 0;}strings ./myprogram...Welcome to my secret program!...That's why text messages are usually the very first clue reverse engineers look for.
Debug vs release builds#
- A debug build keeps extra info (names, line numbers) to help debuggers. Much easier to reverse.
- A release build is optimized: faster and smaller, with names often removed (called stripped). Harder to reverse.
The compiler's optimization level changes the assembly a lot. -O0 means "don't optimize" (long and simple). -O1, -O2 and -O3 make it shorter and cleverer. You'll see both in the next lessons.
Tip: press ← / → to move between lessons, / to search.