xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ObjectFile/Placeholder/ObjectFilePlaceholder.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1*06c3fb27SDimitry Andric //===-- ObjectFilePlaceholder.cpp----------------------------------------===//
2*06c3fb27SDimitry Andric //
3*06c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*06c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*06c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*06c3fb27SDimitry Andric //
7*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
8*06c3fb27SDimitry Andric 
9*06c3fb27SDimitry Andric #include "ObjectFilePlaceholder.h"
10*06c3fb27SDimitry Andric 
11*06c3fb27SDimitry Andric #include "lldb/Core/Module.h"
12*06c3fb27SDimitry Andric #include "lldb/Core/ModuleSpec.h"
13*06c3fb27SDimitry Andric #include "lldb/Core/PluginManager.h"
14*06c3fb27SDimitry Andric #include "lldb/Core/Section.h"
15*06c3fb27SDimitry Andric #include "lldb/Target/SectionLoadList.h"
16*06c3fb27SDimitry Andric #include "lldb/Target/Target.h"
17*06c3fb27SDimitry Andric 
18*06c3fb27SDimitry Andric #include <memory>
19*06c3fb27SDimitry Andric 
20*06c3fb27SDimitry Andric using namespace lldb;
21*06c3fb27SDimitry Andric using namespace lldb_private;
22*06c3fb27SDimitry Andric 
LLDB_PLUGIN_DEFINE(ObjectFilePlaceholder)23*06c3fb27SDimitry Andric LLDB_PLUGIN_DEFINE(ObjectFilePlaceholder)
24*06c3fb27SDimitry Andric 
25*06c3fb27SDimitry Andric ObjectFilePlaceholder::ObjectFilePlaceholder(
26*06c3fb27SDimitry Andric     const lldb::ModuleSP &module_sp,
27*06c3fb27SDimitry Andric     const lldb_private::ModuleSpec &module_spec, lldb::addr_t base,
28*06c3fb27SDimitry Andric     lldb::addr_t size)
29*06c3fb27SDimitry Andric     : ObjectFile(module_sp, &module_spec.GetFileSpec(), /*file_offset*/ 0,
30*06c3fb27SDimitry Andric                  /*length*/ 0, /*data_sp*/ nullptr, /*data_offset*/ 0),
31*06c3fb27SDimitry Andric       m_arch(module_spec.GetArchitecture()), m_uuid(module_spec.GetUUID()),
32*06c3fb27SDimitry Andric       m_base(base), m_size(size) {
33*06c3fb27SDimitry Andric   m_symtab_up = std::make_unique<lldb_private::Symtab>(this);
34*06c3fb27SDimitry Andric }
35*06c3fb27SDimitry Andric 
CreateSections(lldb_private::SectionList & unified_section_list)36*06c3fb27SDimitry Andric void ObjectFilePlaceholder::CreateSections(
37*06c3fb27SDimitry Andric     lldb_private::SectionList &unified_section_list) {
38*06c3fb27SDimitry Andric   m_sections_up = std::make_unique<lldb_private::SectionList>();
39*06c3fb27SDimitry Andric   auto section_sp = std::make_shared<lldb_private::Section>(
40*06c3fb27SDimitry Andric       GetModule(), this, /*sect_id*/ 0,
41*06c3fb27SDimitry Andric       lldb_private::ConstString(".module_image"), eSectionTypeOther, m_base,
42*06c3fb27SDimitry Andric       m_size, /*file_offset*/ 0, /*file_size*/ 0,
43*06c3fb27SDimitry Andric       /*log2align*/ 0, /*flags*/ 0);
44*06c3fb27SDimitry Andric   section_sp->SetPermissions(ePermissionsReadable | ePermissionsExecutable);
45*06c3fb27SDimitry Andric   m_sections_up->AddSection(section_sp);
46*06c3fb27SDimitry Andric   unified_section_list.AddSection(std::move(section_sp));
47*06c3fb27SDimitry Andric }
48*06c3fb27SDimitry Andric 
GetBaseAddress()49*06c3fb27SDimitry Andric lldb_private::Address ObjectFilePlaceholder::GetBaseAddress() {
50*06c3fb27SDimitry Andric   return lldb_private::Address(m_sections_up->GetSectionAtIndex(0), 0);
51*06c3fb27SDimitry Andric }
52*06c3fb27SDimitry Andric 
SetLoadAddress(Target & target,addr_t value,bool value_is_offset)53*06c3fb27SDimitry Andric bool ObjectFilePlaceholder::SetLoadAddress(Target &target, addr_t value,
54*06c3fb27SDimitry Andric                                            bool value_is_offset) {
55*06c3fb27SDimitry Andric   assert(!value_is_offset);
56*06c3fb27SDimitry Andric   assert(value == m_base);
57*06c3fb27SDimitry Andric 
58*06c3fb27SDimitry Andric   // Create sections if they haven't been created already.
59*06c3fb27SDimitry Andric   GetModule()->GetSectionList();
60*06c3fb27SDimitry Andric   assert(m_sections_up->GetNumSections(0) == 1);
61*06c3fb27SDimitry Andric 
62*06c3fb27SDimitry Andric   target.GetSectionLoadList().SetSectionLoadAddress(
63*06c3fb27SDimitry Andric       m_sections_up->GetSectionAtIndex(0), m_base);
64*06c3fb27SDimitry Andric   return true;
65*06c3fb27SDimitry Andric }
66*06c3fb27SDimitry Andric 
Dump(lldb_private::Stream * s)67*06c3fb27SDimitry Andric void ObjectFilePlaceholder::Dump(lldb_private::Stream *s) {
68*06c3fb27SDimitry Andric   s->Format("Placeholder object file for {0} loaded at [{1:x}-{2:x})\n",
69*06c3fb27SDimitry Andric             GetFileSpec(), m_base, m_base + m_size);
70*06c3fb27SDimitry Andric }
71