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 | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 11 | #include "llvm/IR/LLVMContext.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Module.h" |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 13 | #include "llvm/Support/SourceMgr.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 16 | #include "lldb/Core/DataBufferHeap.h" |
| 17 | #include "lldb/Core/DataExtractor.h" |
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" |
| 20 | #include "lldb/Core/Log.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Module.h" |
| 22 | #include "lldb/Core/Section.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/SymbolContext.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 24 | #include "lldb/Expression/IRExecutionUnit.h" |
| 25 | #include "lldb/Target/ExecutionContext.h" |
| 26 | #include "lldb/Target/Target.h" |
Zachary Turner | a78bd7f | 2015-03-03 23:11:11 +0000 | [diff] [blame] | 27 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lldb_private; |
| 30 | |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 31 | IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap, |
| 32 | std::unique_ptr<llvm::Module> &module_ap, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 33 | ConstString &name, |
Sean Callanan | b024d87 | 2013-04-15 17:12:47 +0000 | [diff] [blame] | 34 | const lldb::TargetSP &target_sp, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 35 | std::vector<std::string> &cpu_features) : |
Sean Callanan | b024d87 | 2013-04-15 17:12:47 +0000 | [diff] [blame] | 36 | IRMemoryMap(target_sp), |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 37 | m_context_ap(context_ap.release()), |
| 38 | m_module_ap(module_ap.release()), |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 39 | m_module(m_module_ap.get()), |
Sean Callanan | 2c04735 | 2013-03-19 23:03:21 +0000 | [diff] [blame] | 40 | m_cpu_features(cpu_features), |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 41 | m_name(name), |
| 42 | m_did_jit(false), |
| 43 | m_function_load_addr(LLDB_INVALID_ADDRESS), |
| 44 | m_function_end_load_addr(LLDB_INVALID_ADDRESS) |
| 45 | { |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | lldb::addr_t |
| 49 | IRExecutionUnit::WriteNow (const uint8_t *bytes, |
| 50 | size_t size, |
| 51 | Error &error) |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 52 | { |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame^] | 53 | const bool zero_memory = false; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 54 | lldb::addr_t allocation_process_addr = Malloc (size, |
| 55 | 8, |
| 56 | lldb::ePermissionsWritable | lldb::ePermissionsReadable, |
| 57 | eAllocationPolicyMirror, |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame^] | 58 | zero_memory, |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 59 | error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 60 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 61 | if (!error.Success()) |
| 62 | return LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 63 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 64 | WriteMemory(allocation_process_addr, bytes, size, error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 65 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 66 | if (!error.Success()) |
| 67 | { |
| 68 | Error err; |
| 69 | Free (allocation_process_addr, err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 70 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 71 | return LLDB_INVALID_ADDRESS; |
| 72 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 73 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 74 | if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 75 | { |
| 76 | DataBufferHeap my_buffer(size, 0); |
| 77 | Error err; |
| 78 | ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 79 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 80 | if (err.Success()) |
| 81 | { |
| 82 | DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8); |
Sean Callanan | 4f2c198 | 2014-01-21 00:54:48 +0000 | [diff] [blame] | 83 | my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 84 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 85 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 86 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 87 | return allocation_process_addr; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
| 91 | IRExecutionUnit::FreeNow (lldb::addr_t allocation) |
| 92 | { |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 93 | if (allocation == LLDB_INVALID_ADDRESS) |
| 94 | return; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 95 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 96 | Error err; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 97 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 98 | Free(allocation, err); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | Error |
| 102 | IRExecutionUnit::DisassembleFunction (Stream &stream, |
| 103 | lldb::ProcessSP &process_wp) |
| 104 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 105 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 106 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 107 | ExecutionContext exe_ctx(process_wp); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 108 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 109 | Error ret; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 110 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 111 | ret.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 112 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 113 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 114 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 115 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 116 | for (JittedFunction &function : m_jitted_functions) |
| 117 | { |
| 118 | if (strstr(function.m_name.c_str(), m_name.AsCString())) |
| 119 | { |
| 120 | func_local_addr = function.m_local_addr; |
| 121 | func_remote_addr = function.m_remote_addr; |
| 122 | } |
| 123 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 124 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 125 | if (func_local_addr == LLDB_INVALID_ADDRESS) |
| 126 | { |
| 127 | ret.SetErrorToGenericError(); |
| 128 | ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString()); |
| 129 | return ret; |
| 130 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 131 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 132 | if (log) |
| 133 | log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 134 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 135 | std::pair <lldb::addr_t, lldb::addr_t> func_range; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 136 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 137 | func_range = GetRemoteRangeForLocal(func_local_addr); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 138 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 139 | if (func_range.first == 0 && func_range.second == 0) |
| 140 | { |
| 141 | ret.SetErrorToGenericError(); |
| 142 | ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString()); |
| 143 | return ret; |
| 144 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 145 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 146 | if (log) |
| 147 | log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 148 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 149 | Target *target = exe_ctx.GetTargetPtr(); |
| 150 | if (!target) |
| 151 | { |
| 152 | ret.SetErrorToGenericError(); |
| 153 | ret.SetErrorString("Couldn't find the target"); |
| 154 | return ret; |
| 155 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 156 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 157 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 158 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 159 | Process *process = exe_ctx.GetProcessPtr(); |
| 160 | Error err; |
| 161 | process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 162 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 163 | if (!err.Success()) |
| 164 | { |
| 165 | ret.SetErrorToGenericError(); |
| 166 | ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error")); |
| 167 | return ret; |
| 168 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 169 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 170 | ArchSpec arch(target->GetArchitecture()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 171 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 172 | const char *plugin_name = NULL; |
| 173 | const char *flavor_string = NULL; |
Jim Ingham | 56d4042 | 2013-07-31 02:19:15 +0000 | [diff] [blame] | 174 | lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 175 | |
Jim Ingham | 56d4042 | 2013-07-31 02:19:15 +0000 | [diff] [blame] | 176 | if (!disassembler_sp) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 177 | { |
| 178 | ret.SetErrorToGenericError(); |
| 179 | ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName()); |
| 180 | return ret; |
| 181 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 182 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 183 | if (!process) |
| 184 | { |
| 185 | ret.SetErrorToGenericError(); |
| 186 | ret.SetErrorString("Couldn't find the process"); |
| 187 | return ret; |
| 188 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 189 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 190 | DataExtractor extractor(buffer_sp, |
| 191 | process->GetByteOrder(), |
| 192 | target->GetArchitecture().GetAddressByteSize()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 193 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 194 | if (log) |
| 195 | { |
| 196 | log->Printf("Function data has contents:"); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 197 | extractor.PutToLog (log, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 198 | 0, |
| 199 | extractor.GetByteSize(), |
| 200 | func_remote_addr, |
| 201 | 16, |
| 202 | DataExtractor::TypeUInt8); |
| 203 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 204 | |
Jim Ingham | 56d4042 | 2013-07-31 02:19:15 +0000 | [diff] [blame] | 205 | disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 206 | |
Jim Ingham | 56d4042 | 2013-07-31 02:19:15 +0000 | [diff] [blame] | 207 | InstructionList &instruction_list = disassembler_sp->GetInstructionList(); |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 +0000 | [diff] [blame] | 208 | instruction_list.Dump(&stream, true, true, &exe_ctx); |
| 209 | |
Jim Ingham | 56d4042 | 2013-07-31 02:19:15 +0000 | [diff] [blame] | 210 | // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions. |
| 211 | // I'll fix that but for now, just clear the list and it will go away nicely. |
| 212 | disassembler_sp->GetInstructionList().Clear(); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 216 | static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie) |
| 217 | { |
| 218 | Error *err = static_cast<Error*>(Context); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 219 | |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 220 | if (err && err->Success()) |
| 221 | { |
| 222 | err->SetErrorToGenericError(); |
| 223 | err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str()); |
| 224 | } |
| 225 | } |
| 226 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 227 | void |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 228 | IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) |
| 229 | { |
| 230 | m_failed_lookups.push_back(name); |
| 231 | } |
| 232 | |
| 233 | void |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 234 | IRExecutionUnit::GetRunnableInfo(Error &error, |
| 235 | lldb::addr_t &func_addr, |
| 236 | lldb::addr_t &func_end) |
| 237 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 238 | lldb::ProcessSP process_sp(GetProcessWP().lock()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 239 | |
Sean Callanan | 7c435a5 | 2014-02-06 22:25:20 +0000 | [diff] [blame] | 240 | static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 241 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 242 | func_addr = LLDB_INVALID_ADDRESS; |
| 243 | func_end = LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 244 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 245 | if (!process_sp) |
| 246 | { |
| 247 | error.SetErrorToGenericError(); |
| 248 | error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid"); |
| 249 | return; |
| 250 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 251 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 252 | if (m_did_jit) |
| 253 | { |
| 254 | func_addr = m_function_load_addr; |
| 255 | func_end = m_function_end_load_addr; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 256 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 257 | return; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 258 | }; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 259 | |
Sean Callanan | 7c435a5 | 2014-02-06 22:25:20 +0000 | [diff] [blame] | 260 | Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 261 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 262 | m_did_jit = true; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 263 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 264 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 265 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 266 | std::string error_string; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 267 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 268 | if (log) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 269 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 270 | std::string s; |
| 271 | llvm::raw_string_ostream oss(s); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 272 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 273 | m_module->print(oss, NULL); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 274 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 275 | oss.flush(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 276 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 277 | log->Printf ("Module being sent to JIT: \n%s", s.c_str()); |
| 278 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 279 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 280 | llvm::Triple triple(m_module->getTargetTriple()); |
| 281 | llvm::Function *function = m_module->getFunction (m_name.AsCString()); |
| 282 | llvm::Reloc::Model relocModel; |
| 283 | llvm::CodeModel::Model codeModel; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 284 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 285 | if (triple.isOSBinFormatELF()) |
| 286 | { |
| 287 | relocModel = llvm::Reloc::Static; |
| 288 | // This will be small for 32-bit and large for 64-bit. |
| 289 | codeModel = llvm::CodeModel::JITDefault; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | relocModel = llvm::Reloc::PIC_; |
| 294 | codeModel = llvm::CodeModel::Small; |
| 295 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 296 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 297 | m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 298 | |
Rafael Espindola | 7c03d95 | 2014-08-19 04:27:03 +0000 | [diff] [blame] | 299 | llvm::EngineBuilder builder(std::move(m_module_ap)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 300 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 301 | builder.setEngineKind(llvm::EngineKind::JIT) |
| 302 | .setErrorStr(&error_string) |
| 303 | .setRelocationModel(relocModel) |
David Majnemer | b313e46 | 2014-12-04 21:26:25 +0000 | [diff] [blame] | 304 | .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this))) |
David Majnemer | 8faf937 | 2014-09-16 06:34:29 +0000 | [diff] [blame] | 305 | .setCodeModel(codeModel) |
Reid Kleckner | 5420953 | 2014-09-03 00:40:36 +0000 | [diff] [blame] | 306 | .setOptLevel(llvm::CodeGenOpt::Less); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 307 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 308 | llvm::StringRef mArch; |
| 309 | llvm::StringRef mCPU; |
| 310 | llvm::SmallVector<std::string, 0> mAttrs; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 311 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 312 | for (std::string &feature : m_cpu_features) |
| 313 | mAttrs.push_back(feature); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 314 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 315 | llvm::TargetMachine *target_machine = builder.selectTarget(triple, |
| 316 | mArch, |
| 317 | mCPU, |
| 318 | mAttrs); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 319 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 320 | m_execution_engine_ap.reset(builder.create(target_machine)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 321 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 322 | if (!m_execution_engine_ap.get()) |
| 323 | { |
| 324 | error.SetErrorToGenericError(); |
| 325 | error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str()); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 326 | return; |
| 327 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 328 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 329 | // Make sure we see all sections, including ones that don't have relocations... |
| 330 | m_execution_engine_ap->setProcessAllSections(true); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 331 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 332 | m_execution_engine_ap->DisableLazyCompilation(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 333 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 334 | // We don't actually need the function pointer here, this just forces it to get resolved. |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 335 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 336 | void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 337 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 338 | if (!error.Success()) |
| 339 | { |
| 340 | // We got an error through our callback! |
| 341 | return; |
| 342 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 343 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 344 | if (!function) |
| 345 | { |
| 346 | error.SetErrorToGenericError(); |
| 347 | error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString()); |
| 348 | return; |
| 349 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 350 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 351 | if (!fun_ptr) |
| 352 | { |
| 353 | error.SetErrorToGenericError(); |
| 354 | error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString()); |
| 355 | return; |
| 356 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 357 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 358 | m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 359 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 360 | CommitAllocations(process_sp); |
| 361 | ReportAllocations(*m_execution_engine_ap); |
| 362 | WriteData(process_sp); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 363 | |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 364 | if (m_failed_lookups.size()) |
| 365 | { |
| 366 | StreamString ss; |
| 367 | |
| 368 | ss.PutCString("Couldn't lookup symbols:\n"); |
| 369 | |
| 370 | bool emitNewLine = false; |
| 371 | |
| 372 | for (const ConstString &failed_lookup : m_failed_lookups) |
| 373 | { |
| 374 | if (emitNewLine) |
| 375 | ss.PutCString("\n"); |
| 376 | emitNewLine = true; |
| 377 | ss.PutCString(" "); |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 378 | ss.PutCString(Mangled(failed_lookup).GetDemangledName(lldb::eLanguageTypeObjC_plus_plus).AsCString()); |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | m_failed_lookups.clear(); |
| 382 | |
| 383 | error.SetErrorString(ss.GetData()); |
| 384 | |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | m_function_load_addr = LLDB_INVALID_ADDRESS; |
| 389 | m_function_end_load_addr = LLDB_INVALID_ADDRESS; |
| 390 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 391 | for (JittedFunction &jitted_function : m_jitted_functions) |
| 392 | { |
| 393 | jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 394 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 395 | if (!jitted_function.m_name.compare(m_name.AsCString())) |
| 396 | { |
| 397 | AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr); |
| 398 | m_function_end_load_addr = func_range.first + func_range.second; |
| 399 | m_function_load_addr = jitted_function.m_remote_addr; |
| 400 | } |
| 401 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 402 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 403 | if (log) |
| 404 | { |
| 405 | log->Printf("Code can be run in the target."); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 406 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 407 | StreamString disassembly_stream; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 408 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 409 | Error err = DisassembleFunction(disassembly_stream, process_sp); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 410 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 411 | if (!err.Success()) |
| 412 | { |
| 413 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 418 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 419 | |
Sean Callanan | 4f2c198 | 2014-01-21 00:54:48 +0000 | [diff] [blame] | 420 | log->Printf("Sections: "); |
| 421 | for (AllocationRecord &record : m_records) |
| 422 | { |
| 423 | if (record.m_process_address != LLDB_INVALID_ADDRESS) |
| 424 | { |
| 425 | record.dump(log); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 426 | |
Sean Callanan | 4f2c198 | 2014-01-21 00:54:48 +0000 | [diff] [blame] | 427 | DataBufferHeap my_buffer(record.m_size, 0); |
| 428 | Error err; |
| 429 | ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 430 | |
Sean Callanan | 4f2c198 | 2014-01-21 00:54:48 +0000 | [diff] [blame] | 431 | if (err.Success()) |
| 432 | { |
| 433 | DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8); |
| 434 | my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8); |
| 435 | } |
| 436 | } |
Sean Callanan | 44dbf11 | 2015-10-23 00:39:09 +0000 | [diff] [blame] | 437 | else |
| 438 | { |
| 439 | record.dump(log); |
| 440 | |
| 441 | DataExtractor my_extractor ((const void*)record.m_host_address, record.m_size, lldb::eByteOrderBig, 8); |
| 442 | my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16, DataExtractor::TypeUInt8); |
| 443 | } |
Sean Callanan | 4f2c198 | 2014-01-21 00:54:48 +0000 | [diff] [blame] | 444 | } |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 445 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 446 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 447 | func_addr = m_function_load_addr; |
| 448 | func_end = m_function_end_load_addr; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 449 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 450 | return; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | IRExecutionUnit::~IRExecutionUnit () |
| 454 | { |
Sean Callanan | 14b1bae | 2013-04-16 23:25:35 +0000 | [diff] [blame] | 455 | m_module_ap.reset(); |
| 456 | m_execution_engine_ap.reset(); |
| 457 | m_context_ap.reset(); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) : |
Todd Fiala | d5635cd | 2014-09-24 15:55:47 +0000 | [diff] [blame] | 461 | m_default_mm_ap (new llvm::SectionMemoryManager()), |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 462 | m_parent (parent) |
| 463 | { |
| 464 | } |
| 465 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 466 | IRExecutionUnit::MemoryManager::~MemoryManager () |
| 467 | { |
| 468 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 469 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 470 | lldb::SectionType |
| 471 | IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) |
| 472 | { |
| 473 | lldb::SectionType sect_type = lldb::eSectionTypeCode; |
| 474 | switch (alloc_kind) |
| 475 | { |
| 476 | case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break; |
| 477 | case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break; |
| 478 | case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break; |
| 479 | case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break; |
| 480 | case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break; |
| 481 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 482 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 483 | if (!name.empty()) |
| 484 | { |
| 485 | if (name.equals("__text") || name.equals(".text")) |
| 486 | sect_type = lldb::eSectionTypeCode; |
| 487 | else if (name.equals("__data") || name.equals(".data")) |
| 488 | sect_type = lldb::eSectionTypeCode; |
| 489 | else if (name.startswith("__debug_") || name.startswith(".debug_")) |
| 490 | { |
| 491 | const uint32_t name_idx = name[0] == '_' ? 8 : 7; |
| 492 | llvm::StringRef dwarf_name(name.substr(name_idx)); |
| 493 | switch (dwarf_name[0]) |
| 494 | { |
| 495 | case 'a': |
| 496 | if (dwarf_name.equals("abbrev")) |
| 497 | sect_type = lldb::eSectionTypeDWARFDebugAbbrev; |
| 498 | else if (dwarf_name.equals("aranges")) |
| 499 | sect_type = lldb::eSectionTypeDWARFDebugAranges; |
Tamas Berghammer | c178d4c | 2015-08-25 11:45:58 +0000 | [diff] [blame] | 500 | else if (dwarf_name.equals("addr")) |
| 501 | sect_type = lldb::eSectionTypeDWARFDebugAddr; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 502 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 503 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 504 | case 'f': |
| 505 | if (dwarf_name.equals("frame")) |
| 506 | sect_type = lldb::eSectionTypeDWARFDebugFrame; |
| 507 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 508 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 509 | case 'i': |
| 510 | if (dwarf_name.equals("info")) |
| 511 | sect_type = lldb::eSectionTypeDWARFDebugInfo; |
| 512 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 513 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 514 | case 'l': |
| 515 | if (dwarf_name.equals("line")) |
| 516 | sect_type = lldb::eSectionTypeDWARFDebugLine; |
| 517 | else if (dwarf_name.equals("loc")) |
| 518 | sect_type = lldb::eSectionTypeDWARFDebugLoc; |
| 519 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 520 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 521 | case 'm': |
| 522 | if (dwarf_name.equals("macinfo")) |
| 523 | sect_type = lldb::eSectionTypeDWARFDebugMacInfo; |
| 524 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 525 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 526 | case 'p': |
| 527 | if (dwarf_name.equals("pubnames")) |
| 528 | sect_type = lldb::eSectionTypeDWARFDebugPubNames; |
| 529 | else if (dwarf_name.equals("pubtypes")) |
| 530 | sect_type = lldb::eSectionTypeDWARFDebugPubTypes; |
| 531 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 532 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 533 | case 's': |
| 534 | if (dwarf_name.equals("str")) |
| 535 | sect_type = lldb::eSectionTypeDWARFDebugStr; |
Tamas Berghammer | c178d4c | 2015-08-25 11:45:58 +0000 | [diff] [blame] | 536 | else if (dwarf_name.equals("str_offsets")) |
| 537 | sect_type = lldb::eSectionTypeDWARFDebugStrOffsets; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 538 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 539 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 540 | case 'r': |
| 541 | if (dwarf_name.equals("ranges")) |
| 542 | sect_type = lldb::eSectionTypeDWARFDebugRanges; |
| 543 | break; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 544 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 545 | default: |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | else if (name.startswith("__apple_") || name.startswith(".apple_")) |
| 550 | { |
| 551 | #if 0 |
| 552 | const uint32_t name_idx = name[0] == '_' ? 8 : 7; |
| 553 | llvm::StringRef apple_name(name.substr(name_idx)); |
| 554 | switch (apple_name[0]) |
| 555 | { |
| 556 | case 'n': |
| 557 | if (apple_name.equals("names")) |
| 558 | sect_type = lldb::eSectionTypeDWARFAppleNames; |
| 559 | else if (apple_name.equals("namespac") || apple_name.equals("namespaces")) |
| 560 | sect_type = lldb::eSectionTypeDWARFAppleNamespaces; |
| 561 | break; |
| 562 | case 't': |
| 563 | if (apple_name.equals("types")) |
| 564 | sect_type = lldb::eSectionTypeDWARFAppleTypes; |
| 565 | break; |
| 566 | case 'o': |
| 567 | if (apple_name.equals("objc")) |
| 568 | sect_type = lldb::eSectionTypeDWARFAppleObjC; |
| 569 | break; |
| 570 | default: |
| 571 | break; |
| 572 | } |
| 573 | #else |
| 574 | sect_type = lldb::eSectionTypeInvalid; |
| 575 | #endif |
| 576 | } |
| 577 | else if (name.equals("__objc_imageinfo")) |
| 578 | sect_type = lldb::eSectionTypeOther; |
| 579 | } |
| 580 | return sect_type; |
| 581 | } |
| 582 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 583 | uint8_t * |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 584 | IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size, |
| 585 | unsigned Alignment, |
Filip Pizlo | bfcff68 | 2013-10-02 01:43:46 +0000 | [diff] [blame] | 586 | unsigned SectionID, |
| 587 | llvm::StringRef SectionName) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 588 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 589 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 590 | |
Filip Pizlo | bfcff68 | 2013-10-02 01:43:46 +0000 | [diff] [blame] | 591 | uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 592 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 593 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
Sean Callanan | ec1c0b3 | 2013-04-09 01:13:08 +0000 | [diff] [blame] | 594 | lldb::ePermissionsReadable | lldb::ePermissionsExecutable, |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 595 | GetSectionTypeFromSectionName (SectionName, AllocationKind::Code), |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 596 | Size, |
| 597 | Alignment, |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 598 | SectionID, |
| 599 | SectionName.str().c_str())); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 600 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 601 | if (log) |
| 602 | { |
| 603 | log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", |
David Blaikie | 129b839 | 2015-04-08 20:23:52 +0000 | [diff] [blame] | 604 | (uint64_t)Size, Alignment, SectionID, (void *)return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 605 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 606 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 607 | return return_value; |
| 608 | } |
| 609 | |
| 610 | uint8_t * |
| 611 | IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size, |
| 612 | unsigned Alignment, |
| 613 | unsigned SectionID, |
Filip Pizlo | bfcff68 | 2013-10-02 01:43:46 +0000 | [diff] [blame] | 614 | llvm::StringRef SectionName, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 615 | bool IsReadOnly) |
| 616 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 617 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 618 | |
Filip Pizlo | bfcff68 | 2013-10-02 01:43:46 +0000 | [diff] [blame] | 619 | uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 620 | |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 621 | uint32_t permissions = lldb::ePermissionsReadable; |
| 622 | if (!IsReadOnly) |
| 623 | permissions |= lldb::ePermissionsWritable; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 624 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 625 | permissions, |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 626 | GetSectionTypeFromSectionName (SectionName, AllocationKind::Data), |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 627 | Size, |
| 628 | Alignment, |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 629 | SectionID, |
| 630 | SectionName.str().c_str())); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 631 | if (log) |
| 632 | { |
| 633 | log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", |
David Blaikie | 129b839 | 2015-04-08 20:23:52 +0000 | [diff] [blame] | 634 | (uint64_t)Size, Alignment, SectionID, (void *)return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 635 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 636 | |
| 637 | return return_value; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 640 | uint64_t |
| 641 | IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) |
| 642 | { |
| 643 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 644 | |
| 645 | SymbolContextList sc_list; |
| 646 | |
| 647 | ExecutionContextScope *exe_scope = m_parent.GetBestExecutionContextScope(); |
| 648 | |
| 649 | lldb::TargetSP target_sp = exe_scope->CalculateTarget(); |
| 650 | |
| 651 | const char *name = Name.c_str(); |
| 652 | |
| 653 | ConstString bare_name_cs(name); |
| 654 | ConstString name_cs; |
| 655 | |
| 656 | if (name[0] == '_') |
| 657 | name_cs = ConstString(name + 1); |
| 658 | |
| 659 | if (!target_sp) |
| 660 | { |
| 661 | if (log) |
| 662 | log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <no target>", |
| 663 | Name.c_str()); |
| 664 | |
| 665 | m_parent.ReportSymbolLookupError(name_cs); |
| 666 | |
| 667 | return 0xbad0bad0; |
| 668 | } |
| 669 | |
| 670 | uint32_t num_matches = 0; |
| 671 | lldb::ProcessSP process_sp = exe_scope->CalculateProcess(); |
| 672 | |
| 673 | if (!name_cs.IsEmpty()) |
| 674 | { |
| 675 | target_sp->GetImages().FindSymbolsWithNameAndType(name_cs, lldb::eSymbolTypeAny, sc_list); |
| 676 | num_matches = sc_list.GetSize(); |
| 677 | } |
| 678 | |
| 679 | if (!num_matches) |
| 680 | { |
| 681 | target_sp->GetImages().FindSymbolsWithNameAndType(bare_name_cs, lldb::eSymbolTypeAny, sc_list); |
| 682 | num_matches = sc_list.GetSize(); |
| 683 | } |
| 684 | |
| 685 | lldb::addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; |
| 686 | |
| 687 | for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++) |
| 688 | { |
| 689 | SymbolContext sym_ctx; |
| 690 | sc_list.GetContextAtIndex(i, sym_ctx); |
| 691 | |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 692 | symbol_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target_sp); |
Greg Clayton | d6171a7 | 2015-07-02 16:43:49 +0000 | [diff] [blame] | 693 | |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 694 | if (symbol_load_addr == LLDB_INVALID_ADDRESS) |
Greg Clayton | d6171a7 | 2015-07-02 16:43:49 +0000 | [diff] [blame] | 695 | symbol_load_addr = sym_ctx.symbol->GetAddress().GetLoadAddress(target_sp.get()); |
Jim Ingham | 27e5fe8 | 2015-01-27 18:03:05 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | if (symbol_load_addr == LLDB_INVALID_ADDRESS && process_sp && name_cs) |
| 699 | { |
| 700 | // Try the Objective-C language runtime. |
| 701 | |
| 702 | ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime(); |
| 703 | |
| 704 | if (runtime) |
| 705 | symbol_load_addr = runtime->LookupRuntimeSymbol(name_cs); |
| 706 | } |
| 707 | |
| 708 | if (symbol_load_addr == LLDB_INVALID_ADDRESS) |
| 709 | { |
| 710 | if (log) |
| 711 | log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>", |
| 712 | name); |
| 713 | |
| 714 | m_parent.ReportSymbolLookupError(bare_name_cs); |
| 715 | |
| 716 | return 0xbad0bad0; |
| 717 | } |
| 718 | |
| 719 | if (log) |
| 720 | log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64, |
| 721 | name, |
| 722 | symbol_load_addr); |
| 723 | |
| 724 | if (symbol_load_addr == 0) |
| 725 | return 0xbad00add; |
| 726 | |
| 727 | return symbol_load_addr; |
| 728 | } |
| 729 | |
| 730 | void * |
| 731 | IRExecutionUnit::MemoryManager::getPointerToNamedFunction(const std::string &Name, |
| 732 | bool AbortOnFailure) { |
| 733 | assert (sizeof(void *) == 8); |
| 734 | |
| 735 | return (void*)getSymbolAddress(Name); |
| 736 | } |
| 737 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 738 | lldb::addr_t |
| 739 | IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address) |
| 740 | { |
Sean Callanan | ffae944 | 2013-06-27 01:42:47 +0000 | [diff] [blame] | 741 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 742 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 743 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 744 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 745 | if (local_address >= record.m_host_address && |
| 746 | local_address < record.m_host_address + record.m_size) |
| 747 | { |
| 748 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 749 | return LLDB_INVALID_ADDRESS; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 750 | |
Sean Callanan | ffae944 | 2013-06-27 01:42:47 +0000 | [diff] [blame] | 751 | lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 752 | |
Sean Callanan | ffae944 | 2013-06-27 01:42:47 +0000 | [diff] [blame] | 753 | if (log) |
| 754 | { |
| 755 | log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].", |
| 756 | local_address, |
Michael Sartain | 89c862f | 2013-08-07 19:05:15 +0000 | [diff] [blame] | 757 | (uint64_t)record.m_host_address, |
| 758 | (uint64_t)record.m_host_address + (uint64_t)record.m_size, |
Sean Callanan | ffae944 | 2013-06-27 01:42:47 +0000 | [diff] [blame] | 759 | ret, |
| 760 | record.m_process_address, |
| 761 | record.m_process_address + record.m_size); |
| 762 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 763 | |
Sean Callanan | ffae944 | 2013-06-27 01:42:47 +0000 | [diff] [blame] | 764 | return ret; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 765 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | return LLDB_INVALID_ADDRESS; |
| 769 | } |
| 770 | |
| 771 | IRExecutionUnit::AddrRange |
| 772 | IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address) |
| 773 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 774 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 775 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 776 | if (local_address >= record.m_host_address && |
| 777 | local_address < record.m_host_address + record.m_size) |
| 778 | { |
| 779 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 780 | return AddrRange(0, 0); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 781 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 782 | return AddrRange(record.m_process_address, record.m_size); |
| 783 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 784 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 785 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 786 | return AddrRange (0, 0); |
| 787 | } |
| 788 | |
| 789 | bool |
| 790 | IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp) |
| 791 | { |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 792 | bool ret = true; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 793 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 794 | lldb_private::Error err; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 795 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 796 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 797 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 798 | if (record.m_process_address != LLDB_INVALID_ADDRESS) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 799 | continue; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 800 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 801 | switch (record.m_sect_type) |
| 802 | { |
| 803 | case lldb::eSectionTypeInvalid: |
| 804 | case lldb::eSectionTypeDWARFDebugAbbrev: |
Tamas Berghammer | c178d4c | 2015-08-25 11:45:58 +0000 | [diff] [blame] | 805 | case lldb::eSectionTypeDWARFDebugAddr: |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 806 | case lldb::eSectionTypeDWARFDebugAranges: |
| 807 | case lldb::eSectionTypeDWARFDebugFrame: |
| 808 | case lldb::eSectionTypeDWARFDebugInfo: |
| 809 | case lldb::eSectionTypeDWARFDebugLine: |
| 810 | case lldb::eSectionTypeDWARFDebugLoc: |
| 811 | case lldb::eSectionTypeDWARFDebugMacInfo: |
| 812 | case lldb::eSectionTypeDWARFDebugPubNames: |
| 813 | case lldb::eSectionTypeDWARFDebugPubTypes: |
| 814 | case lldb::eSectionTypeDWARFDebugRanges: |
| 815 | case lldb::eSectionTypeDWARFDebugStr: |
Tamas Berghammer | c178d4c | 2015-08-25 11:45:58 +0000 | [diff] [blame] | 816 | case lldb::eSectionTypeDWARFDebugStrOffsets: |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 817 | case lldb::eSectionTypeDWARFAppleNames: |
| 818 | case lldb::eSectionTypeDWARFAppleTypes: |
| 819 | case lldb::eSectionTypeDWARFAppleNamespaces: |
| 820 | case lldb::eSectionTypeDWARFAppleObjC: |
| 821 | err.Clear(); |
| 822 | break; |
| 823 | default: |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame^] | 824 | const bool zero_memory = false; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 825 | record.m_process_address = Malloc (record.m_size, |
| 826 | record.m_alignment, |
| 827 | record.m_permissions, |
| 828 | eAllocationPolicyProcessOnly, |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame^] | 829 | zero_memory, |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 830 | err); |
| 831 | break; |
| 832 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 833 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 834 | if (!err.Success()) |
| 835 | { |
| 836 | ret = false; |
| 837 | break; |
| 838 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 839 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 840 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 841 | if (!ret) |
| 842 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 843 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 844 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 845 | if (record.m_process_address != LLDB_INVALID_ADDRESS) |
| 846 | { |
| 847 | Free(record.m_process_address, err); |
| 848 | record.m_process_address = LLDB_INVALID_ADDRESS; |
| 849 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 852 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | void |
| 857 | IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine) |
| 858 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 859 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 860 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 861 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 862 | continue; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 863 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 864 | if (record.m_section_id == eSectionIDInvalid) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 865 | continue; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 866 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 867 | engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 868 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 869 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 870 | // Trigger re-application of relocations. |
| 871 | engine.finalizeObject(); |
| 872 | } |
| 873 | |
| 874 | bool |
| 875 | IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp) |
| 876 | { |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 877 | bool wrote_something = false; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 878 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 879 | { |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 880 | if (record.m_process_address != LLDB_INVALID_ADDRESS) |
| 881 | { |
| 882 | lldb_private::Error err; |
| 883 | WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err); |
| 884 | if (err.Success()) |
| 885 | wrote_something = true; |
| 886 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 887 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 888 | return wrote_something; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 891 | void |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 892 | IRExecutionUnit::AllocationRecord::dump (Log *log) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 893 | { |
| 894 | if (!log) |
| 895 | return; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 896 | |
Sean Callanan | 44dbf11 | 2015-10-23 00:39:09 +0000 | [diff] [blame] | 897 | log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)", |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 898 | (unsigned long long)m_host_address, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 899 | (unsigned long long)m_size, |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 900 | (unsigned long long)m_process_address, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 901 | (unsigned)m_alignment, |
Sean Callanan | 44dbf11 | 2015-10-23 00:39:09 +0000 | [diff] [blame] | 902 | (unsigned)m_section_id, |
| 903 | m_name.c_str()); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 904 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 905 | |
| 906 | |
| 907 | lldb::ByteOrder |
| 908 | IRExecutionUnit::GetByteOrder () const |
| 909 | { |
| 910 | ExecutionContext exe_ctx (GetBestExecutionContextScope()); |
| 911 | return exe_ctx.GetByteOrder(); |
| 912 | } |
| 913 | |
| 914 | uint32_t |
| 915 | IRExecutionUnit::GetAddressByteSize () const |
| 916 | { |
| 917 | ExecutionContext exe_ctx (GetBestExecutionContextScope()); |
| 918 | return exe_ctx.GetAddressByteSize(); |
| 919 | } |
| 920 | |
| 921 | void |
| 922 | IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file, |
| 923 | lldb_private::Symtab &symtab) |
| 924 | { |
| 925 | // No symbols yet... |
| 926 | } |
| 927 | |
| 928 | |
| 929 | void |
| 930 | IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file, |
| 931 | lldb_private::SectionList §ion_list) |
| 932 | { |
| 933 | for (AllocationRecord &record : m_records) |
| 934 | { |
| 935 | if (record.m_size > 0) |
| 936 | { |
| 937 | lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(), |
| 938 | obj_file, |
| 939 | record.m_section_id, |
| 940 | ConstString(record.m_name), |
| 941 | record.m_sect_type, |
| 942 | record.m_process_address, |
| 943 | record.m_size, |
| 944 | record.m_host_address, // file_offset (which is the host address for the data) |
| 945 | record.m_size, // file_size |
Greg Clayton | 48672af | 2014-06-24 22:22:43 +0000 | [diff] [blame] | 946 | 0, |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 947 | record.m_permissions)); // flags |
| 948 | section_list.AddSection (section_sp); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | bool |
| 954 | IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch) |
| 955 | { |
| 956 | ExecutionContext exe_ctx (GetBestExecutionContextScope()); |
| 957 | Target *target = exe_ctx.GetTargetPtr(); |
| 958 | if (target) |
| 959 | arch = target->GetArchitecture(); |
| 960 | else |
| 961 | arch.Clear(); |
| 962 | return arch.IsValid(); |
| 963 | } |
| 964 | |
| 965 | lldb::ModuleSP |
| 966 | IRExecutionUnit::GetJITModule () |
| 967 | { |
| 968 | ExecutionContext exe_ctx (GetBestExecutionContextScope()); |
| 969 | Target *target = exe_ctx.GetTargetPtr(); |
| 970 | if (target) |
| 971 | { |
| 972 | lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this())); |
| 973 | if (jit_module_sp) |
| 974 | { |
| 975 | bool changed = false; |
| 976 | jit_module_sp->SetLoadAddress(*target, 0, true, changed); |
| 977 | } |
| 978 | return jit_module_sp; |
| 979 | } |
| 980 | return lldb::ModuleSP(); |
| 981 | } |