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