1 //===-- ObjectFileJIT.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/StringRef.h" 10 11 #include "ObjectFileJIT.h" 12 #include "lldb/Core/Debugger.h" 13 #include "lldb/Core/FileSpecList.h" 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/ModuleSpec.h" 16 #include "lldb/Core/PluginManager.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Core/StreamFile.h" 19 #include "lldb/Host/Host.h" 20 #include "lldb/Symbol/ObjectFile.h" 21 #include "lldb/Target/Platform.h" 22 #include "lldb/Target/Process.h" 23 #include "lldb/Target/SectionLoadList.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Utility/ArchSpec.h" 26 #include "lldb/Utility/DataBuffer.h" 27 #include "lldb/Utility/DataBufferHeap.h" 28 #include "lldb/Utility/FileSpec.h" 29 #include "lldb/Utility/Log.h" 30 #include "lldb/Utility/RangeMap.h" 31 #include "lldb/Utility/StreamString.h" 32 #include "lldb/Utility/Timer.h" 33 #include "lldb/Utility/UUID.h" 34 35 #ifndef __APPLE__ 36 #include "Utility/UuidCompatibility.h" 37 #endif 38 39 using namespace lldb; 40 using namespace lldb_private; 41 42 LLDB_PLUGIN_DEFINE(ObjectFileJIT) 43 44 char ObjectFileJIT::ID; 45 46 void ObjectFileJIT::Initialize() { 47 PluginManager::RegisterPlugin(GetPluginNameStatic(), 48 GetPluginDescriptionStatic(), CreateInstance, 49 CreateMemoryInstance, GetModuleSpecifications); 50 } 51 52 void ObjectFileJIT::Terminate() { 53 PluginManager::UnregisterPlugin(CreateInstance); 54 } 55 56 ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp, 57 DataBufferSP data_sp, 58 lldb::offset_t data_offset, 59 const FileSpec *file, 60 lldb::offset_t file_offset, 61 lldb::offset_t length) { 62 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from 63 // a file 64 return nullptr; 65 } 66 67 ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp, 68 WritableDataBufferSP data_sp, 69 const ProcessSP &process_sp, 70 lldb::addr_t header_addr) { 71 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from 72 // memory 73 return nullptr; 74 } 75 76 size_t ObjectFileJIT::GetModuleSpecifications( 77 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 78 lldb::offset_t data_offset, lldb::offset_t file_offset, 79 lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 80 // JIT'ed object file can't be read from a file on disk 81 return 0; 82 } 83 84 ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp, 85 const ObjectFileJITDelegateSP &delegate_sp) 86 : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() { 87 if (delegate_sp) { 88 m_delegate_wp = delegate_sp; 89 m_data.SetByteOrder(delegate_sp->GetByteOrder()); 90 m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize()); 91 } 92 } 93 94 ObjectFileJIT::~ObjectFileJIT() = default; 95 96 bool ObjectFileJIT::ParseHeader() { 97 // JIT code is never in a file, nor is it required to have any header 98 return false; 99 } 100 101 ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); } 102 103 bool ObjectFileJIT::IsExecutable() const { return false; } 104 105 uint32_t ObjectFileJIT::GetAddressByteSize() const { 106 return m_data.GetAddressByteSize(); 107 } 108 109 void ObjectFileJIT::ParseSymtab(Symtab &symtab) { 110 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 111 if (delegate_sp) 112 delegate_sp->PopulateSymtab(this, symtab); 113 } 114 115 bool ObjectFileJIT::IsStripped() { 116 return false; // JIT code that is in a module is never stripped 117 } 118 119 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) { 120 if (!m_sections_up) { 121 m_sections_up = std::make_unique<SectionList>(); 122 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 123 if (delegate_sp) { 124 delegate_sp->PopulateSectionList(this, *m_sections_up); 125 unified_section_list = *m_sections_up; 126 } 127 } 128 } 129 130 void ObjectFileJIT::Dump(Stream *s) { 131 ModuleSP module_sp(GetModule()); 132 if (module_sp) { 133 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 134 s->Printf("%p: ", static_cast<void *>(this)); 135 s->Indent(); 136 s->PutCString("ObjectFileJIT"); 137 138 if (ArchSpec arch = GetArchitecture()) 139 *s << ", arch = " << arch.GetArchitectureName(); 140 141 s->EOL(); 142 143 SectionList *sections = GetSectionList(); 144 if (sections) 145 sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 146 UINT32_MAX); 147 148 if (m_symtab_up) 149 m_symtab_up->Dump(s, nullptr, eSortOrderNone); 150 } 151 } 152 153 UUID ObjectFileJIT::GetUUID() { 154 // TODO: maybe get from delegate, not needed for first pass 155 return UUID(); 156 } 157 158 uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) { 159 // JIT modules don't have dependencies, but they could 160 // if external functions are called and we know where they are 161 files.Clear(); 162 return 0; 163 } 164 165 lldb_private::Address ObjectFileJIT::GetEntryPointAddress() { 166 return Address(); 167 } 168 169 lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); } 170 171 ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; } 172 173 ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; } 174 175 ArchSpec ObjectFileJIT::GetArchitecture() { 176 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock()) 177 return delegate_sp->GetArchitecture(); 178 return ArchSpec(); 179 } 180 181 bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value, 182 bool value_is_offset) { 183 size_t num_loaded_sections = 0; 184 SectionList *section_list = GetSectionList(); 185 if (section_list) { 186 const size_t num_sections = section_list->GetSize(); 187 // "value" is an offset to apply to each top level segment 188 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 189 // Iterate through the object file sections to find all of the sections 190 // that size on disk (to avoid __PAGEZERO) and load them 191 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 192 if (section_sp && section_sp->GetFileSize() > 0 && 193 !section_sp->IsThreadSpecific()) { 194 if (target.GetSectionLoadList().SetSectionLoadAddress( 195 section_sp, section_sp->GetFileAddress() + value)) 196 ++num_loaded_sections; 197 } 198 } 199 } 200 return num_loaded_sections > 0; 201 } 202 203 size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section, 204 lldb::offset_t section_offset, void *dst, 205 size_t dst_len) { 206 lldb::offset_t file_size = section->GetFileSize(); 207 if (section_offset < file_size) { 208 size_t src_len = file_size - section_offset; 209 if (src_len > dst_len) 210 src_len = dst_len; 211 const uint8_t *src = 212 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset; 213 214 memcpy(dst, src, src_len); 215 return src_len; 216 } 217 return 0; 218 } 219 220 size_t ObjectFileJIT::ReadSectionData( 221 lldb_private::Section *section, 222 lldb_private::DataExtractor §ion_data) { 223 if (section->GetFileSize()) { 224 const void *src = (void *)(uintptr_t)section->GetFileOffset(); 225 226 DataBufferSP data_sp = 227 std::make_shared<DataBufferHeap>(src, section->GetFileSize()); 228 section_data.SetData(data_sp, 0, data_sp->GetByteSize()); 229 section_data.SetByteOrder(GetByteOrder()); 230 section_data.SetAddressByteSize(GetAddressByteSize()); 231 return section_data.GetByteSize(); 232 } 233 section_data.Clear(); 234 return 0; 235 } 236