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