diff options
author | Keno Fischer <kfischer@college.harvard.edu> | 2015-01-27 20:02:31 +0000 |
---|---|---|
committer | Keno Fischer <kfischer@college.harvard.edu> | 2015-01-27 20:02:31 +0000 |
commit | 811b152d85e861bb0b0aeb27a2bbb5e29214d554 (patch) | |
tree | 7253018c54fdec4fc0bb0ec59564b184f64f1b19 /unittests/ExecutionEngine | |
parent | 99b52293c7cb9c6b3a76a07a4922a9d8c8122e6b (diff) |
[ExecutionEngine] Add weak symbol support to RuntimeDyld
Support weak symbols by first looking up if there is an externally visible symbol we can find,
and only if that fails using the one in the object file we're loading.
Reviewed By: lhames
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D6950
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227228 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/ExecutionEngine')
-rw-r--r-- | unittests/ExecutionEngine/MCJIT/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unittests/ExecutionEngine/MCJIT/MCJITTest.cpp | 42 | ||||
-rw-r--r-- | unittests/ExecutionEngine/MCJIT/MCJITTestBase.h | 18 | ||||
-rw-r--r-- | unittests/ExecutionEngine/MCJIT/Makefile | 2 |
4 files changed, 62 insertions, 1 deletions
diff --git a/unittests/ExecutionEngine/MCJIT/CMakeLists.txt b/unittests/ExecutionEngine/MCJIT/CMakeLists.txt index b10cbb4c9ea..dcf8264203e 100644 --- a/unittests/ExecutionEngine/MCJIT/CMakeLists.txt +++ b/unittests/ExecutionEngine/MCJIT/CMakeLists.txt @@ -1,5 +1,6 @@ set(LLVM_LINK_COMPONENTS Analysis + AsmParser Core ExecutionEngine IPO diff --git a/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp b/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp index 64d8c2fca04..f9ca0fc8717 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp +++ b/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ExecutionEngine/MCJIT.h" +#include "llvm/Support/DynamicLibrary.h" #include "MCJITTestBase.h" #include "gtest/gtest.h" @@ -199,4 +200,45 @@ TEST_F(MCJITTest, multiple_decl_lookups) { EXPECT_EQ(A, B) << "Repeat calls to getPointerToFunction fail."; } +// Test weak symbol linking when the weak symbol is present in a shared +// library +TEST_F(MCJITTest, weak_symbol_present) { + SKIP_UNSUPPORTED_PLATFORM; + + int FakeWeakSymbol; + llvm::sys::DynamicLibrary::AddSymbol("FakeWeakSymbol", &FakeWeakSymbol); + createJITFromAssembly( + "$FakeWeakSymbol = comdat any\n" + "@FakeWeakSymbol = linkonce_odr global i32 42, comdat, align 4\n" + "define i32 @weak_test(i32* %arg) {\n" + " %r = icmp eq i32* %arg, @FakeWeakSymbol\n" + " %ret = zext i1 %r to i32\n" + " ret i32 %ret\n" + " }"); + + uint64_t Addr = TheJIT->getFunctionAddress("weak_test");; + EXPECT_TRUE(Addr != 0); + int32_t(*FuncPtr)(int32_t *) = (int32_t(*)(int32_t *))Addr; + EXPECT_EQ(FuncPtr(&FakeWeakSymbol),1); + EXPECT_TRUE(TheJIT->getGlobalValueAddress("FakeWeakSymbol") == 0); +} + +// Test weak symbol linking when the weak symbol is not present in a +// shared library +TEST_F(MCJITTest, weak_symbol_absent) { + SKIP_UNSUPPORTED_PLATFORM; + + SMDiagnostic Error; + createJITFromAssembly( + " $FakeWeakSymbol2 = comdat any\n" + " @FakeWeakSymbol2 = linkonce_odr global i32 42, comdat, align 4\n" + " define i32* @get_weak() {\n" + " ret i32* @FakeWeakSymbol2\n" + " }\n"); + void*(*FuncPtr)() = + (void*(*)(void))TheJIT->getFunctionAddress("get_weak"); + EXPECT_EQ(FuncPtr(),(void*)TheJIT->getGlobalValueAddress("FakeWeakSymbol2")); +} + + } diff --git a/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h b/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h index 35af417bf14..b1943a763d6 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h +++ b/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h @@ -18,6 +18,7 @@ #define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTBASE_H #include "MCJITTestAPICommon.h" +#include "llvm/AsmParser/Parser.h" #include "llvm/Config/config.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/SectionMemoryManager.h" @@ -27,6 +28,8 @@ #include "llvm/IR/Module.h" #include "llvm/IR/TypeBuilder.h" #include "llvm/Support/CodeGen.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" namespace llvm { @@ -338,6 +341,21 @@ protected: assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder"); } + void createJITFromAssembly(const char *Test) { + SMDiagnostic Error; + M = parseAssemblyString(Test, Error, Context); + + std::string errMsg; + raw_string_ostream os(errMsg); + Error.print("", os); + + // A failure here means that the test itself is buggy. + if (!M) + report_fatal_error(os.str().c_str()); + + createJIT(std::move(M)); + } + CodeGenOpt::Level OptLevel; Reloc::Model RelocModel; CodeModel::Model CodeModel; diff --git a/unittests/ExecutionEngine/MCJIT/Makefile b/unittests/ExecutionEngine/MCJIT/Makefile index 2822b20cdda..522ef750df7 100644 --- a/unittests/ExecutionEngine/MCJIT/Makefile +++ b/unittests/ExecutionEngine/MCJIT/Makefile @@ -9,7 +9,7 @@ LEVEL = ../../.. TESTNAME = MCJIT -LINK_COMPONENTS := core ipo mcjit native support +LINK_COMPONENTS := core asmparser ipo mcjit native support include $(LEVEL)/Makefile.config include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest |