Elf64
ELF (Executable and Linkable Format) is a standard file format for executable files, object code, shared libraries and core dumps. Linux and many UNIX-like operating systems use this format. Let's look at the structure of the ELF-64 Object File Format and some definitions in the linux kernel source code which related with it.
An ELF object file consists of the following parts:
    ELF header - describes the main characteristics of the object file: type, CPU architecture, the virtual address of the entry point, the size and offset of the remaining parts, etc...;
    Program header table - lists the available segments and their attributes. Program header table need loaders for placing sections of the file as virtual memory segments;
    Section header table - contains the description of the sections.
Now let's have a closer look on these components.
ELF header
The ELF header is located at the beginning of the object file. Its main purpose is to locate all other parts of the object file. The File header contains the following fields:
    ELF identification - array of bytes which helps identify the file as an ELF object file and also provides information about general object file characteristic;
    Object file type - identifies the object file type. This field can describe that ELF file is a relocatable object file, an executable file, etc...;
    Target architecture;
    Version of the object file format;
    Virtual address of the program entry point;
    File offset of the program header table;
    File offset of the section header table;
    Size of an ELF header;
    Size of a program header table entry;
    and other fields...
You can find the elf64_hdr structure which presents ELF64 header in the linux kernel source code:
1
typedef struct elf64_hdr {
2
unsigned char e_ident[EI_NIDENT];
3
Elf64_Half e_type;
4
Elf64_Half e_machine;
5
Elf64_Word e_version;
6
Elf64_Addr e_entry;
7
Elf64_Off e_phoff;
8
Elf64_Off e_shoff;
9
Elf64_Word e_flags;
10
Elf64_Half e_ehsize;
11
Elf64_Half e_phentsize;
12
Elf64_Half e_phnum;
13
Elf64_Half e_shentsize;
14
Elf64_Half e_shnum;
15
Elf64_Half e_shstrndx;
16
} Elf64_Ehdr;
Copied!
This structure defined in the elf.h​
Sections
All data stores in a sections in an Elf object file. Sections identified by index in the section header table. Section header contains following fields:
    Section name;
    Section type;
    Section attributes;
    Virtual address in memory;
    Offset in file;
    Size of section;
    Link to other section;
    Miscellaneous information;
    Address alignment boundary;
    Size of entries, if section has table;
And presented with the following elf64_shdr structure in the linux kernel:
1
typedef struct elf64_shdr {
2
Elf64_Word sh_name;
3
Elf64_Word sh_type;
4
Elf64_Xword sh_flags;
5
Elf64_Addr sh_addr;
6
Elf64_Off sh_offset;
7
Elf64_Xword sh_size;
8
Elf64_Word sh_link;
9
Elf64_Word sh_info;
10
Elf64_Xword sh_addralign;
11
Elf64_Xword sh_entsize;
12
} Elf64_Shdr;
Copied!
​elf.h​
Program header table
All sections are grouped into segments in an executable or shared object file. Program header is an array of structures which describe every segment. It looks like:
1
typedef struct elf64_phdr {
2
Elf64_Word p_type;
3
Elf64_Word p_flags;
4
Elf64_Off p_offset;
5
Elf64_Addr p_vaddr;
6
Elf64_Addr p_paddr;
7
Elf64_Xword p_filesz;
8
Elf64_Xword p_memsz;
9
Elf64_Xword p_align;
10
} Elf64_Phdr;
Copied!
in the linux kernel source code.
elf64_phdr defined in the same elf.h.
The ELF object file also contains other fields/structures which you can find in the Documentation. Now let's a look at the vmlinux ELF object.

vmlinux

vmlinux is also a relocatable ELF object file . We can take a look at it with the readelf util. First of all let's look at the header:
1
$ readelf -h vmlinux
2
ELF Header:
3
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
4
Class: ELF64
5
Data: 2's complement, little endian
6
Version: 1 (current)
7
OS/ABI: UNIX - System V
8
ABI Version: 0
9
Type: EXEC (Executable file)
10
Machine: Advanced Micro Devices X86-64
11
Version: 0x1
12
Entry point address: 0x1000000
13
Start of program headers: 64 (bytes into file)
14
Start of section headers: 381608416 (bytes into file)
15
Flags: 0x0
16
Size of this header: 64 (bytes)
17
Size of program headers: 56 (bytes)
18
Number of program headers: 5
19
Size of section headers: 64 (bytes)
20
Number of section headers: 73
21
Section header string table index: 70
Copied!
Here we can see that vmlinux is a 64-bit executable file.
We can read from the Documentation/x86/x86_64/mm.txt:
1
ffffffff80000000 - ffffffffa0000000 (=512 MB) kernel text mapping, from phys 0
Copied!
We can then look this address up in the vmlinux ELF object with:
1
$ readelf -s vmlinux | grep ffffffff81000000
2
1: ffffffff81000000 0 SECTION LOCAL DEFAULT 1
3
65099: ffffffff81000000 0 NOTYPE GLOBAL DEFAULT 1 _text
4
90766: ffffffff81000000 0 NOTYPE GLOBAL DEFAULT 1 startup_64
Copied!
Note that the address of the startup_64 routine is not ffffffff80000000, but ffffffff81000000 and now I'll explain why.
We can see following definition in the arch/x86/kernel/vmlinux.lds.S:
1
. = __START_KERNEL;
2
...
3
...
4
..
5
/* Text and read-only data */
6
.text : AT(ADDR(.text) - LOAD_OFFSET) {
7
_text = .;
8
...
9
...
10
...
11
}
Copied!
Where __START_KERNEL is:
1
#define __START_KERNEL (__START_KERNEL_map + __PHYSICAL_START)
Copied!
__START_KERNEL_map is the value from the documentation - ffffffff80000000 and __PHYSICAL_START is 0x1000000. That's why address of the startup_64 is ffffffff81000000.
And at last we can get program headers from vmlinux with the following command:
1
readelf -l vmlinux
2
​
3
Elf file type is EXEC (Executable file)
4
Entry point 0x1000000
5
There are 5 program headers, starting at offset 64
6
​
7
Program Headers:
8
Type Offset VirtAddr PhysAddr
9
FileSiz MemSiz Flags Align
10
LOAD 0x0000000000200000 0xffffffff81000000 0x0000000001000000
11
0x0000000000cfd000 0x0000000000cfd000 R E 200000
12
LOAD 0x0000000001000000 0xffffffff81e00000 0x0000000001e00000
13
0x0000000000100000 0x0000000000100000 RW 200000
14
LOAD 0x0000000001200000 0x0000000000000000 0x0000000001f00000
15
0x0000000000014d98 0x0000000000014d98 RW 200000
16
LOAD 0x0000000001315000 0xffffffff81f15000 0x0000000001f15000
17
0x000000000011d000 0x0000000000279000 RWE 200000
18
NOTE 0x0000000000b17284 0xffffffff81917284 0x0000000001917284
19
0x0000000000000024 0x0000000000000024 4
20
​
21
Section to Segment mapping:
22
Segment Sections...
23
00 .text .notes __ex_table .rodata __bug_table .pci_fixup .builtin_fw
24
.tracedata __ksymtab __ksymtab_gpl __kcrctab __kcrctab_gpl
25
__ksymtab_strings __param __modver
26
01 .data .vvar
27
02 .data..percpu
28
03 .init.text .init.data .x86_cpu_dev.init .altinstructions
29
.altinstr_replacement .iommu_table .apicdrivers .exit.text
30
.smp_locks .data_nosave .bss .brk
Copied!
Here we can see five segments with sections list. You can find all of these sections in the generated linker script at - arch/x86/kernel/vmlinux.lds.
That's all. Of course it's not a full description of ELF (Executable and Linkable Format), but if you want to know more, you can find the documentation - here​
Last modified 1yr ago
Copy link
Contents
vmlinux