LLDB  mainline
ObjectFileJIT.cpp
Go to the documentation of this file.
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"
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  PluginManager::RegisterPlugin(GetPluginNameStatic(),
44  GetPluginDescriptionStatic(), CreateInstance,
45  CreateMemoryInstance, GetModuleSpecifications);
46 }
47 
49  PluginManager::UnregisterPlugin(CreateInstance);
50 }
51 
53  static ConstString g_name("jit");
54  return g_name;
55 }
56 
58  return "JIT code object file";
59 }
60 
61 ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
62  DataBufferSP &data_sp,
63  lldb::offset_t data_offset,
64  const FileSpec *file,
65  lldb::offset_t file_offset,
66  lldb::offset_t length) {
67  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
68  // a file
69  return NULL;
70 }
71 
72 ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
73  DataBufferSP &data_sp,
74  const ProcessSP &process_sp,
75  lldb::addr_t header_addr) {
76  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
77  // memory
78  return NULL;
79 }
80 
82  const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
83  lldb::offset_t data_offset, lldb::offset_t file_offset,
85  // JIT'ed object file can't be read from a file on disk
86  return 0;
87 }
88 
89 ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
90  const ObjectFileJITDelegateSP &delegate_sp)
91  : ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
92  if (delegate_sp) {
93  m_delegate_wp = delegate_sp;
94  m_data.SetByteOrder(delegate_sp->GetByteOrder());
95  m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
96  }
97 }
98 
100 
102  // JIT code is never in a file, nor is it required to have any header
103  return false;
104 }
105 
107 
108 bool ObjectFileJIT::IsExecutable() const { return false; }
109 
111  return m_data.GetAddressByteSize();
112 }
113 
115  ModuleSP module_sp(GetModule());
116  if (module_sp) {
117  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
118  if (m_symtab_up == NULL) {
119  m_symtab_up.reset(new Symtab(this));
120  std::lock_guard<std::recursive_mutex> symtab_guard(
121  m_symtab_up->GetMutex());
122  ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
123  if (delegate_sp)
124  delegate_sp->PopulateSymtab(this, *m_symtab_up);
125  // TODO: get symbols from delegate
126  m_symtab_up->Finalize();
127  }
128  }
129  return m_symtab_up.get();
130 }
131 
133  return false; // JIT code that is in a module is never stripped
134 }
135 
136 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
137  if (!m_sections_up) {
138  m_sections_up.reset(new SectionList());
139  ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
140  if (delegate_sp) {
141  delegate_sp->PopulateSectionList(this, *m_sections_up);
142  unified_section_list = *m_sections_up;
143  }
144  }
145 }
146 
148  ModuleSP module_sp(GetModule());
149  if (module_sp) {
150  std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
151  s->Printf("%p: ", static_cast<void *>(this));
152  s->Indent();
153  s->PutCString("ObjectFileJIT");
154 
155  if (ArchSpec arch = GetArchitecture())
156  *s << ", arch = " << arch.GetArchitectureName();
157 
158  s->EOL();
159 
160  SectionList *sections = GetSectionList();
161  if (sections)
162  sections->Dump(s, NULL, true, UINT32_MAX);
163 
164  if (m_symtab_up)
165  m_symtab_up->Dump(s, NULL, eSortOrderNone);
166  }
167 }
168 
170  // TODO: maybe get from delegate, not needed for first pass
171  return UUID();
172 }
173 
175  // JIT modules don't have dependencies, but they could
176  // if external functions are called and we know where they are
177  files.Clear();
178  return 0;
179 }
180 
182  return Address();
183 }
184 
186 
188 
190 
192  if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
193  return delegate_sp->GetArchitecture();
194  return ArchSpec();
195 }
196 
197 // PluginInterface protocol
199  return GetPluginNameStatic();
200 }
201 
203 
205  bool value_is_offset) {
206  size_t num_loaded_sections = 0;
207  SectionList *section_list = GetSectionList();
208  if (section_list) {
209  const size_t num_sections = section_list->GetSize();
210  // "value" is an offset to apply to each top level segment
211  for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
212  // Iterate through the object file sections to find all of the sections
213  // that size on disk (to avoid __PAGEZERO) and load them
214  SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
215  if (section_sp && section_sp->GetFileSize() > 0 &&
216  !section_sp->IsThreadSpecific()) {
218  section_sp, section_sp->GetFileAddress() + value))
219  ++num_loaded_sections;
220  }
221  }
222  }
223  return num_loaded_sections > 0;
224 }
225 
227  lldb::offset_t section_offset, void *dst,
228  size_t dst_len) {
229  lldb::offset_t file_size = section->GetFileSize();
230  if (section_offset < file_size) {
231  size_t src_len = file_size - section_offset;
232  if (src_len > dst_len)
233  src_len = dst_len;
234  const uint8_t *src =
235  ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
236 
237  memcpy(dst, src, src_len);
238  return src_len;
239  }
240  return 0;
241 }
242 
244  lldb_private::Section *section,
245  lldb_private::DataExtractor &section_data) {
246  if (section->GetFileSize()) {
247  const void *src = (void *)(uintptr_t)section->GetFileOffset();
248 
249  DataBufferSP data_sp(
250  new lldb_private::DataBufferHeap(src, section->GetFileSize()));
251  if (data_sp) {
252  section_data.SetData(data_sp, 0, data_sp->GetByteSize());
253  section_data.SetByteOrder(GetByteOrder());
254  section_data.SetAddressByteSize(GetAddressByteSize());
255  return section_data.GetByteSize();
256  }
257  }
258  section_data.Clear();
259  return 0;
260 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
An data extractor class.
Definition: DataExtractor.h:47
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
static void Terminate()
lldb::offset_t GetFileOffset() const
Definition: Section.h:148
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
A file utility class.
Definition: FileSpec.h:55
An architecture specification class.
Definition: ArchSpec.h:32
A library that can be linked against but not used for execution.
Definition: ObjectFile.h:75
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
lldb::ObjectFileJITDelegateWP m_delegate_wp
Definition: ObjectFileJIT.h:97
ObjectFile::Strata CalculateStrata() override
The object file should be able to calculate the strata of the object file.
std::unique_ptr< lldb_private::Symtab > m_symtab_up
Definition: ObjectFile.h:679
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
lldb_private::Address GetEntryPointAddress() override
Returns the address of the Entry Point in this object file - if the object file doesn&#39;t have an entry...
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
bool IsStripped() override
Detect if this object file has been stripped of local symbols.
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...
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:27
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetFileSize() const
Definition: Section.h:154
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition: ObjectFile.h:678
~ObjectFileJIT() override
bool SetSectionLoadAddress(const lldb::SectionSP &section_sp, lldb::addr_t load_addr, bool warn_multiple=false)
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override
lldb_private::Address GetBaseAddress() override
Returns base address of this object file.
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::ConstString GetPluginName() override
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
bool IsExecutable() const override
Tells whether this object file is capable of being the main executable for a process.
uint64_t offset_t
Definition: lldb-types.h:87
void Clear()
Clears the object state.
static void Initialize()
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1012
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
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:605
lldb_private::ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:497
ObjectFileJIT(const lldb::ModuleSP &module_sp, const lldb::ObjectFileJITDelegateSP &delegate_sp)
bool ParseHeader() override
Attempts to parse the object header.
ObjectFile::Type CalculateType() override
The object file should be able to calculate its type by looking at its file header and possibly the s...
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
A section + offset based address class.
Definition: Address.h:80
static const char * GetPluginDescriptionStatic()
size_t ReadSectionData(lldb_private::Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) override
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
static lldb_private::ConstString GetPluginNameStatic()
DataExtractor m_data
The data for this object file so things can be parsed lazily.
Definition: ObjectFile.h:675
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)
uint64_t addr_t
Definition: lldb-types.h:83
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)
void CreateSections(lldb_private::SectionList &unified_section_list) override
A uniqued constant string class.
Definition: ConstString.h:38
uint32_t GetPluginVersion() override
Definition: SBAddress.h:15
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
size_t GetSize() const
Definition: Section.h:83
void Dump(lldb_private::Stream *s) override
Dump a description of this object to a Stream.
lldb_private::Symtab * GetSymtab() override
Gets the symbol table for the currently selected architecture (and object for archives).
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
uint32_t GetAddressByteSize() const
Get the current address size.
lldb::ByteOrder GetByteOrder() const override
Gets whether endian swapping should occur when extracting data from this object file.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
void Dump(Stream *s, Target *target, bool show_header, uint32_t depth) const
Definition: Section.cpp:591