An Introduction to ELF header, Program headers and Section headers.

ELF headers plays a role as the road map directing how the file format is organized.

An ELF file format can supply the necessary information during the program linking and process loading, section headers and program headers respectively.

Through the ELF headers, we know the number of sections in the file and the number of segments during the execution.

See the following examples.

Section header

$ riscv32-elf-readelf -S main_pic.bin

There are 14 section headers, starting at offset 0x4ad8:

Section Headers:

[Nr] Name Type Addr Off Size ES Flg Lk Inf Al

[ 0] NULL 00000000 000000 000000 00 0 0 0

[ 1] .text PROGBITS 00010074 000074 0029ee 00 AX 0 0 2

[ 2] .rodata PROGBITS 00012a64 002a64 000010 00 A 0 0 4

[ 3] .eh_frame PROGBITS 00012a74 002a74 000004 00 A 0 0 4

[ 4] .init_array INIT_ARRAY 00013000 003000 000004 04 WA 0 0 4

[ 5] .fini_array FINI_ARRAY 00013004 003004 000004 04 WA 0 0 4

[ 6] .data PROGBITS 00013008 003008 000830 00 WA 0 0 8

[ 7] .sdata PROGBITS 00013838 003838 000014 00 WA 0 0 4

[ 8] .sbss NOBITS 0001384c 00384c 000018 00 WA 0 0 4

[ 9] .bss NOBITS 00013864 00384c 000048 00 WA 0 0 4

[10] .comment PROGBITS 00000000 00384c 00003c 01 MS 0 0 1

[11] .symtab SYMTAB 00000000 003888 000b30 10 12 71 4

[12] .strtab STRTAB 00000000 0043b8 0006b2 00 0 0 1

[13] .shstrtab STRTAB 00000000 004a6a 00006c 00 0 0 1

Key to Flags:

W (write), A (alloc), X (execute), M (merge), S (strings), I (info),

L (link order), O (extra OS processing required), G (group), T (TLS),

C (compressed), x (unknown), o (OS specific), E (exclude),

p (processor specific)

Program header

$ riscv32-elf-readelf -l main_pic.bin

Elf file type is EXEC (Executable file)

Entry point 0x1009e

There are 2 program headers, starting at offset 52

Program Headers:

Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align

LOAD 0x000000 0x00010000 0x00010000 0x02a78 0x02a78 R E 0x1000

LOAD 0x003000 0x00013000 0x00013000 0x0084c 0x008ac RW 0x1000

Section to Segment mapping:

Segment Sections...

00 .text .rodata .eh_frame

01 .init_array .fini_array .data .sdata .sbss .bss

ELF header

$ riscv32-elf-readelf -h main_pic.bin

ELF Header:

Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Class: ELF32

Data: 2's complement, little endian

Version: 1 (current)

OS/ABI: UNIX - System V

ABI Version: 0

Type: EXEC (Executable file)

Machine: RISC-V

Version: 0x1

Entry point address: 0x1009e

Start of program headers: 52 (bytes into file)

Start of section headers: 19160 (bytes into file)

Flags: 0x1, RVC, soft-float ABI

Size of this header: 52 (bytes)

Size of program headers: 32 (bytes)

Number of program headers: 2

Size of section headers: 40 (bytes)

Number of section headers: 14

Section header string table index: 13

We can know the starting offset, the number and the size of program headers and section table.

The data structure of ELF header is defined in Elf32_Ehdr.

results matching ""

    No results matching ""