Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- ObjectFileJIT.cpp -------------------------------------------------===// |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ADT/StringRef.h" |
| 10 | |
| 11 | #include "ObjectFileJIT.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Debugger.h" |
| 13 | #include "lldb/Core/FileSpecList.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/ModuleSpec.h" |
| 16 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Section.h" |
| 18 | #include "lldb/Core/StreamFile.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 19 | #include "lldb/Host/Host.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/ObjectFile.h" |
| 21 | #include "lldb/Target/Platform.h" |
| 22 | #include "lldb/Target/Process.h" |
| 23 | #include "lldb/Target/SectionLoadList.h" |
| 24 | #include "lldb/Target/Target.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/DataBuffer.h" |
| 27 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Log.h" |
Pavel Labath | b809331 | 2019-03-06 14:41:43 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/RangeMap.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/StreamString.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/Timer.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/UUID.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 34 | |
| 35 | #ifndef __APPLE__ |
| 36 | #include "Utility/UuidCompatibility.h" |
| 37 | #endif |
| 38 | |
| 39 | using namespace lldb; |
| 40 | using namespace lldb_private; |
| 41 | |
Pavel Labath | e84f784 | 2019-07-31 11:57:34 +0000 | [diff] [blame] | 42 | char ObjectFileJIT::ID; |
| 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) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 69 | // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from |
| 70 | // a file |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 71 | return nullptr; |
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) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 78 | // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from |
| 79 | // memory |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 80 | return nullptr; |
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) |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 93 | : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 120 | if (m_symtab_up == nullptr) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 121 | m_symtab_up.reset(new Symtab(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | std::lock_guard<std::recursive_mutex> symtab_guard( |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 123 | m_symtab_up->GetMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); |
| 125 | if (delegate_sp) |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 126 | delegate_sp->PopulateSymtab(this, *m_symtab_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | // TODO: get symbols from delegate |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 128 | m_symtab_up->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 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 131 | return m_symtab_up.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) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 139 | if (!m_sections_up) { |
| 140 | m_sections_up.reset(new SectionList()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock()); |
| 142 | if (delegate_sp) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 143 | delegate_sp->PopulateSectionList(this, *m_sections_up); |
| 144 | unified_section_list = *m_sections_up; |
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 | |
Pavel Labath | f760f5a | 2019-01-03 10:37:19 +0000 | [diff] [blame] | 157 | if (ArchSpec arch = GetArchitecture()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | *s << ", arch = " << arch.GetArchitectureName(); |
| 159 | |
| 160 | s->EOL(); |
| 161 | |
| 162 | SectionList *sections = GetSectionList(); |
| 163 | if (sections) |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 164 | sections->Dump(s, nullptr, true, UINT32_MAX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 166 | if (m_symtab_up) |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 167 | m_symtab_up->Dump(s, nullptr, eSortOrderNone); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Pavel Labath | bd334ef | 2019-02-11 16:14:02 +0000 | [diff] [blame] | 171 | UUID ObjectFileJIT::GetUUID() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | // TODO: maybe get from delegate, not needed for first pass |
Pavel Labath | bd334ef | 2019-02-11 16:14:02 +0000 | [diff] [blame] | 173 | return UUID(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) { |
| 177 | // JIT modules don't have dependencies, but they could |
| 178 | // if external functions are called and we know where they are |
| 179 | files.Clear(); |
| 180 | return 0; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | lldb_private::Address ObjectFileJIT::GetEntryPointAddress() { |
| 184 | return Address(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Pavel Labath | d1e3fe2 | 2018-12-11 15:21:15 +0000 | [diff] [blame] | 187 | lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 192 | |
Pavel Labath | f760f5a | 2019-01-03 10:37:19 +0000 | [diff] [blame] | 193 | ArchSpec ObjectFileJIT::GetArchitecture() { |
| 194 | if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock()) |
| 195 | return delegate_sp->GetArchitecture(); |
| 196 | return ArchSpec(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 199 | // PluginInterface protocol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | lldb_private::ConstString ObjectFileJIT::GetPluginName() { |
| 201 | return GetPluginNameStatic(); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | uint32_t ObjectFileJIT::GetPluginVersion() { return 1; } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value, |
| 207 | bool value_is_offset) { |
| 208 | size_t num_loaded_sections = 0; |
| 209 | SectionList *section_list = GetSectionList(); |
| 210 | if (section_list) { |
| 211 | const size_t num_sections = section_list->GetSize(); |
| 212 | // "value" is an offset to apply to each top level segment |
| 213 | for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 214 | // Iterate through the object file sections to find all of the sections |
| 215 | // that size on disk (to avoid __PAGEZERO) and load them |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
| 217 | if (section_sp && section_sp->GetFileSize() > 0 && |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 218 | !section_sp->IsThreadSpecific()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | if (target.GetSectionLoadList().SetSectionLoadAddress( |
| 220 | section_sp, section_sp->GetFileAddress() + value)) |
| 221 | ++num_loaded_sections; |
| 222 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 223 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | } |
| 225 | return num_loaded_sections > 0; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 228 | size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | lldb::offset_t section_offset, void *dst, |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 230 | size_t dst_len) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | lldb::offset_t file_size = section->GetFileSize(); |
| 232 | if (section_offset < file_size) { |
| 233 | size_t src_len = file_size - section_offset; |
| 234 | if (src_len > dst_len) |
| 235 | src_len = dst_len; |
| 236 | const uint8_t *src = |
| 237 | ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset; |
| 238 | |
| 239 | memcpy(dst, src, src_len); |
| 240 | return src_len; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | size_t ObjectFileJIT::ReadSectionData( |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 246 | lldb_private::Section *section, |
| 247 | lldb_private::DataExtractor §ion_data) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | if (section->GetFileSize()) { |
| 249 | const void *src = (void *)(uintptr_t)section->GetFileOffset(); |
| 250 | |
Pavel Labath | 0f73709 | 2019-07-01 11:09:15 +0000 | [diff] [blame] | 251 | DataBufferSP data_sp = |
| 252 | std::make_shared<DataBufferHeap>(src, section->GetFileSize()); |
| 253 | section_data.SetData(data_sp, 0, data_sp->GetByteSize()); |
| 254 | section_data.SetByteOrder(GetByteOrder()); |
| 255 | section_data.SetAddressByteSize(GetAddressByteSize()); |
| 256 | return section_data.GetByteSize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | } |
| 258 | section_data.Clear(); |
| 259 | return 0; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 260 | } |