Path: blob/main/contrib/llvm-project/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp
39644 views
//===-- DynamicLoaderStatic.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/Core/Module.h"9#include "lldb/Core/PluginManager.h"10#include "lldb/Core/Section.h"11#include "lldb/Symbol/ObjectFile.h"12#include "lldb/Target/SectionLoadList.h"13#include "lldb/Target/Target.h"1415#include "DynamicLoaderStatic.h"1617using namespace lldb;18using namespace lldb_private;1920LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)2122// Create an instance of this class. This function is filled into the plugin23// info class that gets handed out by the plugin factory and allows the lldb to24// instantiate an instance of this class.25DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,26bool force) {27bool create = force;28if (!create) {29const llvm::Triple &triple_ref =30process->GetTarget().GetArchitecture().GetTriple();31const llvm::Triple::OSType os_type = triple_ref.getOS();32const llvm::Triple::ArchType arch_type = triple_ref.getArch();33if (os_type == llvm::Triple::UnknownOS) {34// The WASM and Hexagon plugin check the ArchType rather than the OSType,35// so explicitly reject those here.36switch(arch_type) {37case llvm::Triple::hexagon:38case llvm::Triple::wasm32:39case llvm::Triple::wasm64:40break;41default:42create = true;43}44}45}4647if (!create) {48Module *exe_module = process->GetTarget().GetExecutableModulePointer();49if (exe_module) {50ObjectFile *object_file = exe_module->GetObjectFile();51if (object_file) {52create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);53}54}55}5657if (create)58return new DynamicLoaderStatic(process);59return nullptr;60}6162// Constructor63DynamicLoaderStatic::DynamicLoaderStatic(Process *process)64: DynamicLoader(process) {}6566/// Called after attaching a process.67///68/// Allow DynamicLoader plug-ins to execute some code after69/// attaching to a process.70void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }7172/// Called after attaching a process.73///74/// Allow DynamicLoader plug-ins to execute some code after75/// attaching to a process.76void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }7778void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {79const ModuleList &module_list = m_process->GetTarget().GetImages();8081ModuleList loaded_module_list;8283// Disable JIT for static dynamic loader targets84m_process->SetCanJIT(false);8586Target &target = m_process->GetTarget();87for (ModuleSP module_sp : module_list.Modules()) {88if (module_sp) {89bool changed = false;90bool no_load_addresses = true;91// If this module has a section with a load address set in92// the target, assume all necessary work is already done. There93// may be sections without a load address set intentionally94// and we don't want to mutate that.95// For a module with no load addresses set, set the load addresses96// to slide == 0, the same as the file addresses, in the target.97ObjectFile *image_object_file = module_sp->GetObjectFile();98if (image_object_file) {99SectionList *section_list = image_object_file->GetSectionList();100if (section_list) {101const size_t num_sections = section_list->GetSize();102for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) {103SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));104if (section_sp) {105if (target.GetSectionLoadList().GetSectionLoadAddress(106section_sp) != LLDB_INVALID_ADDRESS) {107no_load_addresses = false;108break;109}110}111}112}113}114if (no_load_addresses)115module_sp->SetLoadAddress(target, 0, true /*value_is_offset*/, changed);116117if (changed)118loaded_module_list.AppendIfNeeded(module_sp);119}120}121122target.ModulesDidLoad(loaded_module_list);123}124125ThreadPlanSP126DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,127bool stop_others) {128return ThreadPlanSP();129}130131Status DynamicLoaderStatic::CanLoadImage() {132Status error;133error.SetErrorString("can't load images on with a static debug session");134return error;135}136137void DynamicLoaderStatic::Initialize() {138PluginManager::RegisterPlugin(GetPluginNameStatic(),139GetPluginDescriptionStatic(), CreateInstance);140}141142void DynamicLoaderStatic::Terminate() {143PluginManager::UnregisterPlugin(CreateInstance);144}145146llvm::StringRef DynamicLoaderStatic::GetPluginDescriptionStatic() {147return "Dynamic loader plug-in that will load any images at the static "148"addresses contained in each image.";149}150151152