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 "lldb/Core/Module.h"
14#include "lldb/Core/Section.h"
16#include "lldb/Target/Process.h"
18#include "lldb/Target/Target.h"
24#include "lldb/Utility/Log.h"
25#include "lldb/Utility/Timer.h"
26#include "lldb/Utility/UUID.h"
27
28using namespace lldb;
29using namespace lldb_private;
30
32
38
42
44 DataBufferSP data_sp,
45 lldb::offset_t data_offset,
46 const FileSpec *file,
47 lldb::offset_t file_offset,
48 lldb::offset_t length) {
49 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
50 // a file
51 return nullptr;
52}
53
56 const ProcessSP &process_sp,
57 lldb::addr_t header_addr) {
58 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
59 // memory
60 return nullptr;
61}
62
64 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
65 lldb::offset_t data_offset, lldb::offset_t file_offset,
67 // JIT'ed object file can't be read from a file on disk
68 return 0;
69}
70
72 const ObjectFileJITDelegateSP &delegate_sp)
73 : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
74 if (delegate_sp) {
75 m_delegate_wp = delegate_sp;
76 m_data_nsp->SetByteOrder(delegate_sp->GetByteOrder());
77 m_data_nsp->SetAddressByteSize(delegate_sp->GetAddressByteSize());
78 }
79}
80
82
84 // JIT code is never in a file, nor is it required to have any header
85 return false;
86}
87
89 return m_data_nsp->GetByteOrder();
90}
91
92bool ObjectFileJIT::IsExecutable() const { return false; }
93
95 return m_data_nsp->GetAddressByteSize();
96}
97
99 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
100 if (delegate_sp)
101 delegate_sp->PopulateSymtab(this, symtab);
102}
103
105 return false; // JIT code that is in a module is never stripped
106}
107
108void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
109 if (!m_sections_up) {
110 m_sections_up = std::make_unique<SectionList>();
111 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
112 if (delegate_sp) {
113 delegate_sp->PopulateSectionList(this, *m_sections_up);
114 unified_section_list = *m_sections_up;
115 }
116 }
117}
118
120 ModuleSP module_sp(GetModule());
121 if (module_sp) {
122 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
123 s->Printf("%p: ", static_cast<void *>(this));
124 s->Indent();
125 s->PutCString("ObjectFileJIT");
126
127 if (ArchSpec arch = GetArchitecture())
128 *s << ", arch = " << arch.GetArchitectureName();
129
130 s->EOL();
131
132 SectionList *sections = GetSectionList();
133 if (sections)
134 sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
135 UINT32_MAX);
136
137 if (m_symtab_up)
138 m_symtab_up->Dump(s, nullptr, eSortOrderNone);
139 }
140}
141
143 // TODO: maybe get from delegate, not needed for first pass
144 return UUID();
145}
146
148 // JIT modules don't have dependencies, but they could
149 // if external functions are called and we know where they are
150 files.Clear();
151 return 0;
152}
153
157
159
161
163
165 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
166 return delegate_sp->GetArchitecture();
167 return ArchSpec();
168}
169
171 bool value_is_offset) {
172 size_t num_loaded_sections = 0;
173 SectionList *section_list = GetSectionList();
174 if (section_list) {
175 const size_t num_sections = section_list->GetSize();
176 // "value" is an offset to apply to each top level segment
177 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
178 // Iterate through the object file sections to find all of the sections
179 // that size on disk (to avoid __PAGEZERO) and load them
180 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
181 if (section_sp && section_sp->GetFileSize() > 0 &&
182 !section_sp->IsThreadSpecific()) {
183 if (target.SetSectionLoadAddress(section_sp,
184 section_sp->GetFileAddress() + value))
185 ++num_loaded_sections;
186 }
187 }
188 }
189 return num_loaded_sections > 0;
190}
191
193 lldb::offset_t section_offset, void *dst,
194 size_t dst_len) {
195 lldb::offset_t file_size = section->GetFileSize();
196 if (section_offset < file_size) {
197 size_t src_len = file_size - section_offset;
198 if (src_len > dst_len)
199 src_len = dst_len;
200 const uint8_t *src =
201 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
202
203 memcpy(dst, src, src_len);
204 return src_len;
205 }
206 return 0;
207}
208
209size_t
211 lldb_private::DataExtractor &section_data) {
212 if (section->GetFileSize()) {
213 const void *src = (void *)(uintptr_t)section->GetFileOffset();
214
215 DataBufferSP data_sp =
216 std::make_shared<DataBufferHeap>(src, section->GetFileSize());
217 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
218 section_data.SetByteOrder(GetByteOrder());
220 return section_data.GetByteSize();
221 }
222 section_data.Clear();
223 return 0;
224}
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:31
An data extractor class.
void Clear()
Clears the object state.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
A file collection class.
void Clear()
Clears the file list.
A file utility class.
Definition FileSpec.h:57
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
ObjectFileJIT(const lldb::ModuleSP &module_sp, const lldb::ObjectFileJITDelegateSP &delegate_sp)
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override
Extract the dependent modules from an object file.
bool IsStripped() override
Detect if this object file has been stripped of local symbols.
bool IsExecutable() const override
Tells whether this object file is capable of being the main executable for a process.
lldb::ByteOrder GetByteOrder() const override
Gets whether endian swapping should occur when extracting data from this object file.
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)
lldb_private::Address GetBaseAddress() override
Returns base address of this object file.
static llvm::StringRef GetPluginDescriptionStatic()
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
lldb_private::ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
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...
void Dump(lldb_private::Stream *s) override
Dump a description of this object to a Stream.
bool ParseHeader() override
Attempts to parse the object header.
lldb::ObjectFileJITDelegateWP m_delegate_wp
ObjectFile::Strata CalculateStrata() override
The object file should be able to calculate the strata of the object file.
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
static llvm::StringRef GetPluginNameStatic()
void CreateSections(lldb_private::SectionList &unified_section_list) override
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 ReadSectionData(lldb_private::Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) override
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...
void ParseSymtab(lldb_private::Symtab &symtab) override
Parse the symbol table into the provides symbol table object.
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
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)
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h:799
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataBufferSP data_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
std::unique_ptr< lldb_private::Symtab > m_symtab_up
Definition ObjectFile.h:800
DataExtractorNSP m_data_nsp
The data for this object file so things can be parsed lazily.
Definition ObjectFile.h:792
@ eTypeJIT
JIT code that has symbols, sections and possibly debug info.
Definition ObjectFile.h:67
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
size_t GetSize() const
Definition Section.h:77
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, bool show_header, uint32_t depth) const
Definition Section.cpp:650
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:557
lldb::offset_t GetFileOffset() const
Definition Section.h:183
lldb::offset_t GetFileSize() const
Definition Section.h:189
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:406
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
unsigned GetIndentLevel() const
Get the current indentation level.
Definition Stream.cpp:187
bool SetSectionLoadAddress(const lldb::SectionSP &section, lldb::addr_t load_addr, bool warn_multiple=false)
Definition Target.cpp:3334
Represents UUID's of various sizes.
Definition UUID.h:27
#define UINT32_MAX
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::ObjectFileJITDelegate > ObjectFileJITDelegateSP
std::shared_ptr< lldb_private::Module > ModuleSP