10127ef0fSEd Maste //===-- ObjectFileJIT.cpp ---------------------------------------*- C++ -*-===//
20127ef0fSEd Maste //
30127ef0fSEd Maste // The LLVM Compiler Infrastructure
40127ef0fSEd Maste //
50127ef0fSEd Maste // This file is distributed under the University of Illinois Open Source
60127ef0fSEd Maste // License. See LICENSE.TXT for details.
70127ef0fSEd Maste //
80127ef0fSEd Maste //===----------------------------------------------------------------------===//
90127ef0fSEd Maste
100127ef0fSEd Maste #include "llvm/ADT/StringRef.h"
110127ef0fSEd Maste
120127ef0fSEd Maste #include "ObjectFileJIT.h"
130127ef0fSEd Maste #include "lldb/Core/Debugger.h"
140127ef0fSEd Maste #include "lldb/Core/FileSpecList.h"
150127ef0fSEd Maste #include "lldb/Core/Module.h"
160127ef0fSEd Maste #include "lldb/Core/ModuleSpec.h"
170127ef0fSEd Maste #include "lldb/Core/PluginManager.h"
180127ef0fSEd Maste #include "lldb/Core/RangeMap.h"
190127ef0fSEd Maste #include "lldb/Core/Section.h"
200127ef0fSEd Maste #include "lldb/Core/StreamFile.h"
21435933ddSDimitry Andric #include "lldb/Host/Host.h"
220127ef0fSEd Maste #include "lldb/Symbol/ObjectFile.h"
230127ef0fSEd Maste #include "lldb/Target/Platform.h"
240127ef0fSEd Maste #include "lldb/Target/Process.h"
250127ef0fSEd Maste #include "lldb/Target/SectionLoadList.h"
260127ef0fSEd Maste #include "lldb/Target/Target.h"
27acac075bSDimitry Andric #include "lldb/Utility/ArchSpec.h"
28f678e45dSDimitry Andric #include "lldb/Utility/DataBuffer.h"
29f678e45dSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
30f678e45dSDimitry Andric #include "lldb/Utility/FileSpec.h"
31f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
32f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
33a580b014SDimitry Andric #include "lldb/Utility/Timer.h"
34f678e45dSDimitry Andric #include "lldb/Utility/UUID.h"
350127ef0fSEd Maste
360127ef0fSEd Maste #ifndef __APPLE__
370127ef0fSEd Maste #include "Utility/UuidCompatibility.h"
380127ef0fSEd Maste #endif
390127ef0fSEd Maste
400127ef0fSEd Maste using namespace lldb;
410127ef0fSEd Maste using namespace lldb_private;
420127ef0fSEd Maste
Initialize()43435933ddSDimitry Andric void ObjectFileJIT::Initialize() {
440127ef0fSEd Maste PluginManager::RegisterPlugin(GetPluginNameStatic(),
45435933ddSDimitry Andric GetPluginDescriptionStatic(), CreateInstance,
46435933ddSDimitry Andric CreateMemoryInstance, GetModuleSpecifications);
470127ef0fSEd Maste }
480127ef0fSEd Maste
Terminate()49435933ddSDimitry Andric void ObjectFileJIT::Terminate() {
500127ef0fSEd Maste PluginManager::UnregisterPlugin(CreateInstance);
510127ef0fSEd Maste }
520127ef0fSEd Maste
GetPluginNameStatic()53435933ddSDimitry Andric lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() {
540127ef0fSEd Maste static ConstString g_name("jit");
550127ef0fSEd Maste return g_name;
560127ef0fSEd Maste }
570127ef0fSEd Maste
GetPluginDescriptionStatic()58435933ddSDimitry Andric const char *ObjectFileJIT::GetPluginDescriptionStatic() {
590127ef0fSEd Maste return "JIT code object file";
600127ef0fSEd Maste }
610127ef0fSEd Maste
CreateInstance(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,lldb::offset_t data_offset,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t length)62435933ddSDimitry Andric ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
630127ef0fSEd Maste DataBufferSP &data_sp,
640127ef0fSEd Maste lldb::offset_t data_offset,
650127ef0fSEd Maste const FileSpec *file,
660127ef0fSEd Maste lldb::offset_t file_offset,
67435933ddSDimitry Andric lldb::offset_t length) {
684ba319b5SDimitry Andric // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
694ba319b5SDimitry Andric // a file
700127ef0fSEd Maste return NULL;
710127ef0fSEd Maste }
720127ef0fSEd Maste
CreateMemoryInstance(const lldb::ModuleSP & module_sp,DataBufferSP & data_sp,const ProcessSP & process_sp,lldb::addr_t header_addr)73435933ddSDimitry Andric ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
740127ef0fSEd Maste DataBufferSP &data_sp,
750127ef0fSEd Maste const ProcessSP &process_sp,
76435933ddSDimitry Andric lldb::addr_t header_addr) {
774ba319b5SDimitry Andric // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
784ba319b5SDimitry Andric // memory
790127ef0fSEd Maste return NULL;
800127ef0fSEd Maste }
810127ef0fSEd Maste
GetModuleSpecifications(const lldb_private::FileSpec & file,lldb::DataBufferSP & data_sp,lldb::offset_t data_offset,lldb::offset_t file_offset,lldb::offset_t length,lldb_private::ModuleSpecList & specs)82435933ddSDimitry Andric size_t ObjectFileJIT::GetModuleSpecifications(
83435933ddSDimitry Andric const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
84435933ddSDimitry Andric lldb::offset_t data_offset, lldb::offset_t file_offset,
85435933ddSDimitry Andric lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
860127ef0fSEd Maste // JIT'ed object file can't be read from a file on disk
870127ef0fSEd Maste return 0;
880127ef0fSEd Maste }
890127ef0fSEd Maste
ObjectFileJIT(const lldb::ModuleSP & module_sp,const ObjectFileJITDelegateSP & delegate_sp)900127ef0fSEd Maste ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
91435933ddSDimitry Andric const ObjectFileJITDelegateSP &delegate_sp)
92435933ddSDimitry Andric : ObjectFile(module_sp, NULL, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
93435933ddSDimitry Andric if (delegate_sp) {
940127ef0fSEd Maste m_delegate_wp = delegate_sp;
950127ef0fSEd Maste m_data.SetByteOrder(delegate_sp->GetByteOrder());
960127ef0fSEd Maste m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
970127ef0fSEd Maste }
980127ef0fSEd Maste }
990127ef0fSEd Maste
~ObjectFileJIT()100435933ddSDimitry Andric ObjectFileJIT::~ObjectFileJIT() {}
1010127ef0fSEd Maste
ParseHeader()102435933ddSDimitry Andric bool ObjectFileJIT::ParseHeader() {
1030127ef0fSEd Maste // JIT code is never in a file, nor is it required to have any header
1040127ef0fSEd Maste return false;
1050127ef0fSEd Maste }
1060127ef0fSEd Maste
GetByteOrder() const107435933ddSDimitry Andric ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
1080127ef0fSEd Maste
IsExecutable() const109435933ddSDimitry Andric bool ObjectFileJIT::IsExecutable() const { return false; }
1100127ef0fSEd Maste
GetAddressByteSize() const111435933ddSDimitry Andric uint32_t ObjectFileJIT::GetAddressByteSize() const {
1120127ef0fSEd Maste return m_data.GetAddressByteSize();
1130127ef0fSEd Maste }
1140127ef0fSEd Maste
GetSymtab()115435933ddSDimitry Andric Symtab *ObjectFileJIT::GetSymtab() {
1160127ef0fSEd Maste ModuleSP module_sp(GetModule());
117435933ddSDimitry Andric if (module_sp) {
1184bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
119435933ddSDimitry Andric if (m_symtab_ap.get() == NULL) {
1200127ef0fSEd Maste m_symtab_ap.reset(new Symtab(this));
121435933ddSDimitry Andric std::lock_guard<std::recursive_mutex> symtab_guard(
122435933ddSDimitry Andric m_symtab_ap->GetMutex());
1230127ef0fSEd Maste ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
1240127ef0fSEd Maste if (delegate_sp)
1250127ef0fSEd Maste delegate_sp->PopulateSymtab(this, *m_symtab_ap);
1260127ef0fSEd Maste // TODO: get symbols from delegate
1270127ef0fSEd Maste m_symtab_ap->Finalize();
1280127ef0fSEd Maste }
1290127ef0fSEd Maste }
1300127ef0fSEd Maste return m_symtab_ap.get();
1310127ef0fSEd Maste }
1320127ef0fSEd Maste
IsStripped()133435933ddSDimitry Andric bool ObjectFileJIT::IsStripped() {
1340127ef0fSEd Maste return false; // JIT code that is in a module is never stripped
1350127ef0fSEd Maste }
1360127ef0fSEd Maste
CreateSections(SectionList & unified_section_list)137435933ddSDimitry Andric void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
138435933ddSDimitry Andric if (!m_sections_ap.get()) {
1390127ef0fSEd Maste m_sections_ap.reset(new SectionList());
1400127ef0fSEd Maste ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
141435933ddSDimitry Andric if (delegate_sp) {
1420127ef0fSEd Maste delegate_sp->PopulateSectionList(this, *m_sections_ap);
1430127ef0fSEd Maste unified_section_list = *m_sections_ap;
1440127ef0fSEd Maste }
1450127ef0fSEd Maste }
1460127ef0fSEd Maste }
1470127ef0fSEd Maste
Dump(Stream * s)148435933ddSDimitry Andric void ObjectFileJIT::Dump(Stream *s) {
1490127ef0fSEd Maste ModuleSP module_sp(GetModule());
150435933ddSDimitry Andric if (module_sp) {
1514bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
1520127ef0fSEd Maste s->Printf("%p: ", static_cast<void *>(this));
1530127ef0fSEd Maste s->Indent();
1540127ef0fSEd Maste s->PutCString("ObjectFileJIT");
1550127ef0fSEd Maste
156*b5893f02SDimitry Andric if (ArchSpec arch = GetArchitecture())
1570127ef0fSEd Maste *s << ", arch = " << arch.GetArchitectureName();
1580127ef0fSEd Maste
1590127ef0fSEd Maste s->EOL();
1600127ef0fSEd Maste
1610127ef0fSEd Maste SectionList *sections = GetSectionList();
1620127ef0fSEd Maste if (sections)
1630127ef0fSEd Maste sections->Dump(s, NULL, true, UINT32_MAX);
1640127ef0fSEd Maste
1650127ef0fSEd Maste if (m_symtab_ap.get())
1660127ef0fSEd Maste m_symtab_ap->Dump(s, NULL, eSortOrderNone);
1670127ef0fSEd Maste }
1680127ef0fSEd Maste }
1690127ef0fSEd Maste
GetUUID(lldb_private::UUID * uuid)170435933ddSDimitry Andric bool ObjectFileJIT::GetUUID(lldb_private::UUID *uuid) {
1710127ef0fSEd Maste // TODO: maybe get from delegate, not needed for first pass
1720127ef0fSEd Maste return false;
1730127ef0fSEd Maste }
1740127ef0fSEd Maste
GetDependentModules(FileSpecList & files)175435933ddSDimitry Andric uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
1760127ef0fSEd Maste // JIT modules don't have dependencies, but they could
1770127ef0fSEd Maste // if external functions are called and we know where they are
1780127ef0fSEd Maste files.Clear();
1790127ef0fSEd Maste return 0;
1800127ef0fSEd Maste }
1810127ef0fSEd Maste
GetEntryPointAddress()182435933ddSDimitry Andric lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
1830127ef0fSEd Maste return Address();
1840127ef0fSEd Maste }
1850127ef0fSEd Maste
GetBaseAddress()186*b5893f02SDimitry Andric lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
1870127ef0fSEd Maste
CalculateType()188435933ddSDimitry Andric ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
1890127ef0fSEd Maste
CalculateStrata()190435933ddSDimitry Andric ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
1910127ef0fSEd Maste
GetArchitecture()192*b5893f02SDimitry Andric ArchSpec ObjectFileJIT::GetArchitecture() {
193*b5893f02SDimitry Andric if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
194*b5893f02SDimitry Andric return delegate_sp->GetArchitecture();
195*b5893f02SDimitry Andric return ArchSpec();
1960127ef0fSEd Maste }
1970127ef0fSEd Maste
1980127ef0fSEd Maste //------------------------------------------------------------------
1990127ef0fSEd Maste // PluginInterface protocol
2000127ef0fSEd Maste //------------------------------------------------------------------
GetPluginName()201435933ddSDimitry Andric lldb_private::ConstString ObjectFileJIT::GetPluginName() {
2020127ef0fSEd Maste return GetPluginNameStatic();
2030127ef0fSEd Maste }
2040127ef0fSEd Maste
GetPluginVersion()205435933ddSDimitry Andric uint32_t ObjectFileJIT::GetPluginVersion() { return 1; }
2060127ef0fSEd Maste
SetLoadAddress(Target & target,lldb::addr_t value,bool value_is_offset)207435933ddSDimitry Andric bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
208435933ddSDimitry Andric bool value_is_offset) {
2090127ef0fSEd Maste size_t num_loaded_sections = 0;
2100127ef0fSEd Maste SectionList *section_list = GetSectionList();
211435933ddSDimitry Andric if (section_list) {
2120127ef0fSEd Maste const size_t num_sections = section_list->GetSize();
2130127ef0fSEd Maste // "value" is an offset to apply to each top level segment
214435933ddSDimitry Andric for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
2154ba319b5SDimitry Andric // Iterate through the object file sections to find all of the sections
2164ba319b5SDimitry Andric // that size on disk (to avoid __PAGEZERO) and load them
2170127ef0fSEd Maste SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
218435933ddSDimitry Andric if (section_sp && section_sp->GetFileSize() > 0 &&
219*b5893f02SDimitry Andric !section_sp->IsThreadSpecific()) {
220435933ddSDimitry Andric if (target.GetSectionLoadList().SetSectionLoadAddress(
221435933ddSDimitry Andric section_sp, section_sp->GetFileAddress() + value))
2220127ef0fSEd Maste ++num_loaded_sections;
2230127ef0fSEd Maste }
2240127ef0fSEd Maste }
2250127ef0fSEd Maste }
2260127ef0fSEd Maste return num_loaded_sections > 0;
2270127ef0fSEd Maste }
2280127ef0fSEd Maste
ReadSectionData(lldb_private::Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)229acac075bSDimitry Andric size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
230435933ddSDimitry Andric lldb::offset_t section_offset, void *dst,
231acac075bSDimitry Andric size_t dst_len) {
2320127ef0fSEd Maste lldb::offset_t file_size = section->GetFileSize();
233435933ddSDimitry Andric if (section_offset < file_size) {
2340127ef0fSEd Maste size_t src_len = file_size - section_offset;
2350127ef0fSEd Maste if (src_len > dst_len)
2360127ef0fSEd Maste src_len = dst_len;
237435933ddSDimitry Andric const uint8_t *src =
238435933ddSDimitry Andric ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
2390127ef0fSEd Maste
2400127ef0fSEd Maste memcpy(dst, src, src_len);
2410127ef0fSEd Maste return src_len;
2420127ef0fSEd Maste }
2430127ef0fSEd Maste return 0;
2440127ef0fSEd Maste }
2450127ef0fSEd Maste
ReadSectionData(lldb_private::Section * section,lldb_private::DataExtractor & section_data)246435933ddSDimitry Andric size_t ObjectFileJIT::ReadSectionData(
247acac075bSDimitry Andric lldb_private::Section *section,
248acac075bSDimitry Andric lldb_private::DataExtractor §ion_data) {
249435933ddSDimitry Andric if (section->GetFileSize()) {
2500127ef0fSEd Maste const void *src = (void *)(uintptr_t)section->GetFileOffset();
2510127ef0fSEd Maste
252435933ddSDimitry Andric DataBufferSP data_sp(
253435933ddSDimitry Andric new lldb_private::DataBufferHeap(src, section->GetFileSize()));
254435933ddSDimitry Andric if (data_sp) {
2550127ef0fSEd Maste section_data.SetData(data_sp, 0, data_sp->GetByteSize());
2560127ef0fSEd Maste section_data.SetByteOrder(GetByteOrder());
2570127ef0fSEd Maste section_data.SetAddressByteSize(GetAddressByteSize());
2580127ef0fSEd Maste return section_data.GetByteSize();
2590127ef0fSEd Maste }
2600127ef0fSEd Maste }
2610127ef0fSEd Maste section_data.Clear();
2620127ef0fSEd Maste return 0;
2630127ef0fSEd Maste }
264