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" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Debugger.h" |
| 14 | #include "lldb/Core/FileSpecList.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 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" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | #include "lldb/Host/Host.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 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" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/DataBuffer.h" |
| 29 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/StreamString.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/Timer.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 34 | #include "lldb/Utility/UUID.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 35 | |
| 36 | #ifndef __APPLE__ |
| 37 | #include "Utility/UuidCompatibility.h" |
| 38 | #endif |
| 39 | |
| 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | void ObjectFileJIT::Initialize() { |
| 44 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 45 | GetPluginDescriptionStatic(), CreateInstance, |
| 46 | CreateMemoryInstance, GetModuleSpecifications); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | void ObjectFileJIT::Terminate() { |
| 50 | PluginManager::UnregisterPlugin(CreateInstance); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | lldb_private::ConstString ObjectFileJIT::GetPluginNameStatic() { |
| 54 | static ConstString g_name("jit"); |
| 55 | return g_name; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | const char *ObjectFileJIT::GetPluginDescriptionStatic() { |
| 59 | return "JIT code object file"; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 68 | // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from |
| 69 | // a file |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | return NULL; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp, |
| 74 | DataBufferSP &data_sp, |
| 75 | const ProcessSP &process_sp, |
| 76 | lldb::addr_t header_addr) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 77 | // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from |
| 78 | // memory |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | return NULL; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Davide Italiano | 407c691 | 2018-11-02 21:59:14 +0000 | [diff] [blame] | 127 | m_symtab_ap->Finalize(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 128 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | } |
| 130 | return m_symtab_ap.get(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | bool ObjectFileJIT::IsStripped() { |
| 134 | return false; // JIT code that is in a module is never stripped |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 144 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | |
Pavel Labath | f760f5a | 2019-01-03 10:37:19 +0000 | [diff] [blame^] | 156 | if (ArchSpec arch = GetArchitecture()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | *s << ", arch = " << arch.GetArchitectureName(); |
| 158 | |
| 159 | s->EOL(); |
| 160 | |
| 161 | SectionList *sections = GetSectionList(); |
| 162 | if (sections) |
| 163 | sections->Dump(s, NULL, true, UINT32_MAX); |
| 164 | |
| 165 | if (m_symtab_ap.get()) |
| 166 | m_symtab_ap->Dump(s, NULL, eSortOrderNone); |
| 167 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | bool ObjectFileJIT::GetUUID(lldb_private::UUID *uuid) { |
| 171 | // TODO: maybe get from delegate, not needed for first pass |
| 172 | return false; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) { |
| 176 | // JIT modules don't have dependencies, but they could |
| 177 | // if external functions are called and we know where they are |
| 178 | files.Clear(); |
| 179 | return 0; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | lldb_private::Address ObjectFileJIT::GetEntryPointAddress() { |
| 183 | return Address(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Pavel Labath | d1e3fe2 | 2018-12-11 15:21:15 +0000 | [diff] [blame] | 186 | lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 187 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; } |
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::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 191 | |
Pavel Labath | f760f5a | 2019-01-03 10:37:19 +0000 | [diff] [blame^] | 192 | ArchSpec ObjectFileJIT::GetArchitecture() { |
| 193 | if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock()) |
| 194 | return delegate_sp->GetArchitecture(); |
| 195 | return ArchSpec(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | //------------------------------------------------------------------ |
| 199 | // PluginInterface protocol |
| 200 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | lldb_private::ConstString ObjectFileJIT::GetPluginName() { |
| 202 | return GetPluginNameStatic(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | uint32_t ObjectFileJIT::GetPluginVersion() { return 1; } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value, |
| 208 | bool value_is_offset) { |
| 209 | size_t num_loaded_sections = 0; |
| 210 | SectionList *section_list = GetSectionList(); |
| 211 | if (section_list) { |
| 212 | const size_t num_sections = section_list->GetSize(); |
| 213 | // "value" is an offset to apply to each top level segment |
| 214 | for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 215 | // Iterate through the object file sections to find all of the sections |
| 216 | // that size on disk (to avoid __PAGEZERO) and load them |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
| 218 | if (section_sp && section_sp->GetFileSize() > 0 && |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 219 | !section_sp->IsThreadSpecific()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | if (target.GetSectionLoadList().SetSectionLoadAddress( |
| 221 | section_sp, section_sp->GetFileAddress() + value)) |
| 222 | ++num_loaded_sections; |
| 223 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 224 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | } |
| 226 | return num_loaded_sections > 0; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 229 | size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | lldb::offset_t section_offset, void *dst, |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 231 | size_t dst_len) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | lldb::offset_t file_size = section->GetFileSize(); |
| 233 | if (section_offset < file_size) { |
| 234 | size_t src_len = file_size - section_offset; |
| 235 | if (src_len > dst_len) |
| 236 | src_len = dst_len; |
| 237 | const uint8_t *src = |
| 238 | ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset; |
| 239 | |
| 240 | memcpy(dst, src, src_len); |
| 241 | return src_len; |
| 242 | } |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | size_t ObjectFileJIT::ReadSectionData( |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 247 | lldb_private::Section *section, |
| 248 | lldb_private::DataExtractor §ion_data) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | if (section->GetFileSize()) { |
| 250 | const void *src = (void *)(uintptr_t)section->GetFileOffset(); |
| 251 | |
| 252 | DataBufferSP data_sp( |
| 253 | new lldb_private::DataBufferHeap(src, section->GetFileSize())); |
| 254 | if (data_sp) { |
| 255 | section_data.SetData(data_sp, 0, data_sp->GetByteSize()); |
| 256 | section_data.SetByteOrder(GetByteOrder()); |
| 257 | section_data.SetAddressByteSize(GetAddressByteSize()); |
| 258 | return section_data.GetByteSize(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 259 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | } |
| 261 | section_data.Clear(); |
| 262 | return 0; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 263 | } |