Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference of section and segment in ELF file format

From wiki Executable and Linkable Format:

The segments contain information that is necessary for runtime execution of the file, while sections contain important data for linking and relocation. Any byte in the entire file can be owned by at most one section, and there can be orphan bytes which are not owned by any section.

But what is the difference between section and segment? In an executable ELF file, does a segment contain one or more sections?

like image 817
tsing Avatar asked Jan 16 '13 14:01

tsing


People also ask

What is the difference between segment and section?

Thus, a segment is almost exactly the same as a section, and the two can be used interchangeably. However, segment carries a secondary meaning and so can also be used when specifically talking about lines and planes in geometry. So you can use part whenever you want to talk about any piece of a larger entity.

How do I decode an ELF file?

you can use readelf and objdump to read parts of an elf file. You can also use 'hexdump filename' to get a hexdump of the contents of a binary file (this is likely only useful if you like reading machine code or you are writing an assembler).

How do I size an ELF file?

You can use the stat functions family ( stat() , lstat() , fstat() ) to get the size of any file (using the st_size member of the stat member). Do you need something more specific? If you really want to use the ELF structure, use the elf.


1 Answers

But what's difference between section and segment?

Exactly what you quoted: the segments contain information needed at runtime, while the sections contain information needed during linking.

does a segment contain one or more sections?

A segment can contain 0 or more sections. Example:

readelf -l /bin/date  Elf file type is EXEC (Executable file) Entry point 0x402000 There are 9 program headers, starting at offset 64  Program Headers:   Type           Offset             VirtAddr           PhysAddr                  FileSiz            MemSiz              Flags  Align   PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040                  0x00000000000001f8 0x00000000000001f8  R E    8   INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238                  0x000000000000001c 0x000000000000001c  R      1       [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]   LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000                  0x000000000000d5ac 0x000000000000d5ac  R E    200000   LOAD           0x000000000000de10 0x000000000060de10 0x000000000060de10                  0x0000000000000440 0x0000000000000610  RW     200000   DYNAMIC        0x000000000000de38 0x000000000060de38 0x000000000060de38                  0x00000000000001a0 0x00000000000001a0  RW     8   NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254                  0x0000000000000044 0x0000000000000044  R      4   GNU_EH_FRAME   0x000000000000c700 0x000000000040c700 0x000000000040c700                  0x00000000000002a4 0x00000000000002a4  R      4   GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000                  0x0000000000000000 0x0000000000000000  RW     8   GNU_RELRO      0x000000000000de10 0x000000000060de10 0x000000000060de10                  0x00000000000001f0 0x00000000000001f0  R      1   Section to Segment mapping:   Segment Sections...    00         01     .interp     02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame     03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss     04     .dynamic     05     .note.ABI-tag .note.gnu.build-id     06     .eh_frame_hdr     07         08     .ctors .dtors .jcr .dynamic .got  

Here, PHDR segment contains 0 sections, INTERP segment contains .interp section, and the first LOAD segment contains a whole bunch of sections.

Further reading with a nice illustration:

enter image description here

like image 159
Employed Russian Avatar answered Oct 15 '22 08:10

Employed Russian