Har
Har

Reputation: 3918

ELF: Using the section size to calculate an address span

I was wondering about the size of a section in relation to the address space the section occupies. (I am not assuming dynamic loading or MMUs here)

Does a section size include 0 size symbols if there are any?

For instance say a section size is 100 bytes long and starts at address 0. Naively I would assume that the address space taken by this section would be from 0 to 100.

Assuming however that there are symbols there at address 0, 1, 2 and 3 which have a size of 0 but do have an address associated with them then the actual address space would be 0-103 with 0-3 as being empty?

Are there such symbols? I am new to the ELF format and not 100% sure how it would work.

Upvotes: 0

Views: 186

Answers (1)

Employed Russian
Employed Russian

Reputation: 213754

I was wondering about the size of a section in relation to the address space the section occupies.

A section does not normally occupy any address -- a segment does.

ELF stands for executable and linkable format, and serves dual purpose: (static) linking and execution.

During the linking phase, the linker operates on sections, and assigns them to 0 or more segments (but usually to at most 1 loadable segment). Some sections, such as .note or .comment usually don't have SHF_ALLOC flag set, and do not end up in any loadable segment.

Note that sections are not needed after static link, and can be completely stripped out.

During the execution phase, loadable segments are mmaped into the address space. If a section had size 100, had SHF_ALLOC flag, and got assigned to some PT_LOAD segment, then that section will occupy 100 bytes of the address space.

(I am not assuming dynamic loading or MMUs here)

Dynamic linking and MMUs are completely orthogonal to what's happening here. By mentioning them, you only muddy the waters.

For instance say a section size is 100 bytes long and starts at address 0. Naively I would assume that the address space taken by this section would be from 0 to 100.

As stated above, you view of the world is not entirely accurate, and the section is very unlikely to actually occupy the [0, 100) address range.

Assuming however that there are symbols there at address 0, 1, 2 and 3 which have a size of 0 but do have an address associated with them then the actual address space would be 0-103 with 0-3 as being empty?

Symbols are merely labels attached to certain addresses. They don't occupy any address space themselves. They can also be completely stripped after (static) link, though usually they are left in to simplify debugging. The presence of these symbols / labels is what allows the debugger to tell you that your program crashes in e.g. fscanf called from foo, which was called from main.

Upvotes: 1

Related Questions