xref: /llvm-project/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp (revision 88c77d6752ca6b50f69392d05b13bf1ef0dcb724)
1 //===-- TestObjectFileELF.cpp -----------------------------------*- C++ -*-===//
2 //
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
11 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
12 #include "TestingSupport/TestUtilities.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Host/HostInfo.h"
18 #include "lldb/Utility/DataBufferHeap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/Support/Compression.h"
21 #include "llvm/Support/FileUtilities.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Testing/Support/Error.h"
26 #include "gtest/gtest.h"
27 
28 using namespace lldb_private;
29 using namespace lldb;
30 
31 class ObjectFileELFTest : public testing::Test {
32 public:
33   void SetUp() override {
34     FileSystem::Initialize();
35     HostInfo::Initialize();
36     ObjectFileELF::Initialize();
37     SymbolFileSymtab::Initialize();
38   }
39 
40   void TearDown() override {
41     SymbolFileSymtab::Terminate();
42     ObjectFileELF::Terminate();
43     HostInfo::Terminate();
44     FileSystem::Terminate();
45   }
46 
47 protected:
48 };
49 
50 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) {
51   llvm::SmallString<128> obj;
52   ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
53       "sections-resolve-consistently-%%%%%%", "obj", obj));
54   llvm::FileRemover remover(obj);
55   ASSERT_THAT_ERROR(
56       ReadYAMLObjectFile("sections-resolve-consistently.yaml", obj),
57       llvm::Succeeded());
58 
59   ModuleSpec spec{FileSpec(obj)};
60   spec.GetSymbolFileSpec().SetFile(obj, FileSpec::Style::native);
61   auto module_sp = std::make_shared<Module>(spec);
62   SectionList *list = module_sp->GetSectionList();
63   ASSERT_NE(nullptr, list);
64 
65   auto bss_sp = list->FindSectionByName(ConstString(".bss"));
66   ASSERT_NE(nullptr, bss_sp);
67   auto data_sp = list->FindSectionByName(ConstString(".data"));
68   ASSERT_NE(nullptr, data_sp);
69   auto text_sp = list->FindSectionByName(ConstString(".text"));
70   ASSERT_NE(nullptr, text_sp);
71 
72   const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"),
73                                                               eSymbolTypeAny);
74   ASSERT_NE(nullptr, X);
75   EXPECT_EQ(bss_sp, X->GetAddress().GetSection());
76 
77   const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"),
78                                                               eSymbolTypeAny);
79   ASSERT_NE(nullptr, Y);
80   EXPECT_EQ(data_sp, Y->GetAddress().GetSection());
81 
82   const Symbol *start = module_sp->FindFirstSymbolWithNameAndType(
83       ConstString("_start"), eSymbolTypeAny);
84   ASSERT_NE(nullptr, start);
85   EXPECT_EQ(text_sp, start->GetAddress().GetSection());
86 }
87 
88 // Test that GetModuleSpecifications works on an "atypical" object file which
89 // has section headers right after the ELF header (instead of the more common
90 // layout where the section headers are at the very end of the object file).
91 //
92 // Test file generated with yaml2obj (@svn rev 324254) from the following input:
93 /*
94 --- !ELF
95 FileHeader:
96   Class:           ELFCLASS64
97   Data:            ELFDATA2LSB
98   Type:            ET_EXEC
99   Machine:         EM_X86_64
100   Entry:           0x00000000004003D0
101 Sections:
102   - Name:            .note.gnu.build-id
103     Type:            SHT_NOTE
104     Flags:           [ SHF_ALLOC ]
105     Address:         0x0000000000400274
106     AddressAlign:    0x0000000000000004
107     Content:         040000001400000003000000474E55001B8A73AC238390E32A7FF4AC8EBE4D6A41ECF5C9
108   - Name:            .text
109     Type:            SHT_PROGBITS
110     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
111     Address:         0x00000000004003D0
112     AddressAlign:    0x0000000000000010
113     Content:         DEADBEEFBAADF00D
114 ...
115 */
116 TEST_F(ObjectFileELFTest, GetModuleSpecifications_EarlySectionHeaders) {
117   std::string SO = GetInputFilePath("early-section-headers.so");
118   ModuleSpecList Specs;
119   ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
120   ModuleSpec Spec;
121   ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
122   UUID Uuid;
123   Uuid.SetFromStringRef("1b8a73ac238390e32a7ff4ac8ebe4d6a41ecf5c9", 20);
124   EXPECT_EQ(Spec.GetUUID(), Uuid);
125 }
126