13.2 — From C++ to a program file

📘 Chapter 13⏱ about 2 min read✍️ Beginner friendly
Read one box at a time. No rush.

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:

SectionWhat's in it
.textthe machine code — your functions, as CPU instructions
.rodata / .rdataread-only data, like your text in "quotes"
.dataglobal variables that start with a value
.bssglobal variables that start at zero
Executable format

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:

text
8d 04 37      lea  eax, [rdi+rsi]c3            ret

A tool that turns machine code back into assembly is called a disassembler.

Key idea

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:

C++▶ Run
#include <iostream> int main(){    std::cout << "Welcome to my secret program!\n";    return 0;}
text
strings ./myprogram
Output
...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.

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.