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 "lldb/Core/Module.h" 14 #include "lldb/Core/ModuleSpec.h" 15 #include "lldb/Core/Section.h" 16 #include "lldb/Host/HostInfo.h" 17 #include "unittests/Utility/Helpers/TestUtilities.h" 18 #include "llvm/Support/FileUtilities.h" 19 #include "llvm/Support/Path.h" 20 #include "llvm/Support/Program.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include "gtest/gtest.h" 23 24 using namespace lldb_private; 25 using namespace lldb; 26 27 class ObjectFileELFTest : public testing::Test { 28 public: 29 void SetUp() override { 30 HostInfo::Initialize(); 31 ObjectFileELF::Initialize(); 32 SymbolVendorELF::Initialize(); 33 } 34 35 void TearDown() override { 36 SymbolVendorELF::Terminate(); 37 ObjectFileELF::Terminate(); 38 HostInfo::Terminate(); 39 } 40 41 protected: 42 }; 43 44 #define ASSERT_NO_ERROR(x) \ 45 if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 46 llvm::SmallString<128> MessageStorage; \ 47 llvm::raw_svector_ostream Message(MessageStorage); \ 48 Message << #x ": did not return errc::success.\n" \ 49 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 50 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 51 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 52 } else { \ 53 } 54 55 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) { 56 std::string yaml = GetInputFilePath("sections-resolve-consistently.yaml"); 57 llvm::SmallString<128> obj; 58 ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile( 59 "sections-resolve-consistently-%%%%%%", "obj", obj)); 60 61 llvm::FileRemover remover(obj); 62 const char *args[] = {YAML2OBJ, yaml.c_str(), nullptr}; 63 llvm::StringRef obj_ref = obj; 64 const llvm::StringRef *redirects[] = {nullptr, &obj_ref, nullptr}; 65 ASSERT_EQ(0, llvm::sys::ExecuteAndWait(YAML2OBJ, args, nullptr, redirects)); 66 uint64_t size; 67 ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size)); 68 ASSERT_GT(size, 0u); 69 70 ModuleSpec spec{FileSpec(obj, false)}; 71 spec.GetSymbolFileSpec().SetFile(obj, false); 72 auto module_sp = std::make_shared<Module>(spec); 73 SectionList *list = module_sp->GetSectionList(); 74 ASSERT_NE(nullptr, list); 75 76 auto bss_sp = list->FindSectionByName(ConstString(".bss")); 77 ASSERT_NE(nullptr, bss_sp); 78 auto data_sp = list->FindSectionByName(ConstString(".data")); 79 ASSERT_NE(nullptr, data_sp); 80 auto text_sp = list->FindSectionByName(ConstString(".text")); 81 ASSERT_NE(nullptr, text_sp); 82 83 const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"), 84 eSymbolTypeAny); 85 ASSERT_NE(nullptr, X); 86 EXPECT_EQ(bss_sp, X->GetAddress().GetSection()); 87 88 const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"), 89 eSymbolTypeAny); 90 ASSERT_NE(nullptr, Y); 91 EXPECT_EQ(data_sp, Y->GetAddress().GetSection()); 92 93 const Symbol *start = module_sp->FindFirstSymbolWithNameAndType( 94 ConstString("_start"), eSymbolTypeAny); 95 ASSERT_NE(nullptr, start); 96 EXPECT_EQ(text_sp, start->GetAddress().GetSection()); 97 } 98