180814287SRaphael Isemann //===-- ObjectFileJIT.cpp -------------------------------------------------===//
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
42aa2ae6afSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFileJIT)
43aa2ae6afSJonas Devlieghere
44e84f7841SPavel Labath char ObjectFileJIT::ID;
45e84f7841SPavel Labath
Initialize()46b9c1b51eSKate Stone void ObjectFileJIT::Initialize() {
4723f8c95aSGreg Clayton PluginManager::RegisterPlugin(GetPluginNameStatic(),
48b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance,
49b9c1b51eSKate Stone CreateMemoryInstance, GetModuleSpecifications);
5023f8c95aSGreg Clayton }
5123f8c95aSGreg Clayton
Terminate()52b9c1b51eSKate Stone void ObjectFileJIT::Terminate() {
5323f8c95aSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance);
5423f8c95aSGreg Clayton }
5523f8c95aSGreg Clayton
CreateInstance(const lldb::ModuleSP & module_sp,DataBufferSP data_sp,lldb::offset_t data_offset,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t length)56b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
57c69307e5SJonas Devlieghere DataBufferSP data_sp,
5823f8c95aSGreg Clayton lldb::offset_t data_offset,
5923f8c95aSGreg Clayton const FileSpec *file,
6023f8c95aSGreg Clayton lldb::offset_t file_offset,
61b9c1b51eSKate Stone lldb::offset_t length) {
6205097246SAdrian Prantl // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
6305097246SAdrian Prantl // a file
64248a1305SKonrad Kleine return nullptr;
6523f8c95aSGreg Clayton }
6623f8c95aSGreg Clayton
CreateMemoryInstance(const lldb::ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,lldb::addr_t header_addr)67b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
68*f2ea125eSJonas Devlieghere WritableDataBufferSP data_sp,
6923f8c95aSGreg Clayton const ProcessSP &process_sp,
70b9c1b51eSKate Stone lldb::addr_t header_addr) {
7105097246SAdrian Prantl // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
7205097246SAdrian Prantl // memory
73248a1305SKonrad Kleine return nullptr;
7423f8c95aSGreg Clayton }
7523f8c95aSGreg Clayton
GetModuleSpecifications(const lldb_private::FileSpec & file,lldb::DataBufferSP & data_sp,lldb::offset_t data_offset,lldb::offset_t file_offset,lldb::offset_t length,lldb_private::ModuleSpecList & specs)76b9c1b51eSKate Stone size_t ObjectFileJIT::GetModuleSpecifications(
77b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
78b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset,
79b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
8023f8c95aSGreg Clayton // JIT'ed object file can't be read from a file on disk
8123f8c95aSGreg Clayton return 0;
8223f8c95aSGreg Clayton }
8323f8c95aSGreg Clayton
ObjectFileJIT(const lldb::ModuleSP & module_sp,const ObjectFileJITDelegateSP & delegate_sp)8423f8c95aSGreg Clayton ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
85b9c1b51eSKate Stone const ObjectFileJITDelegateSP &delegate_sp)
86248a1305SKonrad Kleine : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
87b9c1b51eSKate Stone if (delegate_sp) {
8823f8c95aSGreg Clayton m_delegate_wp = delegate_sp;
8923f8c95aSGreg Clayton m_data.SetByteOrder(delegate_sp->GetByteOrder());
9023f8c95aSGreg Clayton m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
9123f8c95aSGreg Clayton }
9223f8c95aSGreg Clayton }
9323f8c95aSGreg Clayton
94fd2433e1SJonas Devlieghere ObjectFileJIT::~ObjectFileJIT() = default;
9523f8c95aSGreg Clayton
ParseHeader()96b9c1b51eSKate Stone bool ObjectFileJIT::ParseHeader() {
9723f8c95aSGreg Clayton // JIT code is never in a file, nor is it required to have any header
9823f8c95aSGreg Clayton return false;
9923f8c95aSGreg Clayton }
10023f8c95aSGreg Clayton
GetByteOrder() const101b9c1b51eSKate Stone ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
10223f8c95aSGreg Clayton
IsExecutable() const103b9c1b51eSKate Stone bool ObjectFileJIT::IsExecutable() const { return false; }
10423f8c95aSGreg Clayton
GetAddressByteSize() const105b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetAddressByteSize() const {
10623f8c95aSGreg Clayton return m_data.GetAddressByteSize();
10723f8c95aSGreg Clayton }
10823f8c95aSGreg Clayton
ParseSymtab(Symtab & symtab)1097e6df41fSGreg Clayton void ObjectFileJIT::ParseSymtab(Symtab &symtab) {
11023f8c95aSGreg Clayton ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
11123f8c95aSGreg Clayton if (delegate_sp)
1127e6df41fSGreg Clayton delegate_sp->PopulateSymtab(this, symtab);
11323f8c95aSGreg Clayton }
11423f8c95aSGreg Clayton
IsStripped()115b9c1b51eSKate Stone bool ObjectFileJIT::IsStripped() {
11623f8c95aSGreg Clayton return false; // JIT code that is in a module is never stripped
11723f8c95aSGreg Clayton }
11823f8c95aSGreg Clayton
CreateSections(SectionList & unified_section_list)119b9c1b51eSKate Stone void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
120d5b44036SJonas Devlieghere if (!m_sections_up) {
12106412daeSJonas Devlieghere m_sections_up = std::make_unique<SectionList>();
12223f8c95aSGreg Clayton ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
123b9c1b51eSKate Stone if (delegate_sp) {
124d5b44036SJonas Devlieghere delegate_sp->PopulateSectionList(this, *m_sections_up);
125d5b44036SJonas Devlieghere unified_section_list = *m_sections_up;
12623f8c95aSGreg Clayton }
12723f8c95aSGreg Clayton }
12823f8c95aSGreg Clayton }
12923f8c95aSGreg Clayton
Dump(Stream * s)130b9c1b51eSKate Stone void ObjectFileJIT::Dump(Stream *s) {
13123f8c95aSGreg Clayton ModuleSP module_sp(GetModule());
132b9c1b51eSKate Stone if (module_sp) {
13316ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
134324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this));
13523f8c95aSGreg Clayton s->Indent();
13623f8c95aSGreg Clayton s->PutCString("ObjectFileJIT");
13723f8c95aSGreg Clayton
138f760f5aeSPavel Labath if (ArchSpec arch = GetArchitecture())
13923f8c95aSGreg Clayton *s << ", arch = " << arch.GetArchitectureName();
14023f8c95aSGreg Clayton
14123f8c95aSGreg Clayton s->EOL();
14223f8c95aSGreg Clayton
14323f8c95aSGreg Clayton SectionList *sections = GetSectionList();
14423f8c95aSGreg Clayton if (sections)
1453a168297SPavel Labath sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
1463a168297SPavel Labath UINT32_MAX);
14723f8c95aSGreg Clayton
148d5b44036SJonas Devlieghere if (m_symtab_up)
149248a1305SKonrad Kleine m_symtab_up->Dump(s, nullptr, eSortOrderNone);
15023f8c95aSGreg Clayton }
15123f8c95aSGreg Clayton }
15223f8c95aSGreg Clayton
GetUUID()153bd334efdSPavel Labath UUID ObjectFileJIT::GetUUID() {
15423f8c95aSGreg Clayton // TODO: maybe get from delegate, not needed for first pass
155bd334efdSPavel Labath return UUID();
15623f8c95aSGreg Clayton }
15723f8c95aSGreg Clayton
GetDependentModules(FileSpecList & files)158b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
15923f8c95aSGreg Clayton // JIT modules don't have dependencies, but they could
16023f8c95aSGreg Clayton // if external functions are called and we know where they are
16123f8c95aSGreg Clayton files.Clear();
16223f8c95aSGreg Clayton return 0;
16323f8c95aSGreg Clayton }
16423f8c95aSGreg Clayton
GetEntryPointAddress()165b9c1b51eSKate Stone lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
16623f8c95aSGreg Clayton return Address();
16723f8c95aSGreg Clayton }
16823f8c95aSGreg Clayton
GetBaseAddress()169d1e3fe21SPavel Labath lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
17023f8c95aSGreg Clayton
CalculateType()171b9c1b51eSKate Stone ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
17223f8c95aSGreg Clayton
CalculateStrata()173b9c1b51eSKate Stone ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
17423f8c95aSGreg Clayton
GetArchitecture()175f760f5aeSPavel Labath ArchSpec ObjectFileJIT::GetArchitecture() {
176f760f5aeSPavel Labath if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
177f760f5aeSPavel Labath return delegate_sp->GetArchitecture();
178f760f5aeSPavel Labath return ArchSpec();
17923f8c95aSGreg Clayton }
18023f8c95aSGreg Clayton
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset)181b9c1b51eSKate Stone bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
182b9c1b51eSKate Stone bool value_is_offset) {
18323f8c95aSGreg Clayton size_t num_loaded_sections = 0;
18423f8c95aSGreg Clayton SectionList *section_list = GetSectionList();
185b9c1b51eSKate Stone if (section_list) {
18623f8c95aSGreg Clayton const size_t num_sections = section_list->GetSize();
18723f8c95aSGreg Clayton // "value" is an offset to apply to each top level segment
188b9c1b51eSKate Stone for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
18905097246SAdrian Prantl // Iterate through the object file sections to find all of the sections
19005097246SAdrian Prantl // that size on disk (to avoid __PAGEZERO) and load them
19123f8c95aSGreg Clayton SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
192b9c1b51eSKate Stone if (section_sp && section_sp->GetFileSize() > 0 &&
193a6682a41SJonas Devlieghere !section_sp->IsThreadSpecific()) {
194b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress(
195b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value))
19623f8c95aSGreg Clayton ++num_loaded_sections;
19723f8c95aSGreg Clayton }
19823f8c95aSGreg Clayton }
19923f8c95aSGreg Clayton }
20023f8c95aSGreg Clayton return num_loaded_sections > 0;
20123f8c95aSGreg Clayton }
20223f8c95aSGreg Clayton
ReadSectionData(lldb_private::Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)203d13f691fSEd Maste size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
204b9c1b51eSKate Stone lldb::offset_t section_offset, void *dst,
205d13f691fSEd Maste size_t dst_len) {
20623f8c95aSGreg Clayton lldb::offset_t file_size = section->GetFileSize();
207b9c1b51eSKate Stone if (section_offset < file_size) {
208a746e8e5SZachary Turner size_t src_len = file_size - section_offset;
20923f8c95aSGreg Clayton if (src_len > dst_len)
21023f8c95aSGreg Clayton src_len = dst_len;
211b9c1b51eSKate Stone const uint8_t *src =
212b9c1b51eSKate Stone ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
21323f8c95aSGreg Clayton
21423f8c95aSGreg Clayton memcpy(dst, src, src_len);
21523f8c95aSGreg Clayton return src_len;
21623f8c95aSGreg Clayton }
21723f8c95aSGreg Clayton return 0;
21823f8c95aSGreg Clayton }
219a746e8e5SZachary Turner
ReadSectionData(lldb_private::Section * section,lldb_private::DataExtractor & section_data)220b9c1b51eSKate Stone size_t ObjectFileJIT::ReadSectionData(
221d13f691fSEd Maste lldb_private::Section *section,
222d13f691fSEd Maste lldb_private::DataExtractor §ion_data) {
223b9c1b51eSKate Stone if (section->GetFileSize()) {
22423f8c95aSGreg Clayton const void *src = (void *)(uintptr_t)section->GetFileOffset();
22523f8c95aSGreg Clayton
2260f73709cSPavel Labath DataBufferSP data_sp =
2270f73709cSPavel Labath std::make_shared<DataBufferHeap>(src, section->GetFileSize());
22823f8c95aSGreg Clayton section_data.SetData(data_sp, 0, data_sp->GetByteSize());
22923f8c95aSGreg Clayton section_data.SetByteOrder(GetByteOrder());
23023f8c95aSGreg Clayton section_data.SetAddressByteSize(GetAddressByteSize());
23123f8c95aSGreg Clayton return section_data.GetByteSize();
23223f8c95aSGreg Clayton }
23323f8c95aSGreg Clayton section_data.Clear();
23423f8c95aSGreg Clayton return 0;
23523f8c95aSGreg Clayton }
236