123f8c95aSGreg Clayton //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===// 223f8c95aSGreg Clayton // 323f8c95aSGreg Clayton // The LLVM Compiler Infrastructure 423f8c95aSGreg Clayton // 523f8c95aSGreg Clayton // This file is distributed under the University of Illinois Open Source 623f8c95aSGreg Clayton // License. See LICENSE.TXT for details. 723f8c95aSGreg Clayton // 823f8c95aSGreg Clayton //===----------------------------------------------------------------------===// 923f8c95aSGreg Clayton 1023f8c95aSGreg Clayton #include "llvm/ADT/StringRef.h" 1123f8c95aSGreg Clayton 1223f8c95aSGreg Clayton #include "ObjectFileJIT.h" 1323f8c95aSGreg Clayton 1423f8c95aSGreg Clayton #include "lldb/Core/ArchSpec.h" 1523f8c95aSGreg Clayton #include "lldb/Core/Debugger.h" 1623f8c95aSGreg Clayton #include "lldb/Core/FileSpecList.h" 1723f8c95aSGreg Clayton #include "lldb/Core/Module.h" 1823f8c95aSGreg Clayton #include "lldb/Core/ModuleSpec.h" 1923f8c95aSGreg Clayton #include "lldb/Core/PluginManager.h" 2023f8c95aSGreg Clayton #include "lldb/Core/RangeMap.h" 2123f8c95aSGreg Clayton #include "lldb/Core/Section.h" 2223f8c95aSGreg Clayton #include "lldb/Core/StreamFile.h" 23b9c1b51eSKate Stone #include "lldb/Host/Host.h" 2423f8c95aSGreg Clayton #include "lldb/Symbol/ObjectFile.h" 2523f8c95aSGreg Clayton #include "lldb/Target/Platform.h" 2623f8c95aSGreg Clayton #include "lldb/Target/Process.h" 2723f8c95aSGreg Clayton #include "lldb/Target/SectionLoadList.h" 2823f8c95aSGreg Clayton #include "lldb/Target/Target.h" 29666cc0b2SZachary Turner #include "lldb/Utility/DataBuffer.h" 30666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 315713a05bSZachary Turner #include "lldb/Utility/FileSpec.h" 326f9e6901SZachary Turner #include "lldb/Utility/Log.h" 33bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 34*38d0632eSPavel Labath #include "lldb/Utility/Timer.h" 35666cc0b2SZachary Turner #include "lldb/Utility/UUID.h" 3623f8c95aSGreg Clayton 3723f8c95aSGreg Clayton #ifndef __APPLE__ 3823f8c95aSGreg Clayton #include "Utility/UuidCompatibility.h" 3923f8c95aSGreg Clayton #endif 4023f8c95aSGreg Clayton 4123f8c95aSGreg Clayton using namespace lldb; 4223f8c95aSGreg Clayton using namespace lldb_private; 4323f8c95aSGreg Clayton 44b9c1b51eSKate Stone void ObjectFileJIT::Initialize() { 4523f8c95aSGreg Clayton PluginManager::RegisterPlugin(GetPluginNameStatic(), 46b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 47b9c1b51eSKate Stone CreateMemoryInstance, GetModuleSpecifications); 4823f8c95aSGreg Clayton } 4923f8c95aSGreg Clayton 50b9c1b51eSKate Stone void ObjectFileJIT::Terminate() { 5123f8c95aSGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 5223f8c95aSGreg Clayton } 5323f8c95aSGreg Clayton 54b9c1b51eSKate Stone lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() { 5523f8c95aSGreg Clayton static ConstString g_name("jit"); 5623f8c95aSGreg Clayton return g_name; 5723f8c95aSGreg Clayton } 5823f8c95aSGreg Clayton 59b9c1b51eSKate Stone const char *ObjectFileJIT::GetPluginDescriptionStatic() { 6023f8c95aSGreg Clayton return "JIT code object file"; 6123f8c95aSGreg Clayton } 6223f8c95aSGreg Clayton 63b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp, 6423f8c95aSGreg Clayton DataBufferSP &data_sp, 6523f8c95aSGreg Clayton lldb::offset_t data_offset, 6623f8c95aSGreg Clayton const FileSpec *file, 6723f8c95aSGreg Clayton lldb::offset_t file_offset, 68b9c1b51eSKate Stone lldb::offset_t length) { 6923f8c95aSGreg Clayton // JIT'ed object file is backed by the ObjectFileJITDelegate, never 7023f8c95aSGreg Clayton // read from a file 7123f8c95aSGreg Clayton return NULL; 7223f8c95aSGreg Clayton } 7323f8c95aSGreg Clayton 74b9c1b51eSKate Stone ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp, 7523f8c95aSGreg Clayton DataBufferSP &data_sp, 7623f8c95aSGreg Clayton const ProcessSP &process_sp, 77b9c1b51eSKate Stone lldb::addr_t header_addr) { 7823f8c95aSGreg Clayton // JIT'ed object file is backed by the ObjectFileJITDelegate, never 7923f8c95aSGreg Clayton // read from memory 8023f8c95aSGreg Clayton return NULL; 8123f8c95aSGreg Clayton } 8223f8c95aSGreg Clayton 83b9c1b51eSKate Stone size_t ObjectFileJIT::GetModuleSpecifications( 84b9c1b51eSKate Stone const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 85b9c1b51eSKate Stone lldb::offset_t data_offset, lldb::offset_t file_offset, 86b9c1b51eSKate Stone lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 8723f8c95aSGreg Clayton // JIT'ed object file can't be read from a file on disk 8823f8c95aSGreg Clayton return 0; 8923f8c95aSGreg Clayton } 9023f8c95aSGreg Clayton 9123f8c95aSGreg Clayton ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp, 92b9c1b51eSKate Stone const ObjectFileJITDelegateSP &delegate_sp) 93b9c1b51eSKate Stone : ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0), m_delegate_wp() { 94b9c1b51eSKate Stone if (delegate_sp) { 9523f8c95aSGreg Clayton m_delegate_wp = delegate_sp; 9623f8c95aSGreg Clayton m_data.SetByteOrder(delegate_sp->GetByteOrder()); 9723f8c95aSGreg Clayton m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize()); 9823f8c95aSGreg Clayton } 9923f8c95aSGreg Clayton } 10023f8c95aSGreg Clayton 101b9c1b51eSKate Stone ObjectFileJIT::~ObjectFileJIT() {} 10223f8c95aSGreg Clayton 103b9c1b51eSKate Stone bool ObjectFileJIT::ParseHeader() { 10423f8c95aSGreg Clayton // JIT code is never in a file, nor is it required to have any header 10523f8c95aSGreg Clayton return false; 10623f8c95aSGreg Clayton } 10723f8c95aSGreg Clayton 108b9c1b51eSKate Stone ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); } 10923f8c95aSGreg Clayton 110b9c1b51eSKate Stone bool ObjectFileJIT::IsExecutable() const { return false; } 11123f8c95aSGreg Clayton 112b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetAddressByteSize() const { 11323f8c95aSGreg Clayton return m_data.GetAddressByteSize(); 11423f8c95aSGreg Clayton } 11523f8c95aSGreg Clayton 116b9c1b51eSKate Stone Symtab *ObjectFileJIT::GetSymtab() { 11723f8c95aSGreg Clayton ModuleSP module_sp(GetModule()); 118b9c1b51eSKate Stone if (module_sp) { 11916ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 120b9c1b51eSKate Stone if (m_symtab_ap.get() == NULL) { 12123f8c95aSGreg Clayton m_symtab_ap.reset(new Symtab(this)); 122b9c1b51eSKate Stone std::lock_guard<std::recursive_mutex> symtab_guard( 123b9c1b51eSKate Stone m_symtab_ap->GetMutex()); 12423f8c95aSGreg Clayton ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 12523f8c95aSGreg Clayton if (delegate_sp) 12623f8c95aSGreg Clayton delegate_sp->PopulateSymtab(this, *m_symtab_ap); 12723f8c95aSGreg Clayton // TODO: get symbols from delegate 12823f8c95aSGreg Clayton m_symtab_ap->Finalize(); 12923f8c95aSGreg Clayton } 13023f8c95aSGreg Clayton } 13123f8c95aSGreg Clayton return m_symtab_ap.get(); 13223f8c95aSGreg Clayton } 13323f8c95aSGreg Clayton 134b9c1b51eSKate Stone bool ObjectFileJIT::IsStripped() { 13523f8c95aSGreg Clayton return false; // JIT code that is in a module is never stripped 13623f8c95aSGreg Clayton } 13723f8c95aSGreg Clayton 138b9c1b51eSKate Stone void ObjectFileJIT::CreateSections(SectionList &unified_section_list) { 139b9c1b51eSKate Stone if (!m_sections_ap.get()) { 14023f8c95aSGreg Clayton m_sections_ap.reset(new SectionList()); 14123f8c95aSGreg Clayton ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 142b9c1b51eSKate Stone if (delegate_sp) { 14323f8c95aSGreg Clayton delegate_sp->PopulateSectionList(this, *m_sections_ap); 14423f8c95aSGreg Clayton unified_section_list = *m_sections_ap; 14523f8c95aSGreg Clayton } 14623f8c95aSGreg Clayton } 14723f8c95aSGreg Clayton } 14823f8c95aSGreg Clayton 149b9c1b51eSKate Stone void ObjectFileJIT::Dump(Stream *s) { 15023f8c95aSGreg Clayton ModuleSP module_sp(GetModule()); 151b9c1b51eSKate Stone if (module_sp) { 15216ff8604SSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 153324a1036SSaleem Abdulrasool s->Printf("%p: ", static_cast<void *>(this)); 15423f8c95aSGreg Clayton s->Indent(); 15523f8c95aSGreg Clayton s->PutCString("ObjectFileJIT"); 15623f8c95aSGreg Clayton 15723f8c95aSGreg Clayton ArchSpec arch; 15823f8c95aSGreg Clayton if (GetArchitecture(arch)) 15923f8c95aSGreg Clayton *s << ", arch = " << arch.GetArchitectureName(); 16023f8c95aSGreg Clayton 16123f8c95aSGreg Clayton s->EOL(); 16223f8c95aSGreg Clayton 16323f8c95aSGreg Clayton SectionList *sections = GetSectionList(); 16423f8c95aSGreg Clayton if (sections) 16523f8c95aSGreg Clayton sections->Dump(s, NULL, true, UINT32_MAX); 16623f8c95aSGreg Clayton 16723f8c95aSGreg Clayton if (m_symtab_ap.get()) 16823f8c95aSGreg Clayton m_symtab_ap->Dump(s, NULL, eSortOrderNone); 16923f8c95aSGreg Clayton } 17023f8c95aSGreg Clayton } 17123f8c95aSGreg Clayton 172b9c1b51eSKate Stone bool ObjectFileJIT::GetUUID(lldb_private::UUID *uuid) { 17323f8c95aSGreg Clayton // TODO: maybe get from delegate, not needed for first pass 17423f8c95aSGreg Clayton return false; 17523f8c95aSGreg Clayton } 17623f8c95aSGreg Clayton 177b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) { 17823f8c95aSGreg Clayton // JIT modules don't have dependencies, but they could 17923f8c95aSGreg Clayton // if external functions are called and we know where they are 18023f8c95aSGreg Clayton files.Clear(); 18123f8c95aSGreg Clayton return 0; 18223f8c95aSGreg Clayton } 18323f8c95aSGreg Clayton 184b9c1b51eSKate Stone lldb_private::Address ObjectFileJIT::GetEntryPointAddress() { 18523f8c95aSGreg Clayton return Address(); 18623f8c95aSGreg Clayton } 18723f8c95aSGreg Clayton 188b9c1b51eSKate Stone lldb_private::Address ObjectFileJIT::GetHeaderAddress() { return Address(); } 18923f8c95aSGreg Clayton 190b9c1b51eSKate Stone ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; } 19123f8c95aSGreg Clayton 192b9c1b51eSKate Stone ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; } 19323f8c95aSGreg Clayton 194b9c1b51eSKate Stone bool ObjectFileJIT::GetArchitecture(ArchSpec &arch) { 19523f8c95aSGreg Clayton ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); 19623f8c95aSGreg Clayton if (delegate_sp) 19723f8c95aSGreg Clayton return delegate_sp->GetArchitecture(arch); 19823f8c95aSGreg Clayton return false; 19923f8c95aSGreg Clayton } 20023f8c95aSGreg Clayton 20123f8c95aSGreg Clayton //------------------------------------------------------------------ 20223f8c95aSGreg Clayton // PluginInterface protocol 20323f8c95aSGreg Clayton //------------------------------------------------------------------ 204b9c1b51eSKate Stone lldb_private::ConstString ObjectFileJIT::GetPluginName() { 20523f8c95aSGreg Clayton return GetPluginNameStatic(); 20623f8c95aSGreg Clayton } 20723f8c95aSGreg Clayton 208b9c1b51eSKate Stone uint32_t ObjectFileJIT::GetPluginVersion() { return 1; } 20923f8c95aSGreg Clayton 210b9c1b51eSKate Stone bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value, 211b9c1b51eSKate Stone bool value_is_offset) { 21223f8c95aSGreg Clayton size_t num_loaded_sections = 0; 21323f8c95aSGreg Clayton SectionList *section_list = GetSectionList(); 214b9c1b51eSKate Stone if (section_list) { 21523f8c95aSGreg Clayton const size_t num_sections = section_list->GetSize(); 21623f8c95aSGreg Clayton // "value" is an offset to apply to each top level segment 217b9c1b51eSKate Stone for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 21823f8c95aSGreg Clayton // Iterate through the object file sections to find all 21923f8c95aSGreg Clayton // of the sections that size on disk (to avoid __PAGEZERO) 22023f8c95aSGreg Clayton // and load them 22123f8c95aSGreg Clayton SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 222b9c1b51eSKate Stone if (section_sp && section_sp->GetFileSize() > 0 && 223b9c1b51eSKate Stone section_sp->IsThreadSpecific() == false) { 224b9c1b51eSKate Stone if (target.GetSectionLoadList().SetSectionLoadAddress( 225b9c1b51eSKate Stone section_sp, section_sp->GetFileAddress() + value)) 22623f8c95aSGreg Clayton ++num_loaded_sections; 22723f8c95aSGreg Clayton } 22823f8c95aSGreg Clayton } 22923f8c95aSGreg Clayton } 23023f8c95aSGreg Clayton return num_loaded_sections > 0; 23123f8c95aSGreg Clayton } 23223f8c95aSGreg Clayton 233b9c1b51eSKate Stone size_t ObjectFileJIT::ReadSectionData(const lldb_private::Section *section, 234b9c1b51eSKate Stone lldb::offset_t section_offset, void *dst, 235b9c1b51eSKate Stone size_t dst_len) const { 23623f8c95aSGreg Clayton lldb::offset_t file_size = section->GetFileSize(); 237b9c1b51eSKate Stone if (section_offset < file_size) { 238a746e8e5SZachary Turner size_t src_len = file_size - section_offset; 23923f8c95aSGreg Clayton if (src_len > dst_len) 24023f8c95aSGreg Clayton src_len = dst_len; 241b9c1b51eSKate Stone const uint8_t *src = 242b9c1b51eSKate Stone ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset; 24323f8c95aSGreg Clayton 24423f8c95aSGreg Clayton memcpy(dst, src, src_len); 24523f8c95aSGreg Clayton return src_len; 24623f8c95aSGreg Clayton } 24723f8c95aSGreg Clayton return 0; 24823f8c95aSGreg Clayton } 249a746e8e5SZachary Turner 250b9c1b51eSKate Stone size_t ObjectFileJIT::ReadSectionData( 251b9c1b51eSKate Stone const lldb_private::Section *section, 252b9c1b51eSKate Stone lldb_private::DataExtractor §ion_data) const { 253b9c1b51eSKate Stone if (section->GetFileSize()) { 25423f8c95aSGreg Clayton const void *src = (void *)(uintptr_t)section->GetFileOffset(); 25523f8c95aSGreg Clayton 256b9c1b51eSKate Stone DataBufferSP data_sp( 257b9c1b51eSKate Stone new lldb_private::DataBufferHeap(src, section->GetFileSize())); 258b9c1b51eSKate Stone if (data_sp) { 25923f8c95aSGreg Clayton section_data.SetData(data_sp, 0, data_sp->GetByteSize()); 26023f8c95aSGreg Clayton section_data.SetByteOrder(GetByteOrder()); 26123f8c95aSGreg Clayton section_data.SetAddressByteSize(GetAddressByteSize()); 26223f8c95aSGreg Clayton return section_data.GetByteSize(); 26323f8c95aSGreg Clayton } 26423f8c95aSGreg Clayton } 26523f8c95aSGreg Clayton section_data.Clear(); 26623f8c95aSGreg Clayton return 0; 26723f8c95aSGreg Clayton } 268