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 "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "llvm/Support/FileUtilities.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Program.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "gtest/gtest.h"
22 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
24 extern const char *TestMainArgv0;
26 using namespace lldb_private;
29 class ObjectFileELFTest : public testing::Test {
31 void SetUp() override {
32 HostInfo::Initialize();
33 ObjectFileELF::Initialize();
34 SymbolVendorELF::Initialize();
36 m_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0);
37 llvm::sys::path::append(m_inputs_folder, "Inputs");
38 llvm::sys::fs::make_absolute(m_inputs_folder);
41 void TearDown() override {
42 SymbolVendorELF::Terminate();
43 ObjectFileELF::Terminate();
44 HostInfo::Terminate();
48 llvm::SmallString<128> m_inputs_folder;
51 #define ASSERT_NO_ERROR(x) \
52 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
53 llvm::SmallString<128> MessageStorage; \
54 llvm::raw_svector_ostream Message(MessageStorage); \
55 Message << #x ": did not return errc::success.\n" \
56 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
57 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
58 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
62 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) {
63 llvm::SmallString<128> yaml = m_inputs_folder;
64 llvm::sys::path::append(yaml, "sections-resolve-consistently.yaml");
65 llvm::SmallString<128> obj = m_inputs_folder;
66 ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
67 "sections-resolve-consistently-%%%%%%", "obj", obj));
69 llvm::FileRemover remover(obj);
70 const char *args[] = {YAML2OBJ, yaml.c_str(), nullptr};
71 llvm::StringRef obj_ref = obj;
72 const llvm::StringRef *redirects[] = {nullptr, &obj_ref, nullptr};
73 ASSERT_EQ(0, llvm::sys::ExecuteAndWait(YAML2OBJ, args, nullptr, redirects));
75 ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size));
78 ModuleSpec spec{FileSpec(obj, false)};
79 spec.GetSymbolFileSpec().SetFile(obj, false);
80 auto module_sp = std::make_shared<Module>(spec);
81 SectionList *list = module_sp->GetSectionList();
82 ASSERT_NE(nullptr, list);
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);
91 const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"),
93 ASSERT_NE(nullptr, X);
94 EXPECT_EQ(bss_sp, X->GetAddress().GetSection());
96 const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"),
98 ASSERT_NE(nullptr, Y);
99 EXPECT_EQ(data_sp, Y->GetAddress().GetSection());
101 const Symbol *start = module_sp->FindFirstSymbolWithNameAndType(
102 ConstString("_start"), eSymbolTypeAny);
103 ASSERT_NE(nullptr, start);
104 EXPECT_EQ(text_sp, start->GetAddress().GetSection());