123f8c95aSGreg Clayton //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
223f8c95aSGreg Clayton //
323f8c95aSGreg Clayton //                     The LLVM Compiler Infrastructure
423f8c95aSGreg Clayton //
523f8c95aSGreg Clayton // This file is distributed under the University of Illinois Open Source
623f8c95aSGreg Clayton // License. See LICENSE.TXT for details.
723f8c95aSGreg Clayton //
823f8c95aSGreg Clayton //===----------------------------------------------------------------------===//
923f8c95aSGreg Clayton 
1023f8c95aSGreg Clayton #include "llvm/ADT/StringRef.h"
1123f8c95aSGreg Clayton 
1223f8c95aSGreg Clayton #include "ObjectFileJIT.h"
1323f8c95aSGreg Clayton #include "lldb/Core/Debugger.h"
1423f8c95aSGreg Clayton #include "lldb/Core/FileSpecList.h"
1523f8c95aSGreg Clayton #include "lldb/Core/Module.h"
1623f8c95aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
1723f8c95aSGreg Clayton #include "lldb/Core/PluginManager.h"
1823f8c95aSGreg Clayton #include "lldb/Core/RangeMap.h"
1923f8c95aSGreg Clayton #include "lldb/Core/Section.h"
2023f8c95aSGreg Clayton #include "lldb/Core/StreamFile.h"
21b9c1b51eSKate Stone #include "lldb/Host/Host.h"
2223f8c95aSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
2323f8c95aSGreg Clayton #include "lldb/Target/Platform.h"
2423f8c95aSGreg Clayton #include "lldb/Target/Process.h"
2523f8c95aSGreg Clayton #include "lldb/Target/SectionLoadList.h"
2623f8c95aSGreg Clayton #include "lldb/Target/Target.h"
275f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
28666cc0b2SZachary Turner #include "lldb/Utility/DataBuffer.h"
29666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
305713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
316f9e6901SZachary Turner #include "lldb/Utility/Log.h"
32bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
3338d0632eSPavel Labath #include "lldb/Utility/Timer.h"
34666cc0b2SZachary Turner #include "lldb/Utility/UUID.h"
3523f8c95aSGreg Clayton 
3623f8c95aSGreg Clayton #ifndef __APPLE__
3723f8c95aSGreg Clayton #include "Utility/UuidCompatibility.h"
3823f8c95aSGreg Clayton #endif
3923f8c95aSGreg Clayton 
4023f8c95aSGreg Clayton using namespace lldb;
4123f8c95aSGreg Clayton using namespace lldb_private;
4223f8c95aSGreg Clayton 
43b9c1b51eSKate Stone void ObjectFileJIT::Initialize() {
4423f8c95aSGreg Clayton   PluginManager::RegisterPlugin(GetPluginNameStatic(),
45b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance,
46b9c1b51eSKate Stone                                 CreateMemoryInstance, GetModuleSpecifications);
4723f8c95aSGreg Clayton }
4823f8c95aSGreg Clayton 
49b9c1b51eSKate Stone void ObjectFileJIT::Terminate() {
5023f8c95aSGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
5123f8c95aSGreg Clayton }
5223f8c95aSGreg Clayton 
53b9c1b51eSKate Stone lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() {
5423f8c95aSGreg Clayton   static ConstString g_name("jit");
5523f8c95aSGreg Clayton   return g_name;
5623f8c95aSGreg Clayton }
5723f8c95aSGreg Clayton 
58b9c1b51eSKate Stone const char *ObjectFileJIT::GetPluginDescriptionStatic() {
5923f8c95aSGreg Clayton   return "JIT code object file";
6023f8c95aSGreg Clayton }
6123f8c95aSGreg Clayton 
62b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
6323f8c95aSGreg Clayton                                           DataBufferSP &data_sp,
6423f8c95aSGreg Clayton                                           lldb::offset_t data_offset,
6523f8c95aSGreg Clayton                                           const FileSpec *file,
6623f8c95aSGreg Clayton                                           lldb::offset_t file_offset,
67b9c1b51eSKate Stone                                           lldb::offset_t length) {
6805097246SAdrian Prantl   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
6905097246SAdrian Prantl   // a file
7023f8c95aSGreg Clayton   return NULL;
7123f8c95aSGreg Clayton }
7223f8c95aSGreg Clayton 
73b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
7423f8c95aSGreg Clayton                                                 DataBufferSP &data_sp,
7523f8c95aSGreg Clayton                                                 const ProcessSP &process_sp,
76b9c1b51eSKate Stone                                                 lldb::addr_t header_addr) {
7705097246SAdrian Prantl   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
7805097246SAdrian Prantl   // memory
7923f8c95aSGreg Clayton   return NULL;
8023f8c95aSGreg Clayton }
8123f8c95aSGreg Clayton 
82b9c1b51eSKate Stone size_t ObjectFileJIT::GetModuleSpecifications(
83b9c1b51eSKate Stone     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
84b9c1b51eSKate Stone     lldb::offset_t data_offset, lldb::offset_t file_offset,
85b9c1b51eSKate Stone     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
8623f8c95aSGreg Clayton   // JIT'ed object file can't be read from a file on disk
8723f8c95aSGreg Clayton   return 0;
8823f8c95aSGreg Clayton }
8923f8c95aSGreg Clayton 
9023f8c95aSGreg Clayton ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
91b9c1b51eSKate Stone                              const ObjectFileJITDelegateSP &delegate_sp)
92b9c1b51eSKate Stone     : ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
93b9c1b51eSKate Stone   if (delegate_sp) {
9423f8c95aSGreg Clayton     m_delegate_wp = delegate_sp;
9523f8c95aSGreg Clayton     m_data.SetByteOrder(delegate_sp->GetByteOrder());
9623f8c95aSGreg Clayton     m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
9723f8c95aSGreg Clayton   }
9823f8c95aSGreg Clayton }
9923f8c95aSGreg Clayton 
100b9c1b51eSKate Stone ObjectFileJIT::~ObjectFileJIT() {}
10123f8c95aSGreg Clayton 
102b9c1b51eSKate Stone bool ObjectFileJIT::ParseHeader() {
10323f8c95aSGreg Clayton   // JIT code is never in a file, nor is it required to have any header
10423f8c95aSGreg Clayton   return false;
10523f8c95aSGreg Clayton }
10623f8c95aSGreg Clayton 
107b9c1b51eSKate Stone ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
10823f8c95aSGreg Clayton 
109b9c1b51eSKate Stone bool ObjectFileJIT::IsExecutable() const { return false; }
11023f8c95aSGreg Clayton 
111b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetAddressByteSize() const {
11223f8c95aSGreg Clayton   return m_data.GetAddressByteSize();
11323f8c95aSGreg Clayton }
11423f8c95aSGreg Clayton 
115b9c1b51eSKate Stone Symtab *ObjectFileJIT::GetSymtab() {
11623f8c95aSGreg Clayton   ModuleSP module_sp(GetModule());
117b9c1b51eSKate Stone   if (module_sp) {
11816ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
119b9c1b51eSKate Stone     if (m_symtab_ap.get() == NULL) {
12023f8c95aSGreg Clayton       m_symtab_ap.reset(new Symtab(this));
121b9c1b51eSKate Stone       std::lock_guard<std::recursive_mutex> symtab_guard(
122b9c1b51eSKate Stone           m_symtab_ap->GetMutex());
12323f8c95aSGreg Clayton       ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
12423f8c95aSGreg Clayton       if (delegate_sp)
12523f8c95aSGreg Clayton         delegate_sp->PopulateSymtab(this, *m_symtab_ap);
12623f8c95aSGreg Clayton       // TODO: get symbols from delegate
127407c6910SDavide Italiano       m_symtab_ap->Finalize();
12823f8c95aSGreg Clayton     }
12923f8c95aSGreg Clayton   }
13023f8c95aSGreg Clayton   return m_symtab_ap.get();
13123f8c95aSGreg Clayton }
13223f8c95aSGreg Clayton 
133b9c1b51eSKate Stone bool ObjectFileJIT::IsStripped() {
13423f8c95aSGreg Clayton   return false; // JIT code that is in a module is never stripped
13523f8c95aSGreg Clayton }
13623f8c95aSGreg Clayton 
137b9c1b51eSKate Stone void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
138b9c1b51eSKate Stone   if (!m_sections_ap.get()) {
13923f8c95aSGreg Clayton     m_sections_ap.reset(new SectionList());
14023f8c95aSGreg Clayton     ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
141b9c1b51eSKate Stone     if (delegate_sp) {
14223f8c95aSGreg Clayton       delegate_sp->PopulateSectionList(this, *m_sections_ap);
14323f8c95aSGreg Clayton       unified_section_list = *m_sections_ap;
14423f8c95aSGreg Clayton     }
14523f8c95aSGreg Clayton   }
14623f8c95aSGreg Clayton }
14723f8c95aSGreg Clayton 
148b9c1b51eSKate Stone void ObjectFileJIT::Dump(Stream *s) {
14923f8c95aSGreg Clayton   ModuleSP module_sp(GetModule());
150b9c1b51eSKate Stone   if (module_sp) {
15116ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
152324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(this));
15323f8c95aSGreg Clayton     s->Indent();
15423f8c95aSGreg Clayton     s->PutCString("ObjectFileJIT");
15523f8c95aSGreg Clayton 
156*f760f5aeSPavel Labath     if (ArchSpec arch = GetArchitecture())
15723f8c95aSGreg Clayton       *s << ", arch = " << arch.GetArchitectureName();
15823f8c95aSGreg Clayton 
15923f8c95aSGreg Clayton     s->EOL();
16023f8c95aSGreg Clayton 
16123f8c95aSGreg Clayton     SectionList *sections = GetSectionList();
16223f8c95aSGreg Clayton     if (sections)
16323f8c95aSGreg Clayton       sections->Dump(s, NULL, true, UINT32_MAX);
16423f8c95aSGreg Clayton 
16523f8c95aSGreg Clayton     if (m_symtab_ap.get())
16623f8c95aSGreg Clayton       m_symtab_ap->Dump(s, NULL, eSortOrderNone);
16723f8c95aSGreg Clayton   }
16823f8c95aSGreg Clayton }
16923f8c95aSGreg Clayton 
170b9c1b51eSKate Stone bool ObjectFileJIT::GetUUID(lldb_private::UUID *uuid) {
17123f8c95aSGreg Clayton   // TODO: maybe get from delegate, not needed for first pass
17223f8c95aSGreg Clayton   return false;
17323f8c95aSGreg Clayton }
17423f8c95aSGreg Clayton 
175b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
17623f8c95aSGreg Clayton   // JIT modules don't have dependencies, but they could
17723f8c95aSGreg Clayton   // if external functions are called and we know where they are
17823f8c95aSGreg Clayton   files.Clear();
17923f8c95aSGreg Clayton   return 0;
18023f8c95aSGreg Clayton }
18123f8c95aSGreg Clayton 
182b9c1b51eSKate Stone lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
18323f8c95aSGreg Clayton   return Address();
18423f8c95aSGreg Clayton }
18523f8c95aSGreg Clayton 
186d1e3fe21SPavel Labath lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
18723f8c95aSGreg Clayton 
188b9c1b51eSKate Stone ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
18923f8c95aSGreg Clayton 
190b9c1b51eSKate Stone ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
19123f8c95aSGreg Clayton 
192*f760f5aeSPavel Labath ArchSpec ObjectFileJIT::GetArchitecture() {
193*f760f5aeSPavel Labath   if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
194*f760f5aeSPavel Labath     return delegate_sp->GetArchitecture();
195*f760f5aeSPavel Labath   return ArchSpec();
19623f8c95aSGreg Clayton }
19723f8c95aSGreg Clayton 
19823f8c95aSGreg Clayton //------------------------------------------------------------------
19923f8c95aSGreg Clayton // PluginInterface protocol
20023f8c95aSGreg Clayton //------------------------------------------------------------------
201b9c1b51eSKate Stone lldb_private::ConstString ObjectFileJIT::GetPluginName() {
20223f8c95aSGreg Clayton   return GetPluginNameStatic();
20323f8c95aSGreg Clayton }
20423f8c95aSGreg Clayton 
205b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetPluginVersion() { return 1; }
20623f8c95aSGreg Clayton 
207b9c1b51eSKate Stone bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
208b9c1b51eSKate Stone                                    bool value_is_offset) {
20923f8c95aSGreg Clayton   size_t num_loaded_sections = 0;
21023f8c95aSGreg Clayton   SectionList *section_list = GetSectionList();
211b9c1b51eSKate Stone   if (section_list) {
21223f8c95aSGreg Clayton     const size_t num_sections = section_list->GetSize();
21323f8c95aSGreg Clayton     // "value" is an offset to apply to each top level segment
214b9c1b51eSKate Stone     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
21505097246SAdrian Prantl       // Iterate through the object file sections to find all of the sections
21605097246SAdrian Prantl       // that size on disk (to avoid __PAGEZERO) and load them
21723f8c95aSGreg Clayton       SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
218b9c1b51eSKate Stone       if (section_sp && section_sp->GetFileSize() > 0 &&
219a6682a41SJonas Devlieghere           !section_sp->IsThreadSpecific()) {
220b9c1b51eSKate Stone         if (target.GetSectionLoadList().SetSectionLoadAddress(
221b9c1b51eSKate Stone                 section_sp, section_sp->GetFileAddress() + value))
22223f8c95aSGreg Clayton           ++num_loaded_sections;
22323f8c95aSGreg Clayton       }
22423f8c95aSGreg Clayton     }
22523f8c95aSGreg Clayton   }
22623f8c95aSGreg Clayton   return num_loaded_sections > 0;
22723f8c95aSGreg Clayton }
22823f8c95aSGreg Clayton 
229d13f691fSEd Maste size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
230b9c1b51eSKate Stone                                       lldb::offset_t section_offset, void *dst,
231d13f691fSEd Maste                                       size_t dst_len) {
23223f8c95aSGreg Clayton   lldb::offset_t file_size = section->GetFileSize();
233b9c1b51eSKate Stone   if (section_offset < file_size) {
234a746e8e5SZachary Turner     size_t src_len = file_size - section_offset;
23523f8c95aSGreg Clayton     if (src_len > dst_len)
23623f8c95aSGreg Clayton       src_len = dst_len;
237b9c1b51eSKate Stone     const uint8_t *src =
238b9c1b51eSKate Stone         ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
23923f8c95aSGreg Clayton 
24023f8c95aSGreg Clayton     memcpy(dst, src, src_len);
24123f8c95aSGreg Clayton     return src_len;
24223f8c95aSGreg Clayton   }
24323f8c95aSGreg Clayton   return 0;
24423f8c95aSGreg Clayton }
245a746e8e5SZachary Turner 
246b9c1b51eSKate Stone size_t ObjectFileJIT::ReadSectionData(
247d13f691fSEd Maste     lldb_private::Section *section,
248d13f691fSEd Maste     lldb_private::DataExtractor &section_data) {
249b9c1b51eSKate Stone   if (section->GetFileSize()) {
25023f8c95aSGreg Clayton     const void *src = (void *)(uintptr_t)section->GetFileOffset();
25123f8c95aSGreg Clayton 
252b9c1b51eSKate Stone     DataBufferSP data_sp(
253b9c1b51eSKate Stone         new lldb_private::DataBufferHeap(src, section->GetFileSize()));
254b9c1b51eSKate Stone     if (data_sp) {
25523f8c95aSGreg Clayton       section_data.SetData(data_sp, 0, data_sp->GetByteSize());
25623f8c95aSGreg Clayton       section_data.SetByteOrder(GetByteOrder());
25723f8c95aSGreg Clayton       section_data.SetAddressByteSize(GetAddressByteSize());
25823f8c95aSGreg Clayton       return section_data.GetByteSize();
25923f8c95aSGreg Clayton     }
26023f8c95aSGreg Clayton   }
26123f8c95aSGreg Clayton   section_data.Clear();
26223f8c95aSGreg Clayton   return 0;
26323f8c95aSGreg Clayton }
264