10fda4c47SLang Hames //===-------- ObjectLinkingLayerTest.cpp - ObjectLinkingLayer tests -------===//
20fda4c47SLang Hames //
30fda4c47SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40fda4c47SLang Hames // See https://llvm.org/LICENSE.txt for license information.
50fda4c47SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60fda4c47SLang Hames //
70fda4c47SLang Hames //===----------------------------------------------------------------------===//
80fda4c47SLang Hames 
90fda4c47SLang Hames #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
100fda4c47SLang Hames #include "llvm/ExecutionEngine/JITLink/JITLink.h"
110fda4c47SLang Hames #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
120fda4c47SLang Hames #include "llvm/ExecutionEngine/JITLink/x86_64.h"
130fda4c47SLang Hames #include "llvm/Testing/Support/Error.h"
140fda4c47SLang Hames #include "gtest/gtest.h"
150fda4c47SLang Hames 
160fda4c47SLang Hames using namespace llvm;
170fda4c47SLang Hames using namespace llvm::jitlink;
180fda4c47SLang Hames using namespace llvm::orc;
190fda4c47SLang Hames 
200fda4c47SLang Hames namespace {
210fda4c47SLang Hames 
220fda4c47SLang Hames const char BlockContentBytes[] = {0x01, 0x02, 0x03, 0x04,
230fda4c47SLang Hames                                   0x05, 0x06, 0x07, 0x08};
240fda4c47SLang Hames 
250fda4c47SLang Hames ArrayRef<char> BlockContent(BlockContentBytes);
260fda4c47SLang Hames 
270fda4c47SLang Hames class ObjectLinkingLayerTest : public testing::Test {
280fda4c47SLang Hames public:
~ObjectLinkingLayerTest()290fda4c47SLang Hames   ~ObjectLinkingLayerTest() {
300fda4c47SLang Hames     if (auto Err = ES.endSession())
310fda4c47SLang Hames       ES.reportError(std::move(Err));
320fda4c47SLang Hames   }
330fda4c47SLang Hames 
340fda4c47SLang Hames protected:
352487db1fSLang Hames   ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
360fda4c47SLang Hames   JITDylib &JD = ES.createBareJITDylib("main");
370fda4c47SLang Hames   ObjectLinkingLayer ObjLinkingLayer{
38962a2479SLang Hames       ES, std::make_unique<InProcessMemoryManager>(4096)};
390fda4c47SLang Hames };
400fda4c47SLang Hames 
TEST_F(ObjectLinkingLayerTest,AddLinkGraph)410fda4c47SLang Hames TEST_F(ObjectLinkingLayerTest, AddLinkGraph) {
420fda4c47SLang Hames   auto G =
430fda4c47SLang Hames       std::make_unique<LinkGraph>("foo", Triple("x86_64-apple-darwin"), 8,
440fda4c47SLang Hames                                   support::little, x86_64::getEdgeKindName);
450fda4c47SLang Hames 
46962a2479SLang Hames   auto &Sec1 = G->createSection("__data", MemProt::Read | MemProt::Write);
47*118e953bSLang Hames   auto &B1 = G->createContentBlock(Sec1, BlockContent,
48*118e953bSLang Hames                                    orc::ExecutorAddr(0x1000), 8, 0);
490fda4c47SLang Hames   G->addDefinedSymbol(B1, 4, "_X", 4, Linkage::Strong, Scope::Default, false,
500fda4c47SLang Hames                       false);
510fda4c47SLang Hames 
520fda4c47SLang Hames   EXPECT_THAT_ERROR(ObjLinkingLayer.add(JD, std::move(G)), Succeeded());
530fda4c47SLang Hames 
540fda4c47SLang Hames   EXPECT_THAT_EXPECTED(ES.lookup(&JD, "_X"), Succeeded());
550fda4c47SLang Hames }
560fda4c47SLang Hames 
570fda4c47SLang Hames } // end anonymous namespace
58