Section 12.6

ELF

Full name: Executable and Linking Format.

Definition

ELF is a file format for compiled programs: programs already translated into machine code. An ELF file holds that code and its data with tables describing them. The same format serves object files, which a linker §12.4 combines into a program, executable files, and shared libraries, which programs load when they start. Linux uses it, and so do compilers for Arm microcontrollers §6.1 (single-chip computers). The System V Application Binary Interface, a specification for Unix systems, defines it under the name Executable and Linking Format. Many texts write Executable and Linkable Format.

The idea

A compiled program has two very different readers. The linker, the tool that joins compiled pieces into a program, needs many small, named pieces that it can rearrange: code, constants, variables, the list of names, and notes about bytes that still have to be corrected. The loader, which is whatever puts the program into memory to run it, needs only a short list of orders of the form “put these bytes of the file at that address, allow these kinds of access, and clear this much more memory”.

ELF serves both readers by keeping two tables that describe the same bytes. The section header table is the detailed view for the linker. The program header table is the short view for the loader, and its entries are called segments. The picture is a warehouse with two lists. The catalog lists every box by name. The shipping list groups the boxes onto a few pallets and gives each pallet a destination. Some boxes, such as the lists of names and the information for a debugger (a tool for inspecting a running program), are on no pallet and never reach memory.

How it works

An ELF file has four parts. The field names are those of the specification.

The loader uses the ELF header and the program header table to locate the segments. For an executable loaded at its linked address with no interpreter §2.13:

  1. For each PT_LOAD segment, put the p_filesz bytes that start at file position p_offset into memory at address p_vaddr.

  2. If p_memsz is larger than p_filesz, fill the remaining bytes of the segment with zero.

  3. Apply the permissions in p_flags, and start execution at e_entry.

A position-independent executable can load at a different base address. When an executable names a program interpreter, the kernel loads that interpreter and transfers control to its entry point first. The interpreter then performs dynamic linking and starts the executable. For a microcontroller, a small computer on one chip, the work is split: a programming tool writes the file bytes of each segment into the chip’s flash memory §6.6, which keeps its content without power, and the startup code of the program copies the initial values of variables into RAM and clears the part that must be zero.

Which table a file has depends on its kind. A relocatable object file has sections and needs no program header table, because it is input for the linker and not a program to be started. An executable file must have a program header table, and its section header table is optional.

Worked example

A program for an Arm microcontroller is linked so that its code is in flash memory at address 0 and its variables are in RAM at 0x20000000. The result is a 32-bit ELF file. The tool readelf prints both of its tables. The section header table includes these entries:

section address size in bytes in the file in memory
.text 0x00000000 40,000 yes yes
.data 0x20000000 256 yes yes
.bss 0x20000100 1,024 no yes
.symtab and .strtab none 248 yes no

The program header table has two entries, both of type PT_LOAD:

segment p_offset p_vaddr p_filesz p_memsz sections inside
1 0x1000 0x00000000 0x9C40 0x9C40 .text
2 0xB000 0x20000000 0x100 0x500 .data, .bss

Segment 1 is the code: 0x9C40 is 40,000 bytes, all taken from the file. Segment 2 covers two sections. Its p_filesz is 0x100, which is 256 bytes, the size of .data. Its p_memsz is 0x500, which is 1,280 bytes. The difference is 1280−256=10241280 - 256 = 1024 bytes, exactly the size of .bss. Those 1,024 bytes are not in the file. They are the zero-filled end of the segment, from address 0x20000100 up to 0x20000500.

The result means the following. The file supplies 40000+256=4025640000 + 256 = 40256 bytes of memory content, and the two segments cover 40000+1280=4128040000 + 1280 = 41280 bytes of memory addresses. The symbol table and the names belong to no segment, so loading never touches them. In this file p_paddr of segment 2 is 0x00009C40: the linker recorded there the flash address at which the 256 initial bytes are stored, directly after the code.

Stated precisely

For a segment of type PT_LOAD the specification states:

For sections it states: every section has exactly one section header; each section occupies one unbroken run of bytes in the file, which may be empty; no byte of the file belongs to two sections; and a section of type SHT_NOBITS occupies no bytes in the file.

The link between the two views is containment: a segment comprises one or more sections, and the program header does not record which ones.

Limits and common mistakes

The size of an ELF file says little about memory use. .bss takes memory and no file bytes. The ordinary symbol table and debug information §12.9 usually stay in the file. In a dynamically linked program, the dynamic symbol and string tables are normally mapped into process memory so the dynamic linker §12.49 can resolve names. The stack and the heap, the memory areas that a running program uses for function calls and for memory requested on demand, appear in neither table unless the linker script §12.5, the file that tells the linker where to place the program in memory, creates sections for them.

The value of e_type does not always match everyday speech. An ordinary program built by version 13.3 of the compiler gcc on Ubuntu Linux with default options has e_type 3, the value for a shared library, because it is built so that it can be loaded at any address. readelf reports such a file as a position-independent executable.

A file that follows the format is not thereby safe to load or to run. The positions and sizes in the tables can point outside the file or overlap, so a program that reads ELF files from an untrusted source must check every one of them. The format says nothing about what the code does.

On a microcontroller the ELF file itself usually stays on the developer’s computer. Tools copy the loadable bytes into a raw binary image or into an Intel HEX §12.7 file, a text format that lists addresses and bytes in hexadecimal, and only those bytes reach the device.

Questions answered

How do sections differ from loadable segments?

Sections are the linker’s view of an ELF file, and loadable segments are the loader’s view of the same bytes. ELF (Executable and Linking Format) is the file format for compiled programs on Linux and on many embedded systems. The linker is the tool that joins compiled pieces into one program, and the loader is whatever puts the finished program into memory. A section is one named block of one kind of content, such as the code (.text), the variables with initial values (.data) or the list of names. The section header table lists the sections, and linkers, debuggers and analysis tools read it. A loadable segment is one range of the file that is to be placed in memory at a given address with given permissions. The program header table lists the segments, and the loader reads only that table and the file header.

The practical differences:

For a question about how the file was built, or about what a name refers to, read the sections. For a question about what will be in memory, read the segments.

Related entries: Linker (§12.4), Symbol table (§12.8), pyelftools (§12.35), Program loading: from an ELF file to a running native process (§12.49), Linker script (§12.5), Intel HEX (§12.7), Relocation: turning a symbolic reference into exact encoded bits (§12.50). Further reading: [110].