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
37}
38
41}
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.SetByteOrder(delegate_sp->GetByteOrder());
77 m_data.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
90bool ObjectFileJIT::IsExecutable() const { return false; }
91
94}
95
97 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
98 if (delegate_sp)
99 delegate_sp->PopulateSymtab(this, symtab);
100}
101
103 return false; // JIT code that is in a module is never stripped
104}
105
106void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
107 if (!m_sections_up) {
108 m_sections_up = std::make_unique<SectionList>();
109 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
110 if (delegate_sp) {
111 delegate_sp->PopulateSectionList(this, *m_sections_up);
112 unified_section_list = *m_sections_up;
113 }
114 }
115}
116
118 ModuleSP module_sp(GetModule());
119 if (module_sp) {
120 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
121 s->Printf("%p: ", static_cast<void *>(this));
122 s->Indent();
123 s->PutCString("ObjectFileJIT");
124
125 if (ArchSpec arch = GetArchitecture())
126 *s << ", arch = " << arch.GetArchitectureName();
127
128 s->EOL();
129
130 SectionList *sections = GetSectionList();
131 if (sections)
132 sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
133 UINT32_MAX);
134
135 if (m_symtab_up)
136 m_symtab_up->Dump(s, nullptr, eSortOrderNone);
137 }
138}
139
141 // TODO: maybe get from delegate, not needed for first pass
142 return UUID();
143}
144
146 // JIT modules don't have dependencies, but they could
147 // if external functions are called and we know where they are
148 files.Clear();
149 return 0;
150}
151
153 return Address();
154}
155
157
159
161
163 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
164 return delegate_sp->GetArchitecture();
165 return ArchSpec();
166}
167
169 bool value_is_offset) {
170 size_t num_loaded_sections = 0;
171 SectionList *section_list = GetSectionList();
172 if (section_list) {
173 const size_t num_sections = section_list->GetSize();
174 // "value" is an offset to apply to each top level segment
175 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
176 // Iterate through the object file sections to find all of the sections
177 // that size on disk (to avoid __PAGEZERO) and load them
178 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
179 if (section_sp && section_sp->GetFileSize() > 0 &&
180 !section_sp->IsThreadSpecific()) {
182 section_sp, section_sp->GetFileAddress() + value))
183 ++num_loaded_sections;
184 }
185 }
186 }
187 return num_loaded_sections > 0;
188}
189
191 lldb::offset_t section_offset, void *dst,
192 size_t dst_len) {
193 lldb::offset_t file_size = section->GetFileSize();
194 if (section_offset < file_size) {
195 size_t src_len = file_size - section_offset;
196 if (src_len > dst_len)
197 src_len = dst_len;
198 const uint8_t *src =
199 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
200
201 memcpy(dst, src, src_len);
202 return src_len;
203 }
204 return 0;
205}
206
207size_t
209 lldb_private::DataExtractor &section_data) {
210 if (section->GetFileSize()) {
211 const void *src = (void *)(uintptr_t)section->GetFileOffset();
212
213 DataBufferSP data_sp =
214 std::make_shared<DataBufferHeap>(src, section->GetFileSize());
215 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
216 section_data.SetByteOrder(GetByteOrder());
218 return section_data.GetByteSize();
219 }
220 section_data.Clear();
221 return 0;
222}
A section + offset based address class.
Definition: Address.h:62
An architecture specification class.
Definition: ArchSpec.h:31
An data extractor class.
Definition: DataExtractor.h:48
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.
uint32_t GetAddressByteSize() const
Get the current address size.
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
A file collection class.
Definition: FileSpecList.h:91
void Clear()
Clears the file list.
A file utility class.
Definition: FileSpec.h:56
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
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()
Definition: ObjectFileJIT.h:47
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()
Definition: ObjectFileJIT.h:45
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)
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
DataExtractor m_data
The data for this object file so things can be parsed lazily.
Definition: ObjectFile.h:758
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition: ObjectFile.h:762
std::unique_ptr< lldb_private::Symtab > m_symtab_up
Definition: ObjectFile.h:763
@ eTypeJIT
JIT code that has symbols, sections and possibly debug info.
Definition: ObjectFile.h:65
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:600
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:75
void Dump(llvm::raw_ostream &s, unsigned indent, Target *target, bool show_header, uint32_t depth) const
Definition: Section.cpp:638
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition: Section.cpp:544
bool SetSectionLoadAddress(const lldb::SectionSP &section_sp, lldb::addr_t load_addr, bool warn_multiple=false)
lldb::offset_t GetFileOffset() const
Definition: Section.h:154
lldb::offset_t GetFileSize() const
Definition: Section.h:160
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:401
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
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1143
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:334
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:414
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:335
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::ObjectFileJITDelegate > ObjectFileJITDelegateSP
Definition: lldb-forward.h:376
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371