| 1 | //===-- IRExecutionUnit.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 10 | #include "llvm/ExecutionEngine/ObjectCache.h" |
| 11 | #include "llvm/IR/Constants.h" |
| 12 | #include "llvm/IR/DiagnosticHandler.h" |
| 13 | #include "llvm/IR/DiagnosticInfo.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
| 15 | #include "llvm/IR/Module.h" |
| 16 | #include "llvm/Support/SourceMgr.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
| 19 | #include "lldb/Core/Debugger.h" |
| 20 | #include "lldb/Core/Disassembler.h" |
| 21 | #include "lldb/Core/Module.h" |
| 22 | #include "lldb/Core/Section.h" |
| 23 | #include "lldb/Expression/IRExecutionUnit.h" |
| 24 | #include "lldb/Expression/ObjectFileJIT.h" |
| 25 | #include "lldb/Host/HostInfo.h" |
| 26 | #include "lldb/Symbol/CompileUnit.h" |
| 27 | #include "lldb/Symbol/SymbolContext.h" |
| 28 | #include "lldb/Symbol/SymbolFile.h" |
| 29 | #include "lldb/Symbol/SymbolVendor.h" |
| 30 | #include "lldb/Target/ExecutionContext.h" |
| 31 | #include "lldb/Target/Language.h" |
| 32 | #include "lldb/Target/LanguageRuntime.h" |
| 33 | #include "lldb/Target/Target.h" |
| 34 | #include "lldb/Utility/DataBufferHeap.h" |
| 35 | #include "lldb/Utility/DataExtractor.h" |
| 36 | #include "lldb/Utility/LLDBAssert.h" |
| 37 | #include "lldb/Utility/LLDBLog.h" |
| 38 | #include "lldb/Utility/Log.h" |
| 39 | |
| 40 | #include <optional> |
| 41 | |
| 42 | using namespace lldb_private; |
| 43 | |
| 44 | IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_up, |
| 45 | std::unique_ptr<llvm::Module> &module_up, |
| 46 | ConstString &name, |
| 47 | const lldb::TargetSP &target_sp, |
| 48 | const SymbolContext &sym_ctx, |
| 49 | std::vector<std::string> &cpu_features) |
| 50 | : IRMemoryMap(target_sp), m_context_up(context_up.release()), |
| 51 | m_module_up(module_up.release()), m_module(m_module_up.get()), |
| 52 | m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx), |
| 53 | m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS), |
| 54 | m_function_end_load_addr(LLDB_INVALID_ADDRESS), |
| 55 | m_reported_allocations(false), m_preferred_modules() {} |
| 56 | |
| 57 | lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size, |
| 58 | Status &error) { |
| 59 | const bool zero_memory = false; |
| 60 | lldb::addr_t allocation_process_addr = |
| 61 | Malloc(size, alignment: 8, permissions: lldb::ePermissionsWritable | lldb::ePermissionsReadable, |
| 62 | policy: eAllocationPolicyMirror, zero_memory, error); |
| 63 | |
| 64 | if (!error.Success()) |
| 65 | return LLDB_INVALID_ADDRESS; |
| 66 | |
| 67 | WriteMemory(process_address: allocation_process_addr, bytes, size, error); |
| 68 | |
| 69 | if (!error.Success()) { |
| 70 | Status err; |
| 71 | Free(process_address: allocation_process_addr, error&: err); |
| 72 | |
| 73 | return LLDB_INVALID_ADDRESS; |
| 74 | } |
| 75 | |
| 76 | if (Log *log = GetLog(mask: LLDBLog::Expressions)) { |
| 77 | DataBufferHeap my_buffer(size, 0); |
| 78 | Status err; |
| 79 | ReadMemory(bytes: my_buffer.GetBytes(), process_address: allocation_process_addr, size, error&: err); |
| 80 | |
| 81 | if (err.Success()) { |
| 82 | DataExtractor (my_buffer.GetBytes(), my_buffer.GetByteSize(), |
| 83 | lldb::eByteOrderBig, 8); |
| 84 | my_extractor.PutToLog(log, offset: 0, length: my_buffer.GetByteSize(), |
| 85 | base_addr: allocation_process_addr, num_per_line: 16, |
| 86 | type: DataExtractor::TypeUInt8); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return allocation_process_addr; |
| 91 | } |
| 92 | |
| 93 | void IRExecutionUnit::FreeNow(lldb::addr_t allocation) { |
| 94 | if (allocation == LLDB_INVALID_ADDRESS) |
| 95 | return; |
| 96 | |
| 97 | Status err; |
| 98 | |
| 99 | Free(process_address: allocation, error&: err); |
| 100 | } |
| 101 | |
| 102 | Status IRExecutionUnit::DisassembleFunction(Stream &stream, |
| 103 | lldb::ProcessSP &process_wp) { |
| 104 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 105 | |
| 106 | ExecutionContext exe_ctx(process_wp); |
| 107 | |
| 108 | Status ret; |
| 109 | |
| 110 | ret.Clear(); |
| 111 | |
| 112 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 113 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
| 114 | |
| 115 | for (JittedFunction &function : m_jitted_functions) { |
| 116 | if (function.m_name == m_name) { |
| 117 | func_local_addr = function.m_local_addr; |
| 118 | func_remote_addr = function.m_remote_addr; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (func_local_addr == LLDB_INVALID_ADDRESS) { |
| 123 | ret = Status::FromErrorStringWithFormat( |
| 124 | format: "Couldn't find function %s for disassembly" , m_name.AsCString()); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | LLDB_LOGF(log, |
| 129 | "Found function, has local address 0x%" PRIx64 |
| 130 | " and remote address 0x%" PRIx64, |
| 131 | (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(local_address: func_local_addr); |
| 136 | |
| 137 | if (func_range.first == 0 && func_range.second == 0) { |
| 138 | ret = Status::FromErrorStringWithFormat( |
| 139 | format: "Couldn't find code range for function %s" , m_name.AsCString()); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | LLDB_LOGF(log, "Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]" , |
| 144 | func_range.first, func_range.second); |
| 145 | |
| 146 | Target *target = exe_ctx.GetTargetPtr(); |
| 147 | if (!target) { |
| 148 | ret = Status::FromErrorString(str: "Couldn't find the target" ); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | lldb::WritableDataBufferSP buffer_sp( |
| 153 | new DataBufferHeap(func_range.second, 0)); |
| 154 | |
| 155 | Process *process = exe_ctx.GetProcessPtr(); |
| 156 | Status err; |
| 157 | process->ReadMemory(vm_addr: func_remote_addr, buf: buffer_sp->GetBytes(), |
| 158 | size: buffer_sp->GetByteSize(), error&: err); |
| 159 | |
| 160 | if (!err.Success()) { |
| 161 | ret = Status::FromErrorStringWithFormat(format: "Couldn't read from process: %s" , |
| 162 | err.AsCString(default_error_str: "unknown error" )); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | ArchSpec arch(target->GetArchitecture()); |
| 167 | |
| 168 | const char *plugin_name = nullptr; |
| 169 | const char *flavor_string = nullptr; |
| 170 | const char *cpu_string = nullptr; |
| 171 | const char *features_string = nullptr; |
| 172 | lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin( |
| 173 | arch, flavor: flavor_string, cpu: cpu_string, features: features_string, plugin_name); |
| 174 | |
| 175 | if (!disassembler_sp) { |
| 176 | ret = Status::FromErrorStringWithFormat( |
| 177 | format: "Unable to find disassembler plug-in for %s architecture." , |
| 178 | arch.GetArchitectureName()); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | if (!process) { |
| 183 | ret = Status::FromErrorString(str: "Couldn't find the process" ); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | DataExtractor (buffer_sp, process->GetByteOrder(), |
| 188 | target->GetArchitecture().GetAddressByteSize()); |
| 189 | |
| 190 | if (log) { |
| 191 | LLDB_LOGF(log, "Function data has contents:" ); |
| 192 | extractor.PutToLog(log, offset: 0, length: extractor.GetByteSize(), base_addr: func_remote_addr, num_per_line: 16, |
| 193 | type: DataExtractor::TypeUInt8); |
| 194 | } |
| 195 | |
| 196 | disassembler_sp->DecodeInstructions(base_addr: Address(func_remote_addr), data: extractor, data_offset: 0, |
| 197 | UINT32_MAX, append: false, data_from_file: false); |
| 198 | |
| 199 | InstructionList &instruction_list = disassembler_sp->GetInstructionList(); |
| 200 | instruction_list.Dump(s: &stream, show_address: true, show_bytes: true, /*show_control_flow_kind=*/false, |
| 201 | exe_ctx: &exe_ctx); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | namespace { |
| 207 | struct IRExecDiagnosticHandler : public llvm::DiagnosticHandler { |
| 208 | Status *err; |
| 209 | IRExecDiagnosticHandler(Status *err) : err(err) {} |
| 210 | bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override { |
| 211 | if (DI.getSeverity() == llvm::DS_Error) { |
| 212 | const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(Val: DI); |
| 213 | if (err && err->Success()) { |
| 214 | *err = Status::FromErrorStringWithFormat( |
| 215 | format: "IRExecution error: %s" , |
| 216 | DISM.getSMDiag().getMessage().str().c_str()); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | }; |
| 223 | } // namespace |
| 224 | |
| 225 | void IRExecutionUnit::ReportSymbolLookupError(ConstString name) { |
| 226 | m_failed_lookups.push_back(x: name); |
| 227 | } |
| 228 | |
| 229 | void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr, |
| 230 | lldb::addr_t &func_end) { |
| 231 | lldb::ProcessSP process_sp(GetProcessWP().lock()); |
| 232 | |
| 233 | static std::recursive_mutex s_runnable_info_mutex; |
| 234 | |
| 235 | func_addr = LLDB_INVALID_ADDRESS; |
| 236 | func_end = LLDB_INVALID_ADDRESS; |
| 237 | |
| 238 | if (!process_sp) { |
| 239 | error = |
| 240 | Status::FromErrorString(str: "Couldn't write the JIT compiled code into the " |
| 241 | "process because the process is invalid" ); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (m_did_jit) { |
| 246 | func_addr = m_function_load_addr; |
| 247 | func_end = m_function_end_load_addr; |
| 248 | |
| 249 | return; |
| 250 | }; |
| 251 | |
| 252 | std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex); |
| 253 | |
| 254 | m_did_jit = true; |
| 255 | |
| 256 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 257 | |
| 258 | std::string error_string; |
| 259 | |
| 260 | if (log) { |
| 261 | std::string s; |
| 262 | llvm::raw_string_ostream oss(s); |
| 263 | |
| 264 | m_module->print(OS&: oss, AAW: nullptr); |
| 265 | |
| 266 | LLDB_LOGF(log, "Module being sent to JIT: \n%s" , s.c_str()); |
| 267 | } |
| 268 | |
| 269 | m_module_up->getContext().setDiagnosticHandler( |
| 270 | DH: std::make_unique<IRExecDiagnosticHandler>(args: &error)); |
| 271 | |
| 272 | llvm::EngineBuilder builder(std::move(m_module_up)); |
| 273 | llvm::Triple triple(m_module->getTargetTriple()); |
| 274 | |
| 275 | builder.setEngineKind(llvm::EngineKind::JIT) |
| 276 | .setErrorStr(&error_string) |
| 277 | .setRelocationModel(triple.isOSBinFormatMachO() ? llvm::Reloc::PIC_ |
| 278 | : llvm::Reloc::Static) |
| 279 | .setMCJITMemoryManager(std::make_unique<MemoryManager>(args&: *this)) |
| 280 | .setOptLevel(llvm::CodeGenOptLevel::Less); |
| 281 | |
| 282 | // Resulted jitted code can be placed too far from the code in the binary |
| 283 | // and thus can contain more than +-2GB jumps, that are not available |
| 284 | // in RISC-V without large code model. |
| 285 | if (triple.isRISCV64()) |
| 286 | builder.setCodeModel(llvm::CodeModel::Large); |
| 287 | |
| 288 | llvm::StringRef mArch; |
| 289 | llvm::StringRef mCPU; |
| 290 | llvm::SmallVector<std::string, 0> mAttrs; |
| 291 | |
| 292 | for (std::string &feature : m_cpu_features) |
| 293 | mAttrs.push_back(Elt: feature); |
| 294 | |
| 295 | llvm::TargetMachine *target_machine = |
| 296 | builder.selectTarget(TargetTriple: triple, MArch: mArch, MCPU: mCPU, MAttrs: mAttrs); |
| 297 | |
| 298 | m_execution_engine_up.reset(p: builder.create(TM: target_machine)); |
| 299 | |
| 300 | if (!m_execution_engine_up) { |
| 301 | error = Status::FromErrorStringWithFormat(format: "Couldn't JIT the function: %s" , |
| 302 | error_string.c_str()); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | m_strip_underscore = |
| 307 | (m_execution_engine_up->getDataLayout().getGlobalPrefix() == '_'); |
| 308 | |
| 309 | class ObjectDumper : public llvm::ObjectCache { |
| 310 | public: |
| 311 | ObjectDumper(FileSpec output_dir) : m_out_dir(output_dir) {} |
| 312 | void notifyObjectCompiled(const llvm::Module *module, |
| 313 | llvm::MemoryBufferRef object) override { |
| 314 | int fd = 0; |
| 315 | llvm::SmallVector<char, 256> result_path; |
| 316 | std::string object_name_model = |
| 317 | "jit-object-" + module->getModuleIdentifier() + "-%%%.o" ; |
| 318 | FileSpec model_spec |
| 319 | = m_out_dir.CopyByAppendingPathComponent(component: object_name_model); |
| 320 | std::string model_path = model_spec.GetPath(); |
| 321 | |
| 322 | std::error_code result |
| 323 | = llvm::sys::fs::createUniqueFile(Model: model_path, ResultFD&: fd, ResultPath&: result_path); |
| 324 | if (!result) { |
| 325 | llvm::raw_fd_ostream fds(fd, true); |
| 326 | fds.write(Ptr: object.getBufferStart(), Size: object.getBufferSize()); |
| 327 | } |
| 328 | } |
| 329 | std::unique_ptr<llvm::MemoryBuffer> |
| 330 | getObject(const llvm::Module *module) override { |
| 331 | // Return nothing - we're just abusing the object-cache mechanism to dump |
| 332 | // objects. |
| 333 | return nullptr; |
| 334 | } |
| 335 | private: |
| 336 | FileSpec m_out_dir; |
| 337 | }; |
| 338 | |
| 339 | FileSpec save_objects_dir = process_sp->GetTarget().GetSaveJITObjectsDir(); |
| 340 | if (save_objects_dir) { |
| 341 | m_object_cache_up = std::make_unique<ObjectDumper>(args&: save_objects_dir); |
| 342 | m_execution_engine_up->setObjectCache(m_object_cache_up.get()); |
| 343 | } |
| 344 | |
| 345 | // Make sure we see all sections, including ones that don't have |
| 346 | // relocations... |
| 347 | m_execution_engine_up->setProcessAllSections(true); |
| 348 | |
| 349 | m_execution_engine_up->DisableLazyCompilation(); |
| 350 | |
| 351 | for (llvm::Function &function : *m_module) { |
| 352 | if (function.isDeclaration() || function.hasPrivateLinkage()) |
| 353 | continue; |
| 354 | |
| 355 | const bool external = !function.hasLocalLinkage(); |
| 356 | |
| 357 | void *fun_ptr = m_execution_engine_up->getPointerToFunction(F: &function); |
| 358 | |
| 359 | if (!error.Success()) { |
| 360 | // We got an error through our callback! |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | if (!fun_ptr) { |
| 365 | error = Status::FromErrorStringWithFormat( |
| 366 | format: "'%s' was in the JITted module but wasn't lowered" , |
| 367 | function.getName().str().c_str()); |
| 368 | return; |
| 369 | } |
| 370 | m_jitted_functions.push_back(x: JittedFunction( |
| 371 | function.getName().str().c_str(), external, reinterpret_cast<uintptr_t>(fun_ptr))); |
| 372 | } |
| 373 | |
| 374 | CommitAllocations(process_sp); |
| 375 | ReportAllocations(engine&: *m_execution_engine_up); |
| 376 | |
| 377 | // We have to do this after calling ReportAllocations because for the MCJIT, |
| 378 | // getGlobalValueAddress will cause the JIT to perform all relocations. That |
| 379 | // can only be done once, and has to happen after we do the remapping from |
| 380 | // local -> remote. That means we don't know the local address of the |
| 381 | // Variables, but we don't need that for anything, so that's okay. |
| 382 | |
| 383 | std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this]( |
| 384 | llvm::GlobalValue &val) { |
| 385 | if (val.hasExternalLinkage() && !val.isDeclaration()) { |
| 386 | uint64_t var_ptr_addr = |
| 387 | m_execution_engine_up->getGlobalValueAddress(Name: val.getName().str()); |
| 388 | |
| 389 | lldb::addr_t remote_addr = GetRemoteAddressForLocal(local_address: var_ptr_addr); |
| 390 | |
| 391 | // This is a really unfortunae API that sometimes returns local addresses |
| 392 | // and sometimes returns remote addresses, based on whether the variable |
| 393 | // was relocated during ReportAllocations or not. |
| 394 | |
| 395 | if (remote_addr == LLDB_INVALID_ADDRESS) { |
| 396 | remote_addr = var_ptr_addr; |
| 397 | } |
| 398 | |
| 399 | if (var_ptr_addr != 0) |
| 400 | m_jitted_global_variables.push_back(x: JittedGlobalVariable( |
| 401 | val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr)); |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | for (llvm::GlobalVariable &global_var : m_module->globals()) { |
| 406 | RegisterOneValue(global_var); |
| 407 | } |
| 408 | |
| 409 | for (llvm::GlobalAlias &global_alias : m_module->aliases()) { |
| 410 | RegisterOneValue(global_alias); |
| 411 | } |
| 412 | |
| 413 | WriteData(process_sp); |
| 414 | |
| 415 | if (m_failed_lookups.size()) { |
| 416 | StreamString ss; |
| 417 | |
| 418 | ss.PutCString(cstr: "Couldn't look up symbols:\n" ); |
| 419 | |
| 420 | bool emitNewLine = false; |
| 421 | |
| 422 | for (ConstString failed_lookup : m_failed_lookups) { |
| 423 | if (emitNewLine) |
| 424 | ss.PutCString(cstr: "\n" ); |
| 425 | emitNewLine = true; |
| 426 | ss.PutCString(cstr: " " ); |
| 427 | ss.PutCString(cstr: Mangled(failed_lookup).GetDemangledName().GetStringRef()); |
| 428 | } |
| 429 | |
| 430 | m_failed_lookups.clear(); |
| 431 | ss.PutCString( |
| 432 | cstr: "\nHint: The expression tried to call a function that is not present " |
| 433 | "in the target, perhaps because it was optimized out by the compiler." ); |
| 434 | error = Status(ss.GetString().str()); |
| 435 | |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | m_function_load_addr = LLDB_INVALID_ADDRESS; |
| 440 | m_function_end_load_addr = LLDB_INVALID_ADDRESS; |
| 441 | |
| 442 | for (JittedFunction &jitted_function : m_jitted_functions) { |
| 443 | jitted_function.m_remote_addr = |
| 444 | GetRemoteAddressForLocal(local_address: jitted_function.m_local_addr); |
| 445 | |
| 446 | if (!m_name.IsEmpty() && jitted_function.m_name == m_name) { |
| 447 | AddrRange func_range = |
| 448 | GetRemoteRangeForLocal(local_address: jitted_function.m_local_addr); |
| 449 | m_function_end_load_addr = func_range.first + func_range.second; |
| 450 | m_function_load_addr = jitted_function.m_remote_addr; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (log) { |
| 455 | LLDB_LOGF(log, "Code can be run in the target." ); |
| 456 | |
| 457 | StreamString disassembly_stream; |
| 458 | |
| 459 | Status err = DisassembleFunction(stream&: disassembly_stream, process_wp&: process_sp); |
| 460 | |
| 461 | if (!err.Success()) { |
| 462 | LLDB_LOGF(log, "Couldn't disassemble function : %s" , |
| 463 | err.AsCString("unknown error" )); |
| 464 | } else { |
| 465 | LLDB_LOGF(log, "Function disassembly:\n%s" , disassembly_stream.GetData()); |
| 466 | } |
| 467 | |
| 468 | LLDB_LOGF(log, "Sections: " ); |
| 469 | for (AllocationRecord &record : m_records) { |
| 470 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 471 | record.dump(log); |
| 472 | |
| 473 | DataBufferHeap my_buffer(record.m_size, 0); |
| 474 | Status err; |
| 475 | ReadMemory(bytes: my_buffer.GetBytes(), process_address: record.m_process_address, |
| 476 | size: record.m_size, error&: err); |
| 477 | |
| 478 | if (err.Success()) { |
| 479 | DataExtractor (my_buffer.GetBytes(), |
| 480 | my_buffer.GetByteSize(), |
| 481 | lldb::eByteOrderBig, 8); |
| 482 | my_extractor.PutToLog(log, offset: 0, length: my_buffer.GetByteSize(), |
| 483 | base_addr: record.m_process_address, num_per_line: 16, |
| 484 | type: DataExtractor::TypeUInt8); |
| 485 | } |
| 486 | } else { |
| 487 | record.dump(log); |
| 488 | |
| 489 | DataExtractor ((const void *)record.m_host_address, |
| 490 | record.m_size, lldb::eByteOrderBig, 8); |
| 491 | my_extractor.PutToLog(log, offset: 0, length: record.m_size, base_addr: record.m_host_address, num_per_line: 16, |
| 492 | type: DataExtractor::TypeUInt8); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | func_addr = m_function_load_addr; |
| 498 | func_end = m_function_end_load_addr; |
| 499 | } |
| 500 | |
| 501 | IRExecutionUnit::~IRExecutionUnit() { |
| 502 | m_module_up.reset(); |
| 503 | m_execution_engine_up.reset(); |
| 504 | m_context_up.reset(); |
| 505 | } |
| 506 | |
| 507 | IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent) |
| 508 | : m_default_mm_up(new llvm::SectionMemoryManager()), m_parent(parent) {} |
| 509 | |
| 510 | IRExecutionUnit::MemoryManager::~MemoryManager() = default; |
| 511 | |
| 512 | lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName( |
| 513 | const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) { |
| 514 | lldb::SectionType sect_type = lldb::eSectionTypeCode; |
| 515 | switch (alloc_kind) { |
| 516 | case AllocationKind::Stub: |
| 517 | sect_type = lldb::eSectionTypeCode; |
| 518 | break; |
| 519 | case AllocationKind::Code: |
| 520 | sect_type = lldb::eSectionTypeCode; |
| 521 | break; |
| 522 | case AllocationKind::Data: |
| 523 | sect_type = lldb::eSectionTypeData; |
| 524 | break; |
| 525 | case AllocationKind::Global: |
| 526 | sect_type = lldb::eSectionTypeData; |
| 527 | break; |
| 528 | case AllocationKind::Bytes: |
| 529 | sect_type = lldb::eSectionTypeOther; |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | if (!name.empty()) { |
| 534 | if (name == "__text" || name == ".text" ) |
| 535 | sect_type = lldb::eSectionTypeCode; |
| 536 | else if (name == "__data" || name == ".data" ) |
| 537 | sect_type = lldb::eSectionTypeCode; |
| 538 | else if (name.starts_with(Prefix: "__debug_" ) || name.starts_with(Prefix: ".debug_" )) { |
| 539 | const uint32_t name_idx = name[0] == '_' ? 8 : 7; |
| 540 | llvm::StringRef dwarf_name(name.substr(Start: name_idx)); |
| 541 | switch (dwarf_name[0]) { |
| 542 | case 'a': |
| 543 | if (dwarf_name == "abbrev" ) |
| 544 | sect_type = lldb::eSectionTypeDWARFDebugAbbrev; |
| 545 | else if (dwarf_name == "aranges" ) |
| 546 | sect_type = lldb::eSectionTypeDWARFDebugAranges; |
| 547 | else if (dwarf_name == "addr" ) |
| 548 | sect_type = lldb::eSectionTypeDWARFDebugAddr; |
| 549 | break; |
| 550 | |
| 551 | case 'f': |
| 552 | if (dwarf_name == "frame" ) |
| 553 | sect_type = lldb::eSectionTypeDWARFDebugFrame; |
| 554 | break; |
| 555 | |
| 556 | case 'i': |
| 557 | if (dwarf_name == "info" ) |
| 558 | sect_type = lldb::eSectionTypeDWARFDebugInfo; |
| 559 | break; |
| 560 | |
| 561 | case 'l': |
| 562 | if (dwarf_name == "line" ) |
| 563 | sect_type = lldb::eSectionTypeDWARFDebugLine; |
| 564 | else if (dwarf_name == "loc" ) |
| 565 | sect_type = lldb::eSectionTypeDWARFDebugLoc; |
| 566 | else if (dwarf_name == "loclists" ) |
| 567 | sect_type = lldb::eSectionTypeDWARFDebugLocLists; |
| 568 | break; |
| 569 | |
| 570 | case 'm': |
| 571 | if (dwarf_name == "macinfo" ) |
| 572 | sect_type = lldb::eSectionTypeDWARFDebugMacInfo; |
| 573 | break; |
| 574 | |
| 575 | case 'p': |
| 576 | if (dwarf_name == "pubnames" ) |
| 577 | sect_type = lldb::eSectionTypeDWARFDebugPubNames; |
| 578 | else if (dwarf_name == "pubtypes" ) |
| 579 | sect_type = lldb::eSectionTypeDWARFDebugPubTypes; |
| 580 | break; |
| 581 | |
| 582 | case 's': |
| 583 | if (dwarf_name == "str" ) |
| 584 | sect_type = lldb::eSectionTypeDWARFDebugStr; |
| 585 | else if (dwarf_name == "str_offsets" ) |
| 586 | sect_type = lldb::eSectionTypeDWARFDebugStrOffsets; |
| 587 | break; |
| 588 | |
| 589 | case 'r': |
| 590 | if (dwarf_name == "ranges" ) |
| 591 | sect_type = lldb::eSectionTypeDWARFDebugRanges; |
| 592 | break; |
| 593 | |
| 594 | default: |
| 595 | break; |
| 596 | } |
| 597 | } else if (name.starts_with(Prefix: "__apple_" ) || name.starts_with(Prefix: ".apple_" )) |
| 598 | sect_type = lldb::eSectionTypeInvalid; |
| 599 | else if (name == "__objc_imageinfo" ) |
| 600 | sect_type = lldb::eSectionTypeOther; |
| 601 | } |
| 602 | return sect_type; |
| 603 | } |
| 604 | |
| 605 | uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection( |
| 606 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 607 | llvm::StringRef SectionName) { |
| 608 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 609 | |
| 610 | uint8_t *return_value = m_default_mm_up->allocateCodeSection( |
| 611 | Size, Alignment, SectionID, SectionName); |
| 612 | |
| 613 | m_parent.m_records.push_back(x: AllocationRecord( |
| 614 | (uintptr_t)return_value, |
| 615 | lldb::ePermissionsReadable | lldb::ePermissionsExecutable, |
| 616 | GetSectionTypeFromSectionName(name: SectionName, alloc_kind: AllocationKind::Code), Size, |
| 617 | Alignment, SectionID, SectionName.str().c_str())); |
| 618 | |
| 619 | LLDB_LOGF(log, |
| 620 | "IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 |
| 621 | ", Alignment=%u, SectionID=%u) = %p" , |
| 622 | (uint64_t)Size, Alignment, SectionID, (void *)return_value); |
| 623 | |
| 624 | if (m_parent.m_reported_allocations) { |
| 625 | Status err; |
| 626 | lldb::ProcessSP process_sp = |
| 627 | m_parent.GetBestExecutionContextScope()->CalculateProcess(); |
| 628 | |
| 629 | m_parent.CommitOneAllocation(process_sp, error&: err, record&: m_parent.m_records.back()); |
| 630 | } |
| 631 | |
| 632 | return return_value; |
| 633 | } |
| 634 | |
| 635 | uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection( |
| 636 | uintptr_t Size, unsigned Alignment, unsigned SectionID, |
| 637 | llvm::StringRef SectionName, bool IsReadOnly) { |
| 638 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 639 | |
| 640 | uint8_t *return_value = m_default_mm_up->allocateDataSection( |
| 641 | Size, Alignment, SectionID, SectionName, isReadOnly: IsReadOnly); |
| 642 | |
| 643 | uint32_t permissions = lldb::ePermissionsReadable; |
| 644 | if (!IsReadOnly) |
| 645 | permissions |= lldb::ePermissionsWritable; |
| 646 | m_parent.m_records.push_back(x: AllocationRecord( |
| 647 | (uintptr_t)return_value, permissions, |
| 648 | GetSectionTypeFromSectionName(name: SectionName, alloc_kind: AllocationKind::Data), Size, |
| 649 | Alignment, SectionID, SectionName.str().c_str())); |
| 650 | LLDB_LOGF(log, |
| 651 | "IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 |
| 652 | ", Alignment=%u, SectionID=%u) = %p" , |
| 653 | (uint64_t)Size, Alignment, SectionID, (void *)return_value); |
| 654 | |
| 655 | if (m_parent.m_reported_allocations) { |
| 656 | Status err; |
| 657 | lldb::ProcessSP process_sp = |
| 658 | m_parent.GetBestExecutionContextScope()->CalculateProcess(); |
| 659 | |
| 660 | m_parent.CommitOneAllocation(process_sp, error&: err, record&: m_parent.m_records.back()); |
| 661 | } |
| 662 | |
| 663 | return return_value; |
| 664 | } |
| 665 | |
| 666 | void IRExecutionUnit::CollectCandidateCNames(std::vector<ConstString> &C_names, |
| 667 | ConstString name) { |
| 668 | if (m_strip_underscore && name.AsCString()[0] == '_') |
| 669 | C_names.insert(position: C_names.begin(), x: ConstString(&name.AsCString()[1])); |
| 670 | C_names.push_back(x: name); |
| 671 | } |
| 672 | |
| 673 | void IRExecutionUnit::CollectCandidateCPlusPlusNames( |
| 674 | std::vector<ConstString> &CPP_names, |
| 675 | const std::vector<ConstString> &C_names, const SymbolContext &sc) { |
| 676 | if (auto *cpp_lang = Language::FindPlugin(language: lldb::eLanguageTypeC_plus_plus)) { |
| 677 | for (const ConstString &name : C_names) { |
| 678 | Mangled mangled(name); |
| 679 | if (cpp_lang->SymbolNameFitsToLanguage(name: mangled)) { |
| 680 | if (ConstString best_alternate = |
| 681 | cpp_lang->FindBestAlternateFunctionMangledName(mangled, sym_ctx: sc)) { |
| 682 | CPP_names.push_back(x: best_alternate); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | std::vector<ConstString> alternates = |
| 687 | cpp_lang->GenerateAlternateFunctionManglings(mangled: name); |
| 688 | CPP_names.insert(position: CPP_names.end(), first: alternates.begin(), last: alternates.end()); |
| 689 | |
| 690 | // As a last-ditch fallback, try the base name for C++ names. It's |
| 691 | // terrible, but the DWARF doesn't always encode "extern C" correctly. |
| 692 | ConstString basename = |
| 693 | cpp_lang->GetDemangledFunctionNameWithoutArguments(mangled); |
| 694 | CPP_names.push_back(x: basename); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | class LoadAddressResolver { |
| 700 | public: |
| 701 | LoadAddressResolver(Target *target, bool &symbol_was_missing_weak) |
| 702 | : m_target(target), m_symbol_was_missing_weak(symbol_was_missing_weak) {} |
| 703 | |
| 704 | std::optional<lldb::addr_t> Resolve(SymbolContextList &sc_list) { |
| 705 | if (sc_list.IsEmpty()) |
| 706 | return std::nullopt; |
| 707 | |
| 708 | lldb::addr_t load_address = LLDB_INVALID_ADDRESS; |
| 709 | |
| 710 | // Missing_weak_symbol will be true only if we found only weak undefined |
| 711 | // references to this symbol. |
| 712 | m_symbol_was_missing_weak = true; |
| 713 | |
| 714 | for (auto candidate_sc : sc_list.SymbolContexts()) { |
| 715 | // Only symbols can be weak undefined. |
| 716 | if (!candidate_sc.symbol || |
| 717 | candidate_sc.symbol->GetType() != lldb::eSymbolTypeUndefined || |
| 718 | !candidate_sc.symbol->IsWeak()) |
| 719 | m_symbol_was_missing_weak = false; |
| 720 | |
| 721 | // First try the symbol. |
| 722 | if (candidate_sc.symbol) { |
| 723 | load_address = candidate_sc.symbol->ResolveCallableAddress(target&: *m_target); |
| 724 | if (load_address == LLDB_INVALID_ADDRESS) { |
| 725 | Address addr = candidate_sc.symbol->GetAddress(); |
| 726 | load_address = m_target->GetProcessSP() |
| 727 | ? addr.GetLoadAddress(target: m_target) |
| 728 | : addr.GetFileAddress(); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | // If that didn't work, try the function. |
| 733 | if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) { |
| 734 | Address addr = candidate_sc.function->GetAddress(); |
| 735 | load_address = m_target->GetProcessSP() ? addr.GetLoadAddress(target: m_target) |
| 736 | : addr.GetFileAddress(); |
| 737 | } |
| 738 | |
| 739 | // We found a load address. |
| 740 | if (load_address != LLDB_INVALID_ADDRESS) { |
| 741 | // If the load address is external, we're done. |
| 742 | const bool is_external = |
| 743 | (candidate_sc.function) || |
| 744 | (candidate_sc.symbol && candidate_sc.symbol->IsExternal()); |
| 745 | if (is_external) |
| 746 | return load_address; |
| 747 | |
| 748 | // Otherwise, remember the best internal load address. |
| 749 | if (m_best_internal_load_address == LLDB_INVALID_ADDRESS) |
| 750 | m_best_internal_load_address = load_address; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // You test the address of a weak symbol against NULL to see if it is |
| 755 | // present. So we should return 0 for a missing weak symbol. |
| 756 | if (m_symbol_was_missing_weak) |
| 757 | return 0; |
| 758 | |
| 759 | return std::nullopt; |
| 760 | } |
| 761 | |
| 762 | lldb::addr_t GetBestInternalLoadAddress() const { |
| 763 | return m_best_internal_load_address; |
| 764 | } |
| 765 | |
| 766 | private: |
| 767 | Target *m_target; |
| 768 | bool &m_symbol_was_missing_weak; |
| 769 | lldb::addr_t m_best_internal_load_address = LLDB_INVALID_ADDRESS; |
| 770 | }; |
| 771 | |
| 772 | lldb::addr_t |
| 773 | IRExecutionUnit::FindInSymbols(const std::vector<ConstString> &names, |
| 774 | const lldb_private::SymbolContext &sc, |
| 775 | bool &symbol_was_missing_weak) { |
| 776 | symbol_was_missing_weak = false; |
| 777 | |
| 778 | Target *target = sc.target_sp.get(); |
| 779 | if (!target) { |
| 780 | // We shouldn't be doing any symbol lookup at all without a target. |
| 781 | return LLDB_INVALID_ADDRESS; |
| 782 | } |
| 783 | |
| 784 | ModuleList non_local_images = target->GetImages(); |
| 785 | // We'll process module_sp and any preferred modules separately, before the |
| 786 | // other modules. |
| 787 | non_local_images.Remove(module_sp: sc.module_sp); |
| 788 | for (size_t i = 0; i < m_preferred_modules.GetSize(); ++i) |
| 789 | non_local_images.Remove(module_sp: m_preferred_modules.GetModuleAtIndex(idx: i)); |
| 790 | |
| 791 | LoadAddressResolver resolver(target, symbol_was_missing_weak); |
| 792 | |
| 793 | ModuleFunctionSearchOptions function_options; |
| 794 | function_options.include_symbols = true; |
| 795 | function_options.include_inlines = false; |
| 796 | |
| 797 | for (const ConstString &name : names) { |
| 798 | // The lookup order here is as follows: |
| 799 | // 1) Functions in `sc.module_sp` |
| 800 | // 2) Functions in the preferred modules list |
| 801 | // 3) Functions in the other modules |
| 802 | // 4) Symbols in `sc.module_sp` |
| 803 | // 5) Symbols in the preferred modules list |
| 804 | // 6) Symbols in the other modules |
| 805 | if (sc.module_sp) { |
| 806 | SymbolContextList sc_list; |
| 807 | sc.module_sp->FindFunctions(name, parent_decl_ctx: CompilerDeclContext(), |
| 808 | name_type_mask: lldb::eFunctionNameTypeFull, options: function_options, |
| 809 | sc_list); |
| 810 | if (auto load_addr = resolver.Resolve(sc_list)) |
| 811 | return *load_addr; |
| 812 | } |
| 813 | |
| 814 | { |
| 815 | SymbolContextList sc_list; |
| 816 | m_preferred_modules.FindFunctions(name, name_type_mask: lldb::eFunctionNameTypeFull, |
| 817 | options: function_options, sc_list); |
| 818 | if (auto load_addr = resolver.Resolve(sc_list)) |
| 819 | return *load_addr; |
| 820 | } |
| 821 | |
| 822 | { |
| 823 | SymbolContextList sc_list; |
| 824 | non_local_images.FindFunctions(name, name_type_mask: lldb::eFunctionNameTypeFull, |
| 825 | options: function_options, sc_list); |
| 826 | if (auto load_addr = resolver.Resolve(sc_list)) |
| 827 | return *load_addr; |
| 828 | } |
| 829 | |
| 830 | if (sc.module_sp) { |
| 831 | SymbolContextList sc_list; |
| 832 | sc.module_sp->FindSymbolsWithNameAndType(name, symbol_type: lldb::eSymbolTypeAny, |
| 833 | sc_list); |
| 834 | if (auto load_addr = resolver.Resolve(sc_list)) |
| 835 | return *load_addr; |
| 836 | } |
| 837 | |
| 838 | { |
| 839 | SymbolContextList sc_list; |
| 840 | m_preferred_modules.FindSymbolsWithNameAndType(name, symbol_type: lldb::eSymbolTypeAny, |
| 841 | sc_list); |
| 842 | if (auto load_addr = resolver.Resolve(sc_list)) |
| 843 | return *load_addr; |
| 844 | } |
| 845 | |
| 846 | { |
| 847 | SymbolContextList sc_list; |
| 848 | non_local_images.FindSymbolsWithNameAndType(name, symbol_type: lldb::eSymbolTypeAny, |
| 849 | sc_list); |
| 850 | if (auto load_addr = resolver.Resolve(sc_list)) |
| 851 | return *load_addr; |
| 852 | } |
| 853 | |
| 854 | lldb::addr_t best_internal_load_address = |
| 855 | resolver.GetBestInternalLoadAddress(); |
| 856 | if (best_internal_load_address != LLDB_INVALID_ADDRESS) |
| 857 | return best_internal_load_address; |
| 858 | } |
| 859 | |
| 860 | return LLDB_INVALID_ADDRESS; |
| 861 | } |
| 862 | |
| 863 | lldb::addr_t |
| 864 | IRExecutionUnit::FindInRuntimes(const std::vector<ConstString> &names, |
| 865 | const lldb_private::SymbolContext &sc) { |
| 866 | lldb::TargetSP target_sp = sc.target_sp; |
| 867 | |
| 868 | if (!target_sp) { |
| 869 | return LLDB_INVALID_ADDRESS; |
| 870 | } |
| 871 | |
| 872 | lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP(); |
| 873 | |
| 874 | if (!process_sp) { |
| 875 | return LLDB_INVALID_ADDRESS; |
| 876 | } |
| 877 | |
| 878 | for (const ConstString &name : names) { |
| 879 | for (LanguageRuntime *runtime : process_sp->GetLanguageRuntimes()) { |
| 880 | lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(name); |
| 881 | |
| 882 | if (symbol_load_addr != LLDB_INVALID_ADDRESS) |
| 883 | return symbol_load_addr; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | return LLDB_INVALID_ADDRESS; |
| 888 | } |
| 889 | |
| 890 | lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols( |
| 891 | const std::vector<ConstString> &names, |
| 892 | const lldb_private::SymbolContext &sc) { |
| 893 | lldb::TargetSP target_sp = sc.target_sp; |
| 894 | |
| 895 | for (const ConstString &name : names) { |
| 896 | lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(name); |
| 897 | |
| 898 | if (symbol_load_addr != LLDB_INVALID_ADDRESS) |
| 899 | return symbol_load_addr; |
| 900 | } |
| 901 | |
| 902 | return LLDB_INVALID_ADDRESS; |
| 903 | } |
| 904 | |
| 905 | lldb::addr_t IRExecutionUnit::FindSymbol(lldb_private::ConstString name, |
| 906 | bool &missing_weak) { |
| 907 | std::vector<ConstString> candidate_C_names; |
| 908 | std::vector<ConstString> candidate_CPlusPlus_names; |
| 909 | |
| 910 | CollectCandidateCNames(C_names&: candidate_C_names, name); |
| 911 | |
| 912 | lldb::addr_t ret = FindInSymbols(names: candidate_C_names, sc: m_sym_ctx, symbol_was_missing_weak&: missing_weak); |
| 913 | if (ret != LLDB_INVALID_ADDRESS) |
| 914 | return ret; |
| 915 | |
| 916 | // If we find the symbol in runtimes or user defined symbols it can't be |
| 917 | // a missing weak symbol. |
| 918 | missing_weak = false; |
| 919 | ret = FindInRuntimes(names: candidate_C_names, sc: m_sym_ctx); |
| 920 | if (ret != LLDB_INVALID_ADDRESS) |
| 921 | return ret; |
| 922 | |
| 923 | ret = FindInUserDefinedSymbols(names: candidate_C_names, sc: m_sym_ctx); |
| 924 | if (ret != LLDB_INVALID_ADDRESS) |
| 925 | return ret; |
| 926 | |
| 927 | CollectCandidateCPlusPlusNames(CPP_names&: candidate_CPlusPlus_names, C_names: candidate_C_names, |
| 928 | sc: m_sym_ctx); |
| 929 | ret = FindInSymbols(names: candidate_CPlusPlus_names, sc: m_sym_ctx, symbol_was_missing_weak&: missing_weak); |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | void IRExecutionUnit::GetStaticInitializers( |
| 934 | std::vector<lldb::addr_t> &static_initializers) { |
| 935 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 936 | |
| 937 | llvm::GlobalVariable *global_ctors = |
| 938 | m_module->getNamedGlobal(Name: "llvm.global_ctors" ); |
| 939 | if (!global_ctors) { |
| 940 | LLDB_LOG(log, "Couldn't find llvm.global_ctors." ); |
| 941 | return; |
| 942 | } |
| 943 | auto *ctor_array = |
| 944 | llvm::dyn_cast<llvm::ConstantArray>(Val: global_ctors->getInitializer()); |
| 945 | if (!ctor_array) { |
| 946 | LLDB_LOG(log, "llvm.global_ctors not a ConstantArray." ); |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | for (llvm::Use &ctor_use : ctor_array->operands()) { |
| 951 | auto *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(Val&: ctor_use); |
| 952 | if (!ctor_struct) |
| 953 | continue; |
| 954 | // this is standardized |
| 955 | lldbassert(ctor_struct->getNumOperands() == 3); |
| 956 | auto *ctor_function = |
| 957 | llvm::dyn_cast<llvm::Function>(Val: ctor_struct->getOperand(i_nocapture: 1)); |
| 958 | if (!ctor_function) { |
| 959 | LLDB_LOG(log, "global_ctor doesn't contain an llvm::Function" ); |
| 960 | continue; |
| 961 | } |
| 962 | |
| 963 | ConstString ctor_function_name(ctor_function->getName().str()); |
| 964 | LLDB_LOG(log, "Looking for callable jitted function with name {0}." , |
| 965 | ctor_function_name); |
| 966 | |
| 967 | for (JittedFunction &jitted_function : m_jitted_functions) { |
| 968 | if (ctor_function_name != jitted_function.m_name) |
| 969 | continue; |
| 970 | if (jitted_function.m_remote_addr == LLDB_INVALID_ADDRESS) { |
| 971 | LLDB_LOG(log, "Found jitted function with invalid address." ); |
| 972 | continue; |
| 973 | } |
| 974 | static_initializers.push_back(x: jitted_function.m_remote_addr); |
| 975 | LLDB_LOG(log, "Calling function at address {0:x}." , |
| 976 | jitted_function.m_remote_addr); |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | llvm::JITSymbol |
| 983 | IRExecutionUnit::MemoryManager::findSymbol(const std::string &Name) { |
| 984 | bool missing_weak = false; |
| 985 | uint64_t addr = GetSymbolAddressAndPresence(Name, missing_weak); |
| 986 | // This is a weak symbol: |
| 987 | if (missing_weak) |
| 988 | return llvm::JITSymbol(addr, |
| 989 | llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Weak); |
| 990 | else |
| 991 | return llvm::JITSymbol(addr, llvm::JITSymbolFlags::Exported); |
| 992 | } |
| 993 | |
| 994 | uint64_t |
| 995 | IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) { |
| 996 | bool missing_weak = false; |
| 997 | return GetSymbolAddressAndPresence(Name, missing_weak); |
| 998 | } |
| 999 | |
| 1000 | uint64_t |
| 1001 | IRExecutionUnit::MemoryManager::GetSymbolAddressAndPresence( |
| 1002 | const std::string &Name, bool &missing_weak) { |
| 1003 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 1004 | |
| 1005 | ConstString name_cs(Name.c_str()); |
| 1006 | |
| 1007 | lldb::addr_t ret = m_parent.FindSymbol(name: name_cs, missing_weak); |
| 1008 | |
| 1009 | if (ret == LLDB_INVALID_ADDRESS) { |
| 1010 | LLDB_LOGF(log, |
| 1011 | "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>" , |
| 1012 | Name.c_str()); |
| 1013 | |
| 1014 | m_parent.ReportSymbolLookupError(name: name_cs); |
| 1015 | return 0; |
| 1016 | } else { |
| 1017 | LLDB_LOGF(log, "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64, |
| 1018 | Name.c_str(), ret); |
| 1019 | return ret; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction( |
| 1024 | const std::string &Name, bool AbortOnFailure) { |
| 1025 | return (void *)getSymbolAddress(Name); |
| 1026 | } |
| 1027 | |
| 1028 | lldb::addr_t |
| 1029 | IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) { |
| 1030 | Log *log = GetLog(mask: LLDBLog::Expressions); |
| 1031 | |
| 1032 | for (AllocationRecord &record : m_records) { |
| 1033 | if (local_address >= record.m_host_address && |
| 1034 | local_address < record.m_host_address + record.m_size) { |
| 1035 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 1036 | return LLDB_INVALID_ADDRESS; |
| 1037 | |
| 1038 | lldb::addr_t ret = |
| 1039 | record.m_process_address + (local_address - record.m_host_address); |
| 1040 | |
| 1041 | LLDB_LOGF(log, |
| 1042 | "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 |
| 1043 | " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 |
| 1044 | " from [0x%" PRIx64 "..0x%" PRIx64 "]." , |
| 1045 | local_address, (uint64_t)record.m_host_address, |
| 1046 | (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret, |
| 1047 | record.m_process_address, |
| 1048 | record.m_process_address + record.m_size); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | return LLDB_INVALID_ADDRESS; |
| 1055 | } |
| 1056 | |
| 1057 | IRExecutionUnit::AddrRange |
| 1058 | IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) { |
| 1059 | for (AllocationRecord &record : m_records) { |
| 1060 | if (local_address >= record.m_host_address && |
| 1061 | local_address < record.m_host_address + record.m_size) { |
| 1062 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 1063 | return AddrRange(0, 0); |
| 1064 | |
| 1065 | return AddrRange(record.m_process_address, record.m_size); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return AddrRange(0, 0); |
| 1070 | } |
| 1071 | |
| 1072 | bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp, |
| 1073 | Status &error, |
| 1074 | AllocationRecord &record) { |
| 1075 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 1076 | return true; |
| 1077 | } |
| 1078 | |
| 1079 | switch (record.m_sect_type) { |
| 1080 | case lldb::eSectionTypeInvalid: |
| 1081 | case lldb::eSectionTypeDWARFDebugAbbrev: |
| 1082 | case lldb::eSectionTypeDWARFDebugAddr: |
| 1083 | case lldb::eSectionTypeDWARFDebugAranges: |
| 1084 | case lldb::eSectionTypeDWARFDebugCuIndex: |
| 1085 | case lldb::eSectionTypeDWARFDebugFrame: |
| 1086 | case lldb::eSectionTypeDWARFDebugInfo: |
| 1087 | case lldb::eSectionTypeDWARFDebugLine: |
| 1088 | case lldb::eSectionTypeDWARFDebugLoc: |
| 1089 | case lldb::eSectionTypeDWARFDebugLocLists: |
| 1090 | case lldb::eSectionTypeDWARFDebugMacInfo: |
| 1091 | case lldb::eSectionTypeDWARFDebugPubNames: |
| 1092 | case lldb::eSectionTypeDWARFDebugPubTypes: |
| 1093 | case lldb::eSectionTypeDWARFDebugRanges: |
| 1094 | case lldb::eSectionTypeDWARFDebugStr: |
| 1095 | case lldb::eSectionTypeDWARFDebugStrOffsets: |
| 1096 | case lldb::eSectionTypeDWARFAppleNames: |
| 1097 | case lldb::eSectionTypeDWARFAppleTypes: |
| 1098 | case lldb::eSectionTypeDWARFAppleNamespaces: |
| 1099 | case lldb::eSectionTypeDWARFAppleObjC: |
| 1100 | case lldb::eSectionTypeDWARFGNUDebugAltLink: |
| 1101 | error.Clear(); |
| 1102 | break; |
| 1103 | default: |
| 1104 | const bool zero_memory = false; |
| 1105 | record.m_process_address = |
| 1106 | Malloc(size: record.m_size, alignment: record.m_alignment, permissions: record.m_permissions, |
| 1107 | policy: eAllocationPolicyProcessOnly, zero_memory, error); |
| 1108 | break; |
| 1109 | } |
| 1110 | |
| 1111 | return error.Success(); |
| 1112 | } |
| 1113 | |
| 1114 | bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) { |
| 1115 | bool ret = true; |
| 1116 | |
| 1117 | lldb_private::Status err; |
| 1118 | |
| 1119 | for (AllocationRecord &record : m_records) { |
| 1120 | ret = CommitOneAllocation(process_sp, error&: err, record); |
| 1121 | |
| 1122 | if (!ret) { |
| 1123 | break; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if (!ret) { |
| 1128 | for (AllocationRecord &record : m_records) { |
| 1129 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 1130 | Free(process_address: record.m_process_address, error&: err); |
| 1131 | record.m_process_address = LLDB_INVALID_ADDRESS; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) { |
| 1140 | m_reported_allocations = true; |
| 1141 | |
| 1142 | for (AllocationRecord &record : m_records) { |
| 1143 | if (record.m_process_address == LLDB_INVALID_ADDRESS) |
| 1144 | continue; |
| 1145 | |
| 1146 | if (record.m_section_id == eSectionIDInvalid) |
| 1147 | continue; |
| 1148 | |
| 1149 | engine.mapSectionAddress(LocalAddress: (void *)record.m_host_address, |
| 1150 | TargetAddress: record.m_process_address); |
| 1151 | } |
| 1152 | |
| 1153 | // Trigger re-application of relocations. |
| 1154 | engine.finalizeObject(); |
| 1155 | } |
| 1156 | |
| 1157 | bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) { |
| 1158 | bool wrote_something = false; |
| 1159 | for (AllocationRecord &record : m_records) { |
| 1160 | if (record.m_process_address != LLDB_INVALID_ADDRESS) { |
| 1161 | lldb_private::Status err; |
| 1162 | WriteMemory(process_address: record.m_process_address, bytes: (uint8_t *)record.m_host_address, |
| 1163 | size: record.m_size, error&: err); |
| 1164 | if (err.Success()) |
| 1165 | wrote_something = true; |
| 1166 | } |
| 1167 | } |
| 1168 | return wrote_something; |
| 1169 | } |
| 1170 | |
| 1171 | void IRExecutionUnit::AllocationRecord::dump(Log *log) { |
| 1172 | if (!log) |
| 1173 | return; |
| 1174 | |
| 1175 | LLDB_LOGF(log, |
| 1176 | "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)" , |
| 1177 | (unsigned long long)m_host_address, (unsigned long long)m_size, |
| 1178 | (unsigned long long)m_process_address, (unsigned)m_alignment, |
| 1179 | (unsigned)m_section_id, m_name.c_str()); |
| 1180 | } |
| 1181 | |
| 1182 | lldb::ByteOrder IRExecutionUnit::GetByteOrder() const { |
| 1183 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1184 | return exe_ctx.GetByteOrder(); |
| 1185 | } |
| 1186 | |
| 1187 | uint32_t IRExecutionUnit::GetAddressByteSize() const { |
| 1188 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1189 | return exe_ctx.GetAddressByteSize(); |
| 1190 | } |
| 1191 | |
| 1192 | void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file, |
| 1193 | lldb_private::Symtab &symtab) { |
| 1194 | // No symbols yet... |
| 1195 | } |
| 1196 | |
| 1197 | void IRExecutionUnit::PopulateSectionList( |
| 1198 | lldb_private::ObjectFile *obj_file, |
| 1199 | lldb_private::SectionList §ion_list) { |
| 1200 | for (AllocationRecord &record : m_records) { |
| 1201 | if (record.m_size > 0) { |
| 1202 | lldb::SectionSP section_sp(new lldb_private::Section( |
| 1203 | obj_file->GetModule(), obj_file, record.m_section_id, |
| 1204 | ConstString(record.m_name), record.m_sect_type, |
| 1205 | record.m_process_address, record.m_size, |
| 1206 | record.m_host_address, // file_offset (which is the host address for |
| 1207 | // the data) |
| 1208 | record.m_size, // file_size |
| 1209 | 0, |
| 1210 | record.m_permissions)); // flags |
| 1211 | section_list.AddSection(section_sp); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | ArchSpec IRExecutionUnit::GetArchitecture() { |
| 1217 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1218 | if(Target *target = exe_ctx.GetTargetPtr()) |
| 1219 | return target->GetArchitecture(); |
| 1220 | return ArchSpec(); |
| 1221 | } |
| 1222 | |
| 1223 | lldb::ModuleSP IRExecutionUnit::GetJITModule() { |
| 1224 | ExecutionContext exe_ctx(GetBestExecutionContextScope()); |
| 1225 | Target *target = exe_ctx.GetTargetPtr(); |
| 1226 | if (!target) |
| 1227 | return nullptr; |
| 1228 | |
| 1229 | auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>( |
| 1230 | r: shared_from_this()); |
| 1231 | |
| 1232 | lldb::ModuleSP jit_module_sp = |
| 1233 | lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(args&: Delegate); |
| 1234 | if (!jit_module_sp) |
| 1235 | return nullptr; |
| 1236 | |
| 1237 | bool changed = false; |
| 1238 | jit_module_sp->SetLoadAddress(target&: *target, value: 0, value_is_offset: true, changed); |
| 1239 | return jit_module_sp; |
| 1240 | } |
| 1241 | |