LLDB  mainline
ObjectFileJIT.cpp
Go to the documentation of this file.
1 //===-- ObjectFileJIT.cpp -------------------------------------------------===//
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"
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"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/ArchSpec.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/RangeMap.h"
32 #include "lldb/Utility/Timer.h"
33 #include "lldb/Utility/UUID.h"
34 
35 #ifndef __APPLE__
37 #endif
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
43 
45 
47  PluginManager::RegisterPlugin(GetPluginNameStatic(),
48  GetPluginDescriptionStatic(), CreateInstance,
49  CreateMemoryInstance, GetModuleSpecifications);
50 }
51 
53  PluginManager::UnregisterPlugin(CreateInstance);
54 }
55 
56 ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
57  DataBufferSP &data_sp,
58  lldb::offset_t data_offset,
59  const FileSpec *file,
60  lldb::offset_t file_offset,
61  lldb::offset_t length) {
62  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
63  // a file
64  return nullptr;
65 }
66 
67 ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
68  DataBufferSP &data_sp,
69  const ProcessSP &process_sp,
70  lldb::addr_t header_addr) {
71  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
72  // memory
73  return nullptr;
74 }
75 
77  const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
78  lldb::offset_t data_offset, lldb::offset_t file_offset,
80  // JIT'ed object file can't be read from a file on disk
81  return 0;
82 }
83 
84 ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
85  const ObjectFileJITDelegateSP &delegate_sp)
86  : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
87  if (delegate_sp) {
88  m_delegate_wp = delegate_sp;
89  m_data.SetByteOrder(delegate_sp->GetByteOrder());
90  m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
91  }
92 }
93 
95 
97  // JIT code is never in a file, nor is it required to have any header
98  return false;
99 }
100 
102 
103 bool ObjectFileJIT::IsExecutable() const { return false; }
104 
106  return m_data.GetAddressByteSize();
107 }
108 
110  ModuleSP module_sp(GetModule());
111  if (module_sp) {
112  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
113  if (m_symtab_up == nullptr) {
114  m_symtab_up = std::make_unique<Symtab>(this);
115  std::lock_guard<std::recursive_mutex> symtab_guard(
116  m_symtab_up->GetMutex());
117  ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
118  if (delegate_sp)
119  delegate_sp->PopulateSymtab(this, *m_symtab_up);
120  // TODO: get symbols from delegate
121  m_symtab_up->Finalize();
122  }
123  }
124  return m_symtab_up.get();
125 }
126 
128  return false; // JIT code that is in a module is never stripped
129 }
130 
131 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
132  if (!m_sections_up) {
133  m_sections_up = std::make_unique<SectionList>();
134  ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
135  if (delegate_sp) {
136  delegate_sp->PopulateSectionList(this, *m_sections_up);
137  unified_section_list = *m_sections_up;
138  }
139  }
140 }
141 
143  ModuleSP module_sp(GetModule());
144  if (module_sp) {
145  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
146  s->Printf("%p: ", static_cast<void *>(this));
147  s->Indent();
148  s->PutCString("ObjectFileJIT");
149 
150  if (ArchSpec arch = GetArchitecture())
151  *s << ", arch = " << arch.GetArchitectureName();
152 
153  s->EOL();
154 
155  SectionList *sections = GetSectionList();
156  if (sections)
157  sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
158  UINT32_MAX);
159 
160  if (m_symtab_up)
161  m_symtab_up->Dump(s, nullptr, eSortOrderNone);
162  }
163 }
164 
166  // TODO: maybe get from delegate, not needed for first pass
167  return UUID();
168 }
169 
171  // JIT modules don't have dependencies, but they could
172  // if external functions are called and we know where they are
173  files.Clear();
174  return 0;
175 }
176 
178  return Address();
179 }
180 
182 
184 
186 
188  if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
189  return delegate_sp->GetArchitecture();
190  return ArchSpec();
191 }
192 
194  bool value_is_offset) {
195  size_t num_loaded_sections = 0;
196  SectionList *section_list = GetSectionList();
197  if (section_list) {
198  const size_t num_sections = section_list->GetSize();
199  // "value" is an offset to apply to each top level segment
200  for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
201  // Iterate through the object file sections to find all of the sections
202  // that size on disk (to avoid __PAGEZERO) and load them
203  SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
204  if (section_sp && section_sp->GetFileSize() > 0 &&
205  !section_sp->IsThreadSpecific()) {
207  section_sp, section_sp->GetFileAddress() + value))
208  ++num_loaded_sections;
209  }
210  }
211  }
212  return num_loaded_sections > 0;
213 }
214 
216  lldb::offset_t section_offset, void *dst,
217  size_t dst_len) {
218  lldb::offset_t file_size = section->GetFileSize();
219  if (section_offset < file_size) {
220  size_t src_len = file_size - section_offset;
221  if (src_len > dst_len)
222  src_len = dst_len;
223  const uint8_t *src =
224  ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
225 
226  memcpy(dst, src, src_len);
227  return src_len;
228  }
229  return 0;
230 }
231 
233  lldb_private::Section *section,
234  lldb_private::DataExtractor &section_data) {
235  if (section->GetFileSize()) {
236  const void *src = (void *)(uintptr_t)section->GetFileOffset();
237 
238  DataBufferSP data_sp =
239  std::make_shared<DataBufferHeap>(src, section->GetFileSize());
240  section_data.SetData(data_sp, 0, data_sp->GetByteSize());
241  section_data.SetByteOrder(GetByteOrder());
242  section_data.SetAddressByteSize(GetAddressByteSize());
243  return section_data.GetByteSize();
244  }
245  section_data.Clear();
246  return 0;
247 }
lldb_private::SectionLoadList::SetSectionLoadAddress
bool SetSectionLoadAddress(const lldb::SectionSP &section_sp, lldb::addr_t load_addr, bool warn_multiple=false)
Definition: SectionLoadList.cpp:63
lldb_private::UUID
Definition: UUID.h:23
UuidCompatibility.h
ObjectFileJIT
Definition: ObjectFileJIT.h:17
lldb_private::ArchSpec
Definition: ArchSpec.h:33
ObjectFileJIT::ParseHeader
bool ParseHeader() override
Attempts to parse the object header.
Definition: ObjectFileJIT.cpp:96
lldb_private::SectionList::Dump
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, bool show_header, uint32_t depth) const
Definition: Section.cpp:570
ModuleSpec.h
UUID.h
Host.h
ObjectFileJIT::CreateInstance
static lldb_private::ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
Definition: ObjectFileJIT.cpp:56
ObjectFileJIT::GetByteOrder
lldb::ByteOrder GetByteOrder() const override
Gets whether endian swapping should occur when extracting data from this object file.
Definition: ObjectFileJIT.cpp:101
ObjectFileJIT::ID
static char ID
Definition: ObjectFileJIT.h:52
lldb_private::SectionList::GetSize
size_t GetSize() const
Definition: Section.h:74
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:280
Module.h
lldb_private::ObjectFile::m_symtab_up
std::unique_ptr< lldb_private::Symtab > m_symtab_up
Definition: ObjectFile.h:710
SectionLoadList.h
lldb_private::SectionList
Definition: Section.h:34
lldb_private::Section::GetFileSize
lldb::offset_t GetFileSize() const
Definition: Section.h:146
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
ObjectFileJIT::CalculateStrata
ObjectFile::Strata CalculateStrata() override
The object file should be able to calculate the strata of the object file.
Definition: ObjectFileJIT.cpp:185
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
RangeMap.h
lldb_private::ObjectFile::Type
Type
Definition: ObjectFile.h:64
Debugger.h
lldb_private::Target
Definition: Target.h:451
Section.h
lldb_private::eSortOrderNone
@ eSortOrderNone
Definition: lldb-private-enumerations.h:110
ObjectFileJIT::GetSymtab
lldb_private::Symtab * GetSymtab() override
Gets the symbol table for the currently selected architecture (and object for archives).
Definition: ObjectFileJIT.cpp:109
lldb_private::DataExtractor::SetData
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
Definition: DataExtractor.cpp:225
ObjectFileJIT::GetAddressByteSize
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
Definition: ObjectFileJIT.cpp:105
Process.h
Target.h
lldb_private::DataExtractor::SetByteOrder
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
Definition: DataExtractor.h:931
Platform.h
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::ObjectFile::GetSectionList
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:583
ObjectFileJIT::IsExecutable
bool IsExecutable() const override
Tells whether this object file is capable of being the main executable for a process.
Definition: ObjectFileJIT.cpp:103
lldb_private::Stream::Indent
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
ObjectFileJIT::GetModuleSpecifications
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
Definition: ObjectFileJIT.cpp:76
lldb_private::DataExtractor::GetAddressByteSize
uint32_t GetAddressByteSize() const
Get the current address size.
Definition: DataExtractor.h:264
lldb_private::DataExtractor
Definition: DataExtractor.h:48
Log.h
ObjectFileJIT::GetDependentModules
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override
Definition: ObjectFileJIT.cpp:170
ObjectFileJIT.h
ObjectFileJIT::Initialize
static void Initialize()
Definition: ObjectFileJIT.cpp:46
lldb_private::DataExtractor::SetAddressByteSize
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
Definition: DataExtractor.h:845
StreamString.h
lldb_private::DataExtractor::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
Definition: DataExtractor.h:594
FileSpecList.h
Timer.h
ObjectFileJIT::SetLoadAddress
bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override
Sets the load address for an entire module, assuming a rigid slide of sections, if possible in the im...
Definition: ObjectFileJIT.cpp:193
ObjectFileJIT::ReadSectionData
size_t ReadSectionData(lldb_private::Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) override
Definition: ObjectFileJIT.cpp:215
ObjectFileJIT::~ObjectFileJIT
~ObjectFileJIT() override
lldb_private::SectionList::GetSectionAtIndex
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:476
lldb_private::Stream::GetIndentLevel
unsigned GetIndentLevel() const
Get the current indentation level.
Definition: Stream.cpp:160
ObjectFileJIT::ObjectFileJIT
ObjectFileJIT(const lldb::ModuleSP &module_sp, const lldb::ObjectFileJITDelegateSP &delegate_sp)
Definition: ObjectFileJIT.cpp:84
lldb_private::ObjectFile::eTypeJIT
@ eTypeJIT
JIT code that has symbols, sections and possibly debug info.
Definition: ObjectFile.h:81
ObjectFile.h
ObjectFileJIT::GetEntryPointAddress
lldb_private::Address GetEntryPointAddress() override
Returns the address of the Entry Point in this object file - if the object file doesn't have an entry...
Definition: ObjectFileJIT.cpp:177
lldb_private::Section
Definition: Section.h:96
StreamFile.h
ObjectFileJIT::Terminate
static void Terminate()
Definition: ObjectFileJIT.cpp:52
ObjectFileJIT::CreateMemoryInstance
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
Definition: ObjectFileJIT.cpp:67
lldb_private::Symtab
Definition: Symtab.h:22
uint32_t
lldb_private::Address
Definition: Address.h:59
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
ObjectFileJIT::GetArchitecture
lldb_private::ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
Definition: ObjectFileJIT.cpp:187
ObjectFileJIT::CreateSections
void CreateSections(lldb_private::SectionList &unified_section_list) override
Definition: ObjectFileJIT.cpp:131
ArchSpec.h
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::ObjectFile::m_data
DataExtractor m_data
The data for this object file so things can be parsed lazily.
Definition: ObjectFile.h:706
lldb_private::ModuleChild::GetModule
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
ObjectFileJIT::m_delegate_wp
lldb::ObjectFileJITDelegateWP m_delegate_wp
Definition: ObjectFileJIT.h:104
ObjectFileJIT::CalculateType
ObjectFile::Type CalculateType() override
The object file should be able to calculate its type by looking at its file header and possibly the s...
Definition: ObjectFileJIT.cpp:183
PluginManager.h
lldb_private::Target::GetSectionLoadList
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1040
lldb_private::ObjectFile::eStrataJIT
@ eStrataJIT
Definition: ObjectFile.h:91
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
ObjectFileJIT::IsStripped
bool IsStripped() override
Detect if this object file has been stripped of local symbols.
Definition: ObjectFileJIT.cpp:127
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Section::GetFileOffset
lldb::offset_t GetFileOffset() const
Definition: Section.h:140
FileSpec.h
lldb_private::DataExtractor::Clear
void Clear()
Clears the object state.
Definition: DataExtractor.cpp:194
ObjectFileJIT::GetUUID
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
Definition: ObjectFileJIT.cpp:165
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
DataBuffer.h
lldb_private::DataExtractor::GetByteSize
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
Definition: DataExtractor.h:270
ObjectFileJIT::GetBaseAddress
lldb_private::Address GetBaseAddress() override
Returns base address of this object file.
Definition: ObjectFileJIT.cpp:181
lldb_private::ObjectFile::Strata
Strata
Definition: ObjectFile.h:85
lldb
Definition: SBAddress.h:15
ObjectFileJIT::Dump
void Dump(lldb_private::Stream *s) override
Dump a description of this object to a Stream.
Definition: ObjectFileJIT.cpp:142
lldb_private::ObjectFile
Definition: ObjectFile.h:58
DataBufferHeap.h
lldb_private::ObjectFile::m_sections_up
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition: ObjectFile.h:709
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138