xref: /llvm-project/lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp (revision b37f1ec86131d3963177bffc22449b881db3f98a)
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 "TestingSupport/TestUtilities.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Host/FileSystem.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Utility/DataBufferHeap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/Support/Compression.h"
22 #include "llvm/Support/FileUtilities.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/raw_ostream.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     SymbolVendorELF::Initialize();
38   }
39 
40   void TearDown() override {
41     SymbolVendorELF::Terminate();
42     ObjectFileELF::Terminate();
43     HostInfo::Terminate();
44     FileSystem::Terminate();
45   }
46 
47 protected:
48 };
49 
50 #define ASSERT_NO_ERROR(x)                                                     \
51   if (std::error_code ASSERT_NO_ERROR_ec = x) {                                \
52     llvm::SmallString<128> MessageStorage;                                     \
53     llvm::raw_svector_ostream Message(MessageStorage);                         \
54     Message << #x ": did not return errc::success.\n"                          \
55             << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"          \
56             << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n";      \
57     GTEST_FATAL_FAILURE_(MessageStorage.c_str());                              \
58   } else {                                                                     \
59   }
60 
61 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) {
62   std::string yaml = GetInputFilePath("sections-resolve-consistently.yaml");
63   llvm::SmallString<128> obj;
64   ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
65       "sections-resolve-consistently-%%%%%%", "obj", obj));
66 
67   llvm::FileRemover remover(obj);
68   llvm::StringRef args[] = {YAML2OBJ, yaml};
69   llvm::StringRef obj_ref = obj;
70   const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref,
71                                                        llvm::None};
72   ASSERT_EQ(0,
73             llvm::sys::ExecuteAndWait(YAML2OBJ, args, llvm::None, redirects));
74   uint64_t size;
75   ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size));
76   ASSERT_GT(size, 0u);
77 
78   ModuleSpec spec{FileSpec(obj)};
79   spec.GetSymbolFileSpec().SetFile(obj, FileSpec::Style::native);
80   auto module_sp = std::make_shared<Module>(spec);
81   SectionList *list = module_sp->GetSectionList();
82   ASSERT_NE(nullptr, list);
83 
84   auto bss_sp = list->FindSectionByName(ConstString(".bss"));
85   ASSERT_NE(nullptr, bss_sp);
86   auto data_sp = list->FindSectionByName(ConstString(".data"));
87   ASSERT_NE(nullptr, data_sp);
88   auto text_sp = list->FindSectionByName(ConstString(".text"));
89   ASSERT_NE(nullptr, text_sp);
90 
91   const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"),
92                                                               eSymbolTypeAny);
93   ASSERT_NE(nullptr, X);
94   EXPECT_EQ(bss_sp, X->GetAddress().GetSection());
95 
96   const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"),
97                                                               eSymbolTypeAny);
98   ASSERT_NE(nullptr, Y);
99   EXPECT_EQ(data_sp, Y->GetAddress().GetSection());
100 
101   const Symbol *start = module_sp->FindFirstSymbolWithNameAndType(
102       ConstString("_start"), eSymbolTypeAny);
103   ASSERT_NE(nullptr, start);
104   EXPECT_EQ(text_sp, start->GetAddress().GetSection());
105 }
106 
107 // Test that GetModuleSpecifications works on an "atypical" object file which
108 // has section headers right after the ELF header (instead of the more common
109 // layout where the section headers are at the very end of the object file).
110 //
111 // Test file generated with yaml2obj (@svn rev 324254) from the following input:
112 /*
113 --- !ELF
114 FileHeader:
115   Class:           ELFCLASS64
116   Data:            ELFDATA2LSB
117   Type:            ET_EXEC
118   Machine:         EM_X86_64
119   Entry:           0x00000000004003D0
120 Sections:
121   - Name:            .note.gnu.build-id
122     Type:            SHT_NOTE
123     Flags:           [ SHF_ALLOC ]
124     Address:         0x0000000000400274
125     AddressAlign:    0x0000000000000004
126     Content:         040000001400000003000000474E55001B8A73AC238390E32A7FF4AC8EBE4D6A41ECF5C9
127   - Name:            .text
128     Type:            SHT_PROGBITS
129     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
130     Address:         0x00000000004003D0
131     AddressAlign:    0x0000000000000010
132     Content:         DEADBEEFBAADF00D
133 ...
134 */
135 TEST_F(ObjectFileELFTest, GetModuleSpecifications_EarlySectionHeaders) {
136   std::string SO = GetInputFilePath("early-section-headers.so");
137   ModuleSpecList Specs;
138   ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
139   ModuleSpec Spec;
140   ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
141   UUID Uuid;
142   Uuid.SetFromStringRef("1b8a73ac238390e32a7ff4ac8ebe4d6a41ecf5c9", 20);
143   EXPECT_EQ(Spec.GetUUID(), Uuid);
144 }
145 
146 static void CHECK_ABS32(uint8_t *bytes, uint32_t offset, uint32_t addend) {
147   uint32_t res;
148   memcpy(&res, (uint32_t *)(bytes + offset), sizeof(uint32_t));
149   ASSERT_EQ(addend, res);
150 }
151 
152 static void CHECK_ABS64(uint8_t *bytes, uint64_t offset, uint64_t addend) {
153   uint64_t res;
154   memcpy(&res, (uint64_t *)(bytes + offset), sizeof(uint64_t));
155   ASSERT_EQ(addend, res);
156 }
157 
158 TEST_F(ObjectFileELFTest, TestAARCH64Relocations) {
159   std::string yaml = GetInputFilePath("debug-info-relocations.pcm.yaml");
160   llvm::SmallString<128> obj;
161   ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
162       "debug-info-relocations-%%%%%%", "obj", obj));
163 
164   llvm::FileRemover remover(obj);
165   llvm::StringRef args[] = {YAML2OBJ, yaml};
166   llvm::StringRef obj_ref = obj;
167   const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref,
168                                                        llvm::None};
169   ASSERT_EQ(0,
170             llvm::sys::ExecuteAndWait(YAML2OBJ, args, llvm::None, redirects));
171   uint64_t size;
172   ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size));
173   ASSERT_GT(size, 0u);
174 
175   ModuleSpec spec{FileSpec(obj)};
176   spec.GetSymbolFileSpec().SetFile(obj, FileSpec::Style::native);
177   auto module_sp = std::make_shared<Module>(spec);
178 
179   auto objfile = static_cast<ObjectFileELF *>(module_sp->GetObjectFile());
180   SectionList *section_list = objfile->GetSectionList();
181   ASSERT_NE(nullptr, section_list);
182 
183   auto debug_info_sp =
184       section_list->FindSectionByName(ConstString(".debug_info"));
185   ASSERT_NE(nullptr, debug_info_sp);
186   objfile->RelocateSection(debug_info_sp.get());
187 
188   DataExtractor data;
189   // length of 0x10 is not needed but length 0x0 crashes
190   objfile->GetData(0x00, 0x10, data);
191   DataBufferSP &data_buffer_sp = data.GetSharedDataBuffer();
192   uint8_t *bytes = data_buffer_sp->GetBytes();
193 
194   addr_t debug_info_offset = debug_info_sp->GetFileOffset();
195   bytes += debug_info_offset;
196 
197   // Sanity check - The first byte from the yaml file is 0x47
198   ASSERT_EQ(0x47, *bytes);
199 
200   // .rela.debug_info contains 9 relocations:
201   // 7 R_AARCH64_ABS32 - 2 R_AARCH64_ABS64
202   // None have a value. Four have addends.
203   CHECK_ABS32(bytes, 0x6, 0);
204   CHECK_ABS32(bytes, 0xC, 0);
205   CHECK_ABS32(bytes, 0x12, 45);
206   CHECK_ABS32(bytes, 0x16, 0);
207   CHECK_ABS32(bytes, 0x1A, 55);
208   CHECK_ABS64(bytes, 0x1E, 0);
209   CHECK_ABS64(bytes, 0x2B, 0);
210   CHECK_ABS32(bytes, 0x39, 73);
211   CHECK_ABS32(bytes, 0x44, 75);
212 }
213