xref: /llvm-project/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp (revision a6db41675c44d18555277862a8abdc2c30e62339)
1 //===-- TestObjectFileELF.cpp -----------------------------------*- C++ -*-===//
2 //
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
12 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Host/HostInfo.h"
17 #include "TestingSupport/TestUtilities.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Program.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "gtest/gtest.h"
24 
25 using namespace lldb_private;
26 using namespace lldb;
27 
28 class ObjectFileELFTest : public testing::Test {
29 public:
30   void SetUp() override {
31     HostInfo::Initialize();
32     ObjectFileELF::Initialize();
33     SymbolVendorELF::Initialize();
34   }
35 
36   void TearDown() override {
37     SymbolVendorELF::Terminate();
38     ObjectFileELF::Terminate();
39     HostInfo::Terminate();
40   }
41 
42 protected:
43 };
44 
45 #define ASSERT_NO_ERROR(x)                                                     \
46   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
47     llvm::SmallString<128> MessageStorage;                                     \
48     llvm::raw_svector_ostream Message(MessageStorage);                         \
49     Message << #x ": did not return errc::success.\n"                          \
50             << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"          \
51             << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";      \
52     GTEST_FATAL_FAILURE_(MessageStorage.c_str());                              \
53   } else {                                                                     \
54   }
55 
56 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) {
57   std::string yaml = GetInputFilePath("sections-resolve-consistently.yaml");
58   llvm::SmallString<128> obj;
59   ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
60       "sections-resolve-consistently-%%%%%%", "obj", obj));
61 
62   llvm::FileRemover remover(obj);
63   const char *args[] = {YAML2OBJ, yaml.c_str(), nullptr};
64   llvm::StringRef obj_ref = obj;
65   const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref,
66                                                        llvm::None};
67   ASSERT_EQ(0, llvm::sys::ExecuteAndWait(YAML2OBJ, args, nullptr, redirects));
68   uint64_t size;
69   ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size));
70   ASSERT_GT(size, 0u);
71 
72   ModuleSpec spec{FileSpec(obj, false)};
73   spec.GetSymbolFileSpec().SetFile(obj, false);
74   auto module_sp = std::make_shared<Module>(spec);
75   SectionList *list = module_sp->GetSectionList();
76   ASSERT_NE(nullptr, list);
77 
78   auto bss_sp = list->FindSectionByName(ConstString(".bss"));
79   ASSERT_NE(nullptr, bss_sp);
80   auto data_sp = list->FindSectionByName(ConstString(".data"));
81   ASSERT_NE(nullptr, data_sp);
82   auto text_sp = list->FindSectionByName(ConstString(".text"));
83   ASSERT_NE(nullptr, text_sp);
84 
85   const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"),
86                                                               eSymbolTypeAny);
87   ASSERT_NE(nullptr, X);
88   EXPECT_EQ(bss_sp, X->GetAddress().GetSection());
89 
90   const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"),
91                                                               eSymbolTypeAny);
92   ASSERT_NE(nullptr, Y);
93   EXPECT_EQ(data_sp, Y->GetAddress().GetSection());
94 
95   const Symbol *start = module_sp->FindFirstSymbolWithNameAndType(
96       ConstString("_start"), eSymbolTypeAny);
97   ASSERT_NE(nullptr, start);
98   EXPECT_EQ(text_sp, start->GetAddress().GetSection());
99 }
100