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