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