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 "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"
34 
35 #ifndef __APPLE__
36 #include "Utility/UuidCompatibility.h"
37 #endif
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
42 LLDB_PLUGIN_DEFINE(ObjectFileJIT)
43 
44 char ObjectFileJIT::ID;
45 
46 void ObjectFileJIT::Initialize() {
47   PluginManager::RegisterPlugin(GetPluginNameStatic(),
48                                 GetPluginDescriptionStatic(), CreateInstance,
49                                 CreateMemoryInstance, GetModuleSpecifications);
50 }
51 
52 void ObjectFileJIT::Terminate() {
53   PluginManager::UnregisterPlugin(CreateInstance);
54 }
55 
56 ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
57                                           DataBufferSP &data_sp,
58                                           lldb::offset_t data_offset,
59                                           const FileSpec *file,
60                                           lldb::offset_t file_offset,
61                                           lldb::offset_t length) {
62   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
63   // a file
64   return nullptr;
65 }
66 
67 ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
68                                                 DataBufferSP &data_sp,
69                                                 const ProcessSP &process_sp,
70                                                 lldb::addr_t header_addr) {
71   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
72   // memory
73   return nullptr;
74 }
75 
76 size_t ObjectFileJIT::GetModuleSpecifications(
77     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
78     lldb::offset_t data_offset, lldb::offset_t file_offset,
79     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
80   // JIT'ed object file can't be read from a file on disk
81   return 0;
82 }
83 
84 ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
85                              const ObjectFileJITDelegateSP &delegate_sp)
86     : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
87   if (delegate_sp) {
88     m_delegate_wp = delegate_sp;
89     m_data.SetByteOrder(delegate_sp->GetByteOrder());
90     m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
91   }
92 }
93 
94 ObjectFileJIT::~ObjectFileJIT() = default;
95 
96 bool ObjectFileJIT::ParseHeader() {
97   // JIT code is never in a file, nor is it required to have any header
98   return false;
99 }
100 
101 ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
102 
103 bool ObjectFileJIT::IsExecutable() const { return false; }
104 
105 uint32_t ObjectFileJIT::GetAddressByteSize() const {
106   return m_data.GetAddressByteSize();
107 }
108 
109 Symtab *ObjectFileJIT::GetSymtab() {
110   ModuleSP module_sp(GetModule());
111   if (module_sp) {
112     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
113     if (m_symtab_up == nullptr) {
114       m_symtab_up = std::make_unique<Symtab>(this);
115       std::lock_guard<std::recursive_mutex> symtab_guard(
116           m_symtab_up->GetMutex());
117       ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
118       if (delegate_sp)
119         delegate_sp->PopulateSymtab(this, *m_symtab_up);
120       // TODO: get symbols from delegate
121       m_symtab_up->Finalize();
122     }
123   }
124   return m_symtab_up.get();
125 }
126 
127 bool ObjectFileJIT::IsStripped() {
128   return false; // JIT code that is in a module is never stripped
129 }
130 
131 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
132   if (!m_sections_up) {
133     m_sections_up = std::make_unique<SectionList>();
134     ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
135     if (delegate_sp) {
136       delegate_sp->PopulateSectionList(this, *m_sections_up);
137       unified_section_list = *m_sections_up;
138     }
139   }
140 }
141 
142 void ObjectFileJIT::Dump(Stream *s) {
143   ModuleSP module_sp(GetModule());
144   if (module_sp) {
145     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
146     s->Printf("%p: ", static_cast<void *>(this));
147     s->Indent();
148     s->PutCString("ObjectFileJIT");
149 
150     if (ArchSpec arch = GetArchitecture())
151       *s << ", arch = " << arch.GetArchitectureName();
152 
153     s->EOL();
154 
155     SectionList *sections = GetSectionList();
156     if (sections)
157       sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
158                      UINT32_MAX);
159 
160     if (m_symtab_up)
161       m_symtab_up->Dump(s, nullptr, eSortOrderNone);
162   }
163 }
164 
165 UUID ObjectFileJIT::GetUUID() {
166   // TODO: maybe get from delegate, not needed for first pass
167   return UUID();
168 }
169 
170 uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
171   // JIT modules don't have dependencies, but they could
172   // if external functions are called and we know where they are
173   files.Clear();
174   return 0;
175 }
176 
177 lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
178   return Address();
179 }
180 
181 lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
182 
183 ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
184 
185 ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
186 
187 ArchSpec ObjectFileJIT::GetArchitecture() {
188   if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
189     return delegate_sp->GetArchitecture();
190   return ArchSpec();
191 }
192 
193 bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
194                                    bool value_is_offset) {
195   size_t num_loaded_sections = 0;
196   SectionList *section_list = GetSectionList();
197   if (section_list) {
198     const size_t num_sections = section_list->GetSize();
199     // "value" is an offset to apply to each top level segment
200     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
201       // Iterate through the object file sections to find all of the sections
202       // that size on disk (to avoid __PAGEZERO) and load them
203       SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
204       if (section_sp && section_sp->GetFileSize() > 0 &&
205           !section_sp->IsThreadSpecific()) {
206         if (target.GetSectionLoadList().SetSectionLoadAddress(
207                 section_sp, section_sp->GetFileAddress() + value))
208           ++num_loaded_sections;
209       }
210     }
211   }
212   return num_loaded_sections > 0;
213 }
214 
215 size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
216                                       lldb::offset_t section_offset, void *dst,
217                                       size_t dst_len) {
218   lldb::offset_t file_size = section->GetFileSize();
219   if (section_offset < file_size) {
220     size_t src_len = file_size - section_offset;
221     if (src_len > dst_len)
222       src_len = dst_len;
223     const uint8_t *src =
224         ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
225 
226     memcpy(dst, src, src_len);
227     return src_len;
228   }
229   return 0;
230 }
231 
232 size_t ObjectFileJIT::ReadSectionData(
233     lldb_private::Section *section,
234     lldb_private::DataExtractor &section_data) {
235   if (section->GetFileSize()) {
236     const void *src = (void *)(uintptr_t)section->GetFileOffset();
237 
238     DataBufferSP data_sp =
239         std::make_shared<DataBufferHeap>(src, section->GetFileSize());
240     section_data.SetData(data_sp, 0, data_sp->GetByteSize());
241     section_data.SetByteOrder(GetByteOrder());
242     section_data.SetAddressByteSize(GetAddressByteSize());
243     return section_data.GetByteSize();
244   }
245   section_data.Clear();
246   return 0;
247 }
248