Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How import libraries work exactly? [duplicate]

Tags:

c++

I've heard that import libraries have "stubs" so the executable knows which dll and function we want. Now my question is: What are those stubs? What do they look like? How does the executable set the pointers to right places in the DLL? And how does it know if we are talking about normal library or import library since they both are .lib files?

like image 831
Daniel Nyman Avatar asked Jan 20 '26 16:01

Daniel Nyman


1 Answers

The MSVC linker does not link directly to a DLL, it can only link to a static library (.lib).

Very roughly speaking, an import library is a normal static library, that has a stub function for every DLL exported function.

For example, if a DLL has a function void func1() and exports func1, the corresponding import library will contain a stub function void __imp__func1() { __asm { jmp dword ptr func1; } }

At run time, the "jump" will take the address of func1 from the import table. That's why a stub is needed.

The declaration void __declspec(dllimport) func1(); in your application will in fact refer to __imp__func1().

You can bypass this whole mechanism altogether and call LoadLibrary and GetProcAddress to get the address of func1 at run time. It will have the same effect.

like image 148
rustyx Avatar answered Jan 22 '26 07:01

rustyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!