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 #define CHECK_ABS32(offset, addend) \ 147 ASSERT_EQ((uint32_t)addend, *(uint32_t *)(bytes + offset)) 148 #define CHECK_ABS64(offset, addend) \ 149 ASSERT_EQ((uint64_t)addend, *(uint64_t *)(bytes + offset)) 150 151 TEST_F(ObjectFileELFTest, TestAARCH64Relocations) { 152 std::string yaml = GetInputFilePath("debug-info-relocations.pcm.yaml"); 153 llvm::SmallString<128> obj; 154 ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile( 155 "debug-info-relocations-%%%%%%", "obj", obj)); 156 157 llvm::FileRemover remover(obj); 158 llvm::StringRef args[] = {YAML2OBJ, yaml}; 159 llvm::StringRef obj_ref = obj; 160 const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref, 161 llvm::None}; 162 ASSERT_EQ(0, 163 llvm::sys::ExecuteAndWait(YAML2OBJ, args, llvm::None, redirects)); 164 uint64_t size; 165 ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size)); 166 ASSERT_GT(size, 0u); 167 168 ModuleSpec spec{FileSpec(obj)}; 169 spec.GetSymbolFileSpec().SetFile(obj, FileSpec::Style::native); 170 auto module_sp = std::make_shared<Module>(spec); 171 172 auto objfile = static_cast<ObjectFileELF *>(module_sp->GetObjectFile()); 173 SectionList *section_list = objfile->GetSectionList(); 174 ASSERT_NE(nullptr, section_list); 175 176 auto debug_info_sp = 177 section_list->FindSectionByName(ConstString(".debug_info")); 178 ASSERT_NE(nullptr, debug_info_sp); 179 objfile->RelocateSection(debug_info_sp.get()); 180 181 DataExtractor data; 182 // length of 0x10 is not needed but length 0x0 crashes 183 objfile->GetData(0x00, 0x10, data); 184 DataBufferSP &data_buffer_sp = data.GetSharedDataBuffer(); 185 uint8_t *bytes = data_buffer_sp->GetBytes(); 186 187 addr_t debug_info_offset = debug_info_sp->GetFileOffset(); 188 bytes += debug_info_offset; 189 190 // Sanity check - The first byte from the yaml file is 0x47 191 ASSERT_EQ(0x47, *bytes); 192 193 // .rela.debug_info contains 9 relocations: 194 // 7 R_AARCH64_ABS32 - 2 R_AARCH64_ABS64 195 // None have a value. Four have addends. 196 CHECK_ABS32(0x6, 0); 197 CHECK_ABS32(0xC, 0); 198 CHECK_ABS32(0x12, 45); 199 CHECK_ABS32(0x16, 0); 200 CHECK_ABS32(0x1A, 55); 201 CHECK_ABS64(0x1E, 0); 202 CHECK_ABS64(0x2B, 0); 203 CHECK_ABS32(0x39, 73); 204 CHECK_ABS32(0x44, 75); 205 } 206