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