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 "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 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 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 127 static void CHECK_ABS32(uint8_t *bytes, uint32_t offset, uint32_t addend) { 128 uint32_t res; 129 memcpy(&res, reinterpret_cast<uint32_t *>(bytes + offset), sizeof(uint32_t)); 130 ASSERT_EQ(addend, res); 131 } 132 133 static void CHECK_ABS64(uint8_t *bytes, uint64_t offset, uint64_t addend) { 134 uint64_t res; 135 memcpy(&res, reinterpret_cast<uint64_t *>(bytes + offset), sizeof(uint64_t)); 136 ASSERT_EQ(addend, res); 137 } 138 139 TEST_F(ObjectFileELFTest, TestAARCH64Relocations) { 140 llvm::SmallString<128> obj; 141 ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile( 142 "debug-info-relocations-%%%%%%", "obj", obj)); 143 llvm::FileRemover remover(obj); 144 ASSERT_THAT_ERROR(ReadYAMLObjectFile("debug-info-relocations.pcm.yaml", obj), 145 llvm::Succeeded()); 146 147 ModuleSpec spec{FileSpec(obj)}; 148 spec.GetSymbolFileSpec().SetFile(obj, FileSpec::Style::native); 149 auto module_sp = std::make_shared<Module>(spec); 150 151 auto objfile = static_cast<ObjectFileELF *>(module_sp->GetObjectFile()); 152 SectionList *section_list = objfile->GetSectionList(); 153 ASSERT_NE(nullptr, section_list); 154 155 auto debug_info_sp = 156 section_list->FindSectionByName(ConstString(".debug_info")); 157 ASSERT_NE(nullptr, debug_info_sp); 158 objfile->RelocateSection(debug_info_sp.get()); 159 160 DataExtractor data; 161 // length of 0x10 is not needed but length 0x0 crashes 162 objfile->GetData(0x00, 0x10, data); 163 DataBufferSP &data_buffer_sp = data.GetSharedDataBuffer(); 164 uint8_t *bytes = data_buffer_sp->GetBytes(); 165 166 addr_t debug_info_offset = debug_info_sp->GetFileOffset(); 167 bytes += debug_info_offset; 168 169 // Sanity check - The first byte from the yaml file is 0x47 170 ASSERT_EQ(0x47, *bytes); 171 172 // .rela.debug_info contains 9 relocations: 173 // 7 R_AARCH64_ABS32 - 2 R_AARCH64_ABS64 174 // None have a value. Four have addends. 175 CHECK_ABS32(bytes, 0x6, 0); 176 CHECK_ABS32(bytes, 0xC, 0); 177 CHECK_ABS32(bytes, 0x12, 45); 178 CHECK_ABS32(bytes, 0x16, 0); 179 CHECK_ABS32(bytes, 0x1A, 55); 180 CHECK_ABS64(bytes, 0x1E, 0); 181 CHECK_ABS64(bytes, 0x2B, 0); 182 CHECK_ABS32(bytes, 0x39, 73); 183 CHECK_ABS32(bytes, 0x44, 75); 184 } 185