flat assembler
Message board for the users of flat assembler.
Index
> Linux > Explain Like I'm 5: Segments in ELF files. |
Author |
|
bzt 25 Nov 2018, 13:48
Since you got no answers, I'll try to explain.
The ELF format has two separate views: sections and segments. Sections are used when linking. You typically create different sections for different types of code (.text, .text.init, .text.fini, etc.) and data (.data.readonly, .data.initialized, .bss (uninitialized data) etc.). Sections do not have memory addresses associated with them, only file offsets. When you link your object files, the linker uses these sections to concatenate code and data in their final place. With a linker script you can control what sections you want to see in the object file. With fasm, you can use directives in your assembly file to define those sections. On the final step of linking, when you're producing an executable, those sections are assigned with a virtual memory address (VMA), and according to their attibutes (read, write, execute, initialized etc.) they are gather together into segments. With fasm, you can eliminate this step, and you can create an executable directly using segment directives. When you run your executable, the loader (that's called ld-linux.so under Linux) does not know anything about your sections, it only sees those segments. To understand this a bit better, I suggest to use "readelf -a hello" on your compiled code. You'll see Program Headers (those are the segments that needs to be loaded into memory at different VMAs), a segment mapping of sections (telling you which section ended up in which segment), and a Section Headers table (only used by the linker). You'll see that both segments and sections are referencing the same parts of your ELF file. For example: if you have code section (called text) and read only data (called .data.ro), then you have two different sections, which are used as two independent entities by the linker (one is code and the other is data after all). Despite of this, it's very likely that they will be concatenated into the same single segment, because you have to load and map both non-writable, and the loader does not need to know if the bytes it's loading are code or data. Cheers, bzt |
|||
25 Nov 2018, 13:48 |
|
Tomasz Grysztar 25 Nov 2018, 14:29
This was also discussed in several other threads, like here.
|
|||
25 Nov 2018, 14:29 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.