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 "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