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/HostInfo.h" 18 #include "llvm/ADT/Optional.h" 19 #include "llvm/Support/Compression.h" 20 #include "llvm/Support/FileUtilities.h" 21 #include "llvm/Support/Path.h" 22 #include "llvm/Support/Program.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include "gtest/gtest.h" 25 26 using namespace lldb_private; 27 using namespace lldb; 28 29 class ObjectFileELFTest : public testing::Test { 30 public: 31 void SetUp() override { 32 HostInfo::Initialize(); 33 ObjectFileELF::Initialize(); 34 SymbolVendorELF::Initialize(); 35 } 36 37 void TearDown() override { 38 SymbolVendorELF::Terminate(); 39 ObjectFileELF::Terminate(); 40 HostInfo::Terminate(); 41 } 42 43 protected: 44 }; 45 46 #define ASSERT_NO_ERROR(x) \ 47 if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 48 llvm::SmallString<128> MessageStorage; \ 49 llvm::raw_svector_ostream Message(MessageStorage); \ 50 Message << #x ": did not return errc::success.\n" \ 51 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 52 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 53 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 54 } else { \ 55 } 56 57 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) { 58 std::string yaml = GetInputFilePath("sections-resolve-consistently.yaml"); 59 llvm::SmallString<128> obj; 60 ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile( 61 "sections-resolve-consistently-%%%%%%", "obj", obj)); 62 63 llvm::FileRemover remover(obj); 64 llvm::StringRef args[] = {YAML2OBJ, yaml}; 65 llvm::StringRef obj_ref = obj; 66 const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref, 67 llvm::None}; 68 ASSERT_EQ(0, 69 llvm::sys::ExecuteAndWait(YAML2OBJ, args, llvm::None, redirects)); 70 uint64_t size; 71 ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size)); 72 ASSERT_GT(size, 0u); 73 74 ModuleSpec spec{FileSpec(obj, false)}; 75 spec.GetSymbolFileSpec().SetFile(obj, false, FileSpec::Style::native); 76 auto module_sp = std::make_shared<Module>(spec); 77 SectionList *list = module_sp->GetSectionList(); 78 ASSERT_NE(nullptr, list); 79 80 auto bss_sp = list->FindSectionByName(ConstString(".bss")); 81 ASSERT_NE(nullptr, bss_sp); 82 auto data_sp = list->FindSectionByName(ConstString(".data")); 83 ASSERT_NE(nullptr, data_sp); 84 auto text_sp = list->FindSectionByName(ConstString(".text")); 85 ASSERT_NE(nullptr, text_sp); 86 87 const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"), 88 eSymbolTypeAny); 89 ASSERT_NE(nullptr, X); 90 EXPECT_EQ(bss_sp, X->GetAddress().GetSection()); 91 92 const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"), 93 eSymbolTypeAny); 94 ASSERT_NE(nullptr, Y); 95 EXPECT_EQ(data_sp, Y->GetAddress().GetSection()); 96 97 const Symbol *start = module_sp->FindFirstSymbolWithNameAndType( 98 ConstString("_start"), eSymbolTypeAny); 99 ASSERT_NE(nullptr, start); 100 EXPECT_EQ(text_sp, start->GetAddress().GetSection()); 101 } 102 103 // Test that GetModuleSpecifications works on an "atypical" object file which 104 // has section headers right after the ELF header (instead of the more common 105 // layout where the section headers are at the very end of the object file). 106 // 107 // Test file generated with yaml2obj (@svn rev 324254) from the following input: 108 /* 109 --- !ELF 110 FileHeader: 111 Class: ELFCLASS64 112 Data: ELFDATA2LSB 113 Type: ET_EXEC 114 Machine: EM_X86_64 115 Entry: 0x00000000004003D0 116 Sections: 117 - Name: .note.gnu.build-id 118 Type: SHT_NOTE 119 Flags: [ SHF_ALLOC ] 120 Address: 0x0000000000400274 121 AddressAlign: 0x0000000000000004 122 Content: 040000001400000003000000474E55001B8A73AC238390E32A7FF4AC8EBE4D6A41ECF5C9 123 - Name: .text 124 Type: SHT_PROGBITS 125 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 126 Address: 0x00000000004003D0 127 AddressAlign: 0x0000000000000010 128 Content: DEADBEEFBAADF00D 129 ... 130 */ 131 TEST_F(ObjectFileELFTest, GetModuleSpecifications_EarlySectionHeaders) { 132 std::string SO = GetInputFilePath("early-section-headers.so"); 133 ModuleSpecList Specs; 134 ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO, false), 0, 0, Specs)); 135 ModuleSpec Spec; 136 ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ; 137 UUID Uuid; 138 Uuid.SetFromStringRef("1b8a73ac238390e32a7ff4ac8ebe4d6a41ecf5c9", 20); 139 EXPECT_EQ(Spec.GetUUID(), Uuid); 140 } 141