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