Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 1 | //===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===// |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 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 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 10 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Sean Callanan | 5deb06e | 2016-09-26 20:18:51 +0000 | [diff] [blame] | 11 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Constants.h" |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 13 | #include "llvm/IR/LLVMContext.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Module.h" |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 15 | #include "llvm/Support/SourceMgr.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Debugger.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Disassembler.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Module.h" |
| 21 | #include "lldb/Core/Section.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/IRExecutionUnit.h" |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/CompileUnit.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/SymbolContext.h" |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/SymbolFile.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/SymbolVendor.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 27 | #include "lldb/Target/ExecutionContext.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 28 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/DataBufferHeap.h" |
| 31 | #include "lldb/Utility/DataExtractor.h" |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/Log.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 34 | |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 35 | #include "lldb/../../source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" |
Zachary Turner | 8e8d4b9 | 2018-05-23 23:56:09 +0000 | [diff] [blame^] | 36 | #include "lldb/../../source/Plugins/ObjectFile/JIT/ObjectFileJIT.h" |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 37 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 38 | using namespace lldb_private; |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_ap, |
| 41 | std::unique_ptr<llvm::Module> &module_ap, |
| 42 | ConstString &name, |
| 43 | const lldb::TargetSP &target_sp, |
| 44 | const SymbolContext &sym_ctx, |
| 45 | std::vector<std::string> &cpu_features) |
| 46 | : IRMemoryMap(target_sp), m_context_ap(context_ap.release()), |
| 47 | m_module_ap(module_ap.release()), m_module(m_module_ap.get()), |
| 48 | m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx), |
| 49 | m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS), |
| 50 | m_function_end_load_addr(LLDB_INVALID_ADDRESS), |
| 51 | m_reported_allocations(false) {} |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 54 | Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | const bool zero_memory = false; |
| 56 | lldb::addr_t allocation_process_addr = |
| 57 | Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable, |
| 58 | eAllocationPolicyMirror, zero_memory, error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | if (!error.Success()) |
| 61 | return LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | WriteMemory(allocation_process_addr, bytes, size, error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | if (!error.Success()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 66 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | Free(allocation_process_addr, err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | return LLDB_INVALID_ADDRESS; |
| 70 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | if (Log *log = |
| 73 | lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) { |
| 74 | DataBufferHeap my_buffer(size, 0); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 75 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | if (err.Success()) { |
| 79 | DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), |
| 80 | lldb::eByteOrderBig, 8); |
| 81 | my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), |
| 82 | allocation_process_addr, 16, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 83 | DataExtractor::TypeUInt8); |
| 84 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | return allocation_process_addr; |
| 88 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | void IRExecutionUnit::FreeNow(lldb::addr_t allocation) { |
| 91 | if (allocation == LLDB_INVALID_ADDRESS) |
| 92 | return; |
| 93 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 94 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | |
| 96 | Free(allocation, err); |
| 97 | } |
| 98 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 99 | Status IRExecutionUnit::DisassembleFunction(Stream &stream, |
| 100 | lldb::ProcessSP &process_wp) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 102 | |
| 103 | ExecutionContext exe_ctx(process_wp); |
| 104 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 105 | Status ret; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | |
| 107 | ret.Clear(); |
| 108 | |
| 109 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 110 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
| 111 | |
| 112 | for (JittedFunction &function : m_jitted_functions) { |
| 113 | if (function.m_name == m_name) { |
| 114 | func_local_addr = function.m_local_addr; |
| 115 | func_remote_addr = function.m_remote_addr; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (func_local_addr == LLDB_INVALID_ADDRESS) { |
| 120 | ret.SetErrorToGenericError(); |
| 121 | ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", |
| 122 | m_name.AsCString()); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 123 | return ret; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (log) |
| 127 | log->Printf("Found function, has local address 0x%" PRIx64 |
| 128 | " and remote address 0x%" PRIx64, |
| 129 | (uint64_t)func_local_addr, (uint64_t)func_remote_addr); |
| 130 | |
| 131 | std::pair<lldb::addr_t, lldb::addr_t> func_range; |
| 132 | |
| 133 | func_range = GetRemoteRangeForLocal(func_local_addr); |
| 134 | |
| 135 | if (func_range.first == 0 && func_range.second == 0) { |
| 136 | ret.SetErrorToGenericError(); |
| 137 | ret.SetErrorStringWithFormat("Couldn't find code range for function %s", |
| 138 | m_name.AsCString()); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | if (log) |
| 143 | log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", |
| 144 | func_range.first, func_range.second); |
| 145 | |
| 146 | Target *target = exe_ctx.GetTargetPtr(); |
| 147 | if (!target) { |
| 148 | ret.SetErrorToGenericError(); |
| 149 | ret.SetErrorString("Couldn't find the target"); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0)); |
| 154 | |
| 155 | Process *process = exe_ctx.GetProcessPtr(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 156 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), |
| 158 | buffer_sp->GetByteSize(), err); |
| 159 | |
| 160 | if (!err.Success()) { |
| 161 | ret.SetErrorToGenericError(); |
| 162 | ret.SetErrorStringWithFormat("Couldn't read from process: %s", |
| 163 | err.AsCString("unknown error")); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | ArchSpec arch(target->GetArchitecture()); |
| 168 | |
| 169 | const char *plugin_name = NULL; |
| 170 | const char *flavor_string = NULL; |
| 171 | lldb::DisassemblerSP disassembler_sp = |
| 172 | Disassembler::FindPlugin(arch, flavor_string, plugin_name); |
| 173 | |
| 174 | if (!disassembler_sp) { |
| 175 | ret.SetErrorToGenericError(); |
| 176 | ret.SetErrorStringWithFormat( |
| 177 | "Unable to find disassembler plug-in for %s architecture.", |
| 178 | arch.GetArchitectureName()); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | if (!process) { |
| 183 | ret.SetErrorToGenericError(); |
| 184 | ret.SetErrorString("Couldn't find the process"); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | DataExtractor extractor(buffer_sp, process->GetByteOrder(), |
| 189 | target->GetArchitecture().GetAddressByteSize()); |
| 190 | |
| 191 | if (log) { |
| 192 | log->Printf("Function data has contents:"); |
| 193 | extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16, |
| 194 | DataExtractor::TypeUInt8); |
| 195 | } |
| 196 | |
| 197 | disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0, |
| 198 | UINT32_MAX, false, false); |
| 199 | |
| 200 | InstructionList &instruction_list = disassembler_sp->GetInstructionList(); |
| 201 | instruction_list.Dump(&stream, true, true, &exe_ctx); |
| 202 | return ret; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, |
| 206 | void *Context, unsigned LocCookie) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 207 | Status *err = static_cast<Status *>(Context); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | if (err && err->Success()) { |
| 210 | err->SetErrorToGenericError(); |
| 211 | err->SetErrorStringWithFormat("Inline assembly error: %s", |
| 212 | diagnostic.getMessage().str().c_str()); |
| 213 | } |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | void IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) { |
| 217 | m_failed_lookups.push_back(name); |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 220 | void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | lldb::addr_t &func_end) { |
| 222 | lldb::ProcessSP process_sp(GetProcessWP().lock()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | static std::recursive_mutex s_runnable_info_mutex; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | func_addr = LLDB_INVALID_ADDRESS; |
| 227 | func_end = LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | if (!process_sp) { |
| 230 | error.SetErrorToGenericError(); |
| 231 | error.SetErrorString("Couldn't write the JIT compiled code into the " |
| 232 | "process because the process is invalid"); |
| 233 | return; |
| 234 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | if (m_did_jit) { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 237 | func_addr = m_function_load_addr; |
| 238 | func_end = m_function_end_load_addr; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 239 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 240 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | }; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 244 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | m_did_jit = true; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 246 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 248 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | std::string error_string; |
| 250 | |
| 251 | if (log) { |
| 252 | std::string s; |
| 253 | llvm::raw_string_ostream oss(s); |
| 254 | |
| 255 | m_module->print(oss, NULL); |
| 256 | |
| 257 | oss.flush(); |
| 258 | |
| 259 | log->Printf("Module being sent to JIT: \n%s", s.c_str()); |
| 260 | } |
| 261 | |
| 262 | llvm::Triple triple(m_module->getTargetTriple()); |
| 263 | llvm::Reloc::Model relocModel; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | |
| 265 | if (triple.isOSBinFormatELF()) { |
| 266 | relocModel = llvm::Reloc::Static; |
| 267 | } else { |
| 268 | relocModel = llvm::Reloc::PIC_; |
| 269 | } |
| 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, |
| 272 | &error); |
| 273 | |
| 274 | llvm::EngineBuilder builder(std::move(m_module_ap)); |
| 275 | |
| 276 | builder.setEngineKind(llvm::EngineKind::JIT) |
| 277 | .setErrorStr(&error_string) |
| 278 | .setRelocationModel(relocModel) |
| 279 | .setMCJITMemoryManager( |
| 280 | std::unique_ptr<MemoryManager>(new MemoryManager(*this))) |
Pavel Labath | d739636 | 2017-11-13 14:03:17 +0000 | [diff] [blame] | 281 | .setOptLevel(llvm::CodeGenOpt::Less); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | |
| 283 | llvm::StringRef mArch; |
| 284 | llvm::StringRef mCPU; |
| 285 | llvm::SmallVector<std::string, 0> mAttrs; |
| 286 | |
| 287 | for (std::string &feature : m_cpu_features) |
| 288 | mAttrs.push_back(feature); |
| 289 | |
| 290 | llvm::TargetMachine *target_machine = |
| 291 | builder.selectTarget(triple, mArch, mCPU, mAttrs); |
| 292 | |
| 293 | m_execution_engine_ap.reset(builder.create(target_machine)); |
| 294 | |
| 295 | m_strip_underscore = |
| 296 | (m_execution_engine_ap->getDataLayout().getGlobalPrefix() == '_'); |
| 297 | |
| 298 | if (!m_execution_engine_ap.get()) { |
| 299 | error.SetErrorToGenericError(); |
| 300 | error.SetErrorStringWithFormat("Couldn't JIT the function: %s", |
| 301 | error_string.c_str()); |
| 302 | return; |
| 303 | } |
| 304 | |
Sean Callanan | 5deb06e | 2016-09-26 20:18:51 +0000 | [diff] [blame] | 305 | class ObjectDumper : public llvm::ObjectCache { |
| 306 | public: |
| 307 | void notifyObjectCompiled(const llvm::Module *module, |
| 308 | llvm::MemoryBufferRef object) override { |
| 309 | int fd = 0; |
| 310 | llvm::SmallVector<char, 256> result_path; |
| 311 | std::string object_name_model = |
| 312 | "jit-object-" + module->getModuleIdentifier() + "-%%%.o"; |
| 313 | (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path); |
| 314 | llvm::raw_fd_ostream fds(fd, true); |
| 315 | fds.write(object.getBufferStart(), object.getBufferSize()); |
| 316 | } |
| 317 | |
| 318 | std::unique_ptr<llvm::MemoryBuffer> |
| 319 | getObject(const llvm::Module *module) override { |
| 320 | // Return nothing - we're just abusing the object-cache mechanism to dump |
| 321 | // objects. |
| 322 | return nullptr; |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | if (process_sp->GetTarget().GetEnableSaveObjects()) { |
| 327 | m_object_cache_ap = llvm::make_unique<ObjectDumper>(); |
| 328 | m_execution_engine_ap->setObjectCache(m_object_cache_ap.get()); |
| 329 | } |
| 330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | // Make sure we see all sections, including ones that don't have |
| 332 | // relocations... |
| 333 | m_execution_engine_ap->setProcessAllSections(true); |
| 334 | |
| 335 | m_execution_engine_ap->DisableLazyCompilation(); |
| 336 | |
| 337 | for (llvm::Function &function : *m_module) { |
| 338 | if (function.isDeclaration() || function.hasPrivateLinkage()) |
| 339 | continue; |
| 340 | |
| 341 | const bool external = |
| 342 | function.hasExternalLinkage() || function.hasLinkOnceODRLinkage(); |
| 343 | |
| 344 | void *fun_ptr = m_execution_engine_ap->getPointerToFunction(&function); |
| 345 | |
| 346 | if (!error.Success()) { |
| 347 | // We got an error through our callback! |
| 348 | return; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 349 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | if (!fun_ptr) { |
| 352 | error.SetErrorToGenericError(); |
| 353 | error.SetErrorStringWithFormat( |
| 354 | "'%s' was in the JITted module but wasn't lowered", |
| 355 | function.getName().str().c_str()); |
| 356 | return; |
| 357 | } |
| 358 | m_jitted_functions.push_back(JittedFunction( |
| 359 | function.getName().str().c_str(), external, (lldb::addr_t)fun_ptr)); |
| 360 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 361 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | CommitAllocations(process_sp); |
| 363 | ReportAllocations(*m_execution_engine_ap); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 364 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | // We have to do this after calling ReportAllocations because for the MCJIT, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 366 | // getGlobalValueAddress will cause the JIT to perform all relocations. That |
| 367 | // can only be done once, and has to happen after we do the remapping from |
| 368 | // local -> remote. That means we don't know the local address of the |
| 369 | // Variables, but we don't need that for anything, so that's okay. |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 370 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this]( |
| 372 | llvm::GlobalValue &val) { |
| 373 | if (val.hasExternalLinkage() && !val.isDeclaration()) { |
| 374 | uint64_t var_ptr_addr = |
| 375 | m_execution_engine_ap->getGlobalValueAddress(val.getName().str()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 378 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | // This is a really unfortunae API that sometimes returns local addresses |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 380 | // and sometimes returns remote addresses, based on whether the variable |
| 381 | // was relocated during ReportAllocations or not. |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | if (remote_addr == LLDB_INVALID_ADDRESS) { |
| 384 | remote_addr = var_ptr_addr; |
| 385 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 386 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | if (var_ptr_addr != 0) |
| 388 | m_jitted_global_variables.push_back(JittedGlobalVariable( |
| 389 | val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr)); |
| 390 | } |
| 391 | }; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) { |
| 394 | RegisterOneValue(global_var); |
| 395 | } |
| 396 | |
| 397 | for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) { |
| 398 | RegisterOneValue(global_alias); |
| 399 | } |
| 400 | |
| 401 | WriteData(process_sp); |
| 402 | |
| 403 | if (m_failed_lookups.size()) { |
| 404 | StreamString ss; |
| 405 | |
| 406 | ss.PutCString("Couldn't lookup symbols:\n"); |
| 407 | |
| 408 | bool emitNewLine = false; |
| 409 | |
| 410 | for (const ConstString &failed_lookup : m_failed_lookups) { |
| 411 | if (emitNewLine) |
| 412 | ss.PutCString("\n"); |
| 413 | emitNewLine = true; |
| 414 | ss.PutCString(" "); |
| 415 | ss.PutCString(Mangled(failed_lookup) |
| 416 | .GetDemangledName(lldb::eLanguageTypeObjC_plus_plus) |
| 417 | .AsCString()); |
| 418 | } |
| 419 | |
| 420 | m_failed_lookups.clear(); |
| 421 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 422 | error.SetErrorString(ss.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | m_function_load_addr = LLDB_INVALID_ADDRESS; |
| 428 | m_function_end_load_addr = LLDB_INVALID_ADDRESS; |
| 429 | |
| 430 | for (JittedFunction &jitted_function : m_jitted_functions) { |
| 431 | jitted_function.m_remote_addr = |
| 432 | GetRemoteAddressForLocal(jitted_function.m_local_addr); |
| 433 | |
| 434 | if (!m_name.IsEmpty() && jitted_function.m_name == m_name) { |
| 435 | AddrRange func_range = |
| 436 | GetRemoteRangeForLocal(jitted_function.m_local_addr); |
| 437 | m_function_end_load_addr = func_range.first + func_range.second; |
| 438 | m_function_load_addr = jitted_function.m_remote_addr; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (log) { |
| 443 | log->Printf("Code can be run in the target."); |
| 444 | |
| 445 | StreamString disassembly_stream; |
| 446 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 447 | Status err = DisassembleFunction(disassembly_stream, process_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 448 | |
| 449 | if (!err.Success()) { |
| 450 | log->Printf("Couldn't disassemble function : %s", |
| 451 | err.AsCString("unknown error")); |
| 452 | } else { |
| 453 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 454 | } |
| 455 | |
| 456 | log->Printf("Sections: "); |
| 457 | for (AllocationRecord &record : m_records) { |
| 458 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 459 | record.dump(log); |
| 460 | |
| 461 | DataBufferHeap my_buffer(record.m_size, 0); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 462 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | ReadMemory(my_buffer.GetBytes(), record.m_process_address, |
| 464 | record.m_size, err); |
| 465 | |
| 466 | if (err.Success()) { |
| 467 | DataExtractor my_extractor(my_buffer.GetBytes(), |
| 468 | my_buffer.GetByteSize(), |
| 469 | lldb::eByteOrderBig, 8); |
| 470 | my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), |
| 471 | record.m_process_address, 16, |
| 472 | DataExtractor::TypeUInt8); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 473 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | } else { |
| 475 | record.dump(log); |
| 476 | |
| 477 | DataExtractor my_extractor((const void *)record.m_host_address, |
| 478 | record.m_size, lldb::eByteOrderBig, 8); |
| 479 | my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16, |
| 480 | DataExtractor::TypeUInt8); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | func_addr = m_function_load_addr; |
| 486 | func_end = m_function_end_load_addr; |
| 487 | |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | IRExecutionUnit::~IRExecutionUnit() { |
| 492 | m_module_ap.reset(); |
| 493 | m_execution_engine_ap.reset(); |
| 494 | m_context_ap.reset(); |
| 495 | } |
| 496 | |
| 497 | IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent) |
| 498 | : m_default_mm_ap(new llvm::SectionMemoryManager()), m_parent(parent) {} |
| 499 | |
| 500 | IRExecutionUnit::MemoryManager::~MemoryManager() {} |
| 501 | |
| 502 | lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName( |
| 503 | const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) { |
| 504 | lldb::SectionType sect_type = lldb::eSectionTypeCode; |
| 505 | switch (alloc_kind) { |
| 506 | case AllocationKind::Stub: |
| 507 | sect_type = lldb::eSectionTypeCode; |
| 508 | break; |
| 509 | case AllocationKind::Code: |
| 510 | sect_type = lldb::eSectionTypeCode; |
| 511 | break; |
| 512 | case AllocationKind::Data: |
| 513 | sect_type = lldb::eSectionTypeData; |
| 514 | break; |
| 515 | case AllocationKind::Global: |
| 516 | sect_type = lldb::eSectionTypeData; |
| 517 | break; |
| 518 | case AllocationKind::Bytes: |
| 519 | sect_type = lldb::eSectionTypeOther; |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | if (!name.empty()) { |
| 524 | if (name.equals("__text") || name.equals(".text")) |
| 525 | sect_type = lldb::eSectionTypeCode; |
| 526 | else if (name.equals("__data") || name.equals(".data")) |
| 527 | sect_type = lldb::eSectionTypeCode; |
| 528 | else if (name.startswith("__debug_") || name.startswith(".debug_")) { |
| 529 | const uint32_t name_idx = name[0] == '_' ? 8 : 7; |
| 530 | llvm::StringRef dwarf_name(name.substr(name_idx)); |
| 531 | switch (dwarf_name[0]) { |
| 532 | case 'a': |
| 533 | if (dwarf_name.equals("abbrev")) |
| 534 | sect_type = lldb::eSectionTypeDWARFDebugAbbrev; |
| 535 | else if (dwarf_name.equals("aranges")) |
| 536 | sect_type = lldb::eSectionTypeDWARFDebugAranges; |
| 537 | else if (dwarf_name.equals("addr")) |
| 538 | sect_type = lldb::eSectionTypeDWARFDebugAddr; |
| 539 | break; |
| 540 | |
| 541 | case 'f': |
| 542 | if (dwarf_name.equals("frame")) |
| 543 | sect_type = lldb::eSectionTypeDWARFDebugFrame; |
| 544 | break; |
| 545 | |
| 546 | case 'i': |
| 547 | if (dwarf_name.equals("info")) |
| 548 | sect_type = lldb::eSectionTypeDWARFDebugInfo; |
| 549 | break; |
| 550 | |
| 551 | case 'l': |
| 552 | if (dwarf_name.equals("line")) |
| 553 | sect_type = lldb::eSectionTypeDWARFDebugLine; |
| 554 | else if (dwarf_name.equals("loc")) |
| 555 | sect_type = lldb::eSectionTypeDWARFDebugLoc; |
| 556 | break; |
| 557 | |
| 558 | case 'm': |
| 559 | if (dwarf_name.equals("macinfo")) |
| 560 | sect_type = lldb::eSectionTypeDWARFDebugMacInfo; |
| 561 | break; |
| 562 | |
| 563 | case 'p': |
| 564 | if (dwarf_name.equals("pubnames")) |
| 565 | sect_type = lldb::eSectionTypeDWARFDebugPubNames; |
| 566 | else if (dwarf_name.equals("pubtypes")) |
| 567 | sect_type = lldb::eSectionTypeDWARFDebugPubTypes; |
| 568 | break; |
| 569 | |
| 570 | case 's': |
| 571 | if (dwarf_name.equals("str")) |
| 572 | sect_type = lldb::eSectionTypeDWARFDebugStr; |
| 573 | else if (dwarf_name.equals("str_offsets")) |
| 574 | sect_type = lldb::eSectionTypeDWARFDebugStrOffsets; |
| 575 | break; |
| 576 | |
| 577 | case 'r': |
| 578 | if (dwarf_name.equals("ranges")) |
| 579 | sect_type = lldb::eSectionTypeDWARFDebugRanges; |
| 580 | break; |
| 581 | |
| 582 | default: |
| 583 | break; |
| 584 | } |
Davide Italiano | 45925d7 | 2018-01-04 23:37:18 +0000 | [diff] [blame] | 585 | } else if (name.startswith("__apple_") || name.startswith(".apple_")) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | sect_type = lldb::eSectionTypeInvalid; |
Davide Italiano | 45925d7 | 2018-01-04 23:37:18 +0000 | [diff] [blame] | 587 | else if (name.equals("__objc_imageinfo")) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | sect_type = lldb::eSectionTypeOther; |
| 589 | } |
| 590 | return sect_type; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 593 | uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection( |
| 594 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 595 | llvm::StringRef SectionName) { |
| 596 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 597 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 598 | uint8_t *return_value = m_default_mm_ap->allocateCodeSection( |
| 599 | Size, Alignment, SectionID, SectionName); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 600 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | m_parent.m_records.push_back(AllocationRecord( |
| 602 | (uintptr_t)return_value, |
| 603 | lldb::ePermissionsReadable | lldb::ePermissionsExecutable, |
| 604 | GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size, |
| 605 | Alignment, SectionID, SectionName.str().c_str())); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 606 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 607 | if (log) { |
| 608 | log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 |
| 609 | ", Alignment=%u, SectionID=%u) = %p", |
| 610 | (uint64_t)Size, Alignment, SectionID, (void *)return_value); |
| 611 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 612 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 613 | if (m_parent.m_reported_allocations) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 614 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 615 | lldb::ProcessSP process_sp = |
| 616 | m_parent.GetBestExecutionContextScope()->CalculateProcess(); |
| 617 | |
| 618 | m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back()); |
| 619 | } |
| 620 | |
| 621 | return return_value; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 624 | uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection( |
| 625 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 626 | llvm::StringRef SectionName, bool IsReadOnly) { |
| 627 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 628 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | uint8_t *return_value = m_default_mm_ap->allocateDataSection( |
| 630 | Size, Alignment, SectionID, SectionName, IsReadOnly); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 631 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 632 | uint32_t permissions = lldb::ePermissionsReadable; |
| 633 | if (!IsReadOnly) |
| 634 | permissions |= lldb::ePermissionsWritable; |
| 635 | m_parent.m_records.push_back(AllocationRecord( |
| 636 | (uintptr_t)return_value, permissions, |
| 637 | GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size, |
| 638 | Alignment, SectionID, SectionName.str().c_str())); |
| 639 | if (log) { |
| 640 | log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 |
| 641 | ", Alignment=%u, SectionID=%u) = %p", |
| 642 | (uint64_t)Size, Alignment, SectionID, (void *)return_value); |
| 643 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 644 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 645 | if (m_parent.m_reported_allocations) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 646 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | lldb::ProcessSP process_sp = |
| 648 | m_parent.GetBestExecutionContextScope()->CalculateProcess(); |
| 649 | |
| 650 | m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back()); |
| 651 | } |
| 652 | |
| 653 | return return_value; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 656 | static ConstString |
| 657 | FindBestAlternateMangledName(const ConstString &demangled, |
| 658 | const lldb::LanguageType &lang_type, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 659 | const SymbolContext &sym_ctx) { |
| 660 | CPlusPlusLanguage::MethodName cpp_name(demangled); |
| 661 | std::string scope_qualified_name = cpp_name.GetScopeQualifiedName(); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 662 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 663 | if (!scope_qualified_name.size()) |
| 664 | return ConstString(); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 665 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 666 | if (!sym_ctx.module_sp) |
| 667 | return ConstString(); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 668 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor(); |
| 670 | if (!sym_vendor) |
| 671 | return ConstString(); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 672 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 673 | lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 674 | if (!sym_file) |
| 675 | return ConstString(); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 676 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 677 | std::vector<ConstString> alternates; |
| 678 | sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 679 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 680 | std::vector<ConstString> param_and_qual_matches; |
| 681 | std::vector<ConstString> param_matches; |
| 682 | for (size_t i = 0; i < alternates.size(); i++) { |
| 683 | ConstString alternate_mangled_name = alternates[i]; |
| 684 | Mangled mangled(alternate_mangled_name, true); |
| 685 | ConstString demangled = mangled.GetDemangledName(lang_type); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 687 | CPlusPlusLanguage::MethodName alternate_cpp_name(demangled); |
| 688 | if (!cpp_name.IsValid()) |
| 689 | continue; |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 690 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) { |
| 692 | if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers()) |
| 693 | param_and_qual_matches.push_back(alternate_mangled_name); |
| 694 | else |
| 695 | param_matches.push_back(alternate_mangled_name); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 696 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 697 | } |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 698 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 699 | if (param_and_qual_matches.size()) |
| 700 | return param_and_qual_matches[0]; // It is assumed that there will be only |
| 701 | // one! |
| 702 | else if (param_matches.size()) |
| 703 | return param_matches[0]; // Return one of them as a best match |
| 704 | else |
| 705 | return ConstString(); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 708 | struct IRExecutionUnit::SearchSpec { |
| 709 | ConstString name; |
| 710 | uint32_t mask; |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeFull) |
| 713 | : name(n), mask(m) {} |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 714 | }; |
| 715 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 716 | void IRExecutionUnit::CollectCandidateCNames( |
| 717 | std::vector<IRExecutionUnit::SearchSpec> &C_specs, |
| 718 | const ConstString &name) { |
| 719 | if (m_strip_underscore && name.AsCString()[0] == '_') |
| 720 | C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1])); |
| 721 | C_specs.push_back(SearchSpec(name)); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 724 | void IRExecutionUnit::CollectCandidateCPlusPlusNames( |
| 725 | std::vector<IRExecutionUnit::SearchSpec> &CPP_specs, |
| 726 | const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) { |
| 727 | for (const SearchSpec &C_spec : C_specs) { |
| 728 | const ConstString &name = C_spec.name; |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 730 | if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) { |
| 731 | Mangled mangled(name, true); |
| 732 | ConstString demangled = |
| 733 | mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus); |
Chaoren Lin | 74a1fc6 | 2016-02-24 03:15:21 +0000 | [diff] [blame] | 734 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 735 | if (demangled) { |
| 736 | ConstString best_alternate_mangled_name = FindBestAlternateMangledName( |
| 737 | demangled, lldb::eLanguageTypeC_plus_plus, sc); |
Chaoren Lin | 74a1fc6 | 2016-02-24 03:15:21 +0000 | [diff] [blame] | 738 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 739 | if (best_alternate_mangled_name) { |
| 740 | CPP_specs.push_back(best_alternate_mangled_name); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 741 | } |
Chaoren Lin | 74a1fc6 | 2016-02-24 03:15:21 +0000 | [diff] [blame] | 742 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 743 | CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull)); |
| 744 | } |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 745 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | |
Luke Drummond | f5bb1d6 | 2016-12-19 17:22:44 +0000 | [diff] [blame] | 747 | std::set<ConstString> alternates; |
| 748 | CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates); |
| 749 | CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | } |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | void IRExecutionUnit::CollectFallbackNames( |
| 754 | std::vector<SearchSpec> &fallback_specs, |
| 755 | const std::vector<SearchSpec> &C_specs) { |
| 756 | // As a last-ditch fallback, try the base name for C++ names. It's terrible, |
| 757 | // but the DWARF doesn't always encode "extern C" correctly. |
| 758 | |
| 759 | for (const SearchSpec &C_spec : C_specs) { |
| 760 | const ConstString &name = C_spec.name; |
| 761 | |
| 762 | if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) { |
| 763 | Mangled mangled_name(name); |
| 764 | ConstString demangled_name = |
| 765 | mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus); |
| 766 | if (!demangled_name.IsEmpty()) { |
| 767 | const char *demangled_cstr = demangled_name.AsCString(); |
| 768 | const char *lparen_loc = strchr(demangled_cstr, '('); |
| 769 | if (lparen_loc) { |
| 770 | llvm::StringRef base_name(demangled_cstr, |
| 771 | lparen_loc - demangled_cstr); |
| 772 | fallback_specs.push_back(ConstString(base_name)); |
Sean Callanan | 34ab28a | 2016-06-02 17:59:47 +0000 | [diff] [blame] | 773 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | } |
Sean Callanan | 34ab28a | 2016-06-02 17:59:47 +0000 | [diff] [blame] | 775 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 776 | } |
Sean Callanan | 34ab28a | 2016-06-02 17:59:47 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 779 | lldb::addr_t IRExecutionUnit::FindInSymbols( |
| 780 | const std::vector<IRExecutionUnit::SearchSpec> &specs, |
| 781 | const lldb_private::SymbolContext &sc) { |
| 782 | Target *target = sc.target_sp.get(); |
Sean Callanan | 34ab28a | 2016-06-02 17:59:47 +0000 | [diff] [blame] | 783 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 784 | if (!target) { |
| 785 | // we shouldn't be doing any symbol lookup at all without a target |
| 786 | return LLDB_INVALID_ADDRESS; |
| 787 | } |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 788 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 789 | for (const SearchSpec &spec : specs) { |
| 790 | SymbolContextList sc_list; |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 791 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 792 | lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS; |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 793 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 794 | std::function<bool(lldb::addr_t &, SymbolContextList &, |
| 795 | const lldb_private::SymbolContext &)> |
| 796 | get_external_load_address = [&best_internal_load_address, target]( |
| 797 | lldb::addr_t &load_address, SymbolContextList &sc_list, |
| 798 | const lldb_private::SymbolContext &sc) -> lldb::addr_t { |
| 799 | load_address = LLDB_INVALID_ADDRESS; |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 800 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 801 | for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) { |
| 802 | SymbolContext candidate_sc; |
| 803 | |
| 804 | sc_list.GetContextAtIndex(si, candidate_sc); |
| 805 | |
| 806 | const bool is_external = |
| 807 | (candidate_sc.function) || |
| 808 | (candidate_sc.symbol && candidate_sc.symbol->IsExternal()); |
| 809 | if (candidate_sc.symbol) { |
| 810 | load_address = candidate_sc.symbol->ResolveCallableAddress(*target); |
| 811 | |
| 812 | if (load_address == LLDB_INVALID_ADDRESS) { |
| 813 | if (target->GetProcessSP()) |
| 814 | load_address = |
| 815 | candidate_sc.symbol->GetAddress().GetLoadAddress(target); |
| 816 | else |
| 817 | load_address = candidate_sc.symbol->GetAddress().GetFileAddress(); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) { |
| 822 | if (target->GetProcessSP()) |
| 823 | load_address = candidate_sc.function->GetAddressRange() |
| 824 | .GetBaseAddress() |
| 825 | .GetLoadAddress(target); |
| 826 | else |
| 827 | load_address = candidate_sc.function->GetAddressRange() |
| 828 | .GetBaseAddress() |
| 829 | .GetFileAddress(); |
| 830 | } |
| 831 | |
| 832 | if (load_address != LLDB_INVALID_ADDRESS) { |
| 833 | if (is_external) { |
| 834 | return true; |
| 835 | } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) { |
| 836 | best_internal_load_address = load_address; |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 837 | load_address = LLDB_INVALID_ADDRESS; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 841 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | return false; |
| 843 | }; |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 844 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 845 | if (sc.module_sp) { |
| 846 | sc.module_sp->FindFunctions(spec.name, NULL, spec.mask, |
| 847 | true, // include_symbols |
| 848 | false, // include_inlines |
| 849 | true, // append |
| 850 | sc_list); |
| 851 | } |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 852 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 853 | lldb::addr_t load_address = LLDB_INVALID_ADDRESS; |
Ted Woodward | eee554d | 2016-03-09 22:05:17 +0000 | [diff] [blame] | 854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | if (get_external_load_address(load_address, sc_list, sc)) { |
| 856 | return load_address; |
| 857 | } else { |
| 858 | sc_list.Clear(); |
| 859 | } |
| 860 | |
| 861 | if (sc_list.GetSize() == 0 && sc.target_sp) { |
| 862 | sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask, |
| 863 | true, // include_symbols |
| 864 | false, // include_inlines |
| 865 | true, // append |
| 866 | sc_list); |
| 867 | } |
| 868 | |
| 869 | if (get_external_load_address(load_address, sc_list, sc)) { |
| 870 | return load_address; |
| 871 | } else { |
| 872 | sc_list.Clear(); |
| 873 | } |
| 874 | |
| 875 | if (sc_list.GetSize() == 0 && sc.target_sp) { |
| 876 | sc.target_sp->GetImages().FindSymbolsWithNameAndType( |
| 877 | spec.name, lldb::eSymbolTypeAny, sc_list); |
| 878 | } |
| 879 | |
| 880 | if (get_external_load_address(load_address, sc_list, sc)) { |
| 881 | return load_address; |
| 882 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 883 | // if there are any searches we try after this, add an sc_list.Clear() in |
| 884 | // an "else" clause here |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 885 | |
| 886 | if (best_internal_load_address != LLDB_INVALID_ADDRESS) { |
| 887 | return best_internal_load_address; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | return LLDB_INVALID_ADDRESS; |
| 892 | } |
| 893 | |
| 894 | lldb::addr_t |
| 895 | IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs, |
| 896 | const lldb_private::SymbolContext &sc) { |
| 897 | lldb::TargetSP target_sp = sc.target_sp; |
| 898 | |
| 899 | if (!target_sp) { |
| 900 | return LLDB_INVALID_ADDRESS; |
| 901 | } |
| 902 | |
| 903 | lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP(); |
| 904 | |
| 905 | if (!process_sp) { |
| 906 | return LLDB_INVALID_ADDRESS; |
| 907 | } |
| 908 | |
| 909 | ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime(); |
| 910 | |
| 911 | if (runtime) { |
| 912 | for (const SearchSpec &spec : specs) { |
| 913 | lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name); |
| 914 | |
| 915 | if (symbol_load_addr != LLDB_INVALID_ADDRESS) |
| 916 | return symbol_load_addr; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | return LLDB_INVALID_ADDRESS; |
| 921 | } |
| 922 | |
| 923 | lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols( |
| 924 | const std::vector<SearchSpec> &specs, |
| 925 | const lldb_private::SymbolContext &sc) { |
| 926 | lldb::TargetSP target_sp = sc.target_sp; |
| 927 | |
| 928 | for (const SearchSpec &spec : specs) { |
| 929 | lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name); |
| 930 | |
| 931 | if (symbol_load_addr != LLDB_INVALID_ADDRESS) |
| 932 | return symbol_load_addr; |
| 933 | } |
| 934 | |
| 935 | return LLDB_INVALID_ADDRESS; |
| 936 | } |
| 937 | |
| 938 | lldb::addr_t |
| 939 | IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name) { |
| 940 | std::vector<SearchSpec> candidate_C_names; |
| 941 | std::vector<SearchSpec> candidate_CPlusPlus_names; |
| 942 | |
| 943 | CollectCandidateCNames(candidate_C_names, name); |
| 944 | |
| 945 | lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx); |
| 946 | if (ret == LLDB_INVALID_ADDRESS) |
| 947 | ret = FindInRuntimes(candidate_C_names, m_sym_ctx); |
| 948 | |
| 949 | if (ret == LLDB_INVALID_ADDRESS) |
| 950 | ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx); |
| 951 | |
| 952 | if (ret == LLDB_INVALID_ADDRESS) { |
| 953 | CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names, |
| 954 | m_sym_ctx); |
| 955 | ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx); |
| 956 | } |
| 957 | |
| 958 | if (ret == LLDB_INVALID_ADDRESS) { |
| 959 | std::vector<SearchSpec> candidate_fallback_names; |
| 960 | |
| 961 | CollectFallbackNames(candidate_fallback_names, candidate_C_names); |
| 962 | ret = FindInSymbols(candidate_fallback_names, m_sym_ctx); |
| 963 | } |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | void IRExecutionUnit::GetStaticInitializers( |
| 969 | std::vector<lldb::addr_t> &static_initializers) { |
| 970 | if (llvm::GlobalVariable *global_ctors = |
| 971 | m_module->getNamedGlobal("llvm.global_ctors")) { |
| 972 | if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>( |
| 973 | global_ctors->getInitializer())) { |
| 974 | for (llvm::Use &ctor_use : ctor_array->operands()) { |
| 975 | if (llvm::ConstantStruct *ctor_struct = |
| 976 | llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) { |
| 977 | lldbassert(ctor_struct->getNumOperands() == |
| 978 | 3); // this is standardized |
| 979 | if (llvm::Function *ctor_function = |
| 980 | llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 981 | ConstString ctor_function_name_cs(ctor_function->getName().str()); |
| 982 | |
| 983 | for (JittedFunction &jitted_function : m_jitted_functions) { |
| 984 | if (ctor_function_name_cs == jitted_function.m_name) { |
| 985 | if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) { |
| 986 | static_initializers.push_back(jitted_function.m_remote_addr); |
Ted Woodward | eee554d | 2016-03-09 22:05:17 +0000 | [diff] [blame] | 987 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 988 | break; |
| 989 | } |
Sean Callanan | fed0e758 | 2016-02-23 23:09:06 +0000 | [diff] [blame] | 990 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 991 | } |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 992 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 993 | } |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 994 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 995 | } |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 998 | uint64_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 999 | IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) { |
| 1000 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Greg Clayton | d6171a7 | 2015-07-02 16:43:49 +0000 | [diff] [blame] | 1001 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | ConstString name_cs(Name.c_str()); |
Chaoren Lin | 74a1fc6 | 2016-02-24 03:15:21 +0000 | [diff] [blame] | 1003 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1004 | lldb::addr_t ret = m_parent.FindSymbol(name_cs); |
Chaoren Lin | 74a1fc6 | 2016-02-24 03:15:21 +0000 | [diff] [blame] | 1005 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1006 | if (ret == LLDB_INVALID_ADDRESS) { |
| 1007 | if (log) |
| 1008 | log->Printf( |
| 1009 | "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>", |
| 1010 | Name.c_str()); |
Sean Callanan | b281480 | 2016-02-12 21:11:25 +0000 | [diff] [blame] | 1011 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | m_parent.ReportSymbolLookupError(name_cs); |
| 1013 | return 0xbad0bad0; |
| 1014 | } else { |
| 1015 | if (log) |
| 1016 | log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64, |
| 1017 | Name.c_str(), ret); |
| 1018 | return ret; |
| 1019 | } |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction( |
| 1023 | const std::string &Name, bool AbortOnFailure) { |
| 1024 | assert(sizeof(void *) == 8); |
Chaoren Lin | 74a1fc6 | 2016-02-24 03:15:21 +0000 | [diff] [blame] | 1025 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1026 | return (void *)getSymbolAddress(Name); |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1029 | lldb::addr_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1030 | IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) { |
| 1031 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | ffae944 | 2013-06-27 01:42:47 +0000 | [diff] [blame] | 1032 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1033 | for (AllocationRecord &record : m_records) { |
| 1034 | if (local_address >= record.m_host_address && |
| 1035 | local_address < record.m_host_address + record.m_size) { |
| 1036 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 1037 | return LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1038 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1039 | lldb::addr_t ret = |
| 1040 | record.m_process_address + (local_address - record.m_host_address); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1041 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1042 | if (log) { |
| 1043 | log->Printf( |
| 1044 | "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 |
| 1045 | " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 |
| 1046 | " from [0x%" PRIx64 "..0x%" PRIx64 "].", |
| 1047 | local_address, (uint64_t)record.m_host_address, |
| 1048 | (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret, |
| 1049 | record.m_process_address, record.m_process_address + record.m_size); |
| 1050 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1051 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1052 | return ret; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1053 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1054 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1055 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1056 | return LLDB_INVALID_ADDRESS; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | IRExecutionUnit::AddrRange |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1060 | IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) { |
| 1061 | for (AllocationRecord &record : m_records) { |
| 1062 | if (local_address >= record.m_host_address && |
| 1063 | local_address < record.m_host_address + record.m_size) { |
| 1064 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 1065 | return AddrRange(0, 0); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1066 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1067 | return AddrRange(record.m_process_address, record.m_size); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1068 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1070 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1071 | return AddrRange(0, 0); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1074 | bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1075 | Status &error, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | AllocationRecord &record) { |
| 1077 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
| 1081 | switch (record.m_sect_type) { |
| 1082 | case lldb::eSectionTypeInvalid: |
| 1083 | case lldb::eSectionTypeDWARFDebugAbbrev: |
| 1084 | case lldb::eSectionTypeDWARFDebugAddr: |
| 1085 | case lldb::eSectionTypeDWARFDebugAranges: |
Tamas Berghammer | 963ce48 | 2017-08-25 13:56:14 +0000 | [diff] [blame] | 1086 | case lldb::eSectionTypeDWARFDebugCuIndex: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1087 | case lldb::eSectionTypeDWARFDebugFrame: |
| 1088 | case lldb::eSectionTypeDWARFDebugInfo: |
| 1089 | case lldb::eSectionTypeDWARFDebugLine: |
| 1090 | case lldb::eSectionTypeDWARFDebugLoc: |
| 1091 | case lldb::eSectionTypeDWARFDebugMacInfo: |
| 1092 | case lldb::eSectionTypeDWARFDebugPubNames: |
| 1093 | case lldb::eSectionTypeDWARFDebugPubTypes: |
| 1094 | case lldb::eSectionTypeDWARFDebugRanges: |
| 1095 | case lldb::eSectionTypeDWARFDebugStr: |
| 1096 | case lldb::eSectionTypeDWARFDebugStrOffsets: |
| 1097 | case lldb::eSectionTypeDWARFAppleNames: |
| 1098 | case lldb::eSectionTypeDWARFAppleTypes: |
| 1099 | case lldb::eSectionTypeDWARFAppleNamespaces: |
| 1100 | case lldb::eSectionTypeDWARFAppleObjC: |
Jan Kratochvil | e4777a9 | 2018-04-29 19:47:48 +0000 | [diff] [blame] | 1101 | case lldb::eSectionTypeDWARFGNUDebugAltLink: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | error.Clear(); |
| 1103 | break; |
| 1104 | default: |
| 1105 | const bool zero_memory = false; |
| 1106 | record.m_process_address = |
| 1107 | Malloc(record.m_size, record.m_alignment, record.m_permissions, |
| 1108 | eAllocationPolicyProcessOnly, zero_memory, error); |
| 1109 | break; |
| 1110 | } |
| 1111 | |
| 1112 | return error.Success(); |
| 1113 | } |
| 1114 | |
| 1115 | bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) { |
| 1116 | bool ret = true; |
| 1117 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1118 | lldb_private::Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1119 | |
| 1120 | for (AllocationRecord &record : m_records) { |
| 1121 | ret = CommitOneAllocation(process_sp, err, record); |
| 1122 | |
| 1123 | if (!ret) { |
| 1124 | break; |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1125 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | if (!ret) { |
| 1129 | for (AllocationRecord &record : m_records) { |
| 1130 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 1131 | Free(record.m_process_address, err); |
| 1132 | record.m_process_address = LLDB_INVALID_ADDRESS; |
| 1133 | } |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1134 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | return ret; |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1138 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1140 | void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) { |
| 1141 | m_reported_allocations = true; |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1143 | for (AllocationRecord &record : m_records) { |
| 1144 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 1145 | continue; |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1147 | if (record.m_section_id == eSectionIDInvalid) |
| 1148 | continue; |
| 1149 | |
| 1150 | engine.mapSectionAddress((void *)record.m_host_address, |
| 1151 | record.m_process_address); |
| 1152 | } |
| 1153 | |
| 1154 | // Trigger re-application of relocations. |
| 1155 | engine.finalizeObject(); |
| 1156 | } |
| 1157 | |
| 1158 | bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) { |
| 1159 | bool wrote_something = false; |
| 1160 | for (AllocationRecord &record : m_records) { |
| 1161 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1162 | lldb_private::Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1163 | WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address, |
| 1164 | record.m_size, err); |
| 1165 | if (err.Success()) |
| 1166 | wrote_something = true; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1167 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1168 | } |
| 1169 | return wrote_something; |
| 1170 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1172 | void IRExecutionUnit::AllocationRecord::dump(Log *log) { |
| 1173 | if (!log) |
| 1174 | return; |
| 1175 | |
| 1176 | log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)", |
| 1177 | (unsigned long long)m_host_address, (unsigned long long)m_size, |
| 1178 | (unsigned long long)m_process_address, (unsigned)m_alignment, |
| 1179 | (unsigned)m_section_id, m_name.c_str()); |
| 1180 | } |
| 1181 | |
| 1182 | lldb::ByteOrder IRExecutionUnit::GetByteOrder() const { |
| 1183 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1184 | return exe_ctx.GetByteOrder(); |
| 1185 | } |
| 1186 | |
| 1187 | uint32_t IRExecutionUnit::GetAddressByteSize() const { |
| 1188 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1189 | return exe_ctx.GetAddressByteSize(); |
| 1190 | } |
| 1191 | |
| 1192 | void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file, |
| 1193 | lldb_private::Symtab &symtab) { |
| 1194 | // No symbols yet... |
| 1195 | } |
| 1196 | |
| 1197 | void IRExecutionUnit::PopulateSectionList( |
| 1198 | lldb_private::ObjectFile *obj_file, |
| 1199 | lldb_private::SectionList §ion_list) { |
| 1200 | for (AllocationRecord &record : m_records) { |
| 1201 | if (record.m_size > 0) { |
| 1202 | lldb::SectionSP section_sp(new lldb_private::Section( |
| 1203 | obj_file->GetModule(), obj_file, record.m_section_id, |
| 1204 | ConstString(record.m_name), record.m_sect_type, |
| 1205 | record.m_process_address, record.m_size, |
| 1206 | record.m_host_address, // file_offset (which is the host address for |
| 1207 | // the data) |
| 1208 | record.m_size, // file_size |
| 1209 | 0, |
| 1210 | record.m_permissions)); // flags |
| 1211 | section_list.AddSection(section_sp); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1212 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1213 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1216 | bool IRExecutionUnit::GetArchitecture(lldb_private::ArchSpec &arch) { |
| 1217 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1218 | Target *target = exe_ctx.GetTargetPtr(); |
| 1219 | if (target) |
| 1220 | arch = target->GetArchitecture(); |
| 1221 | else |
| 1222 | arch.Clear(); |
| 1223 | return arch.IsValid(); |
| 1224 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1226 | lldb::ModuleSP IRExecutionUnit::GetJITModule() { |
| 1227 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1228 | Target *target = exe_ctx.GetTargetPtr(); |
Zachary Turner | 8e8d4b9 | 2018-05-23 23:56:09 +0000 | [diff] [blame^] | 1229 | if (!target) |
| 1230 | return nullptr; |
| 1231 | |
| 1232 | auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>( |
| 1233 | shared_from_this()); |
| 1234 | |
| 1235 | lldb::ModuleSP jit_module_sp = |
| 1236 | lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate); |
| 1237 | if (!jit_module_sp) |
| 1238 | return nullptr; |
| 1239 | |
| 1240 | bool changed = false; |
| 1241 | jit_module_sp->SetLoadAddress(*target, 0, true, changed); |
| 1242 | return jit_module_sp; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 1243 | } |