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