Page 1 of 1

Dynamic linking. Import / Export of symbols.

Posted: Wed Jun 16, 2010 6:19 am
by mutex
Hi,

I compile my loadable modules as objects that export a set of functions. That works well.

Now i need to import functions aswell for those same modules. My code works for both ELF and PE, but the PE loader is more functional at the moment. My goal is to support both ELF and PE dynamlic loading, relocation and calling between both formats.

Now my issue is when i compile the modules and include kernel functions from the kernel headers. I have configured the KERNEL_API macro and the MODULE_API macro to be either __declspec(dllimport) / dllexport based on what is compiling. That seems to work, but the problem is my module gets "unresolvable symbol" during linking. The binary have exports and imports + relocations in its file. The exports look normal, but my imports is prefixed with __IMP__ on the symbol name.

Any idea what is wrong? I guess the __IMP__ should be a code-stub/trampoline in the file that can be patched to jump to the function in mention in the kernel for this case.. Do i need to create some sort of lib from the kernel (that makes this stubs) and link it into my module statically?

I use MS vc++ 2010 for compiling now. Makefiles are working for gcc and intel c++ answell, but i dont think this is a compiler issue,, more like linker usage / config..

Anyone got any ideas how i fix this?

-
Thomas

Re: Dynamic linking. Import / Export of symbols.

Posted: Wed Jun 16, 2010 10:25 am
by mutex
I actually managed to fix it.

Seems that when i compile my kernel with some of its functions __declspec(export) it creates a .exp and a .lib file.

When i compile my moduleXXX.c and include from kernel.h my exported functions are __declspec(dllimport) by my macro ifndef/else/endif. That makes the module want to link against a lib file for some __imp__ symbols. These symbols are actually generated on when the kernel are compiled.

So basically linking the kernel.lib file and the moduleXXX.obj to a dll/sys/whatever makes it work.

All that is linked in from kernel.lib is some data variables "__imp__" stuff and that makes the compiled module happy.

So no my modules both export functions and import some from the kernel itself. NICE :)

I can take some time and complement the MSVC++ kernel development info on the WIKI with my findings if others come into the same issues.

I guess for ELF the solution is basically the same...

-
Thomas