ELF format
- C course
- Expert
- Lesson 35/47
- 5 min
Learn the ELF file format.
The ELF file format
ELF (Executable and Linkable Format) is the standard file format on Linux and Unix systems. Object files (.o), executables and shared libraries (.so) are all in ELF format.
An ELF file has three main parts: the ELF header tells the file type and architecture; the section header table lists sections the linker sees; the program header table lists segments the loader sees. The first four bytes are the magic number 0x7f, 'E', 'L', 'F'. This format is the common language of the linker and loader.
Sections and segments
In an ELF file, sections and segments are two different views. The linker works with sections: .text, .data, .bss and so on. The loader works with segments: a segment combines several sections and loads them into memory.
For example, the .text and .rodata sections may go into the same read-only segment; .data and .bss may go into the same writable segment. This split reduces file size and enables memory protection: read-only segments are protected against writing, writable segments against execution.
Practice Simulate ELF sections
Define int data_val = 42; (initialized, goes to .data). Define int bss_val; (uninitialized, goes to .bss). Write void show(void) that calls printf("Kod: hazır\n") (this goes to .text). Write int main(void); call printf("Veri: %d\n", data_val), printf("BSS: %d\n", bss_val) and show().
Starter code
#include <stdio.h>
// başlatılmış genel değişken
// başlatılmamış genel değişken
// fonksiyon tanımla
// main'de üçünü de yazdırSample solution
#include <stdio.h>
int data_val = 42;
int bss_val;
void show(void) {
printf("Kod: hazır\n");
}
int main(void) {
printf("Veri: %d\n", data_val);
printf("BSS: %d\n", bss_val);
show();
return 0;
}Expected output
Veri: 42 BSS: 0 Kod: hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match ELF part
| Term | Meaning |
|---|---|
ELF header |
File type |
Section table |
Linker |
Program table |
Loader |
Magic number |
0x7f E L F |
2 What does ELF stand for?
- Executable Link Format
- Extended Link File
- Executable and Linkable Format Correct answer
- External Library Format
Explanation: ELF: Executable and Linkable Format.
3 Complete the ELF magic number
unsigned char elf_magic[] = { 0x7f, 'E', 'L', '____' };
unsigned char elf_magic[] = { 0x7f, 'E', 'L', 'F' };
Explanation: The ELF magic ends with 0x7f, E, L, F.