F van der Meeren
2010-Aug-12 17:43 UTC
[LLVMdev] LLVM-C: Calling functions contained in other libraries
Hello, I have a question concerning llvm-c. I have set up a function that needs to invoke an external method, in a other library. It has the following signature: void* NSFullUserName(void); The void* can be replaced with a i8*, that far I was able to get, but when my call is invoked, the engine gives me the following message: LLVM ERROR: Tried to execute an unknown external function: i8* ()* NSFullUserName I have dumped the module (without the clutter): declare i8* @NSFullUserName() define i8* @MyFunction() { entrypoint: %myCall = call i8* @NSFullUserName() ; <i8*> [#uses=1] ret i8* %myCall } Where am I going wrong here? To add some code to my previous question: LLVMTypeRef i8Ptr(void) { return LLVMPointerType(LLVMInt8Type(), 0); } LLVMValueRef d(LLVMModuleRef module) { LLVMValueRef result; LLVMBasicBlockRef block; LLVMBuilderRef builder = LLVMCreateBuilder(); LLVMValueRef fullUsername = LLVMAddFunction(module, "NSFullUserName", LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0), NULL, 0, 0)); LLVMSetLinkage(fullUsername, LLVMExternalLinkage); result = LLVMAddFunction(module, "MyFunction", LLVMFunctionType(i8Ptr(), NULL, 0, 0)); block = LLVMAppendBasicBlock(result, "entrypoint"); LLVMPositionBuilderAtEnd(builder, block); LLVMValueRef returnVal = LLVMBuildCall(builder, fullUsername, NULL, 0, "myCall"); LLVMBuildRet(builder, returnVal); LLVMDisposeBuilder(builder); return result; } int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; char * error = NULL; LLVMExecutionEngineRef engine; LLVMModuleRef module = LLVMModuleCreateWithName("MyModule"); LLVMValueRef toCall = d(module); LLVMDumpModule(module); LLVMLinkInInterpreter(); LLVMCreateInterpreterForModule(&engine, module, &error); LLVMGenericValueRef result = LLVMRunFunction(engine, toCall, 0, NULL); LLVMDisposeModule(module); [pool drain]; return 0; } Thank you, Filip -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100812/388c33da/attachment.html>
Eric Christopher
2010-Aug-12 17:55 UTC
[LLVMdev] LLVM-C: Calling functions contained in other libraries
On Aug 12, 2010, at 10:43 AM, F van der Meeren wrote:> Where am I going wrong here? >Did you link against the library that contains the function? -eric
F van der Meeren
2010-Aug-12 18:10 UTC
[LLVMdev] LLVM-C: Calling functions contained in other libraries
The function is called in my code, just to be sure it is reachable... On 12 Aug 2010, at 19:55, Eric Christopher wrote:> > On Aug 12, 2010, at 10:43 AM, F van der Meeren wrote: > >> Where am I going wrong here? >> > > Did you link against the library that contains the function? > > -eric
F van der Meeren
2010-Aug-12 21:21 UTC
[LLVMdev] LLVM-C: Calling functions contained in other libraries
This might be a bug (or feature depending on vision of the software). I have tried this with the global mapping, and it still won't work. But by replacing the calling code with the JIT: LLVMInitializeNativeTarget(); LLVMLinkInJIT(); LLVMCreateJITCompilerForModule(&engine, module, 0, &error); //LLVMAddGlobalMapping(engine, toCall, (void*)&NSFullUserName); LLVMGenericValueRef result = LLVMRunFunction(engine, toCall, 0, NULL); NSLog(@"Second attempt: %@", LLVMGenericValueToPointer(result)); It suddenly works like a charm, without any global mapping required. Filip On 12 Aug 2010, at 19:55, Eric Christopher wrote:> > On Aug 12, 2010, at 10:43 AM, F van der Meeren wrote: > >> Where am I going wrong here? >> > > Did you link against the library that contains the function? > > -eric