Systems and protocols · Intermediate

ELF: sections, segments and loading

Separate the linker’s sections from the loader’s segments and inspect an executable without running it.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. What the format describes
  2. Two different views of a file
  3. Inspect before executing
  4. Dependencies remain at runtime
  5. Ask Logeia about this articleCheck your understanding

What the format describes

ELF is an executable and linking format used on systems including Linux. It can describe executable files, relocatable objects, shared objects and core dumps. Its initial header identifies properties such as file type and target machine, allowing tools to interpret the remaining structures.

Sharing an ELF container format does not make binaries universally executable. Machine instructions, ABI conventions and runtime dependencies must also match the execution environment. An incompatible architecture remains incompatible even if a tool can successfully parse the file’s header.

Two different views of a file

Sections organize code, data, symbols and relocation information for linking and analysis. Segments describe the loader’s view: what portions to map into memory, where to map them and with which permissions. They are related views, not identical partitions of the file.

Several sections can belong to one loadable segment. Distinguishing program headers from section headers also explains why removing some analysis-oriented information can leave a program executable. Do not assume every named section becomes an independently mapped region at runtime.

Inspect before executing

For a small program you built yourself, readelf -h reports the file header, readelf -l reports program headers and readelf -S reports sections. Compare the views and identify which information answers a loading question versus a linking question.

Read-only inspection and execution have different risk profiles. Do not run an unfamiliar binary just to see whether it works. Begin with a controlled example, record the compiler and target, and use the output to establish the relationship between the file representation and the process image.

sh
readelf -h ./demo
readelf -l ./demo
readelf -S ./demo

Dependencies remain at runtime

A dynamically linked executable depends on an appropriate loader and shared libraries. Parsing its ELF header successfully does not prove those dependencies exist. Startup can fail before the application reaches its own main function, so investigate loading separately from application behavior.

Address layout and protection also depend on operating-system policies and build choices. ELF does not determine every runtime address or guarantee program safety. Trace the stages in order: file format, mapping, dynamic linking and finally application execution. That ordering prevents an early loader failure from being misdiagnosed as a later logic bug.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 20

Choose a learning path