Binaries

What Is Inside an Executable File

By RestBinary Editorial Team · · 5 min read

When you double click a program or type its name in a terminal, the operating system loads a file and starts running it. That file is an executable, and it is more organised than it might appear. It is not just a long list of machine instructions. It is a structured package with a header, a layout plan, code, data and information that tells the system how to start it.

This article takes a tour of that structure. It stays at the level of ideas that apply to most systems, and points to free tools that let you look inside a real file yourself.

Executable formats

Different operating systems use different file formats for programs.

  • ELF (Executable and Linkable Format) is used on Linux and many other Unix like systems.
  • PE (Portable Executable) is used on Windows, for files ending in .exe and .dll.
  • Mach-O is used on macOS and iOS.

The details differ, but all three do the same job: they describe how to turn a file on disk into a running process in memory. Scripts are a separate case. A script file is text that begins with a shebang line naming an interpreter, and the system starts that interpreter instead.

The header: the first bytes

Every format begins with a header that identifies the file. ELF files start with four bytes: the value 0x7F followed by the letters ELF. Windows PE files start with the letters MZ, followed later by a pointer to a PE header. These recognisable starting bytes are called magic numbers, and the system checks them first.

The header also records basic facts:

  • The processor architecture the code was built for, such as x86-64 or ARM.
  • Whether it is 32 bit or 64 bit.
  • The byte order used for numbers.
  • The entry point, which is the address where execution begins.
  • Where to find the rest of the structure.

This is why a program built for one processor will not start on another. The header says what it needs, and the loader refuses if the machine does not match.

Sections and segments

The body is divided into pieces by purpose. The names below are from ELF, and other formats have close equivalents.

  • .text holds the machine code, the actual instructions.
  • .data holds initialised variables, such as a global that starts with a value.
  • .bss describes space for variables that start empty. It takes almost no room in the file, but the loader reserves memory for it.
  • .rodata holds read only data, such as text strings in the program.

When the program runs, the loader maps these pieces into memory with suitable permissions. Code is marked as readable and executable but not writable, and data is writable but not executable. Separating them is a basic security measure, since it makes it harder for a bug to turn data into running code.

Symbols and debugging information

A compiled program contains names, such as function and variable names, in a symbol table. These help linkers and debuggers. Release builds often strip them out to shrink the file and make it harder to inspect, which is why crash traces from such programs show only addresses.

Debug builds may also include extra information that maps machine instructions back to source file names and line numbers. That mapping is what lets a debugger show you the line of code that is running.

Libraries and linking

Most programs rely on shared code, such as the standard library of the language. There are two ways to include it.

  1. Static linking copies the needed library code into the executable. The file is larger, but it is self contained.
  2. Dynamic linking records only the names of libraries, such as libc.so on Linux or a .dll on Windows. At start up, a dynamic loader finds those libraries and connects the program to them.

Dynamic linking saves disk space and lets many programs share one copy of a library, and fixes to the library benefit every program. The cost is that the right library version must be present on the machine. A "missing library" error is a symptom of this.

From file to running process

Roughly, this is what happens when you launch a program:

  1. The system opens the file and checks the magic number and header.
  2. It creates a new process with its own address space.
  3. It maps the code and data sections into memory with the right permissions.
  4. If the file is dynamically linked, the dynamic loader loads and connects the libraries.
  5. It sets up the stack, passes in arguments and environment variables, and jumps to the entry point.

Modern systems often place the program at a random address each time, a feature called address space layout randomisation, which makes some attacks harder.

Tools for looking inside

You do not need to guess. Free tools can show you the structure of real files.

  • file program reports the format and architecture.
  • readelf -h program and objdump -x program display ELF headers and sections on Linux.
  • ldd program lists the shared libraries a Linux program needs.
  • strings program prints readable text found inside, which often reveals messages and file paths.
  • nm program lists symbols, if they were not stripped.
  • xxd program | head shows the first bytes in hexadecimal so you can spot the magic number.

Try these on a harmless program such as ls. Only inspect files you trust and own, and remember that looking at a file is safe, but running an unknown program is not.

Frequently Asked Questions

Why can't I run a Windows program on Linux directly?

The two systems use different executable formats and provide different system interfaces. A compatibility layer can translate between them, but the file itself is not understood by the Linux loader.

What does it mean when a binary is stripped?

Stripping removes symbol names and debugging data. The program runs the same, but the file is smaller and harder to analyse, since function names are gone.

Is a script an executable?

In practice, yes: if it has execute permission and a shebang line, you can run it directly. Technically the system runs the interpreter named on the first line, and that interpreter reads the script.

Related reading