Mike Woodworth
Mike Woodworth

Reputation: 123

RTDyldMemoryManager::getSymbolAddressInProcess not finding symbols in host process

I have a static lib (.a) that uses the llvm JIT to run code. I want it to be able to call functions in the main app. I have created an extern declaration and called it in my IR. I can see the JIT is trying to resolve my symbol by calling getSymbolAddress on my memory manager subclass. For this symbol I should be able to just find it using:

 if (auto SymAddr = RTDyldMemoryManager::getSymbolAddressInProcess(name)){
            return SymAddr;
        }

But I'm finding getSymbolAddressInProcess always returns 0.

The function I'm trying to find is declared extern "C".

I have confirmed it is not a mangling issue (i try to resolve with and without the prefixed _). I've tried calling the function in my host to confirm it isn't being stripped by the compiler/linker. I can call it from the debugger as well, confirming it exists.

Similar questions pointed to the need to call

llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr, &error); 

which I am doing, and no error is returned.

Upvotes: 1

Views: 165

Answers (1)

Mike Woodworth
Mike Woodworth

Reputation: 123

It turns out symbol I was trying to find in the host process was being marked local (t) as discovered by nm.

nm HostProcess | grep _function_name
00000001000f8590 t _function_name

The solution was to add

__attribute__((visibility("default")))

to the function declaration. nm returns global (T) now and getSymbolAddressInProcess returns the function ptr.

Upvotes: 2

Related Questions