]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[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 #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       m_symtab_ap->Finalize();
128     }
129   }
130   return m_symtab_ap.get();
131 }
132
133 bool ObjectFileJIT::IsStripped() {
134   return false; // JIT code that is in a module is never stripped
135 }
136
137 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
138   if (!m_sections_ap.get()) {
139     m_sections_ap.reset(new SectionList());
140     ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
141     if (delegate_sp) {
142       delegate_sp->PopulateSectionList(this, *m_sections_ap);
143       unified_section_list = *m_sections_ap;
144     }
145   }
146 }
147
148 void ObjectFileJIT::Dump(Stream *s) {
149   ModuleSP module_sp(GetModule());
150   if (module_sp) {
151     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
152     s->Printf("%p: ", static_cast<void *>(this));
153     s->Indent();
154     s->PutCString("ObjectFileJIT");
155
156     if (ArchSpec arch = GetArchitecture())
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::GetBaseAddress() { return Address(); }
187
188 ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
189
190 ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
191
192 ArchSpec ObjectFileJIT::GetArchitecture() {
193   if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
194     return delegate_sp->GetArchitecture();
195   return ArchSpec();
196 }
197
198 //------------------------------------------------------------------
199 // PluginInterface protocol
200 //------------------------------------------------------------------
201 lldb_private::ConstString ObjectFileJIT::GetPluginName() {
202   return GetPluginNameStatic();
203 }
204
205 uint32_t ObjectFileJIT::GetPluginVersion() { return 1; }
206
207 bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
208                                    bool value_is_offset) {
209   size_t num_loaded_sections = 0;
210   SectionList *section_list = GetSectionList();
211   if (section_list) {
212     const size_t num_sections = section_list->GetSize();
213     // "value" is an offset to apply to each top level segment
214     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
215       // Iterate through the object file sections to find all of the sections
216       // that size on disk (to avoid __PAGEZERO) and load them
217       SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
218       if (section_sp && section_sp->GetFileSize() > 0 &&
219           !section_sp->IsThreadSpecific()) {
220         if (target.GetSectionLoadList().SetSectionLoadAddress(
221                 section_sp, section_sp->GetFileAddress() + value))
222           ++num_loaded_sections;
223       }
224     }
225   }
226   return num_loaded_sections > 0;
227 }
228
229 size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
230                                       lldb::offset_t section_offset, void *dst,
231                                       size_t dst_len) {
232   lldb::offset_t file_size = section->GetFileSize();
233   if (section_offset < file_size) {
234     size_t src_len = file_size - section_offset;
235     if (src_len > dst_len)
236       src_len = dst_len;
237     const uint8_t *src =
238         ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
239
240     memcpy(dst, src, src_len);
241     return src_len;
242   }
243   return 0;
244 }
245
246 size_t ObjectFileJIT::ReadSectionData(
247     lldb_private::Section *section,
248     lldb_private::DataExtractor &section_data) {
249   if (section->GetFileSize()) {
250     const void *src = (void *)(uintptr_t)section->GetFileOffset();
251
252     DataBufferSP data_sp(
253         new lldb_private::DataBufferHeap(src, section->GetFileSize()));
254     if (data_sp) {
255       section_data.SetData(data_sp, 0, data_sp->GetByteSize());
256       section_data.SetByteOrder(GetByteOrder());
257       section_data.SetAddressByteSize(GetAddressByteSize());
258       return section_data.GetByteSize();
259     }
260   }
261   section_data.Clear();
262   return 0;
263 }