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 "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"
26 using namespace lldb_private;
29 class ObjectFileELFTest : public testing::Test {
31 void SetUp() override {
32 HostInfo::Initialize();
33 ObjectFileELF::Initialize();
34 SymbolVendorELF::Initialize();
37 void TearDown() override {
38 SymbolVendorELF::Terminate();
39 ObjectFileELF::Terminate();
40 HostInfo::Terminate();
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()); \
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));
63 llvm::FileRemover remover(obj);
64 const char *args[] = {YAML2OBJ, yaml.c_str(), nullptr};
65 llvm::StringRef obj_ref = obj;
66 const llvm::Optional<llvm::StringRef> redirects[] = {llvm::None, obj_ref,
68 ASSERT_EQ(0, llvm::sys::ExecuteAndWait(YAML2OBJ, args, nullptr, redirects));
70 ASSERT_NO_ERROR(llvm::sys::fs::file_size(obj, size));
73 ModuleSpec spec{FileSpec(obj, false)};
74 spec.GetSymbolFileSpec().SetFile(obj, false);
75 auto module_sp = std::make_shared<Module>(spec);
76 SectionList *list = module_sp->GetSectionList();
77 ASSERT_NE(nullptr, list);
79 auto bss_sp = list->FindSectionByName(ConstString(".bss"));
80 ASSERT_NE(nullptr, bss_sp);
81 auto data_sp = list->FindSectionByName(ConstString(".data"));
82 ASSERT_NE(nullptr, data_sp);
83 auto text_sp = list->FindSectionByName(ConstString(".text"));
84 ASSERT_NE(nullptr, text_sp);
86 const Symbol *X = module_sp->FindFirstSymbolWithNameAndType(ConstString("X"),
88 ASSERT_NE(nullptr, X);
89 EXPECT_EQ(bss_sp, X->GetAddress().GetSection());
91 const Symbol *Y = module_sp->FindFirstSymbolWithNameAndType(ConstString("Y"),
93 ASSERT_NE(nullptr, Y);
94 EXPECT_EQ(data_sp, Y->GetAddress().GetSection());
96 const Symbol *start = module_sp->FindFirstSymbolWithNameAndType(
97 ConstString("_start"), eSymbolTypeAny);
98 ASSERT_NE(nullptr, start);
99 EXPECT_EQ(text_sp, start->GetAddress().GetSection());