1 //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===// 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/RangeMap.h" 18 #include "lldb/Core/Section.h" 19 #include "lldb/Core/StreamFile.h" 20 #include "lldb/Host/Host.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Target/Platform.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/SectionLoadList.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Utility/ArchSpec.h" 27 #include "lldb/Utility/DataBuffer.h" 28 #include "lldb/Utility/DataBufferHeap.h" 29 #include "lldb/Utility/FileSpec.h" 30 #include "lldb/Utility/Log.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 void ObjectFileJIT::Initialize() { 43 PluginManager::RegisterPlugin(GetPluginNameStatic(), 44 GetPluginDescriptionStatic(), CreateInstance, 45 CreateMemoryInstance, GetModuleSpecifications); 46 } 47 48 void ObjectFileJIT::Terminate() { 49 PluginManager::UnregisterPlugin(CreateInstance); 50 } 51 52 lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() { 53 static ConstString g_name("jit"); 54 return g_name; 55 } 56 57 const char *ObjectFileJIT::GetPluginDescriptionStatic() { 58 return "JIT code object file"; 59 } 60 61 ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp, 62 DataBufferSP &data_sp, 63 lldb::offset_t data_offset, 64 const FileSpec *file, 65 lldb::offset_t file_offset, 66 lldb::offset_t length) { 67 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from 68 // a file 69 return NULL; 70 } 71 72 ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp, 73 DataBufferSP &data_sp, 74 const ProcessSP &process_sp, 75 lldb::addr_t header_addr) { 76 // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from 77 // memory 78 return NULL; 79 } 80 81 size_t ObjectFileJIT::GetModuleSpecifications( 82 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 83 lldb::offset_t data_offset, lldb::offset_t file_offset, 84 lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 85 // JIT'ed object file can't be read from a file on disk 86 return 0; 87 } 88 89 ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp, 90 const ObjectFileJITDelegateSP &delegate_sp) 91 : ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0), m_delegate_wp() { 92 if (delegate_sp) { 93 m_delegate_wp = delegate_sp; 94 m_data.SetByteOrder(delegate_sp->GetByteOrder()); 95 m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize()); 96 } 97 } 98 99 ObjectFileJIT::~ObjectFileJIT() {} 100 101 bool ObjectFileJIT::ParseHeader() { 102 // JIT code is never in a file, nor is it required to have any header 103 return false; 104 } 105 106 ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); } 107 108 bool ObjectFileJIT::IsExecutable() const { return false; } 109 110 uint32_t ObjectFileJIT::GetAddressByteSize() const { 111 return m_data.GetAddressByteSize(); 112 } 113 114 Symtab *ObjectFileJIT::GetSymtab() { 115 ModuleSP module_sp(GetModule()); 116 if (module_sp) { 117 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 118 if (m_symtab_ap == NULL) { 119 m_symtab_ap.reset(new Symtab(this)); 120 std::lock_guard<std::recursive_mutex> symtab_guard( 121 m_symtab_ap->GetMutex()); 122 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 123 if (delegate_sp) 124 delegate_sp->PopulateSymtab(this, *m_symtab_ap); 125 // TODO: get symbols from delegate 126 m_symtab_ap->Finalize(); 127 } 128 } 129 return m_symtab_ap.get(); 130 } 131 132 bool ObjectFileJIT::IsStripped() { 133 return false; // JIT code that is in a module is never stripped 134 } 135 136 void ObjectFileJIT::CreateSections(SectionList &unified_section_list) { 137 if (!m_sections_ap) { 138 m_sections_ap.reset(new SectionList()); 139 ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 140 if (delegate_sp) { 141 delegate_sp->PopulateSectionList(this, *m_sections_ap); 142 unified_section_list = *m_sections_ap; 143 } 144 } 145 } 146 147 void ObjectFileJIT::Dump(Stream *s) { 148 ModuleSP module_sp(GetModule()); 149 if (module_sp) { 150 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 151 s->Printf("%p: ", static_cast<void *>(this)); 152 s->Indent(); 153 s->PutCString("ObjectFileJIT"); 154 155 if (ArchSpec arch = GetArchitecture()) 156 *s << ", arch = " << arch.GetArchitectureName(); 157 158 s->EOL(); 159 160 SectionList *sections = GetSectionList(); 161 if (sections) 162 sections->Dump(s, NULL, true, UINT32_MAX); 163 164 if (m_symtab_ap) 165 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 166 } 167 } 168 169 UUID ObjectFileJIT::GetUUID() { 170 // TODO: maybe get from delegate, not needed for first pass 171 return UUID(); 172 } 173 174 uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) { 175 // JIT modules don't have dependencies, but they could 176 // if external functions are called and we know where they are 177 files.Clear(); 178 return 0; 179 } 180 181 lldb_private::Address ObjectFileJIT::GetEntryPointAddress() { 182 return Address(); 183 } 184 185 lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); } 186 187 ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; } 188 189 ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; } 190 191 ArchSpec ObjectFileJIT::GetArchitecture() { 192 if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock()) 193 return delegate_sp->GetArchitecture(); 194 return ArchSpec(); 195 } 196 197 //------------------------------------------------------------------ 198 // PluginInterface protocol 199 //------------------------------------------------------------------ 200 lldb_private::ConstString ObjectFileJIT::GetPluginName() { 201 return GetPluginNameStatic(); 202 } 203 204 uint32_t ObjectFileJIT::GetPluginVersion() { return 1; } 205 206 bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value, 207 bool value_is_offset) { 208 size_t num_loaded_sections = 0; 209 SectionList *section_list = GetSectionList(); 210 if (section_list) { 211 const size_t num_sections = section_list->GetSize(); 212 // "value" is an offset to apply to each top level segment 213 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 214 // Iterate through the object file sections to find all of the sections 215 // that size on disk (to avoid __PAGEZERO) and load them 216 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 217 if (section_sp && section_sp->GetFileSize() > 0 && 218 !section_sp->IsThreadSpecific()) { 219 if (target.GetSectionLoadList().SetSectionLoadAddress( 220 section_sp, section_sp->GetFileAddress() + value)) 221 ++num_loaded_sections; 222 } 223 } 224 } 225 return num_loaded_sections > 0; 226 } 227 228 size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section, 229 lldb::offset_t section_offset, void *dst, 230 size_t dst_len) { 231 lldb::offset_t file_size = section->GetFileSize(); 232 if (section_offset < file_size) { 233 size_t src_len = file_size - section_offset; 234 if (src_len > dst_len) 235 src_len = dst_len; 236 const uint8_t *src = 237 ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset; 238 239 memcpy(dst, src, src_len); 240 return src_len; 241 } 242 return 0; 243 } 244 245 size_t ObjectFileJIT::ReadSectionData( 246 lldb_private::Section *section, 247 lldb_private::DataExtractor §ion_data) { 248 if (section->GetFileSize()) { 249 const void *src = (void *)(uintptr_t)section->GetFileOffset(); 250 251 DataBufferSP data_sp( 252 new lldb_private::DataBufferHeap(src, section->GetFileSize())); 253 if (data_sp) { 254 section_data.SetData(data_sp, 0, data_sp->GetByteSize()); 255 section_data.SetByteOrder(GetByteOrder()); 256 section_data.SetAddressByteSize(GetAddressByteSize()); 257 return section_data.GetByteSize(); 258 } 259 } 260 section_data.Clear(); 261 return 0; 262 } 263