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