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 DataBufferSP &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 Symtab *ObjectFileJIT::GetSymtab() { 110 ModuleSP module_sp(GetModule()); 111 if (module_sp) { 112 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 113 if (m_symtab_up == nullptr) { 114 ElapsedTime elapsed(module_sp->GetSymtabParseTime()); 115 m_symtab_up = std::make_unique<Symtab>(this); 116 std::lock_guard<std::recursive_mutex> symtab_guard( 117 m_symtab_up->GetMutex()); 118 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 119 if (delegate_sp) 120 delegate_sp->PopulateSymtab(this, *m_symtab_up); 121 // TODO: get symbols from delegate 122 m_symtab_up->Finalize(); 123 } 124 } 125 return m_symtab_up.get(); 126 } 127 128 bool ObjectFileJIT::IsStripped() { 129 return false; // JIT code that is in a module is never stripped 130 } 131 132 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) { 133 if (!m_sections_up) { 134 m_sections_up = std::make_unique<SectionList>(); 135 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 136 if (delegate_sp) { 137 delegate_sp->PopulateSectionList(this, *m_sections_up); 138 unified_section_list = *m_sections_up; 139 } 140 } 141 } 142 143 void ObjectFileJIT::Dump(Stream *s) { 144 ModuleSP module_sp(GetModule()); 145 if (module_sp) { 146 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 147 s->Printf("%p: ", static_cast<void *>(this)); 148 s->Indent(); 149 s->PutCString("ObjectFileJIT"); 150 151 if (ArchSpec arch = GetArchitecture()) 152 *s << ", arch = " << arch.GetArchitectureName(); 153 154 s->EOL(); 155 156 SectionList *sections = GetSectionList(); 157 if (sections) 158 sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 159 UINT32_MAX); 160 161 if (m_symtab_up) 162 m_symtab_up->Dump(s, nullptr, eSortOrderNone); 163 } 164 } 165 166 UUID ObjectFileJIT::GetUUID() { 167 // TODO: maybe get from delegate, not needed for first pass 168 return UUID(); 169 } 170 171 uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) { 172 // JIT modules don't have dependencies, but they could 173 // if external functions are called and we know where they are 174 files.Clear(); 175 return 0; 176 } 177 178 lldb_private::Address ObjectFileJIT::GetEntryPointAddress() { 179 return Address(); 180 } 181 182 lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); } 183 184 ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; } 185 186 ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; } 187 188 ArchSpec ObjectFileJIT::GetArchitecture() { 189 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock()) 190 return delegate_sp->GetArchitecture(); 191 return ArchSpec(); 192 } 193 194 bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value, 195 bool value_is_offset) { 196 size_t num_loaded_sections = 0; 197 SectionList *section_list = GetSectionList(); 198 if (section_list) { 199 const size_t num_sections = section_list->GetSize(); 200 // "value" is an offset to apply to each top level segment 201 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 202 // Iterate through the object file sections to find all of the sections 203 // that size on disk (to avoid __PAGEZERO) and load them 204 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 205 if (section_sp && section_sp->GetFileSize() > 0 && 206 !section_sp->IsThreadSpecific()) { 207 if (target.GetSectionLoadList().SetSectionLoadAddress( 208 section_sp, section_sp->GetFileAddress() + value)) 209 ++num_loaded_sections; 210 } 211 } 212 } 213 return num_loaded_sections > 0; 214 } 215 216 size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section, 217 lldb::offset_t section_offset, void *dst, 218 size_t dst_len) { 219 lldb::offset_t file_size = section->GetFileSize(); 220 if (section_offset < file_size) { 221 size_t src_len = file_size - section_offset; 222 if (src_len > dst_len) 223 src_len = dst_len; 224 const uint8_t *src = 225 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset; 226 227 memcpy(dst, src, src_len); 228 return src_len; 229 } 230 return 0; 231 } 232 233 size_t ObjectFileJIT::ReadSectionData( 234 lldb_private::Section *section, 235 lldb_private::DataExtractor §ion_data) { 236 if (section->GetFileSize()) { 237 const void *src = (void *)(uintptr_t)section->GetFileOffset(); 238 239 DataBufferSP data_sp = 240 std::make_shared<DataBufferHeap>(src, section->GetFileSize()); 241 section_data.SetData(data_sp, 0, data_sp->GetByteSize()); 242 section_data.SetByteOrder(GetByteOrder()); 243 section_data.SetAddressByteSize(GetAddressByteSize()); 244 return section_data.GetByteSize(); 245 } 246 section_data.Clear(); 247 return 0; 248 } 249