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 | |
| 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 14 | #include "llvm/IR/LLVMContext.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Module.h" |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 16 | #include "llvm/Support/SourceMgr.h" |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 17 | // Project includes |
| 18 | #include "lldb/Core/DataBufferHeap.h" |
| 19 | #include "lldb/Core/DataExtractor.h" |
| 20 | #include "lldb/Core/Disassembler.h" |
| 21 | #include "lldb/Core/Log.h" |
| 22 | #include "lldb/Expression/IRExecutionUnit.h" |
| 23 | #include "lldb/Target/ExecutionContext.h" |
| 24 | #include "lldb/Target/Target.h" |
| 25 | |
| 26 | using namespace lldb_private; |
| 27 | |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame^] | 28 | IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap, |
| 29 | std::unique_ptr<llvm::Module> &module_ap, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 30 | ConstString &name, |
Sean Callanan | b024d87 | 2013-04-15 17:12:47 +0000 | [diff] [blame] | 31 | const lldb::TargetSP &target_sp, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 32 | std::vector<std::string> &cpu_features) : |
Sean Callanan | b024d87 | 2013-04-15 17:12:47 +0000 | [diff] [blame] | 33 | IRMemoryMap(target_sp), |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 34 | m_context_ap(context_ap.release()), |
| 35 | m_module_ap(module_ap.release()), |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 36 | m_module(m_module_ap.get()), |
Sean Callanan | 2c04735 | 2013-03-19 23:03:21 +0000 | [diff] [blame] | 37 | m_cpu_features(cpu_features), |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 38 | m_name(name), |
| 39 | m_did_jit(false), |
| 40 | m_function_load_addr(LLDB_INVALID_ADDRESS), |
| 41 | m_function_end_load_addr(LLDB_INVALID_ADDRESS) |
| 42 | { |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | lldb::addr_t |
| 46 | IRExecutionUnit::WriteNow (const uint8_t *bytes, |
| 47 | size_t size, |
| 48 | Error &error) |
| 49 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 50 | lldb::addr_t allocation_process_addr = Malloc (size, |
| 51 | 8, |
| 52 | lldb::ePermissionsWritable | lldb::ePermissionsReadable, |
| 53 | eAllocationPolicyMirror, |
| 54 | error); |
| 55 | |
| 56 | if (!error.Success()) |
| 57 | return LLDB_INVALID_ADDRESS; |
| 58 | |
| 59 | WriteMemory(allocation_process_addr, bytes, size, error); |
| 60 | |
| 61 | if (!error.Success()) |
| 62 | { |
| 63 | Error err; |
| 64 | Free (allocation_process_addr, err); |
| 65 | |
| 66 | return LLDB_INVALID_ADDRESS; |
| 67 | } |
| 68 | |
| 69 | if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 70 | { |
| 71 | DataBufferHeap my_buffer(size, 0); |
| 72 | Error err; |
| 73 | ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err); |
| 74 | |
| 75 | if (err.Success()) |
| 76 | { |
| 77 | DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 78 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 79 | StreamString ss; |
| 80 | |
| 81 | my_extractor.Dump(&ss, 0, lldb::eFormatBytesWithASCII, 1, my_buffer.GetByteSize(), 32, allocation_process_addr, 0, 0); |
| 82 | |
| 83 | log->PutCString(ss.GetData()); |
| 84 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 87 | return allocation_process_addr; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
| 91 | IRExecutionUnit::FreeNow (lldb::addr_t allocation) |
| 92 | { |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 93 | if (allocation == LLDB_INVALID_ADDRESS) |
| 94 | return; |
| 95 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 96 | Error err; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 97 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 98 | Free(allocation, err); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | Error |
| 102 | IRExecutionUnit::DisassembleFunction (Stream &stream, |
| 103 | lldb::ProcessSP &process_wp) |
| 104 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 105 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 106 | |
| 107 | ExecutionContext exe_ctx(process_wp); |
| 108 | |
| 109 | Error ret; |
| 110 | |
| 111 | ret.Clear(); |
| 112 | |
| 113 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 114 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
| 115 | |
| 116 | for (JittedFunction &function : m_jitted_functions) |
| 117 | { |
| 118 | if (strstr(function.m_name.c_str(), m_name.AsCString())) |
| 119 | { |
| 120 | func_local_addr = function.m_local_addr; |
| 121 | func_remote_addr = function.m_remote_addr; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (func_local_addr == LLDB_INVALID_ADDRESS) |
| 126 | { |
| 127 | ret.SetErrorToGenericError(); |
| 128 | ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString()); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | if (log) |
| 133 | log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr); |
| 134 | |
| 135 | std::pair <lldb::addr_t, lldb::addr_t> func_range; |
| 136 | |
| 137 | func_range = GetRemoteRangeForLocal(func_local_addr); |
| 138 | |
| 139 | if (func_range.first == 0 && func_range.second == 0) |
| 140 | { |
| 141 | ret.SetErrorToGenericError(); |
| 142 | ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString()); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | if (log) |
| 147 | log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second); |
| 148 | |
| 149 | Target *target = exe_ctx.GetTargetPtr(); |
| 150 | if (!target) |
| 151 | { |
| 152 | ret.SetErrorToGenericError(); |
| 153 | ret.SetErrorString("Couldn't find the target"); |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0)); |
| 158 | |
| 159 | Process *process = exe_ctx.GetProcessPtr(); |
| 160 | Error err; |
| 161 | process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err); |
| 162 | |
| 163 | if (!err.Success()) |
| 164 | { |
| 165 | ret.SetErrorToGenericError(); |
| 166 | ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error")); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | ArchSpec arch(target->GetArchitecture()); |
| 171 | |
| 172 | const char *plugin_name = NULL; |
| 173 | const char *flavor_string = NULL; |
| 174 | lldb::DisassemblerSP disassembler = Disassembler::FindPlugin(arch, flavor_string, plugin_name); |
| 175 | |
| 176 | if (!disassembler) |
| 177 | { |
| 178 | ret.SetErrorToGenericError(); |
| 179 | ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName()); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | if (!process) |
| 184 | { |
| 185 | ret.SetErrorToGenericError(); |
| 186 | ret.SetErrorString("Couldn't find the process"); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | DataExtractor extractor(buffer_sp, |
| 191 | process->GetByteOrder(), |
| 192 | target->GetArchitecture().GetAddressByteSize()); |
| 193 | |
| 194 | if (log) |
| 195 | { |
| 196 | log->Printf("Function data has contents:"); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 197 | extractor.PutToLog (log, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 198 | 0, |
| 199 | extractor.GetByteSize(), |
| 200 | func_remote_addr, |
| 201 | 16, |
| 202 | DataExtractor::TypeUInt8); |
| 203 | } |
| 204 | |
Greg Clayton | 3faf47c | 2013-03-28 23:42:53 +0000 | [diff] [blame] | 205 | disassembler->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 206 | |
| 207 | InstructionList &instruction_list = disassembler->GetInstructionList(); |
| 208 | const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize(); |
| 209 | |
| 210 | for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize(); |
| 211 | instruction_index < num_instructions; |
| 212 | ++instruction_index) |
| 213 | { |
| 214 | Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get(); |
| 215 | instruction->Dump (&stream, |
| 216 | max_opcode_byte_size, |
| 217 | true, |
| 218 | true, |
| 219 | &exe_ctx); |
| 220 | stream.PutChar('\n'); |
| 221 | } |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
Sean Callanan | 44bc657 | 2013-03-27 03:09:55 +0000 | [diff] [blame] | 226 | static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie) |
| 227 | { |
| 228 | Error *err = static_cast<Error*>(Context); |
| 229 | |
| 230 | if (err && err->Success()) |
| 231 | { |
| 232 | err->SetErrorToGenericError(); |
| 233 | err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str()); |
| 234 | } |
| 235 | } |
| 236 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 237 | void |
| 238 | IRExecutionUnit::GetRunnableInfo(Error &error, |
| 239 | lldb::addr_t &func_addr, |
| 240 | lldb::addr_t &func_end) |
| 241 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 242 | lldb::ProcessSP process_sp(GetProcessWP().lock()); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 243 | |
| 244 | func_addr = LLDB_INVALID_ADDRESS; |
| 245 | func_end = LLDB_INVALID_ADDRESS; |
| 246 | |
| 247 | if (!process_sp) |
| 248 | { |
| 249 | error.SetErrorToGenericError(); |
| 250 | error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid"); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | if (m_did_jit) |
| 255 | { |
| 256 | func_addr = m_function_load_addr; |
| 257 | func_end = m_function_end_load_addr; |
| 258 | |
| 259 | return; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 260 | }; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 261 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 262 | m_did_jit = true; |
| 263 | |
| 264 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 265 | |
| 266 | std::string error_string; |
| 267 | |
| 268 | if (log) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 269 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 270 | std::string s; |
| 271 | llvm::raw_string_ostream oss(s); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 272 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 273 | m_module->print(oss, NULL); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 274 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 275 | oss.flush(); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 276 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 277 | log->Printf ("Module being sent to JIT: \n%s", s.c_str()); |
| 278 | } |
| 279 | |
| 280 | llvm::Triple triple(m_module->getTargetTriple()); |
| 281 | llvm::Function *function = m_module->getFunction (m_name.AsCString()); |
| 282 | llvm::Reloc::Model relocModel; |
| 283 | llvm::CodeModel::Model codeModel; |
| 284 | |
| 285 | if (triple.isOSBinFormatELF()) |
| 286 | { |
| 287 | relocModel = llvm::Reloc::Static; |
| 288 | // This will be small for 32-bit and large for 64-bit. |
| 289 | codeModel = llvm::CodeModel::JITDefault; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | relocModel = llvm::Reloc::PIC_; |
| 294 | codeModel = llvm::CodeModel::Small; |
| 295 | } |
| 296 | |
| 297 | m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error); |
| 298 | |
| 299 | llvm::EngineBuilder builder(m_module_ap.get()); |
| 300 | |
| 301 | builder.setEngineKind(llvm::EngineKind::JIT) |
| 302 | .setErrorStr(&error_string) |
| 303 | .setRelocationModel(relocModel) |
| 304 | .setJITMemoryManager(new MemoryManager(*this)) |
| 305 | .setOptLevel(llvm::CodeGenOpt::Less) |
| 306 | .setAllocateGVsWithCode(true) |
| 307 | .setCodeModel(codeModel) |
| 308 | .setUseMCJIT(true); |
| 309 | |
| 310 | llvm::StringRef mArch; |
| 311 | llvm::StringRef mCPU; |
| 312 | llvm::SmallVector<std::string, 0> mAttrs; |
| 313 | |
| 314 | for (std::string &feature : m_cpu_features) |
| 315 | mAttrs.push_back(feature); |
| 316 | |
| 317 | llvm::TargetMachine *target_machine = builder.selectTarget(triple, |
| 318 | mArch, |
| 319 | mCPU, |
| 320 | mAttrs); |
| 321 | |
| 322 | m_execution_engine_ap.reset(builder.create(target_machine)); |
| 323 | |
| 324 | if (!m_execution_engine_ap.get()) |
| 325 | { |
| 326 | error.SetErrorToGenericError(); |
| 327 | error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str()); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 328 | return; |
| 329 | } |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 330 | else |
| 331 | { |
| 332 | m_module_ap.release(); // ownership was transferred |
| 333 | } |
| 334 | |
| 335 | m_execution_engine_ap->DisableLazyCompilation(); |
| 336 | |
| 337 | // We don't actually need the function pointer here, this just forces it to get resolved. |
| 338 | |
| 339 | void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function); |
| 340 | |
| 341 | if (!error.Success()) |
| 342 | { |
| 343 | // We got an error through our callback! |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | if (!function) |
| 348 | { |
| 349 | error.SetErrorToGenericError(); |
| 350 | error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString()); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (!fun_ptr) |
| 355 | { |
| 356 | error.SetErrorToGenericError(); |
| 357 | error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString()); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr)); |
| 362 | |
| 363 | CommitAllocations(process_sp); |
| 364 | ReportAllocations(*m_execution_engine_ap); |
| 365 | WriteData(process_sp); |
| 366 | |
| 367 | for (JittedFunction &jitted_function : m_jitted_functions) |
| 368 | { |
| 369 | jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr); |
| 370 | |
| 371 | if (!jitted_function.m_name.compare(m_name.AsCString())) |
| 372 | { |
| 373 | AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr); |
| 374 | m_function_end_load_addr = func_range.first + func_range.second; |
| 375 | m_function_load_addr = jitted_function.m_remote_addr; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (log) |
| 380 | { |
| 381 | log->Printf("Code can be run in the target."); |
| 382 | |
| 383 | StreamString disassembly_stream; |
| 384 | |
| 385 | Error err = DisassembleFunction(disassembly_stream, process_sp); |
| 386 | |
| 387 | if (!err.Success()) |
| 388 | { |
| 389 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | func_addr = m_function_load_addr; |
| 398 | func_end = m_function_end_load_addr; |
| 399 | |
| 400 | return; |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | IRExecutionUnit::~IRExecutionUnit () |
| 404 | { |
Sean Callanan | 14b1bae | 2013-04-16 23:25:35 +0000 | [diff] [blame] | 405 | m_module_ap.reset(); |
| 406 | m_execution_engine_ap.reset(); |
| 407 | m_context_ap.reset(); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) : |
| 411 | m_default_mm_ap (llvm::JITMemoryManager::CreateDefaultMemManager()), |
| 412 | m_parent (parent) |
| 413 | { |
| 414 | } |
| 415 | |
| 416 | void |
| 417 | IRExecutionUnit::MemoryManager::setMemoryWritable () |
| 418 | { |
| 419 | m_default_mm_ap->setMemoryWritable(); |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | IRExecutionUnit::MemoryManager::setMemoryExecutable () |
| 424 | { |
| 425 | m_default_mm_ap->setMemoryExecutable(); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | uint8_t * |
| 430 | IRExecutionUnit::MemoryManager::startFunctionBody(const llvm::Function *F, |
| 431 | uintptr_t &ActualSize) |
| 432 | { |
| 433 | return m_default_mm_ap->startFunctionBody(F, ActualSize); |
| 434 | } |
| 435 | |
| 436 | uint8_t * |
| 437 | IRExecutionUnit::MemoryManager::allocateStub(const llvm::GlobalValue* F, |
| 438 | unsigned StubSize, |
| 439 | unsigned Alignment) |
| 440 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 441 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 442 | |
| 443 | uint8_t *return_value = m_default_mm_ap->allocateStub(F, StubSize, Alignment); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 444 | |
| 445 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
| 446 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 447 | StubSize, |
| 448 | Alignment)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 449 | |
| 450 | if (log) |
| 451 | { |
| 452 | log->Printf("IRExecutionUnit::allocateStub (F=%p, StubSize=%u, Alignment=%u) = %p", |
| 453 | F, StubSize, Alignment, return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 454 | } |
Sean Callanan | 2c04735 | 2013-03-19 23:03:21 +0000 | [diff] [blame] | 455 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 456 | return return_value; |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | IRExecutionUnit::MemoryManager::endFunctionBody(const llvm::Function *F, |
| 461 | uint8_t *FunctionStart, |
| 462 | uint8_t *FunctionEnd) |
| 463 | { |
| 464 | m_default_mm_ap->endFunctionBody(F, FunctionStart, FunctionEnd); |
| 465 | } |
| 466 | |
| 467 | uint8_t * |
| 468 | IRExecutionUnit::MemoryManager::allocateSpace(intptr_t Size, unsigned Alignment) |
| 469 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 470 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 471 | |
| 472 | uint8_t *return_value = m_default_mm_ap->allocateSpace(Size, Alignment); |
| 473 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 474 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
| 475 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 476 | Size, |
| 477 | Alignment)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 478 | |
| 479 | if (log) |
| 480 | { |
| 481 | log->Printf("IRExecutionUnit::allocateSpace(Size=%" PRIu64 ", Alignment=%u) = %p", |
| 482 | (uint64_t)Size, Alignment, return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 483 | } |
Sean Callanan | 2c04735 | 2013-03-19 23:03:21 +0000 | [diff] [blame] | 484 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 485 | return return_value; |
| 486 | } |
| 487 | |
| 488 | uint8_t * |
| 489 | IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size, |
| 490 | unsigned Alignment, |
| 491 | unsigned SectionID) |
| 492 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 493 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 494 | |
| 495 | uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID); |
| 496 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 497 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
Sean Callanan | ec1c0b3 | 2013-04-09 01:13:08 +0000 | [diff] [blame] | 498 | lldb::ePermissionsReadable | lldb::ePermissionsExecutable, |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 499 | Size, |
| 500 | Alignment, |
| 501 | SectionID)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 502 | |
| 503 | if (log) |
| 504 | { |
| 505 | log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", |
| 506 | (uint64_t)Size, Alignment, SectionID, return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 507 | } |
Sean Callanan | 2c04735 | 2013-03-19 23:03:21 +0000 | [diff] [blame] | 508 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 509 | return return_value; |
| 510 | } |
| 511 | |
| 512 | uint8_t * |
| 513 | IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size, |
| 514 | unsigned Alignment, |
| 515 | unsigned SectionID, |
| 516 | bool IsReadOnly) |
| 517 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 518 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 519 | |
| 520 | uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, IsReadOnly); |
| 521 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 522 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
| 523 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 524 | Size, |
| 525 | Alignment, |
| 526 | SectionID)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 527 | if (log) |
| 528 | { |
| 529 | log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p", |
| 530 | (uint64_t)Size, Alignment, SectionID, return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 531 | } |
Sean Callanan | 2c04735 | 2013-03-19 23:03:21 +0000 | [diff] [blame] | 532 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 533 | return return_value; |
| 534 | } |
| 535 | |
| 536 | uint8_t * |
| 537 | IRExecutionUnit::MemoryManager::allocateGlobal(uintptr_t Size, |
| 538 | unsigned Alignment) |
| 539 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 540 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 541 | |
| 542 | uint8_t *return_value = m_default_mm_ap->allocateGlobal(Size, Alignment); |
| 543 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 544 | m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value, |
| 545 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 546 | Size, |
| 547 | Alignment)); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 548 | |
| 549 | if (log) |
| 550 | { |
| 551 | log->Printf("IRExecutionUnit::allocateGlobal(Size=0x%" PRIx64 ", Alignment=%u) = %p", |
| 552 | (uint64_t)Size, Alignment, return_value); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 555 | return return_value; |
| 556 | } |
| 557 | |
| 558 | void |
| 559 | IRExecutionUnit::MemoryManager::deallocateFunctionBody(void *Body) |
| 560 | { |
| 561 | m_default_mm_ap->deallocateFunctionBody(Body); |
| 562 | } |
| 563 | |
| 564 | uint8_t* |
| 565 | IRExecutionUnit::MemoryManager::startExceptionTable(const llvm::Function* F, |
| 566 | uintptr_t &ActualSize) |
| 567 | { |
| 568 | return m_default_mm_ap->startExceptionTable(F, ActualSize); |
| 569 | } |
| 570 | |
| 571 | void |
| 572 | IRExecutionUnit::MemoryManager::endExceptionTable(const llvm::Function *F, |
| 573 | uint8_t *TableStart, |
| 574 | uint8_t *TableEnd, |
| 575 | uint8_t* FrameRegister) |
| 576 | { |
| 577 | m_default_mm_ap->endExceptionTable(F, TableStart, TableEnd, FrameRegister); |
| 578 | } |
| 579 | |
| 580 | void |
| 581 | IRExecutionUnit::MemoryManager::deallocateExceptionTable(void *ET) |
| 582 | { |
| 583 | m_default_mm_ap->deallocateExceptionTable (ET); |
| 584 | } |
| 585 | |
| 586 | lldb::addr_t |
| 587 | IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address) |
| 588 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 589 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 590 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 591 | if (local_address >= record.m_host_address && |
| 592 | local_address < record.m_host_address + record.m_size) |
| 593 | { |
| 594 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 595 | return LLDB_INVALID_ADDRESS; |
| 596 | } |
| 597 | |
| 598 | return record.m_process_address + (local_address - record.m_host_address); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | return LLDB_INVALID_ADDRESS; |
| 602 | } |
| 603 | |
| 604 | IRExecutionUnit::AddrRange |
| 605 | IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address) |
| 606 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 607 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 608 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 609 | if (local_address >= record.m_host_address && |
| 610 | local_address < record.m_host_address + record.m_size) |
| 611 | { |
| 612 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 613 | return AddrRange(0, 0); |
| 614 | |
| 615 | return AddrRange(record.m_process_address, record.m_size); |
| 616 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | return AddrRange (0, 0); |
| 620 | } |
| 621 | |
| 622 | bool |
| 623 | IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp) |
| 624 | { |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 625 | bool ret = true; |
| 626 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 627 | lldb_private::Error err; |
| 628 | |
| 629 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 630 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 631 | if (record.m_process_address != LLDB_INVALID_ADDRESS) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 632 | continue; |
| 633 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 634 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 635 | record.m_process_address = Malloc(record.m_size, |
| 636 | record.m_alignment, |
| 637 | record.m_permissions, |
| 638 | eAllocationPolicyProcessOnly, |
| 639 | err); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 640 | |
| 641 | if (!err.Success()) |
| 642 | { |
| 643 | ret = false; |
| 644 | break; |
| 645 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | if (!ret) |
| 649 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 650 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 651 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 652 | if (record.m_process_address != LLDB_INVALID_ADDRESS) |
| 653 | { |
| 654 | Free(record.m_process_address, err); |
| 655 | record.m_process_address = LLDB_INVALID_ADDRESS; |
| 656 | } |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | void |
| 664 | IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine) |
| 665 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 666 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 667 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 668 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 669 | continue; |
| 670 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 671 | if (record.m_section_id == eSectionIDInvalid) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 672 | continue; |
| 673 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 674 | engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 675 | } |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 676 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 677 | // Trigger re-application of relocations. |
| 678 | engine.finalizeObject(); |
| 679 | } |
| 680 | |
| 681 | bool |
| 682 | IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp) |
| 683 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 684 | for (AllocationRecord &record : m_records) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 685 | { |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 686 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 687 | return false; |
| 688 | |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 689 | lldb_private::Error err; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 690 | |
| 691 | WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err); |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | void |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 698 | IRExecutionUnit::AllocationRecord::dump (Log *log) |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 699 | { |
| 700 | if (!log) |
| 701 | return; |
| 702 | |
| 703 | 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] | 704 | (unsigned long long)m_host_address, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 705 | (unsigned long long)m_size, |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 706 | (unsigned long long)m_process_address, |
Sean Callanan | 8dfb68e | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 707 | (unsigned)m_alignment, |
| 708 | (unsigned)m_section_id); |
| 709 | } |