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