20

An ELF file for executables has a program (segment) header and a section header, which can be seen through readelf -a, here is an example:

enter image description here

enter image description here

The two pictures above are section header and program (segment) header, respectively. It can be seen that a segment header is composed of several section headers, which is used for loading program into the memory.

Is it only necessary for .text, .rodata, .data, .bss sections to be loaded into the memory?

Are all of the other sections in the segment (e.g. .ctors, .dtors .jcr in the 3rd segment) used for aligning?

1
  • @Adriano,yes aligning to the page size, so different protection can be used for different part of the program, for example .text(r-x),.data(rw-) Commented May 2, 2012 at 16:03

2 Answers 2

28

Sections and segments are two different concepts completely. Sections pertain the the semantics of the data stored there (i.e. what it will be used for) and are actually irrelevant once a program or shared library is linked except for debugging purposes. You could even remove the section headers entirely (or overwrite them with random garbage) and a program would still work.

Segments (i.e. program header load directives) are what the kernel and/or dynamic linker actually look at when loading a program. For example, in your case you have two load directives. The first one causes the first 4k (1 page) of the file to be mapped at address 0x08048000, and indicates that only the first 0x4b8 bytes of this mapping are actually to be used (the rest is alignment). The second causes the first 8k (2 pages) of the file to be mapped at address 0x08049000. The vast majority of that is alignment. The first 0xf14 bytes are not part of the load directive (just alignment) and will be wasted. Beginning at 0x08049f14, 0x108 bytes mapped from the file are actually used, and another 0x10 bytes (to reach the MemSize of 0x118) are zero-filled by the loader (kernel or dynamic linker). This spans up to 0x0804a02c (in the second mapped page). The rest of the second mapped page is unused/wasted (but malloc might be able to recover it for use as part of the heap).

Finally, while the section headers will not be used at all, the contents of many different sections may be used by your program while it's running. Note that the address ranges of .ctors and .dtors lie in the beginning of the second load mapping, so they are mapped and accessible by the program at runtime (the runtime startup/exit code will use them to run global constructors and destructors, if C++ or "GNU C" code with ctor/dtor attribute was used). Also note that .data starts at address 0x0804a00c, in the second mapped page. This allows the first page to be protected read-only after relocations are applied (the RELRO directive in the program header).

Sign up to request clarification or add additional context in comments.

7 Comments

Does C have constructors or destructors as C++ ?
No, but GCC is not C, it's "GNU C". You can do all sorts of nonstandard stuff like exceptions and constructors.
@Soumen: The page size is an arch-specific ABI or runtime constraint; for x86_64 it's a multiple of 4k and in practice it's always 4k. Since the length of the segment exceeds the distance from the start (0xf14 mod 4k) to the next 4k boundary, it will take 2 pages at a 4k page size.
@R..I have another question The first 0xf14 bytes are not part of the load directive (just alignment) and will be wasted. - why this alignment is required? (as f14 seems quite a strange number for alignment)
@Soumen: I think I probably stated that poorly. It's more like packing than alignment, and it's a tradeoff that saves a small amount of (logical, probably not even real) disk space in exchange for wasting an extra page at runtime. The first 0xf14 bytes are ELF headers and the first (read-only) load segment's contents. If the linker had waited until offset 0x1000 to start the second load map, it would all have fit in one page.
|
0

I think these sections will be loaded into the memory.

1

1 Comment

Please don't show pictures of text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.