From f33309823146dc7176d8b8437732668ff3e8a28b Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Sun, 18 Jun 2023 17:54:22 +0300 Subject: [PATCH] more progress on symbols decoding --- CMakeLists.txt | 2 +- src/Core/PS4/Linker.cpp | 63 ++++++++++++++++++++++++++++++++++++++++- src/Core/PS4/Linker.h | 2 ++ src/Util/StringUtil.h | 26 +++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/Util/StringUtil.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ec0be38c..023b1289 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ add_executable(shadps4 src/Core/Memory.h src/Core/PS4/Linker.cpp src/Core/PS4/Linker.h - "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h") + "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Util/StringUtil.h") find_package(OpenGL REQUIRED) diff --git a/src/Core/PS4/Linker.cpp b/src/Core/PS4/Linker.cpp index 7ab8b00f..d269a138 100644 --- a/src/Core/PS4/Linker.cpp +++ b/src/Core/PS4/Linker.cpp @@ -2,6 +2,7 @@ #include "../Memory.h" #include "../../Util/Log.h" #include "../../Util/Disassembler.h" +#include "../../Util/StringUtil.h" constexpr bool debug_loader = true; @@ -354,6 +355,56 @@ void Linker::LoadDynamicInfo(Module* m) } } +const ModuleInfo* Linker::FindModule(const Module& m, const std::string& id) +{ + const auto& import_modules = m.dynamic_info->import_modules; + int index = 0; + for (auto mod : import_modules) + { + if (mod.enc_id.compare(id) == 0) + { + return &import_modules.at(index); + } + index++; + } + const auto& export_modules = m.dynamic_info->export_modules; + index = 0; + for (auto mod : export_modules) + { + if (mod.enc_id.compare(id) == 0) + { + return &export_modules.at(index); + } + index++; + } + return nullptr; +} + +const LibraryInfo* Linker::FindLibrary(const Module& m, const std::string& id) +{ + const auto& import_libs = m.dynamic_info->import_libs; + int index = 0; + for (auto lib : import_libs) + { + if (lib.enc_id.compare(id) == 0) + { + return &import_libs.at(index); + } + index++; + } + const auto& export_libs = m.dynamic_info->export_libs; + index = 0; + for (auto lib : export_libs) + { + if (lib.enc_id.compare(id) == 0) + { + return &export_libs.at(index); + } + index++; + } + return nullptr; +} + void Linker::LoadSymbols(Module* m) { if (m->dynamic_info->symbol_table == nullptr || m->dynamic_info->str_table == nullptr || m->dynamic_info->symbol_table_total_size==0) @@ -366,6 +417,16 @@ void Linker::LoadSymbols(Module* m) sym++) { std::string id = std::string(m->dynamic_info->str_table + sym->st_name); - LOG_INFO_IF(debug_loader, "symbol {}\n", id.c_str()); + auto ids = StringUtil::split(id, '#'); + if (ids.size() == 3)//symbols are 3 parts name , library , module + { + const auto* library = FindLibrary(*m, ids.at(1)); + const auto* module = FindModule(*m, ids.at(2)); + + if (library != nullptr || module != nullptr) + { + LOG_INFO_IF(debug_loader, "name {} library {} module {}\n", ids.at(0),library->name,module->name); + } + } } } \ No newline at end of file diff --git a/src/Core/PS4/Linker.h b/src/Core/PS4/Linker.h index edbc78f8..e61d2440 100644 --- a/src/Core/PS4/Linker.h +++ b/src/Core/PS4/Linker.h @@ -106,5 +106,7 @@ public: void LoadSymbols(Module* m); private: + const ModuleInfo* FindModule(const Module& m, const std::string& id); + const LibraryInfo* FindLibrary(const Module& program, const std::string& id); std::vector m_modules; }; \ No newline at end of file diff --git a/src/Util/StringUtil.h b/src/Util/StringUtil.h new file mode 100644 index 00000000..3ebc6ae7 --- /dev/null +++ b/src/Util/StringUtil.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include + +namespace StringUtil { + + static std::vector split(const std::string& s, char seperator) + { + std::vector output; + + std::string::size_type prev_pos = 0, pos = 0; + + while ((pos = s.find(seperator, pos)) != std::string::npos) + { + std::string substring(s.substr(prev_pos, pos - prev_pos)); + + output.push_back(substring); + + prev_pos = ++pos; + } + + output.push_back(s.substr(prev_pos, pos - prev_pos)); // Last word + + return output; + } +} \ No newline at end of file