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 DataExtractorSP extractor_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, DataExtractorSP(), 0),
75 if (delegate_sp) {
76 m_delegate_wp = delegate_sp;
77 m_data_nsp->SetByteOrder(delegate_sp->GetByteOrder());
78 m_data_nsp->SetAddressByteSize(delegate_sp->GetAddressByteSize());
79 }
80}
81
83
85 // JIT code is never in a file, nor is it required to have any header
86 return false;
87}
88
90 return m_data_nsp->GetByteOrder();
91}
92
93bool ObjectFileJIT::IsExecutable() const { return false; }
94
96 return m_data_nsp->GetAddressByteSize();
97}
98
100 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
101 if (delegate_sp)
102 delegate_sp->PopulateSymtab(this, symtab);
103}
104
106 return false; // JIT code that is in a module is never stripped
107}
108
109void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
110 if (!m_sections_up) {
111 m_sections_up = std::make_unique<SectionList>();
112 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
113 if (delegate_sp) {
114 delegate_sp->PopulateSectionList(this, *m_sections_up);
115 unified_section_list = *m_sections_up;
116 }
117 }
118}
119
121 ModuleSP module_sp(GetModule());
122 if (module_sp) {
123 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
124 s->Printf("%p: ", static_cast<void *>(this));
125 s->Indent();
126 s->PutCString("ObjectFileJIT");
127
128 if (ArchSpec arch = GetArchitecture())
129 *s << ", arch = " << arch.GetArchitectureName();
130
131 s->EOL();
132
133 SectionList *sections = GetSectionList();
134 if (sections)
135 sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
136 UINT32_MAX);
137
138 if (m_symtab_up)
139 m_symtab_up->Dump(s, nullptr, eSortOrderNone);
140 }
141}
142
144 // TODO: maybe get from delegate, not needed for first pass
145 return UUID();
146}
147
149 // JIT modules don't have dependencies, but they could
150 // if external functions are called and we know where they are
151 files.Clear();
152 return 0;
153}
154
158
160
162
164
166 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
167 return delegate_sp->GetArchitecture();
168 return ArchSpec();
169}
170
172 bool value_is_offset) {
173 size_t num_loaded_sections = 0;
174 SectionList *section_list = GetSectionList();
175 if (section_list) {
176 const size_t num_sections = section_list->GetSize();
177 // "value" is an offset to apply to each top level segment
178 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
179 // Iterate through the object file sections to find all of the sections
180 // that size on disk (to avoid __PAGEZERO) and load them
181 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
182 if (section_sp && section_sp->GetFileSize() > 0 &&
183 !section_sp->IsThreadSpecific()) {
184 if (target.SetSectionLoadAddress(section_sp,
185 section_sp->GetFileAddress() + value))
186 ++num_loaded_sections;
187 }
188 }
189 }
190 return num_loaded_sections > 0;
191}
192
194 lldb::offset_t section_offset, void *dst,
195 size_t dst_len) {
196 lldb::offset_t file_size = section->GetFileSize();
197 if (section_offset < file_size) {
198 size_t src_len = file_size - section_offset;
199 if (src_len > dst_len)
200 src_len = dst_len;
201 const uint8_t *src =
202 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
203
204 memcpy(dst, src, src_len);
205 return src_len;
206 }
207 return 0;
208}
209
210size_t
212 lldb_private::DataExtractor &section_data) {
213 if (section->GetFileSize()) {
214 const void *src = (void *)(uintptr_t)section->GetFileOffset();
215
216 DataBufferSP data_sp =
217 std::make_shared<DataBufferHeap>(src, section->GetFileSize());
218 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
219 section_data.SetByteOrder(GetByteOrder());
221 return section_data.GetByteSize();
222 }
223 section_data.Clear();
224 return 0;
225}
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.
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.
static lldb_private::ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
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
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).
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
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::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP