A segment of PT_LOAD type, flags PF_write | PF_read, p_filesiz = x and p_memsiz = x+y it's up to the kernel allocate p_memsiz, correct? How does this allocation actually works? the kernel allocate p_memsiz and put the begging of this block memory on p_vaddr so that the write/read memory region of this segment is from p_vaddr up to p_vaddr + p_memsiz? if so, what values(address) is the kernel expecting at p_vaddr at time before it does the allocation?
Add a comment |
1 Answer
Not exactly sure what you are asking, but I can recommend looking into the source code of the dynamic linker, ld.so, which actually loads the ELF objects.
See the source code of _dl_map_object_from_fd , have a look at the __mmap invocations there (one case for relocatable objects, one case for objects that need to be loaded at a fixed address). The parameters of those calls should tell you everything on how the memory is allocated.
7 Comments
The Mask
I don't how to explain better. But let me try again...
v = malloc(n) allocate n bytes and return v, the address of the begging of memory block allocated (assuming success). The memory block is from from v up to n. I want to know the equivalent to my segment. In my context, n is l.p_memsiz and I want to know where is v. I think it's p_vaddr but I'm not sure. (l of of Elf32_phdr as defined in elf.h)Thomas McGuire
In the dynamic linker code, you'll see that it uses mmap() to allocate the memory, not malloc(). In the case of relocatable objects, the dynamic linker allocates all segments in one go. The first parameter to mmap() is "mappref", which seems to be p_vaddr of the first PT_LOAD header. Note that this is just a suggestion to mmap(), since MAP_FIXED is not passed - the OS might choose an arbitrary address. This is because p_vaddr is just the preferred load address for relocatable ELF objects.
The Mask
Actually
malloc() usage was just an example. I wanted to desmotre a type of memory request and how I can find its address. Also, even if executable is static and not dynamic could OS still choose an arbitrary address?Thomas McGuire
There are two cases: relocatable (what you call "dynamic") and non-relocatable (what you call "static") ELF objects. Almost all shared libraries are relocatable, almost all executables are non-relocatable. Relocatable ELF objects sometimes have a preferred load address (such as when prelinking is used), but most often the dynamic linker is free to chose an arbitrary address when loading these ELF objects. Non-relocatable ELF objects have a fixed load address - the dynamic linker is forced to allocate memory at exactly that address, which is done by passing the MAP_FIXED flag to mmap().
The Mask
Thanks again. Also, are the contents of a segment followed by its respective value or it's structured so that it's one segment following each one other?
|