user11224591
user11224591

Reputation:

does the dynamic linker modify the reference after executable is copied to memory?

let's say we have the following code:

main.c

extern void some_function();

int main()
{
   some_function();
   return 0;
}

mylib.c

void some_function()
{
   ...
}

and I have create a share library mylib.so and link i to executable object file prog

linux> gcc -shared -fpic -o mylib.so mylib.c
linux> gcc -o prog main.c ./mylib.so

and let's say the picture below is the executable object format of prog

enter image description here

By dynamic linking, we know that none of the code or data sections from mylib.so are actually copied into the executable prog2l at this point. Instead, the linker copies some relocation and symbol table information that will allow references to code and data in mylib.so to be resolved at load time.

and I just want double check if my understanding is correct:

when prog is loaded into the memory by the loader as the picture below show

enter image description here

then the dynamic linker will modify the .data section of prog in memory so that it can be linked/relocated to the instruction address of some_function in the .text section of mylib.so.

Is my understanding correct?

Upvotes: 1

Views: 151

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215637

Fairly close. The dynamic linker will modify something in the data segment, not specifically the .data section - segments are a coarser-grained thing corresponding to how the file is mapped into memory rather than the original semantic breakdown. The actual section is usually called .got or .got.plt but may vary by platform. The modification is not "relocating it to the instruction address" but resolving a relocation reference to the function name to get the address it was loaded at, and filling that address in.

Upvotes: 1

Related Questions