1 //===-- ObjectLinkingLayerTest.cpp - Unit tests for object linking layer --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ExecutionEngine/ExecutionEngine.h"
11 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
12 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
13 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
14 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "gtest/gtest.h"
18 
19 using namespace llvm;
20 using namespace llvm::orc;
21 
22 namespace {
23 
TEST(ObjectLinkingLayerTest,TestSetProcessAllSections)24 TEST(ObjectLinkingLayerTest, TestSetProcessAllSections) {
25 
26   class SectionMemoryManagerWrapper : public SectionMemoryManager {
27   public:
28     SectionMemoryManagerWrapper(bool &DebugSeen) : DebugSeen(DebugSeen) {}
29     uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
30                                  unsigned SectionID,
31                                  StringRef SectionName,
32                                  bool IsReadOnly) override {
33       if (SectionName == ".debug_str")
34         DebugSeen = true;
35       return SectionMemoryManager::allocateDataSection(Size, Alignment,
36                                                          SectionID,
37                                                          SectionName,
38                                                          IsReadOnly);
39     }
40   private:
41     bool DebugSeen;
42   };
43 
44   ObjectLinkingLayer<> ObjLayer;
45 
46   auto M = llvm::make_unique<Module>("", getGlobalContext());
47   M->setTargetTriple("x86_64-unknown-linux-gnu");
48   Type *Int32Ty = IntegerType::get(getGlobalContext(), 32);
49   GlobalVariable *GV =
50     new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
51                          ConstantInt::get(Int32Ty, 42), "foo");
52 
53   GV->setSection(".debug_str");
54 
55   std::unique_ptr<TargetMachine> TM(
56     EngineBuilder().selectTarget(Triple(M->getTargetTriple()), "", "",
57                                  SmallVector<std::string, 1>()));
58   if (!TM)
59     return;
60 
61   auto OwningObj = SimpleCompiler(*TM)(*M);
62   std::vector<object::ObjectFile*> Objs;
63   Objs.push_back(OwningObj.getBinary());
64 
65   bool DebugSectionSeen = false;
66   SectionMemoryManagerWrapper SMMW(DebugSectionSeen);
67   auto Resolver =
68     createLambdaResolver(
69       [](const std::string &Name) {
70         return RuntimeDyld::SymbolInfo(nullptr);
71       },
72       [](const std::string &Name) {
73         return RuntimeDyld::SymbolInfo(nullptr);
74       });
75 
76   {
77     // Test with ProcessAllSections = false (the default).
78     auto H = ObjLayer.addObjectSet(Objs, &SMMW, &*Resolver);
79     EXPECT_EQ(DebugSectionSeen, false)
80       << "Unexpected debug info section";
81     ObjLayer.removeObjectSet(H);
82   }
83 
84   {
85     // Test with ProcessAllSections = true.
86     ObjLayer.setProcessAllSections(true);
87     auto H = ObjLayer.addObjectSet(Objs, &SMMW, &*Resolver);
88     EXPECT_EQ(DebugSectionSeen, true)
89       << "Expected debug info section not seen";
90     ObjLayer.removeObjectSet(H);
91   }
92 }
93 
94 }
95