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