Path: blob/main/contrib/llvm-project/lldb/source/Core/DynamicLoader.cpp
39587 views
//===-- DynamicLoader.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Target/DynamicLoader.h"910#include "lldb/Core/Debugger.h"11#include "lldb/Core/Module.h"12#include "lldb/Core/ModuleList.h"13#include "lldb/Core/ModuleSpec.h"14#include "lldb/Core/PluginManager.h"15#include "lldb/Core/Progress.h"16#include "lldb/Core/Section.h"17#include "lldb/Symbol/ObjectFile.h"18#include "lldb/Target/MemoryRegionInfo.h"19#include "lldb/Target/Platform.h"20#include "lldb/Target/Process.h"21#include "lldb/Target/Target.h"22#include "lldb/Utility/ConstString.h"23#include "lldb/Utility/LLDBLog.h"24#include "lldb/Utility/Log.h"25#include "lldb/lldb-private-interfaces.h"2627#include "llvm/ADT/StringRef.h"2829#include <memory>3031#include <cassert>3233using namespace lldb;34using namespace lldb_private;3536DynamicLoader *DynamicLoader::FindPlugin(Process *process,37llvm::StringRef plugin_name) {38DynamicLoaderCreateInstance create_callback = nullptr;39if (!plugin_name.empty()) {40create_callback =41PluginManager::GetDynamicLoaderCreateCallbackForPluginName(plugin_name);42if (create_callback) {43std::unique_ptr<DynamicLoader> instance_up(44create_callback(process, true));45if (instance_up)46return instance_up.release();47}48} else {49for (uint32_t idx = 0;50(create_callback =51PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) !=52nullptr;53++idx) {54std::unique_ptr<DynamicLoader> instance_up(55create_callback(process, false));56if (instance_up)57return instance_up.release();58}59}60return nullptr;61}6263DynamicLoader::DynamicLoader(Process *process) : m_process(process) {}6465// Accessors to the global setting as to whether to stop at image (shared66// library) loading/unloading.6768bool DynamicLoader::GetStopWhenImagesChange() const {69return m_process->GetStopOnSharedLibraryEvents();70}7172void DynamicLoader::SetStopWhenImagesChange(bool stop) {73m_process->SetStopOnSharedLibraryEvents(stop);74}7576ModuleSP DynamicLoader::GetTargetExecutable() {77Target &target = m_process->GetTarget();78ModuleSP executable = target.GetExecutableModule();7980if (executable) {81if (FileSystem::Instance().Exists(executable->GetFileSpec())) {82ModuleSpec module_spec(executable->GetFileSpec(),83executable->GetArchitecture());84auto module_sp = std::make_shared<Module>(module_spec);8586// Check if the executable has changed and set it to the target87// executable if they differ.88if (module_sp && module_sp->GetUUID().IsValid() &&89executable->GetUUID().IsValid()) {90if (module_sp->GetUUID() != executable->GetUUID())91executable.reset();92} else if (executable->FileHasChanged()) {93executable.reset();94}9596if (!executable) {97executable = target.GetOrCreateModule(module_spec, true /* notify */);98if (executable.get() != target.GetExecutableModulePointer()) {99// Don't load dependent images since we are in dyld where we will100// know and find out about all images that are loaded101target.SetExecutableModule(executable, eLoadDependentsNo);102}103}104}105}106return executable;107}108109void DynamicLoader::UpdateLoadedSections(ModuleSP module, addr_t link_map_addr,110addr_t base_addr,111bool base_addr_is_offset) {112UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);113}114115void DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,116addr_t base_addr,117bool base_addr_is_offset) {118bool changed;119module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,120changed);121}122123void DynamicLoader::UnloadSections(const ModuleSP module) {124UnloadSectionsCommon(module);125}126127void DynamicLoader::UnloadSectionsCommon(const ModuleSP module) {128Target &target = m_process->GetTarget();129const SectionList *sections = GetSectionListFromModule(module);130131assert(sections && "SectionList missing from unloaded module.");132133const size_t num_sections = sections->GetSize();134for (size_t i = 0; i < num_sections; ++i) {135SectionSP section_sp(sections->GetSectionAtIndex(i));136target.SetSectionUnloaded(section_sp);137}138}139140const SectionList *141DynamicLoader::GetSectionListFromModule(const ModuleSP module) const {142SectionList *sections = nullptr;143if (module) {144ObjectFile *obj_file = module->GetObjectFile();145if (obj_file != nullptr) {146sections = obj_file->GetSectionList();147}148}149return sections;150}151152ModuleSP DynamicLoader::FindModuleViaTarget(const FileSpec &file) {153Target &target = m_process->GetTarget();154ModuleSpec module_spec(file, target.GetArchitecture());155156if (ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec))157return module_sp;158159if (ModuleSP module_sp = target.GetOrCreateModule(module_spec, false))160return module_sp;161162return nullptr;163}164165ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,166addr_t link_map_addr,167addr_t base_addr,168bool base_addr_is_offset) {169if (ModuleSP module_sp = FindModuleViaTarget(file)) {170UpdateLoadedSections(module_sp, link_map_addr, base_addr,171base_addr_is_offset);172return module_sp;173}174175return nullptr;176}177178static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr,179llvm::StringRef name) {180char namebuf[80];181if (name.empty()) {182snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr);183name = namebuf;184}185return process->ReadModuleFromMemory(FileSpec(name), addr);186}187188ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(189Process *process, llvm::StringRef name, UUID uuid, addr_t value,190bool value_is_offset, bool force_symbol_search, bool notify,191bool set_address_in_target, bool allow_memory_image_last_resort) {192ModuleSP memory_module_sp;193ModuleSP module_sp;194PlatformSP platform_sp = process->GetTarget().GetPlatform();195Target &target = process->GetTarget();196Status error;197198StreamString prog_str;199if (!name.empty()) {200prog_str << name.str() << " ";201}202if (uuid.IsValid())203prog_str << uuid.GetAsString();204if (value_is_offset == 0 && value != LLDB_INVALID_ADDRESS) {205prog_str << "at 0x";206prog_str.PutHex64(value);207}208209if (!uuid.IsValid() && !value_is_offset) {210memory_module_sp = ReadUnnamedMemoryModule(process, value, name);211212if (memory_module_sp) {213uuid = memory_module_sp->GetUUID();214if (uuid.IsValid()) {215prog_str << " ";216prog_str << uuid.GetAsString();217}218}219}220ModuleSpec module_spec;221module_spec.GetUUID() = uuid;222FileSpec name_filespec(name);223224if (uuid.IsValid()) {225Progress progress("Locating binary", prog_str.GetString().str());226227// Has lldb already seen a module with this UUID?228// Or have external lookup enabled in DebugSymbols on macOS.229if (!module_sp)230error = ModuleList::GetSharedModule(module_spec, module_sp, nullptr,231nullptr, nullptr);232233// Can lldb's symbol/executable location schemes234// find an executable and symbol file.235if (!module_sp) {236FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();237module_spec.GetSymbolFileSpec() =238PluginManager::LocateExecutableSymbolFile(module_spec, search_paths);239ModuleSpec objfile_module_spec =240PluginManager::LocateExecutableObjectFile(module_spec);241module_spec.GetFileSpec() = objfile_module_spec.GetFileSpec();242if (FileSystem::Instance().Exists(module_spec.GetFileSpec()) &&243FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec())) {244module_sp = std::make_shared<Module>(module_spec);245}246}247248// If we haven't found a binary, or we don't have a SymbolFile, see249// if there is an external search tool that can find it.250if (!module_sp || !module_sp->GetSymbolFileFileSpec()) {251PluginManager::DownloadObjectAndSymbolFile(module_spec, error,252force_symbol_search);253if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {254module_sp = std::make_shared<Module>(module_spec);255} else if (force_symbol_search && error.AsCString("") &&256error.AsCString("")[0] != '\0') {257target.GetDebugger().GetErrorStream() << error.AsCString();258}259}260261// If we only found the executable, create a Module based on that.262if (!module_sp && FileSystem::Instance().Exists(module_spec.GetFileSpec()))263module_sp = std::make_shared<Module>(module_spec);264}265266// If we couldn't find the binary anywhere else, as a last resort,267// read it out of memory.268if (allow_memory_image_last_resort && !module_sp.get() &&269value != LLDB_INVALID_ADDRESS && !value_is_offset) {270if (!memory_module_sp)271memory_module_sp = ReadUnnamedMemoryModule(process, value, name);272if (memory_module_sp)273module_sp = memory_module_sp;274}275276Log *log = GetLog(LLDBLog::DynamicLoader);277if (module_sp.get()) {278// Ensure the Target has an architecture set in case279// we need it while processing this binary/eh_frame/debug info.280if (!target.GetArchitecture().IsValid())281target.SetArchitecture(module_sp->GetArchitecture());282target.GetImages().AppendIfNeeded(module_sp, false);283284bool changed = false;285if (set_address_in_target) {286if (module_sp->GetObjectFile()) {287if (value != LLDB_INVALID_ADDRESS) {288LLDB_LOGF(log,289"DynamicLoader::LoadBinaryWithUUIDAndAddress Loading "290"binary %s UUID %s at %s 0x%" PRIx64,291name.str().c_str(), uuid.GetAsString().c_str(),292value_is_offset ? "offset" : "address", value);293module_sp->SetLoadAddress(target, value, value_is_offset, changed);294} else {295// No address/offset/slide, load the binary at file address,296// offset 0.297LLDB_LOGF(log,298"DynamicLoader::LoadBinaryWithUUIDAndAddress Loading "299"binary %s UUID %s at file address",300name.str().c_str(), uuid.GetAsString().c_str());301module_sp->SetLoadAddress(target, 0, true /* value_is_slide */,302changed);303}304} else {305// In-memory image, load at its true address, offset 0.306LLDB_LOGF(log,307"DynamicLoader::LoadBinaryWithUUIDAndAddress Loading binary "308"%s UUID %s from memory at address 0x%" PRIx64,309name.str().c_str(), uuid.GetAsString().c_str(), value);310module_sp->SetLoadAddress(target, 0, true /* value_is_slide */,311changed);312}313}314315if (notify) {316ModuleList added_module;317added_module.Append(module_sp, false);318target.ModulesDidLoad(added_module);319}320} else {321if (force_symbol_search) {322Stream &s = target.GetDebugger().GetErrorStream();323s.Printf("Unable to find file");324if (!name.empty())325s.Printf(" %s", name.str().c_str());326if (uuid.IsValid())327s.Printf(" with UUID %s", uuid.GetAsString().c_str());328if (value != LLDB_INVALID_ADDRESS) {329if (value_is_offset)330s.Printf(" with slide 0x%" PRIx64, value);331else332s.Printf(" at address 0x%" PRIx64, value);333}334s.Printf("\n");335}336LLDB_LOGF(log,337"Unable to find binary %s with UUID %s and load it at "338"%s 0x%" PRIx64,339name.str().c_str(), uuid.GetAsString().c_str(),340value_is_offset ? "offset" : "address", value);341}342343return module_sp;344}345346int64_t DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr,347int size_in_bytes) {348Status error;349uint64_t value =350m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);351if (error.Fail())352return -1;353else354return (int64_t)value;355}356357addr_t DynamicLoader::ReadPointer(addr_t addr) {358Status error;359addr_t value = m_process->ReadPointerFromMemory(addr, error);360if (error.Fail())361return LLDB_INVALID_ADDRESS;362else363return value;364}365366void DynamicLoader::LoadOperatingSystemPlugin(bool flush)367{368if (m_process)369m_process->LoadOperatingSystemPlugin(flush);370}371372373374