2010-06-09 00:52:24 +08:00
|
|
|
//===-- DynamicLoader.cpp ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
// C Includes
|
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
// Project includes
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/lldb-private.h"
|
2014-02-07 03:02:19 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
|
|
|
#include "lldb/Core/ModuleSpec.h"
|
2016-07-22 20:55:35 +08:00
|
|
|
#include "lldb/Core/PluginManager.h"
|
2014-02-07 03:02:19 +08:00
|
|
|
#include "lldb/Core/Section.h"
|
2016-07-22 20:55:35 +08:00
|
|
|
#include "lldb/Target/DynamicLoader.h"
|
|
|
|
#include "lldb/Target/MemoryRegionInfo.h"
|
|
|
|
#include "lldb/Target/Process.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
DynamicLoader*
|
|
|
|
DynamicLoader::FindPlugin (Process *process, const char *plugin_name)
|
|
|
|
{
|
2016-03-03 08:51:40 +08:00
|
|
|
DynamicLoaderCreateInstance create_callback = nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
if (plugin_name)
|
|
|
|
{
|
2013-05-11 05:47:16 +08:00
|
|
|
ConstString const_plugin_name(plugin_name);
|
|
|
|
create_callback = PluginManager::GetDynamicLoaderCreateCallbackForPluginName (const_plugin_name);
|
2010-06-09 00:52:24 +08:00
|
|
|
if (create_callback)
|
|
|
|
{
|
2013-04-19 06:45:39 +08:00
|
|
|
std::unique_ptr<DynamicLoader> instance_ap(create_callback(process, true));
|
2016-03-03 08:51:40 +08:00
|
|
|
if (instance_ap)
|
2010-06-09 00:52:24 +08:00
|
|
|
return instance_ap.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-03 08:51:40 +08:00
|
|
|
for (uint32_t idx = 0; (create_callback = PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != nullptr; ++idx)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2013-04-19 06:45:39 +08:00
|
|
|
std::unique_ptr<DynamicLoader> instance_ap(create_callback(process, false));
|
2016-03-03 08:51:40 +08:00
|
|
|
if (instance_ap)
|
2010-06-09 00:52:24 +08:00
|
|
|
return instance_ap.release();
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 08:51:40 +08:00
|
|
|
return nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicLoader::DynamicLoader(Process *process) :
|
2013-01-26 10:19:28 +08:00
|
|
|
m_process (process)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
DynamicLoader::~DynamicLoader() = default;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2010-09-18 11:37:20 +08:00
|
|
|
// Accessosors to the global setting as to whether to stop at image
|
2010-06-09 00:52:24 +08:00
|
|
|
// (shared library) loading/unloading.
|
|
|
|
//----------------------------------------------------------------------
|
2016-03-03 08:51:40 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
bool
|
|
|
|
DynamicLoader::GetStopWhenImagesChange () const
|
|
|
|
{
|
2013-01-26 10:19:28 +08:00
|
|
|
return m_process->GetStopOnSharedLibraryEvents();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DynamicLoader::SetStopWhenImagesChange (bool stop)
|
|
|
|
{
|
2013-01-26 10:19:28 +08:00
|
|
|
m_process->SetStopOnSharedLibraryEvents (stop);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:02:19 +08:00
|
|
|
ModuleSP
|
|
|
|
DynamicLoader::GetTargetExecutable()
|
|
|
|
{
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
ModuleSP executable = target.GetExecutableModule();
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
if (executable)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
if (executable->GetFileSpec().Exists())
|
|
|
|
{
|
|
|
|
ModuleSpec module_spec (executable->GetFileSpec(), executable->GetArchitecture());
|
|
|
|
ModuleSP module_sp (new Module (module_spec));
|
|
|
|
|
|
|
|
// Check if the executable has changed and set it to the target executable if they differ.
|
2016-03-03 08:51:40 +08:00
|
|
|
if (module_sp && module_sp->GetUUID().IsValid() && executable->GetUUID().IsValid())
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
if (module_sp->GetUUID() != executable->GetUUID())
|
|
|
|
executable.reset();
|
|
|
|
}
|
|
|
|
else if (executable->FileHasChanged())
|
|
|
|
{
|
|
|
|
executable.reset();
|
|
|
|
}
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
if (!executable)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
executable = target.GetSharedModule(module_spec);
|
|
|
|
if (executable.get() != target.GetExecutableModulePointer())
|
|
|
|
{
|
|
|
|
// Don't load dependent images since we are in dyld where we will know
|
|
|
|
// and find out about all images that are loaded
|
|
|
|
const bool get_dependent_images = false;
|
|
|
|
target.SetExecutableModule(executable, get_dependent_images);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return executable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-24 18:21:55 +08:00
|
|
|
DynamicLoader::UpdateLoadedSections(ModuleSP module,
|
|
|
|
addr_t link_map_addr,
|
|
|
|
addr_t base_addr,
|
|
|
|
bool base_addr_is_offset)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
2015-08-24 18:21:55 +08:00
|
|
|
UpdateLoadedSectionsCommon(module, base_addr, base_addr_is_offset);
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-24 18:21:55 +08:00
|
|
|
DynamicLoader::UpdateLoadedSectionsCommon(ModuleSP module,
|
|
|
|
addr_t base_addr,
|
|
|
|
bool base_addr_is_offset)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
bool changed;
|
2014-02-08 06:54:47 +08:00
|
|
|
module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset, changed);
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DynamicLoader::UnloadSections(const ModuleSP module)
|
|
|
|
{
|
|
|
|
UnloadSectionsCommon(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DynamicLoader::UnloadSectionsCommon(const ModuleSP module)
|
|
|
|
{
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
const SectionList *sections = GetSectionListFromModule(module);
|
|
|
|
|
|
|
|
assert(sections && "SectionList missing from unloaded module.");
|
|
|
|
|
|
|
|
const size_t num_sections = sections->GetSize();
|
|
|
|
for (size_t i = 0; i < num_sections; ++i)
|
|
|
|
{
|
|
|
|
SectionSP section_sp (sections->GetSectionAtIndex(i));
|
|
|
|
target.SetSectionUnloaded(section_sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SectionList *
|
|
|
|
DynamicLoader::GetSectionListFromModule(const ModuleSP module) const
|
|
|
|
{
|
|
|
|
SectionList *sections = nullptr;
|
2016-03-03 08:51:40 +08:00
|
|
|
if (module)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
ObjectFile *obj_file = module->GetObjectFile();
|
2016-03-03 08:51:40 +08:00
|
|
|
if (obj_file != nullptr)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
sections = obj_file->GetSectionList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleSP
|
2015-08-24 18:21:55 +08:00
|
|
|
DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
|
|
|
|
addr_t link_map_addr,
|
|
|
|
addr_t base_addr,
|
|
|
|
bool base_addr_is_offset)
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
ModuleList &modules = target.GetImages();
|
2016-07-22 20:55:35 +08:00
|
|
|
ModuleSpec module_spec (file, target.GetArchitecture());
|
2014-02-07 03:02:19 +08:00
|
|
|
ModuleSP module_sp;
|
|
|
|
|
|
|
|
if ((module_sp = modules.FindFirstModule (module_spec)))
|
|
|
|
{
|
2015-08-24 18:21:55 +08:00
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, base_addr_is_offset);
|
2016-07-22 20:55:35 +08:00
|
|
|
return module_sp;
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
2016-07-22 20:55:35 +08:00
|
|
|
|
|
|
|
if ((module_sp = target.GetSharedModule(module_spec)))
|
2014-02-07 03:02:19 +08:00
|
|
|
{
|
2015-08-24 18:21:55 +08:00
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, base_addr_is_offset);
|
2016-07-22 20:55:35 +08:00
|
|
|
return module_sp;
|
2014-02-07 03:02:19 +08:00
|
|
|
}
|
2016-07-22 20:55:35 +08:00
|
|
|
|
|
|
|
bool check_alternative_file_name = true;
|
|
|
|
if (base_addr_is_offset)
|
2015-06-30 18:41:23 +08:00
|
|
|
{
|
2016-07-22 20:55:35 +08:00
|
|
|
// Try to fetch the load address of the file from the process as we need absolute load
|
|
|
|
// address to read the file out of the memory instead of a load bias.
|
|
|
|
bool is_loaded = false;
|
|
|
|
lldb::addr_t load_addr;
|
|
|
|
Error error = m_process->GetFileLoadAddress(file, is_loaded, load_addr);
|
|
|
|
if (error.Success() && is_loaded)
|
2015-06-30 18:41:23 +08:00
|
|
|
{
|
2016-07-22 20:55:35 +08:00
|
|
|
check_alternative_file_name = false;
|
|
|
|
base_addr = load_addr;
|
2015-08-24 18:21:55 +08:00
|
|
|
}
|
2016-07-22 20:55:35 +08:00
|
|
|
}
|
2015-08-24 18:21:55 +08:00
|
|
|
|
2016-07-22 20:55:35 +08:00
|
|
|
// We failed to find the module based on its name. Lets try to check if we can find a
|
|
|
|
// different name based on the memory region info.
|
|
|
|
if (check_alternative_file_name)
|
|
|
|
{
|
|
|
|
MemoryRegionInfo memory_info;
|
|
|
|
Error error = m_process->GetMemoryRegionInfo(base_addr, memory_info);
|
|
|
|
if (error.Success() && memory_info.GetMapped() && memory_info.GetRange().GetRangeBase() == base_addr)
|
2015-08-24 18:21:55 +08:00
|
|
|
{
|
2016-07-22 20:55:35 +08:00
|
|
|
ModuleSpec new_module_spec(FileSpec(memory_info.GetName().AsCString(), false),
|
|
|
|
target.GetArchitecture());
|
|
|
|
|
|
|
|
if ((module_sp = modules.FindFirstModule(new_module_spec)))
|
|
|
|
{
|
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
|
|
|
|
return module_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((module_sp = target.GetSharedModule(new_module_spec)))
|
|
|
|
{
|
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
|
|
|
|
return module_sp;
|
|
|
|
}
|
2015-06-30 18:41:23 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 03:02:19 +08:00
|
|
|
|
2016-07-22 20:55:35 +08:00
|
|
|
if ((module_sp = m_process->ReadModuleFromMemory(file, base_addr)))
|
|
|
|
{
|
|
|
|
UpdateLoadedSections(module_sp, link_map_addr, base_addr, false);
|
|
|
|
target.GetImages().AppendIfNeeded(module_sp);
|
|
|
|
}
|
|
|
|
|
2014-02-07 03:02:19 +08:00
|
|
|
return module_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
DynamicLoader::ReadUnsignedIntWithSizeInBytes(addr_t addr, int size_in_bytes)
|
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
uint64_t value = m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return (int64_t)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr_t
|
|
|
|
DynamicLoader::ReadPointer(addr_t addr)
|
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
addr_t value = m_process->ReadPointerFromMemory(addr, error);
|
|
|
|
if (error.Fail())
|
|
|
|
return LLDB_INVALID_ADDRESS;
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|