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