1//===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/StringRef.h"
10
11#include "ObjectFileJIT.h"
12#include "lldb/Core/Debugger.h"
13#include "lldb/Core/FileSpecList.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/ModuleSpec.h"
16#include "lldb/Core/PluginManager.h"
17#include "lldb/Core/Section.h"
18#include "lldb/Core/StreamFile.h"
19#include "lldb/Host/Host.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Target/Platform.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/SectionLoadList.h"
24#include "lldb/Target/Target.h"
25#include "lldb/Utility/ArchSpec.h"
26#include "lldb/Utility/DataBuffer.h"
27#include "lldb/Utility/DataBufferHeap.h"
28#include "lldb/Utility/FileSpec.h"
29#include "lldb/Utility/Log.h"
30#include "lldb/Utility/RangeMap.h"
31#include "lldb/Utility/StreamString.h"
32#include "lldb/Utility/Timer.h"
33#include "lldb/Utility/UUID.h"
34
35#ifndef __APPLE__
36#include "Utility/UuidCompatibility.h"
37#endif
38
39using namespace lldb;
40using namespace lldb_private;
41
42char ObjectFileJIT::ID;
43
44void ObjectFileJIT::Initialize() {
45  PluginManager::RegisterPlugin(GetPluginNameStatic(),
46                                GetPluginDescriptionStatic(), CreateInstance,
47                                CreateMemoryInstance, GetModuleSpecifications);
48}
49
50void ObjectFileJIT::Terminate() {
51  PluginManager::UnregisterPlugin(CreateInstance);
52}
53
54lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() {
55  static ConstString g_name("jit");
56  return g_name;
57}
58
59const char *ObjectFileJIT::GetPluginDescriptionStatic() {
60  return "JIT code object file";
61}
62
63ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
64                                          DataBufferSP &data_sp,
65                                          lldb::offset_t data_offset,
66                                          const FileSpec *file,
67                                          lldb::offset_t file_offset,
68                                          lldb::offset_t length) {
69  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
70  // a file
71  return nullptr;
72}
73
74ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
75                                                DataBufferSP &data_sp,
76                                                const ProcessSP &process_sp,
77                                                lldb::addr_t header_addr) {
78  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
79  // memory
80  return nullptr;
81}
82
83size_t ObjectFileJIT::GetModuleSpecifications(
84    const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
85    lldb::offset_t data_offset, lldb::offset_t file_offset,
86    lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
87  // JIT'ed object file can't be read from a file on disk
88  return 0;
89}
90
91ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
92                             const ObjectFileJITDelegateSP &delegate_sp)
93    : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
94  if (delegate_sp) {
95    m_delegate_wp = delegate_sp;
96    m_data.SetByteOrder(delegate_sp->GetByteOrder());
97    m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
98  }
99}
100
101ObjectFileJIT::~ObjectFileJIT() {}
102
103bool ObjectFileJIT::ParseHeader() {
104  // JIT code is never in a file, nor is it required to have any header
105  return false;
106}
107
108ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
109
110bool ObjectFileJIT::IsExecutable() const { return false; }
111
112uint32_t ObjectFileJIT::GetAddressByteSize() const {
113  return m_data.GetAddressByteSize();
114}
115
116Symtab *ObjectFileJIT::GetSymtab() {
117  ModuleSP module_sp(GetModule());
118  if (module_sp) {
119    std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
120    if (m_symtab_up == nullptr) {
121      m_symtab_up.reset(new Symtab(this));
122      std::lock_guard<std::recursive_mutex> symtab_guard(
123          m_symtab_up->GetMutex());
124      ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
125      if (delegate_sp)
126        delegate_sp->PopulateSymtab(this, *m_symtab_up);
127      // TODO: get symbols from delegate
128      m_symtab_up->Finalize();
129    }
130  }
131  return m_symtab_up.get();
132}
133
134bool ObjectFileJIT::IsStripped() {
135  return false; // JIT code that is in a module is never stripped
136}
137
138void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
139  if (!m_sections_up) {
140    m_sections_up.reset(new SectionList());
141    ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
142    if (delegate_sp) {
143      delegate_sp->PopulateSectionList(this, *m_sections_up);
144      unified_section_list = *m_sections_up;
145    }
146  }
147}
148
149void ObjectFileJIT::Dump(Stream *s) {
150  ModuleSP module_sp(GetModule());
151  if (module_sp) {
152    std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
153    s->Printf("%p: ", static_cast<void *>(this));
154    s->Indent();
155    s->PutCString("ObjectFileJIT");
156
157    if (ArchSpec arch = GetArchitecture())
158      *s << ", arch = " << arch.GetArchitectureName();
159
160    s->EOL();
161
162    SectionList *sections = GetSectionList();
163    if (sections)
164      sections->Dump(s, nullptr, true, UINT32_MAX);
165
166    if (m_symtab_up)
167      m_symtab_up->Dump(s, nullptr, eSortOrderNone);
168  }
169}
170
171UUID ObjectFileJIT::GetUUID() {
172  // TODO: maybe get from delegate, not needed for first pass
173  return UUID();
174}
175
176uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
177  // JIT modules don't have dependencies, but they could
178  // if external functions are called and we know where they are
179  files.Clear();
180  return 0;
181}
182
183lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
184  return Address();
185}
186
187lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
188
189ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
190
191ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
192
193ArchSpec ObjectFileJIT::GetArchitecture() {
194  if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
195    return delegate_sp->GetArchitecture();
196  return ArchSpec();
197}
198
199// PluginInterface protocol
200lldb_private::ConstString ObjectFileJIT::GetPluginName() {
201  return GetPluginNameStatic();
202}
203
204uint32_t ObjectFileJIT::GetPluginVersion() { return 1; }
205
206bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
207                                   bool value_is_offset) {
208  size_t num_loaded_sections = 0;
209  SectionList *section_list = GetSectionList();
210  if (section_list) {
211    const size_t num_sections = section_list->GetSize();
212    // "value" is an offset to apply to each top level segment
213    for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
214      // Iterate through the object file sections to find all of the sections
215      // that size on disk (to avoid __PAGEZERO) and load them
216      SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
217      if (section_sp && section_sp->GetFileSize() > 0 &&
218          !section_sp->IsThreadSpecific()) {
219        if (target.GetSectionLoadList().SetSectionLoadAddress(
220                section_sp, section_sp->GetFileAddress() + value))
221          ++num_loaded_sections;
222      }
223    }
224  }
225  return num_loaded_sections > 0;
226}
227
228size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
229                                      lldb::offset_t section_offset, void *dst,
230                                      size_t dst_len) {
231  lldb::offset_t file_size = section->GetFileSize();
232  if (section_offset < file_size) {
233    size_t src_len = file_size - section_offset;
234    if (src_len > dst_len)
235      src_len = dst_len;
236    const uint8_t *src =
237        ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
238
239    memcpy(dst, src, src_len);
240    return src_len;
241  }
242  return 0;
243}
244
245size_t ObjectFileJIT::ReadSectionData(
246    lldb_private::Section *section,
247    lldb_private::DataExtractor &section_data) {
248  if (section->GetFileSize()) {
249    const void *src = (void *)(uintptr_t)section->GetFileOffset();
250
251    DataBufferSP data_sp =
252        std::make_shared<DataBufferHeap>(src, section->GetFileSize());
253    section_data.SetData(data_sp, 0, data_sp->GetByteSize());
254    section_data.SetByteOrder(GetByteOrder());
255    section_data.SetAddressByteSize(GetAddressByteSize());
256    return section_data.GetByteSize();
257  }
258  section_data.Clear();
259  return 0;
260}
261