I'm really interested in understanding the linkage mechanizm.
Specifically I wish to understand how does dll linking work.As I understand it,
calling i开发者_运维技巧nternal function, is actually converted by the compiler to the method's address. That isdoSomething();
is (sortof) converted to jmp 00102356
.
I know this is over simplifying, since it's actually a call
instruction.
But the idea is, that the IP
is told where to jmp
, because we know the method's address.
What happens with methods from external dlls?
Are they always assumed to lie in a specific fixed place in memory, to which we call?Many thanks :)
For a call into a DLL, there is a table of all addresses of functions in the DLL. The code generates a lookup into that table, then an indirect call to the correct address of the loaded function. Functions are not always at specific, fixed places in memory; think of function pointers (which is what you use when loading modules dynamically). See the Wikipedia page on DLLs for much more explanation.
At run-time, external DLL references are also resolved to absolute memory addresses that have been mapped into the executable's address space.
The executable file contains a list of required DLL files and these are loaded or mapped into memory and all the 'call' references in machine language are modified to the correct address for any exported 'dllexport' functions.
Shared DLL's are only loaded once into physical memory, but this code is logically mapped into the address space of any exe that is using them.
When everything's loaded, it looks like one monolithic machine language program to the CPU.
Alternatively, a programmer can use the LoadLibrary Windows API function to load a DLL into the program's address space at runtime and the GetProcAddress API function returns the physical address, which can be used to call DLL functions through a function pointer variable.
精彩评论