[LLVMdev] having troubles mixing ExecutionEngine::runStaticConstructorsDestructors() and Linker::LinkModules()

Graham Wakefield wakefield at mat.ucsb.edu
Fri Jun 18 01:23:26 PDT 2010

The use case is to create a module A, JIT from it, then extend the module with new code (create a new module B and use Linker::LinkModules() to merge into A, and be able to JIT the new symbols).  This appears to work ok, except that static constructors are giving me a headache.

Generally, I start like this:

Create module A (e.g. via Clang, LLVM API etc.)
EE->runStaticConstructorsDestructors(A, false);

Then, create module B (e.g. via Clang, LLVM API etc.) to be used to extend module A. I've tried out the following sequences of calls to get this to work with statics, with the following difficulties:

Option 1);

Linker::LinkModules(A, B);								
EE->runStaticConstructorsDestructors(B, false);			// <- note, Module B
--> llvm error Program used external function '__cxx_global_initialization' which could not be resolved! 

Presumably because module B no longer exists.

Option 2);

Linker::LinkModules(A, B);								
EE->runStaticConstructorsDestructors(A, false);			// <- note, Module A
EE->getPointerToFunction(A->getFunction(funcname));	 // e.g. funcname was originally defined in module A

Usually leads to a crash - apparently the statics originally defined in module A get called twice, while the statics defined in module B never get called.

Option 3);

EE->runStaticConstructorsDestructors(B, false);			// appears to trigger B's statics (even though B is not in EE yet...)
Linker::LinkModules(A, B);								
EE->getPointerToFunction(A->getFunction(funcname));	 // e.g. funcname was originally defined in module B but now in A

The problem is that the code emitted in the last getPointerToFunction() does not appear to use the statics initialized from module B... presumably because __cxx_global_initialization was already defined.

Is there any possible solution for what I want to do? Or should I always keep the modules separate (not use LinkModules()) for this situation?

Thanks in advance

More information about the llvm-dev mailing list