1*fe013be4SDimitry Andric //===-- ObjectFileJIT.cpp -------------------------------------------------===//
2*fe013be4SDimitry Andric //
3*fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe013be4SDimitry Andric //
7*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8*fe013be4SDimitry Andric 
9*fe013be4SDimitry Andric #include "llvm/ADT/StringRef.h"
10*fe013be4SDimitry Andric 
11*fe013be4SDimitry Andric #include "lldb/Core/Module.h"
12*fe013be4SDimitry Andric #include "lldb/Core/ModuleSpec.h"
13*fe013be4SDimitry Andric #include "lldb/Core/PluginManager.h"
14*fe013be4SDimitry Andric #include "lldb/Core/Section.h"
15*fe013be4SDimitry Andric #include "lldb/Expression/ObjectFileJIT.h"
16*fe013be4SDimitry Andric #include "lldb/Target/Process.h"
17*fe013be4SDimitry Andric #include "lldb/Target/SectionLoadList.h"
18*fe013be4SDimitry Andric #include "lldb/Target/Target.h"
19*fe013be4SDimitry Andric #include "lldb/Utility/ArchSpec.h"
20*fe013be4SDimitry Andric #include "lldb/Utility/DataBuffer.h"
21*fe013be4SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
22*fe013be4SDimitry Andric #include "lldb/Utility/FileSpec.h"
23*fe013be4SDimitry Andric #include "lldb/Utility/FileSpecList.h"
24*fe013be4SDimitry Andric #include "lldb/Utility/Log.h"
25*fe013be4SDimitry Andric #include "lldb/Utility/Timer.h"
26*fe013be4SDimitry Andric #include "lldb/Utility/UUID.h"
27*fe013be4SDimitry Andric 
28*fe013be4SDimitry Andric using namespace lldb;
29*fe013be4SDimitry Andric using namespace lldb_private;
30*fe013be4SDimitry Andric 
31*fe013be4SDimitry Andric char ObjectFileJIT::ID;
32*fe013be4SDimitry Andric 
Initialize()33*fe013be4SDimitry Andric void ObjectFileJIT::Initialize() {
34*fe013be4SDimitry Andric   PluginManager::RegisterPlugin(GetPluginNameStatic(),
35*fe013be4SDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance,
36*fe013be4SDimitry Andric                                 CreateMemoryInstance, GetModuleSpecifications);
37*fe013be4SDimitry Andric }
38*fe013be4SDimitry Andric 
Terminate()39*fe013be4SDimitry Andric void ObjectFileJIT::Terminate() {
40*fe013be4SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
41*fe013be4SDimitry Andric }
42*fe013be4SDimitry Andric 
CreateInstance(const lldb::ModuleSP & module_sp,DataBufferSP data_sp,lldb::offset_t data_offset,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t length)43*fe013be4SDimitry Andric ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
44*fe013be4SDimitry Andric                                           DataBufferSP data_sp,
45*fe013be4SDimitry Andric                                           lldb::offset_t data_offset,
46*fe013be4SDimitry Andric                                           const FileSpec *file,
47*fe013be4SDimitry Andric                                           lldb::offset_t file_offset,
48*fe013be4SDimitry Andric                                           lldb::offset_t length) {
49*fe013be4SDimitry Andric   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
50*fe013be4SDimitry Andric   // a file
51*fe013be4SDimitry Andric   return nullptr;
52*fe013be4SDimitry Andric }
53*fe013be4SDimitry Andric 
CreateMemoryInstance(const lldb::ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,lldb::addr_t header_addr)54*fe013be4SDimitry Andric ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
55*fe013be4SDimitry Andric                                                 WritableDataBufferSP data_sp,
56*fe013be4SDimitry Andric                                                 const ProcessSP &process_sp,
57*fe013be4SDimitry Andric                                                 lldb::addr_t header_addr) {
58*fe013be4SDimitry Andric   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
59*fe013be4SDimitry Andric   // memory
60*fe013be4SDimitry Andric   return nullptr;
61*fe013be4SDimitry Andric }
62*fe013be4SDimitry Andric 
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)63*fe013be4SDimitry Andric size_t ObjectFileJIT::GetModuleSpecifications(
64*fe013be4SDimitry Andric     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
65*fe013be4SDimitry Andric     lldb::offset_t data_offset, lldb::offset_t file_offset,
66*fe013be4SDimitry Andric     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
67*fe013be4SDimitry Andric   // JIT'ed object file can't be read from a file on disk
68*fe013be4SDimitry Andric   return 0;
69*fe013be4SDimitry Andric }
70*fe013be4SDimitry Andric 
ObjectFileJIT(const lldb::ModuleSP & module_sp,const ObjectFileJITDelegateSP & delegate_sp)71*fe013be4SDimitry Andric ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
72*fe013be4SDimitry Andric                              const ObjectFileJITDelegateSP &delegate_sp)
73*fe013be4SDimitry Andric     : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
74*fe013be4SDimitry Andric   if (delegate_sp) {
75*fe013be4SDimitry Andric     m_delegate_wp = delegate_sp;
76*fe013be4SDimitry Andric     m_data.SetByteOrder(delegate_sp->GetByteOrder());
77*fe013be4SDimitry Andric     m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
78*fe013be4SDimitry Andric   }
79*fe013be4SDimitry Andric }
80*fe013be4SDimitry Andric 
81*fe013be4SDimitry Andric ObjectFileJIT::~ObjectFileJIT() = default;
82*fe013be4SDimitry Andric 
ParseHeader()83*fe013be4SDimitry Andric bool ObjectFileJIT::ParseHeader() {
84*fe013be4SDimitry Andric   // JIT code is never in a file, nor is it required to have any header
85*fe013be4SDimitry Andric   return false;
86*fe013be4SDimitry Andric }
87*fe013be4SDimitry Andric 
GetByteOrder() const88*fe013be4SDimitry Andric ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
89*fe013be4SDimitry Andric 
IsExecutable() const90*fe013be4SDimitry Andric bool ObjectFileJIT::IsExecutable() const { return false; }
91*fe013be4SDimitry Andric 
GetAddressByteSize() const92*fe013be4SDimitry Andric uint32_t ObjectFileJIT::GetAddressByteSize() const {
93*fe013be4SDimitry Andric   return m_data.GetAddressByteSize();
94*fe013be4SDimitry Andric }
95*fe013be4SDimitry Andric 
ParseSymtab(Symtab & symtab)96*fe013be4SDimitry Andric void ObjectFileJIT::ParseSymtab(Symtab &symtab) {
97*fe013be4SDimitry Andric   ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
98*fe013be4SDimitry Andric   if (delegate_sp)
99*fe013be4SDimitry Andric     delegate_sp->PopulateSymtab(this, symtab);
100*fe013be4SDimitry Andric }
101*fe013be4SDimitry Andric 
IsStripped()102*fe013be4SDimitry Andric bool ObjectFileJIT::IsStripped() {
103*fe013be4SDimitry Andric   return false; // JIT code that is in a module is never stripped
104*fe013be4SDimitry Andric }
105*fe013be4SDimitry Andric 
CreateSections(SectionList & unified_section_list)106*fe013be4SDimitry Andric void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
107*fe013be4SDimitry Andric   if (!m_sections_up) {
108*fe013be4SDimitry Andric     m_sections_up = std::make_unique<SectionList>();
109*fe013be4SDimitry Andric     ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
110*fe013be4SDimitry Andric     if (delegate_sp) {
111*fe013be4SDimitry Andric       delegate_sp->PopulateSectionList(this, *m_sections_up);
112*fe013be4SDimitry Andric       unified_section_list = *m_sections_up;
113*fe013be4SDimitry Andric     }
114*fe013be4SDimitry Andric   }
115*fe013be4SDimitry Andric }
116*fe013be4SDimitry Andric 
Dump(Stream * s)117*fe013be4SDimitry Andric void ObjectFileJIT::Dump(Stream *s) {
118*fe013be4SDimitry Andric   ModuleSP module_sp(GetModule());
119*fe013be4SDimitry Andric   if (module_sp) {
120*fe013be4SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
121*fe013be4SDimitry Andric     s->Printf("%p: ", static_cast<void *>(this));
122*fe013be4SDimitry Andric     s->Indent();
123*fe013be4SDimitry Andric     s->PutCString("ObjectFileJIT");
124*fe013be4SDimitry Andric 
125*fe013be4SDimitry Andric     if (ArchSpec arch = GetArchitecture())
126*fe013be4SDimitry Andric       *s << ", arch = " << arch.GetArchitectureName();
127*fe013be4SDimitry Andric 
128*fe013be4SDimitry Andric     s->EOL();
129*fe013be4SDimitry Andric 
130*fe013be4SDimitry Andric     SectionList *sections = GetSectionList();
131*fe013be4SDimitry Andric     if (sections)
132*fe013be4SDimitry Andric       sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
133*fe013be4SDimitry Andric                      UINT32_MAX);
134*fe013be4SDimitry Andric 
135*fe013be4SDimitry Andric     if (m_symtab_up)
136*fe013be4SDimitry Andric       m_symtab_up->Dump(s, nullptr, eSortOrderNone);
137*fe013be4SDimitry Andric   }
138*fe013be4SDimitry Andric }
139*fe013be4SDimitry Andric 
GetUUID()140*fe013be4SDimitry Andric UUID ObjectFileJIT::GetUUID() {
141*fe013be4SDimitry Andric   // TODO: maybe get from delegate, not needed for first pass
142*fe013be4SDimitry Andric   return UUID();
143*fe013be4SDimitry Andric }
144*fe013be4SDimitry Andric 
GetDependentModules(FileSpecList & files)145*fe013be4SDimitry Andric uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
146*fe013be4SDimitry Andric   // JIT modules don't have dependencies, but they could
147*fe013be4SDimitry Andric   // if external functions are called and we know where they are
148*fe013be4SDimitry Andric   files.Clear();
149*fe013be4SDimitry Andric   return 0;
150*fe013be4SDimitry Andric }
151*fe013be4SDimitry Andric 
GetEntryPointAddress()152*fe013be4SDimitry Andric lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
153*fe013be4SDimitry Andric   return Address();
154*fe013be4SDimitry Andric }
155*fe013be4SDimitry Andric 
GetBaseAddress()156*fe013be4SDimitry Andric lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
157*fe013be4SDimitry Andric 
CalculateType()158*fe013be4SDimitry Andric ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
159*fe013be4SDimitry Andric 
CalculateStrata()160*fe013be4SDimitry Andric ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
161*fe013be4SDimitry Andric 
GetArchitecture()162*fe013be4SDimitry Andric ArchSpec ObjectFileJIT::GetArchitecture() {
163*fe013be4SDimitry Andric   if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
164*fe013be4SDimitry Andric     return delegate_sp->GetArchitecture();
165*fe013be4SDimitry Andric   return ArchSpec();
166*fe013be4SDimitry Andric }
167*fe013be4SDimitry Andric 
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset)168*fe013be4SDimitry Andric bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
169*fe013be4SDimitry Andric                                    bool value_is_offset) {
170*fe013be4SDimitry Andric   size_t num_loaded_sections = 0;
171*fe013be4SDimitry Andric   SectionList *section_list = GetSectionList();
172*fe013be4SDimitry Andric   if (section_list) {
173*fe013be4SDimitry Andric     const size_t num_sections = section_list->GetSize();
174*fe013be4SDimitry Andric     // "value" is an offset to apply to each top level segment
175*fe013be4SDimitry Andric     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
176*fe013be4SDimitry Andric       // Iterate through the object file sections to find all of the sections
177*fe013be4SDimitry Andric       // that size on disk (to avoid __PAGEZERO) and load them
178*fe013be4SDimitry Andric       SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
179*fe013be4SDimitry Andric       if (section_sp && section_sp->GetFileSize() > 0 &&
180*fe013be4SDimitry Andric           !section_sp->IsThreadSpecific()) {
181*fe013be4SDimitry Andric         if (target.GetSectionLoadList().SetSectionLoadAddress(
182*fe013be4SDimitry Andric                 section_sp, section_sp->GetFileAddress() + value))
183*fe013be4SDimitry Andric           ++num_loaded_sections;
184*fe013be4SDimitry Andric       }
185*fe013be4SDimitry Andric     }
186*fe013be4SDimitry Andric   }
187*fe013be4SDimitry Andric   return num_loaded_sections > 0;
188*fe013be4SDimitry Andric }
189*fe013be4SDimitry Andric 
ReadSectionData(lldb_private::Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)190*fe013be4SDimitry Andric size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
191*fe013be4SDimitry Andric                                       lldb::offset_t section_offset, void *dst,
192*fe013be4SDimitry Andric                                       size_t dst_len) {
193*fe013be4SDimitry Andric   lldb::offset_t file_size = section->GetFileSize();
194*fe013be4SDimitry Andric   if (section_offset < file_size) {
195*fe013be4SDimitry Andric     size_t src_len = file_size - section_offset;
196*fe013be4SDimitry Andric     if (src_len > dst_len)
197*fe013be4SDimitry Andric       src_len = dst_len;
198*fe013be4SDimitry Andric     const uint8_t *src =
199*fe013be4SDimitry Andric         ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
200*fe013be4SDimitry Andric 
201*fe013be4SDimitry Andric     memcpy(dst, src, src_len);
202*fe013be4SDimitry Andric     return src_len;
203*fe013be4SDimitry Andric   }
204*fe013be4SDimitry Andric   return 0;
205*fe013be4SDimitry Andric }
206*fe013be4SDimitry Andric 
207*fe013be4SDimitry Andric size_t
ReadSectionData(lldb_private::Section * section,lldb_private::DataExtractor & section_data)208*fe013be4SDimitry Andric ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
209*fe013be4SDimitry Andric                                lldb_private::DataExtractor &section_data) {
210*fe013be4SDimitry Andric   if (section->GetFileSize()) {
211*fe013be4SDimitry Andric     const void *src = (void *)(uintptr_t)section->GetFileOffset();
212*fe013be4SDimitry Andric 
213*fe013be4SDimitry Andric     DataBufferSP data_sp =
214*fe013be4SDimitry Andric         std::make_shared<DataBufferHeap>(src, section->GetFileSize());
215*fe013be4SDimitry Andric     section_data.SetData(data_sp, 0, data_sp->GetByteSize());
216*fe013be4SDimitry Andric     section_data.SetByteOrder(GetByteOrder());
217*fe013be4SDimitry Andric     section_data.SetAddressByteSize(GetAddressByteSize());
218*fe013be4SDimitry Andric     return section_data.GetByteSize();
219*fe013be4SDimitry Andric   }
220*fe013be4SDimitry Andric   section_data.Clear();
221*fe013be4SDimitry Andric   return 0;
222*fe013be4SDimitry Andric }
223