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