1 //===-- TestObjectFileELF.cpp -----------------------------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
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"
24 using namespace lldb_private;
27 class ObjectFileELFTest : public testing::Test {
29 void SetUp() override {
30 HostInfo::Initialize();
31 ObjectFileELF::Initialize();
32 SymbolVendorELF::Initialize();
35 void TearDown() override {
36 SymbolVendorELF::Terminate();
37 ObjectFileELF::Terminate();
38 HostInfo::Terminate();
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()); \
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));
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));
67 ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size));
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);
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);
83 const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"),
85 ASSERT_NE(nullptr, X);
86 EXPECT_EQ(bss_sp, X->GetAddress().GetSection());
88 const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"),
90 ASSERT_NE(nullptr, Y);
91 EXPECT_EQ(data_sp, Y->GetAddress().GetSection());
93 const Symbol *start = module_sp->FindFirstSymbolWithNameAndType(
94 ConstString("_start"), eSymbolTypeAny);
95 ASSERT_NE(nullptr, start);
96 EXPECT_EQ(text_sp, start->GetAddress().GetSection());