2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-09-19 04:17:41 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/SymbolDB.h"
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2018-05-27 21:37:52 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2017-08-16 01:40:24 +00:00
|
|
|
static std::string GetStrippedFunctionName(const std::string& symbol_name)
|
|
|
|
{
|
|
|
|
std::string name = symbol_name.substr(0, symbol_name.find('('));
|
|
|
|
size_t position = name.find(' ');
|
|
|
|
if (position != std::string::npos)
|
|
|
|
name.erase(position);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:25:20 +00:00
|
|
|
SymbolDB::SymbolDB() = default;
|
|
|
|
|
|
|
|
SymbolDB::~SymbolDB() = default;
|
|
|
|
|
2017-08-16 01:40:24 +00:00
|
|
|
void Symbol::Rename(const std::string& symbol_name)
|
|
|
|
{
|
|
|
|
this->name = symbol_name;
|
|
|
|
this->function_name = GetStrippedFunctionName(symbol_name);
|
|
|
|
}
|
|
|
|
|
2009-06-21 08:39:21 +00:00
|
|
|
void SymbolDB::List()
|
|
|
|
{
|
2018-05-27 21:15:20 +00:00
|
|
|
for (const auto& func : m_functions)
|
2009-06-21 08:39:21 +00:00
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
DEBUG_LOG_FMT(OSHLE, "{} @ {:08x}: {} bytes (hash {:08x}) : {} calls", func.second.name,
|
|
|
|
func.second.address, func.second.size, func.second.hash, func.second.num_calls);
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
2020-10-23 18:41:30 +00:00
|
|
|
INFO_LOG_FMT(OSHLE, "{} functions known in this program above.", m_functions.size());
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2019-05-01 15:32:45 +00:00
|
|
|
bool SymbolDB::IsEmpty() const
|
|
|
|
{
|
|
|
|
return m_functions.empty();
|
|
|
|
}
|
|
|
|
|
2016-01-21 19:46:25 +00:00
|
|
|
void SymbolDB::Clear(const char* prefix)
|
2009-06-21 08:39:21 +00:00
|
|
|
{
|
|
|
|
// TODO: honor prefix
|
2018-05-27 21:15:20 +00:00
|
|
|
m_functions.clear();
|
|
|
|
m_checksum_to_function.clear();
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2009-06-21 08:39:21 +00:00
|
|
|
void SymbolDB::Index()
|
|
|
|
{
|
|
|
|
int i = 0;
|
2018-05-27 21:15:20 +00:00
|
|
|
for (auto& func : m_functions)
|
2009-06-21 08:39:21 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
func.second.index = i++;
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2019-06-16 05:09:58 +00:00
|
|
|
Symbol* SymbolDB::GetSymbolFromName(std::string_view name)
|
2009-06-21 08:39:21 +00:00
|
|
|
{
|
2018-05-27 21:15:20 +00:00
|
|
|
for (auto& func : m_functions)
|
2009-06-21 08:39:21 +00:00
|
|
|
{
|
2016-09-26 13:41:57 +00:00
|
|
|
if (func.second.function_name == name)
|
2013-10-29 05:09:01 +00:00
|
|
|
return &func.second;
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
2014-03-12 19:33:41 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2019-06-16 05:09:58 +00:00
|
|
|
std::vector<Symbol*> SymbolDB::GetSymbolsFromName(std::string_view name)
|
2016-10-04 16:33:25 +00:00
|
|
|
{
|
|
|
|
std::vector<Symbol*> symbols;
|
|
|
|
|
2018-05-27 21:15:20 +00:00
|
|
|
for (auto& func : m_functions)
|
2016-10-04 16:33:25 +00:00
|
|
|
{
|
|
|
|
if (func.second.function_name == name)
|
|
|
|
symbols.push_back(&func.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return symbols;
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:51:12 +00:00
|
|
|
Symbol* SymbolDB::GetSymbolFromHash(u32 hash)
|
|
|
|
{
|
2018-05-27 21:15:20 +00:00
|
|
|
auto iter = m_checksum_to_function.find(hash);
|
|
|
|
if (iter == m_checksum_to_function.end())
|
2016-10-05 14:51:12 +00:00
|
|
|
return nullptr;
|
2018-05-27 21:15:20 +00:00
|
|
|
|
|
|
|
return *iter->second.begin();
|
2016-10-05 14:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Symbol*> SymbolDB::GetSymbolsFromHash(u32 hash)
|
|
|
|
{
|
2018-05-27 21:15:20 +00:00
|
|
|
const auto iter = m_checksum_to_function.find(hash);
|
2016-10-05 14:51:12 +00:00
|
|
|
|
2018-05-27 21:15:20 +00:00
|
|
|
if (iter == m_checksum_to_function.cend())
|
2017-02-18 10:26:49 +00:00
|
|
|
return {};
|
2016-10-05 14:51:12 +00:00
|
|
|
|
2017-02-18 10:26:49 +00:00
|
|
|
return {iter->second.cbegin(), iter->second.cend()};
|
2016-10-05 14:51:12 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 20:27:56 +00:00
|
|
|
void SymbolDB::AddCompleteSymbol(const Symbol& symbol)
|
2009-06-21 08:39:21 +00:00
|
|
|
{
|
2018-05-27 21:15:20 +00:00
|
|
|
m_functions.emplace(symbol.address, symbol);
|
2009-06-21 08:39:21 +00:00
|
|
|
}
|
2018-05-27 21:37:52 +00:00
|
|
|
} // namespace Common
|