1 //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringRef.h"
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"
16 #include "lldb/Core/PluginManager.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"
23 #include "lldb/Target/SectionLoadList.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/ArchSpec.h"
26 #include "lldb/Utility/DataBuffer.h"
27 #include "lldb/Utility/DataBufferHeap.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/RangeMap.h"
31 #include "lldb/Utility/StreamString.h"
32 #include "lldb/Utility/Timer.h"
33 #include "lldb/Utility/UUID.h"
36 #include "Utility/UuidCompatibility.h"
40 using namespace lldb_private;
42 char ObjectFileJIT::ID;
44 void ObjectFileJIT::Initialize() {
45 PluginManager::RegisterPlugin(GetPluginNameStatic(),
46 GetPluginDescriptionStatic(), CreateInstance,
47 CreateMemoryInstance, GetModuleSpecifications);
50 void ObjectFileJIT::Terminate() {
51 PluginManager::UnregisterPlugin(CreateInstance);
54 lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() {
55 static ConstString g_name("jit");
59 const char *ObjectFileJIT::GetPluginDescriptionStatic() {
60 return "JIT code object file";
63 ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
64 DataBufferSP &data_sp,
65 lldb::offset_t data_offset,
67 lldb::offset_t file_offset,
68 lldb::offset_t length) {
69 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
74 ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
75 DataBufferSP &data_sp,
76 const ProcessSP &process_sp,
77 lldb::addr_t header_addr) {
78 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
83 size_t ObjectFileJIT::GetModuleSpecifications(
84 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
85 lldb::offset_t data_offset, lldb::offset_t file_offset,
86 lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
87 // JIT'ed object file can't be read from a file on disk
91 ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
92 const ObjectFileJITDelegateSP &delegate_sp)
93 : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
95 m_delegate_wp = delegate_sp;
96 m_data.SetByteOrder(delegate_sp->GetByteOrder());
97 m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
101 ObjectFileJIT::~ObjectFileJIT() {}
103 bool ObjectFileJIT::ParseHeader() {
104 // JIT code is never in a file, nor is it required to have any header
108 ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
110 bool ObjectFileJIT::IsExecutable() const { return false; }
112 uint32_t ObjectFileJIT::GetAddressByteSize() const {
113 return m_data.GetAddressByteSize();
116 Symtab *ObjectFileJIT::GetSymtab() {
117 ModuleSP module_sp(GetModule());
119 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
120 if (m_symtab_up == nullptr) {
121 m_symtab_up.reset(new Symtab(this));
122 std::lock_guard<std::recursive_mutex> symtab_guard(
123 m_symtab_up->GetMutex());
124 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
126 delegate_sp->PopulateSymtab(this, *m_symtab_up);
127 // TODO: get symbols from delegate
128 m_symtab_up->Finalize();
131 return m_symtab_up.get();
134 bool ObjectFileJIT::IsStripped() {
135 return false; // JIT code that is in a module is never stripped
138 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
139 if (!m_sections_up) {
140 m_sections_up.reset(new SectionList());
141 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
143 delegate_sp->PopulateSectionList(this, *m_sections_up);
144 unified_section_list = *m_sections_up;
149 void ObjectFileJIT::Dump(Stream *s) {
150 ModuleSP module_sp(GetModule());
152 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
153 s->Printf("%p: ", static_cast<void *>(this));
155 s->PutCString("ObjectFileJIT");
157 if (ArchSpec arch = GetArchitecture())
158 *s << ", arch = " << arch.GetArchitectureName();
162 SectionList *sections = GetSectionList();
164 sections->Dump(s, nullptr, true, UINT32_MAX);
167 m_symtab_up->Dump(s, nullptr, eSortOrderNone);
171 UUID ObjectFileJIT::GetUUID() {
172 // TODO: maybe get from delegate, not needed for first pass
176 uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
177 // JIT modules don't have dependencies, but they could
178 // if external functions are called and we know where they are
183 lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
187 lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
189 ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
191 ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
193 ArchSpec ObjectFileJIT::GetArchitecture() {
194 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
195 return delegate_sp->GetArchitecture();
199 // PluginInterface protocol
200 lldb_private::ConstString ObjectFileJIT::GetPluginName() {
201 return GetPluginNameStatic();
204 uint32_t ObjectFileJIT::GetPluginVersion() { return 1; }
206 bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
207 bool value_is_offset) {
208 size_t num_loaded_sections = 0;
209 SectionList *section_list = GetSectionList();
211 const size_t num_sections = section_list->GetSize();
212 // "value" is an offset to apply to each top level segment
213 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
214 // Iterate through the object file sections to find all of the sections
215 // that size on disk (to avoid __PAGEZERO) and load them
216 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
217 if (section_sp && section_sp->GetFileSize() > 0 &&
218 !section_sp->IsThreadSpecific()) {
219 if (target.GetSectionLoadList().SetSectionLoadAddress(
220 section_sp, section_sp->GetFileAddress() + value))
221 ++num_loaded_sections;
225 return num_loaded_sections > 0;
228 size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
229 lldb::offset_t section_offset, void *dst,
231 lldb::offset_t file_size = section->GetFileSize();
232 if (section_offset < file_size) {
233 size_t src_len = file_size - section_offset;
234 if (src_len > dst_len)
237 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
239 memcpy(dst, src, src_len);
245 size_t ObjectFileJIT::ReadSectionData(
246 lldb_private::Section *section,
247 lldb_private::DataExtractor §ion_data) {
248 if (section->GetFileSize()) {
249 const void *src = (void *)(uintptr_t)section->GetFileOffset();
251 DataBufferSP data_sp =
252 std::make_shared<DataBufferHeap>(src, section->GetFileSize());
253 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
254 section_data.SetByteOrder(GetByteOrder());
255 section_data.SetAddressByteSize(GetAddressByteSize());
256 return section_data.GetByteSize();
258 section_data.Clear();