brintos

brintos / llvm-project-archived public Read only

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