123f8c95aSGreg Clayton //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
223f8c95aSGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
623f8c95aSGreg Clayton //
723f8c95aSGreg Clayton //===----------------------------------------------------------------------===//
823f8c95aSGreg Clayton 
923f8c95aSGreg Clayton #include "llvm/ADT/StringRef.h"
1023f8c95aSGreg Clayton 
1123f8c95aSGreg Clayton #include "ObjectFileJIT.h"
1223f8c95aSGreg Clayton #include "lldb/Core/Debugger.h"
1323f8c95aSGreg Clayton #include "lldb/Core/FileSpecList.h"
1423f8c95aSGreg Clayton #include "lldb/Core/Module.h"
1523f8c95aSGreg Clayton #include "lldb/Core/ModuleSpec.h"
1623f8c95aSGreg Clayton #include "lldb/Core/PluginManager.h"
1723f8c95aSGreg Clayton #include "lldb/Core/Section.h"
1823f8c95aSGreg Clayton #include "lldb/Core/StreamFile.h"
19b9c1b51eSKate Stone #include "lldb/Host/Host.h"
2023f8c95aSGreg Clayton #include "lldb/Symbol/ObjectFile.h"
2123f8c95aSGreg Clayton #include "lldb/Target/Platform.h"
2223f8c95aSGreg Clayton #include "lldb/Target/Process.h"
2323f8c95aSGreg Clayton #include "lldb/Target/SectionLoadList.h"
2423f8c95aSGreg Clayton #include "lldb/Target/Target.h"
255f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
26666cc0b2SZachary Turner #include "lldb/Utility/DataBuffer.h"
27666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
285713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
296f9e6901SZachary Turner #include "lldb/Utility/Log.h"
30b8093314SPavel Labath #include "lldb/Utility/RangeMap.h"
31bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
3238d0632eSPavel Labath #include "lldb/Utility/Timer.h"
33666cc0b2SZachary Turner #include "lldb/Utility/UUID.h"
3423f8c95aSGreg Clayton 
3523f8c95aSGreg Clayton #ifndef __APPLE__
3623f8c95aSGreg Clayton #include "Utility/UuidCompatibility.h"
3723f8c95aSGreg Clayton #endif
3823f8c95aSGreg Clayton 
3923f8c95aSGreg Clayton using namespace lldb;
4023f8c95aSGreg Clayton using namespace lldb_private;
4123f8c95aSGreg Clayton 
42b9c1b51eSKate Stone void ObjectFileJIT::Initialize() {
4323f8c95aSGreg Clayton   PluginManager::RegisterPlugin(GetPluginNameStatic(),
44b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance,
45b9c1b51eSKate Stone                                 CreateMemoryInstance, GetModuleSpecifications);
4623f8c95aSGreg Clayton }
4723f8c95aSGreg Clayton 
48b9c1b51eSKate Stone void ObjectFileJIT::Terminate() {
4923f8c95aSGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
5023f8c95aSGreg Clayton }
5123f8c95aSGreg Clayton 
52b9c1b51eSKate Stone lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() {
5323f8c95aSGreg Clayton   static ConstString g_name("jit");
5423f8c95aSGreg Clayton   return g_name;
5523f8c95aSGreg Clayton }
5623f8c95aSGreg Clayton 
57b9c1b51eSKate Stone const char *ObjectFileJIT::GetPluginDescriptionStatic() {
5823f8c95aSGreg Clayton   return "JIT code object file";
5923f8c95aSGreg Clayton }
6023f8c95aSGreg Clayton 
61b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
6223f8c95aSGreg Clayton                                           DataBufferSP &data_sp,
6323f8c95aSGreg Clayton                                           lldb::offset_t data_offset,
6423f8c95aSGreg Clayton                                           const FileSpec *file,
6523f8c95aSGreg Clayton                                           lldb::offset_t file_offset,
66b9c1b51eSKate Stone                                           lldb::offset_t length) {
6705097246SAdrian Prantl   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
6805097246SAdrian Prantl   // a file
69248a1305SKonrad Kleine   return nullptr;
7023f8c95aSGreg Clayton }
7123f8c95aSGreg Clayton 
72b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
7323f8c95aSGreg Clayton                                                 DataBufferSP &data_sp,
7423f8c95aSGreg Clayton                                                 const ProcessSP &process_sp,
75b9c1b51eSKate Stone                                                 lldb::addr_t header_addr) {
7605097246SAdrian Prantl   // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
7705097246SAdrian Prantl   // memory
78248a1305SKonrad Kleine   return nullptr;
7923f8c95aSGreg Clayton }
8023f8c95aSGreg Clayton 
81b9c1b51eSKate Stone size_t ObjectFileJIT::GetModuleSpecifications(
82b9c1b51eSKate Stone     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
83b9c1b51eSKate Stone     lldb::offset_t data_offset, lldb::offset_t file_offset,
84b9c1b51eSKate Stone     lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
8523f8c95aSGreg Clayton   // JIT'ed object file can't be read from a file on disk
8623f8c95aSGreg Clayton   return 0;
8723f8c95aSGreg Clayton }
8823f8c95aSGreg Clayton 
8923f8c95aSGreg Clayton ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
90b9c1b51eSKate Stone                              const ObjectFileJITDelegateSP &delegate_sp)
91248a1305SKonrad Kleine     : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
92b9c1b51eSKate Stone   if (delegate_sp) {
9323f8c95aSGreg Clayton     m_delegate_wp = delegate_sp;
9423f8c95aSGreg Clayton     m_data.SetByteOrder(delegate_sp->GetByteOrder());
9523f8c95aSGreg Clayton     m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
9623f8c95aSGreg Clayton   }
9723f8c95aSGreg Clayton }
9823f8c95aSGreg Clayton 
99b9c1b51eSKate Stone ObjectFileJIT::~ObjectFileJIT() {}
10023f8c95aSGreg Clayton 
101b9c1b51eSKate Stone bool ObjectFileJIT::ParseHeader() {
10223f8c95aSGreg Clayton   // JIT code is never in a file, nor is it required to have any header
10323f8c95aSGreg Clayton   return false;
10423f8c95aSGreg Clayton }
10523f8c95aSGreg Clayton 
106b9c1b51eSKate Stone ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
10723f8c95aSGreg Clayton 
108b9c1b51eSKate Stone bool ObjectFileJIT::IsExecutable() const { return false; }
10923f8c95aSGreg Clayton 
110b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetAddressByteSize() const {
11123f8c95aSGreg Clayton   return m_data.GetAddressByteSize();
11223f8c95aSGreg Clayton }
11323f8c95aSGreg Clayton 
114b9c1b51eSKate Stone Symtab *ObjectFileJIT::GetSymtab() {
11523f8c95aSGreg Clayton   ModuleSP module_sp(GetModule());
116b9c1b51eSKate Stone   if (module_sp) {
11716ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
118248a1305SKonrad Kleine     if (m_symtab_up == nullptr) {
119d5b44036SJonas Devlieghere       m_symtab_up.reset(new Symtab(this));
120b9c1b51eSKate Stone       std::lock_guard<std::recursive_mutex> symtab_guard(
121d5b44036SJonas Devlieghere           m_symtab_up->GetMutex());
12223f8c95aSGreg Clayton       ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
12323f8c95aSGreg Clayton       if (delegate_sp)
124d5b44036SJonas Devlieghere         delegate_sp->PopulateSymtab(this, *m_symtab_up);
12523f8c95aSGreg Clayton       // TODO: get symbols from delegate
126d5b44036SJonas Devlieghere       m_symtab_up->Finalize();
12723f8c95aSGreg Clayton     }
12823f8c95aSGreg Clayton   }
129d5b44036SJonas Devlieghere   return m_symtab_up.get();
13023f8c95aSGreg Clayton }
13123f8c95aSGreg Clayton 
132b9c1b51eSKate Stone bool ObjectFileJIT::IsStripped() {
13323f8c95aSGreg Clayton   return false; // JIT code that is in a module is never stripped
13423f8c95aSGreg Clayton }
13523f8c95aSGreg Clayton 
136b9c1b51eSKate Stone void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
137d5b44036SJonas Devlieghere   if (!m_sections_up) {
138d5b44036SJonas Devlieghere     m_sections_up.reset(new SectionList());
13923f8c95aSGreg Clayton     ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
140b9c1b51eSKate Stone     if (delegate_sp) {
141d5b44036SJonas Devlieghere       delegate_sp->PopulateSectionList(this, *m_sections_up);
142d5b44036SJonas Devlieghere       unified_section_list = *m_sections_up;
14323f8c95aSGreg Clayton     }
14423f8c95aSGreg Clayton   }
14523f8c95aSGreg Clayton }
14623f8c95aSGreg Clayton 
147b9c1b51eSKate Stone void ObjectFileJIT::Dump(Stream *s) {
14823f8c95aSGreg Clayton   ModuleSP module_sp(GetModule());
149b9c1b51eSKate Stone   if (module_sp) {
15016ff8604SSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
151324a1036SSaleem Abdulrasool     s->Printf("%p: ", static_cast<void *>(this));
15223f8c95aSGreg Clayton     s->Indent();
15323f8c95aSGreg Clayton     s->PutCString("ObjectFileJIT");
15423f8c95aSGreg Clayton 
155f760f5aeSPavel Labath     if (ArchSpec arch = GetArchitecture())
15623f8c95aSGreg Clayton       *s << ", arch = " << arch.GetArchitectureName();
15723f8c95aSGreg Clayton 
15823f8c95aSGreg Clayton     s->EOL();
15923f8c95aSGreg Clayton 
16023f8c95aSGreg Clayton     SectionList *sections = GetSectionList();
16123f8c95aSGreg Clayton     if (sections)
162248a1305SKonrad Kleine       sections->Dump(s, nullptr, true, UINT32_MAX);
16323f8c95aSGreg Clayton 
164d5b44036SJonas Devlieghere     if (m_symtab_up)
165248a1305SKonrad Kleine       m_symtab_up->Dump(s, nullptr, eSortOrderNone);
16623f8c95aSGreg Clayton   }
16723f8c95aSGreg Clayton }
16823f8c95aSGreg Clayton 
169bd334efdSPavel Labath UUID ObjectFileJIT::GetUUID() {
17023f8c95aSGreg Clayton   // TODO: maybe get from delegate, not needed for first pass
171bd334efdSPavel Labath   return UUID();
17223f8c95aSGreg Clayton }
17323f8c95aSGreg Clayton 
174b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
17523f8c95aSGreg Clayton   // JIT modules don't have dependencies, but they could
17623f8c95aSGreg Clayton   // if external functions are called and we know where they are
17723f8c95aSGreg Clayton   files.Clear();
17823f8c95aSGreg Clayton   return 0;
17923f8c95aSGreg Clayton }
18023f8c95aSGreg Clayton 
181b9c1b51eSKate Stone lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
18223f8c95aSGreg Clayton   return Address();
18323f8c95aSGreg Clayton }
18423f8c95aSGreg Clayton 
185d1e3fe21SPavel Labath lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
18623f8c95aSGreg Clayton 
187b9c1b51eSKate Stone ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
18823f8c95aSGreg Clayton 
189b9c1b51eSKate Stone ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
19023f8c95aSGreg Clayton 
191f760f5aeSPavel Labath ArchSpec ObjectFileJIT::GetArchitecture() {
192f760f5aeSPavel Labath   if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
193f760f5aeSPavel Labath     return delegate_sp->GetArchitecture();
194f760f5aeSPavel Labath   return ArchSpec();
19523f8c95aSGreg Clayton }
19623f8c95aSGreg Clayton 
19723f8c95aSGreg Clayton // PluginInterface protocol
198b9c1b51eSKate Stone lldb_private::ConstString ObjectFileJIT::GetPluginName() {
19923f8c95aSGreg Clayton   return GetPluginNameStatic();
20023f8c95aSGreg Clayton }
20123f8c95aSGreg Clayton 
202b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetPluginVersion() { return 1; }
20323f8c95aSGreg Clayton 
204b9c1b51eSKate Stone bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
205b9c1b51eSKate Stone                                    bool value_is_offset) {
20623f8c95aSGreg Clayton   size_t num_loaded_sections = 0;
20723f8c95aSGreg Clayton   SectionList *section_list = GetSectionList();
208b9c1b51eSKate Stone   if (section_list) {
20923f8c95aSGreg Clayton     const size_t num_sections = section_list->GetSize();
21023f8c95aSGreg Clayton     // "value" is an offset to apply to each top level segment
211b9c1b51eSKate Stone     for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
21205097246SAdrian Prantl       // Iterate through the object file sections to find all of the sections
21305097246SAdrian Prantl       // that size on disk (to avoid __PAGEZERO) and load them
21423f8c95aSGreg Clayton       SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
215b9c1b51eSKate Stone       if (section_sp && section_sp->GetFileSize() > 0 &&
216a6682a41SJonas Devlieghere           !section_sp->IsThreadSpecific()) {
217b9c1b51eSKate Stone         if (target.GetSectionLoadList().SetSectionLoadAddress(
218b9c1b51eSKate Stone                 section_sp, section_sp->GetFileAddress() + value))
21923f8c95aSGreg Clayton           ++num_loaded_sections;
22023f8c95aSGreg Clayton       }
22123f8c95aSGreg Clayton     }
22223f8c95aSGreg Clayton   }
22323f8c95aSGreg Clayton   return num_loaded_sections > 0;
22423f8c95aSGreg Clayton }
22523f8c95aSGreg Clayton 
226d13f691fSEd Maste size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
227b9c1b51eSKate Stone                                       lldb::offset_t section_offset, void *dst,
228d13f691fSEd Maste                                       size_t dst_len) {
22923f8c95aSGreg Clayton   lldb::offset_t file_size = section->GetFileSize();
230b9c1b51eSKate Stone   if (section_offset < file_size) {
231a746e8e5SZachary Turner     size_t src_len = file_size - section_offset;
23223f8c95aSGreg Clayton     if (src_len > dst_len)
23323f8c95aSGreg Clayton       src_len = dst_len;
234b9c1b51eSKate Stone     const uint8_t *src =
235b9c1b51eSKate Stone         ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
23623f8c95aSGreg Clayton 
23723f8c95aSGreg Clayton     memcpy(dst, src, src_len);
23823f8c95aSGreg Clayton     return src_len;
23923f8c95aSGreg Clayton   }
24023f8c95aSGreg Clayton   return 0;
24123f8c95aSGreg Clayton }
242a746e8e5SZachary Turner 
243b9c1b51eSKate Stone size_t ObjectFileJIT::ReadSectionData(
244d13f691fSEd Maste     lldb_private::Section *section,
245d13f691fSEd Maste     lldb_private::DataExtractor &section_data) {
246b9c1b51eSKate Stone   if (section->GetFileSize()) {
24723f8c95aSGreg Clayton     const void *src = (void *)(uintptr_t)section->GetFileOffset();
24823f8c95aSGreg Clayton 
249*0f73709cSPavel Labath     DataBufferSP data_sp =
250*0f73709cSPavel Labath         std::make_shared<DataBufferHeap>(src, section->GetFileSize());
25123f8c95aSGreg Clayton     section_data.SetData(data_sp, 0, data_sp->GetByteSize());
25223f8c95aSGreg Clayton     section_data.SetByteOrder(GetByteOrder());
25323f8c95aSGreg Clayton     section_data.SetAddressByteSize(GetAddressByteSize());
25423f8c95aSGreg Clayton     return section_data.GetByteSize();
25523f8c95aSGreg Clayton   }
25623f8c95aSGreg Clayton   section_data.Clear();
25723f8c95aSGreg Clayton   return 0;
25823f8c95aSGreg Clayton }
259