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.
ELF header, at the start of the file. It begins with the four bytes
0x7F,E,L,F, where the prefix0xmarks a number written in hexadecimal, base 16. It says whether the file is for a 32-bit or a 64-bit machine, in which byte order §4.21 (endianness) its numbers are stored, what kind of file it is (e_type: 1 relocatable, meaning an object file that is still to be linked, 2 executable, 3 shared library), which processor it is for (e_machine: 40 for Arm, 62 for x86-64), at which address execution starts (e_entry), and where the two tables are.Sections: the content. Common ones are
.text(code),.rodata(constants),.data(variables with initial values),.bss(variables that start as zero),.symtaband.strtab(the list of names, called the symbol table §12.8, and the text of those names), and sections whose names start with.debug(information for a debugger).Section header table: one entry for each section, with its name, type, flags, memory address, position in the file and size. The flag
SHF_ALLOCmarks a section that occupies memory when the program runs. The typeSHT_NOBITS, used for.bss, marks a section that has a size and no bytes in the file.Program header table: one entry for each segment. A loadable segment has the type
PT_LOADand these fields:p_offset(where its bytes start in the file),p_vaddr(its memory address),p_paddr(a second address, which the specification reserves for systems where physical addresses matter),p_filesz(how many of its bytes come from the file),p_memsz(how many bytes it occupies in memory) andp_flags(permission to read, write and execute).
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:
For each
PT_LOADsegment, put thep_fileszbytes that start at file positionp_offsetinto memory at addressp_vaddr.If
p_memszis larger thanp_filesz, fill the remaining bytes of the segment with zero.Apply the permissions in
p_flags, and start execution ate_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 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 bytes of memory content, and the two segments cover 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:
p_fileszis never larger thanp_memsz;the
p_fileszbytes of the file that start at positionp_offsetare the beginning of the segment in memory, which starts at addressp_vaddr;the remaining bytes of the segment in memory,
p_memszminusp_fileszof them, hold zero and follow directly after.
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:
One segment usually contains several sections.
Some sections are in no loadable segment and never reach memory, for example the ordinary symbol table and debugger-only information.
A segment can be larger in memory than in the file. The extra bytes are filled with zero. That is how variables that start as zero (
.bss) get memory without taking space in the file.An object file that is still to be linked has sections and no segments. A finished executable needs segments and can do without its section header table.
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].