}
std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
+ assert(GV->hasName() && "Global must have name.");
+
MutexGuard locked(lock);
- Mangler Mang;
SmallString<128> FullName;
- Mang.getNameWithPrefix(FullName, GV, false);
+
+ const DataLayout &DL =
+ GV->getParent()->getDataLayout().isDefault()
+ ? getDataLayout()
+ : GV->getParent()->getDataLayout();
+
+ Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
return FullName.str();
}
RuntimeDyld::SymbolInfo MCJIT::findExistingSymbol(const std::string &Name) {
SmallString<128> FullName;
Mangler::getNameWithPrefix(FullName, Name, getDataLayout());
+
+ if (void *Addr = getPointerToGlobalIfAvailable(FullName))
+ return RuntimeDyld::SymbolInfo(static_cast<uint64_t>(
+ reinterpret_cast<uintptr_t>(Addr)),
+ JITSymbolFlags::Exported);
+
return Dyld.getSymbol(FullName);
}
EXPECT_TRUE(didCallYield);
}
+static int localTestFunc() {
+ return 42;
+}
+
+TEST_F(MCJITCAPITest, addGlobalMapping) {
+ SKIP_UNSUPPORTED_PLATFORM;
+
+ Module = LLVMModuleCreateWithName("testModule");
+ LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), NULL, 0, 0);
+ LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType);
+
+ Function = LLVMAddFunction(Module, "test_fn", FunctionType);
+ LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "");
+ LLVMBuilderRef Builder = LLVMCreateBuilder();
+ LLVMPositionBuilderAtEnd(Builder, Entry);
+ LLVMValueRef RetVal = LLVMBuildCall(Builder, MappedFn, NULL, 0, "");
+ LLVMBuildRet(Builder, RetVal);
+
+ LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
+ LLVMDisposeMessage(Error);
+
+ buildMCJITOptions();
+ buildMCJITEngine();
+
+ LLVMAddGlobalMapping(Engine, MappedFn, reinterpret_cast<void*>(&localTestFunc));
+
+ buildAndRunPasses();
+
+ uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn");
+ int (*usable)() = (int (*)()) raw;
+
+ EXPECT_EQ(42, usable());
+}