2019-07-12 13:31:00 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-07-12 13:31:00 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-07-12 13:31:00 +01:00
|
|
|
#include "creport_modules.hpp"
|
|
|
|
#include "creport_utils.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::creport {
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Convenience definitions/types. */
|
|
|
|
constexpr size_t ModulePathLengthMax = 0x200;
|
|
|
|
constexpr u8 GnuSignature[4] = {'G', 'N', 'U', 0};
|
|
|
|
|
|
|
|
struct ModulePath {
|
|
|
|
u32 zero;
|
2021-11-02 04:43:29 +00:00
|
|
|
s32 path_length;
|
2019-07-12 13:31:00 +01:00
|
|
|
char path[ModulePathLengthMax];
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ModulePath) == 0x208, "ModulePath definition!");
|
|
|
|
|
|
|
|
struct RoDataStart {
|
|
|
|
union {
|
|
|
|
u64 deprecated_rwdata_offset;
|
|
|
|
ModulePath module_path;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(RoDataStart) == sizeof(ModulePath), "RoDataStart definition!");
|
|
|
|
|
2020-02-23 07:16:53 +00:00
|
|
|
/* Globals. */
|
|
|
|
u8 g_last_rodata_pages[2 * os::MemoryPageSize];
|
|
|
|
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 09:45:12 +00:00
|
|
|
void ModuleList::SaveToFile(ScopedFile &file) {
|
2021-10-10 08:14:06 +01:00
|
|
|
file.WriteFormat(" Number of Modules: %zu\n", m_num_modules);
|
|
|
|
for (size_t i = 0; i < m_num_modules; i++) {
|
|
|
|
const auto& module = m_modules[i];
|
2020-03-08 09:45:12 +00:00
|
|
|
file.WriteFormat(" Module %02zu:\n", i);
|
|
|
|
file.WriteFormat(" Address: %016lx-%016lx\n", module.start_address, module.end_address);
|
2021-10-10 08:14:06 +01:00
|
|
|
if (std::strcmp(m_modules[i].name, "") != 0) {
|
2020-03-08 09:45:12 +00:00
|
|
|
file.WriteFormat(" Name: %s\n", module.name);
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
2021-10-11 02:22:32 +01:00
|
|
|
file.DumpMemory(" Module Id: ", module.module_id, sizeof(module.module_id));
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 22:18:00 +01:00
|
|
|
void ModuleList::FindModulesFromThreadInfo(os::NativeHandle debug_handle, const ThreadInfo &thread, bool is_64_bit) {
|
2019-07-12 13:31:00 +01:00
|
|
|
/* Set the debug handle, for access in other member functions. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_debug_handle = debug_handle;
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Try to add the thread's PC. */
|
2021-10-30 22:18:00 +01:00
|
|
|
this->TryAddModule(thread.GetPC(), is_64_bit);
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Try to add the thread's LR. */
|
2021-10-30 22:18:00 +01:00
|
|
|
this->TryAddModule(thread.GetLR(), is_64_bit);
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Try to add all the addresses in the thread's stacktrace. */
|
|
|
|
for (size_t i = 0; i < thread.GetStackTraceSize(); i++) {
|
2021-10-30 22:18:00 +01:00
|
|
|
this->TryAddModule(thread.GetStackTrace(i), is_64_bit);
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 22:18:00 +01:00
|
|
|
void ModuleList::TryAddModule(uintptr_t guess, bool is_64_bit) {
|
2019-07-12 13:31:00 +01:00
|
|
|
/* Try to locate module from guess. */
|
|
|
|
uintptr_t base_address = 0;
|
2021-10-30 22:18:00 +01:00
|
|
|
if (!this->TryFindModule(std::addressof(base_address), guess, is_64_bit)) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether we already have this module. */
|
2021-10-10 08:14:06 +01:00
|
|
|
for (size_t i = 0; i < m_num_modules; i++) {
|
|
|
|
if (m_modules[i].start_address <= base_address && base_address < m_modules[i].end_address) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add all contiguous modules. */
|
|
|
|
uintptr_t cur_address = base_address;
|
2021-10-10 08:14:06 +01:00
|
|
|
while (m_num_modules < ModuleCountMax) {
|
2019-07-12 13:31:00 +01:00
|
|
|
/* Get the region extents. */
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::MemoryInfo mi;
|
|
|
|
svc::PageInfo pi;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, cur_address))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse module. */
|
2021-10-05 23:16:54 +01:00
|
|
|
if (mi.permission == svc::MemoryPermission_ReadExecute) {
|
2021-10-10 08:14:06 +01:00
|
|
|
auto& module = m_modules[m_num_modules++];
|
2021-10-05 23:16:54 +01:00
|
|
|
module.start_address = mi.base_address;
|
|
|
|
module.end_address = mi.base_address + mi.size;
|
2019-07-12 13:31:00 +01:00
|
|
|
GetModuleName(module.name, module.start_address, module.end_address);
|
2021-10-11 02:22:32 +01:00
|
|
|
GetModuleId(module.module_id, module.end_address);
|
2021-10-30 22:18:00 +01:00
|
|
|
|
|
|
|
/* Default to no symbol table. */
|
|
|
|
module.has_sym_table = false;
|
|
|
|
|
2019-07-12 13:31:00 +01:00
|
|
|
if (std::strcmp(module.name, "") == 0) {
|
2021-10-30 22:18:00 +01:00
|
|
|
/* Some homebrew won't have a name. Add a fake one for readability. */
|
2021-10-11 02:22:32 +01:00
|
|
|
util::SNPrintf(module.name, sizeof(module.name), "[%02x%02x%02x%02x]", module.module_id[0], module.module_id[1], module.module_id[2], module.module_id[3]);
|
2021-10-30 22:18:00 +01:00
|
|
|
} else {
|
|
|
|
/* The module has a name, and so might have a symbol table. Try to add it, if it does. */
|
|
|
|
if (is_64_bit) {
|
|
|
|
DetectModuleSymbolTable(module);
|
|
|
|
}
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're out of readable memory, we're done reading code. */
|
2021-10-04 22:54:13 +01:00
|
|
|
if (mi.state == svc::MemoryState_Free || mi.state == svc::MemoryState_Inaccessible) {
|
2019-07-12 13:31:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify we're not getting stuck in an infinite loop. */
|
|
|
|
if (mi.size == 0 || cur_address + mi.size <= cur_address) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_address += mi.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 22:18:00 +01:00
|
|
|
bool ModuleList::TryFindModule(uintptr_t *out_address, uintptr_t guess, bool is_64_bit) {
|
|
|
|
AMS_UNUSED(is_64_bit);
|
|
|
|
|
2019-07-12 13:31:00 +01:00
|
|
|
/* Query the memory region our guess falls in. */
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::MemoryInfo mi;
|
|
|
|
svc::PageInfo pi;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, guess))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we fall into a RW region, it may be rwdata. Query the region before it, which may be rodata or text. */
|
2021-10-05 23:16:54 +01:00
|
|
|
if (mi.permission == svc::MemoryPermission_ReadWrite) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, mi.base_address - 4))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we fall into an RO region, it may be rodata. Query the region before it, which should be text. */
|
2021-10-05 23:16:54 +01:00
|
|
|
if (mi.permission == svc::MemoryPermission_Read) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, mi.base_address - 4))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We should, at this point, be looking at an executable region (text). */
|
2021-10-05 23:16:54 +01:00
|
|
|
if (mi.permission != svc::MemoryPermission_ReadExecute) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modules are a series of contiguous (text/rodata/rwdata) regions. */
|
|
|
|
/* Iterate backwards until we find unmapped memory, to find the start of the set of modules loaded here. */
|
2021-10-05 23:16:54 +01:00
|
|
|
while (mi.base_address > 0) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, mi.base_address - 4))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:13 +01:00
|
|
|
if (mi.state == svc::MemoryState_Free) {
|
2019-07-12 13:31:00 +01:00
|
|
|
/* We've found unmapped memory, so output the mapped memory afterwards. */
|
2021-10-05 23:16:54 +01:00
|
|
|
*out_address = mi.base_address + mi.size;
|
2019-07-12 13:31:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Something weird happened here. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleList::GetModuleName(char *out_name, uintptr_t text_start_address, uintptr_t ro_start_address) {
|
|
|
|
/* Clear output. */
|
|
|
|
std::memset(out_name, 0, ModuleNameLengthMax);
|
|
|
|
|
|
|
|
/* Read module path from process memory. */
|
|
|
|
RoDataStart rodata_start;
|
|
|
|
{
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::MemoryInfo mi;
|
|
|
|
svc::PageInfo pi;
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Verify .rodata is read-only. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, ro_start_address)) || mi.permission != svc::MemoryPermission_Read) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate start of rwdata. */
|
2021-10-05 23:16:54 +01:00
|
|
|
const u64 rw_start_address = mi.base_address + mi.size;
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Read start of .rodata. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(rodata_start)), m_debug_handle, ro_start_address, sizeof(rodata_start)))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If data is valid under deprecated format, there's no name. */
|
|
|
|
if (text_start_address + rodata_start.deprecated_rwdata_offset == rw_start_address) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Also validate that we're looking at a valid name. */
|
2021-11-02 17:10:47 +00:00
|
|
|
if (rodata_start.module_path.zero != 0 || rodata_start.module_path.path_length <= 0) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Start after last slash in path. */
|
|
|
|
const char *path = rodata_start.module_path.path;
|
|
|
|
int ofs;
|
2021-11-02 04:43:29 +00:00
|
|
|
for (ofs = std::min<size_t>(rodata_start.module_path.path_length, sizeof(rodata_start.module_path.path)); ofs >= 0; ofs--) {
|
2019-07-12 13:31:00 +01:00
|
|
|
if (path[ofs] == '/' || path[ofs] == '\\') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ofs++;
|
|
|
|
|
|
|
|
/* Copy name to output. */
|
2021-11-02 04:43:29 +00:00
|
|
|
const size_t name_size = std::min(ModuleNameLengthMax, std::min<size_t>(rodata_start.module_path.path_length, sizeof(rodata_start.module_path.path)) - ofs);
|
|
|
|
std::memcpy(out_name, path + ofs, name_size);
|
2019-07-12 13:31:00 +01:00
|
|
|
out_name[ModuleNameLengthMax - 1] = '\x00';
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:22:32 +01:00
|
|
|
void ModuleList::GetModuleId(u8 *out, uintptr_t ro_start_address) {
|
2019-07-12 13:31:00 +01:00
|
|
|
/* Clear output. */
|
2021-10-11 02:22:32 +01:00
|
|
|
std::memset(out, 0, ModuleIdSize);
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* Verify .rodata is read-only. */
|
2021-10-04 22:54:13 +01:00
|
|
|
svc::MemoryInfo mi;
|
|
|
|
svc::PageInfo pi;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, ro_start_address)) || mi.permission != svc::MemoryPermission_Read) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We want to read the last two pages of .rodata. */
|
2020-02-23 07:16:53 +00:00
|
|
|
const size_t read_size = mi.size >= sizeof(g_last_rodata_pages) ? sizeof(g_last_rodata_pages) : (sizeof(g_last_rodata_pages) / 2);
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(g_last_rodata_pages), m_debug_handle, mi.base_address + mi.size - read_size, read_size))) {
|
2019-07-12 13:31:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:22:32 +01:00
|
|
|
/* Find GNU\x00 to locate start of module id (GNU build id). */
|
|
|
|
for (int ofs = read_size - sizeof(GnuSignature) - ModuleIdSize; ofs >= 0; ofs--) {
|
2020-02-23 07:16:53 +00:00
|
|
|
if (std::memcmp(g_last_rodata_pages + ofs, GnuSignature, sizeof(GnuSignature)) == 0) {
|
2021-10-11 02:22:32 +01:00
|
|
|
std::memcpy(out, g_last_rodata_pages + ofs + sizeof(GnuSignature), ModuleIdSize);
|
2019-07-12 13:31:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 22:18:00 +01:00
|
|
|
void ModuleList::DetectModuleSymbolTable(ModuleInfo &module) {
|
|
|
|
/* If we already have a symbol table, no more parsing is needed. */
|
|
|
|
if (module.has_sym_table) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Declare temporaries. */
|
|
|
|
u64 temp_64;
|
|
|
|
u32 temp_32;
|
|
|
|
|
|
|
|
/* Get module state. */
|
|
|
|
svc::MemoryInfo mi;
|
|
|
|
svc::PageInfo pi;
|
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, module.start_address))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto module_state = mi.state;
|
|
|
|
|
|
|
|
/* Verify .rodata is read-only with same state as .text. */
|
|
|
|
if (R_FAILED(svc::QueryDebugProcessMemory(std::addressof(mi), std::addressof(pi), m_debug_handle, module.end_address)) || mi.permission != svc::MemoryPermission_Read || mi.state != module_state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the first instruction of .text. */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_32)), m_debug_handle, module.start_address, sizeof(temp_32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We want to find the symbol table/.dynamic. */
|
|
|
|
uintptr_t dyn_address = 0;
|
|
|
|
uintptr_t sym_tab = 0;
|
|
|
|
uintptr_t str_tab = 0;
|
|
|
|
size_t num_sym = 0;
|
|
|
|
|
|
|
|
/* Detect module type. */
|
|
|
|
if (temp_32 == 0) {
|
|
|
|
/* Module is dynamically loaded by rtld. */
|
|
|
|
u32 mod_offset;
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(mod_offset)), m_debug_handle, module.start_address + sizeof(u32), sizeof(u32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_32)), m_debug_handle, module.start_address + mod_offset, sizeof(u32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_32 != rocrt::ModuleHeaderVersion) { /* MOD0 */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_32)), m_debug_handle, module.start_address + mod_offset + sizeof(u32), sizeof(u32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dyn_address = module.start_address + mod_offset + temp_32;
|
|
|
|
} else if (temp_32 == 0x14000002) {
|
|
|
|
/* Module embeds rtld. */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_32)), m_debug_handle, module.start_address + 0x5C, sizeof(u32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_32 != 0x94000002) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_32)), m_debug_handle, module.start_address + 0x60, sizeof(u32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dyn_address = module.start_address + 0x60 + temp_32;
|
|
|
|
} else {
|
|
|
|
/* Module has unknown format. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Locate tables inside .dyn. */
|
|
|
|
for (size_t ofs = 0; /* ... */; ofs += 0x10) {
|
|
|
|
/* Read the DynamicTag. */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_64)), m_debug_handle, dyn_address + ofs, sizeof(u64)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_64 == 0) {
|
|
|
|
/* We're done parsing .dyn. */
|
|
|
|
break;
|
|
|
|
} else if (temp_64 == 4) {
|
|
|
|
/* We found DT_HASH */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_64)), m_debug_handle, dyn_address + ofs + sizeof(u64), sizeof(u64)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read nchain, to get the number of symbols. */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_32)), m_debug_handle, module.start_address + temp_64 + sizeof(u32), sizeof(u32)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_sym = temp_32;
|
|
|
|
} else if (temp_64 == 5) {
|
|
|
|
/* We found DT_STRTAB */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_64)), m_debug_handle, dyn_address + ofs + sizeof(u64), sizeof(u64)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str_tab = module.start_address + temp_64;
|
|
|
|
} else if (temp_64 == 6) {
|
|
|
|
/* We found DT_SYMTAB */
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(temp_64)), m_debug_handle, dyn_address + ofs + sizeof(u64), sizeof(u64)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sym_tab = module.start_address + temp_64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we found all the tables. */
|
|
|
|
if (!(sym_tab != 0 && str_tab != 0 && num_sym != 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.has_sym_table = true;
|
|
|
|
module.sym_tab = sym_tab;
|
|
|
|
module.str_tab = str_tab;
|
|
|
|
module.num_sym = static_cast<u32>(num_sym);
|
|
|
|
}
|
|
|
|
|
2019-07-12 13:31:00 +01:00
|
|
|
const char *ModuleList::GetFormattedAddressString(uintptr_t address) {
|
|
|
|
/* Print default formatted string. */
|
2021-10-10 08:14:06 +01:00
|
|
|
util::SNPrintf(m_address_str_buf, sizeof(m_address_str_buf), "%016lx", address);
|
2019-07-12 13:31:00 +01:00
|
|
|
|
|
|
|
/* See if the address is inside a module, for pretty-printing. */
|
2021-10-10 08:14:06 +01:00
|
|
|
for (size_t i = 0; i < m_num_modules; i++) {
|
|
|
|
const auto& module = m_modules[i];
|
2019-07-12 13:31:00 +01:00
|
|
|
if (module.start_address <= address && address < module.end_address) {
|
2021-10-30 22:18:00 +01:00
|
|
|
if (module.has_sym_table) {
|
|
|
|
/* Try to locate an appropriate symbol. */
|
|
|
|
for (size_t j = 0; j < module.num_sym; ++j) {
|
|
|
|
/* Read symbol from the module's symbol table. */
|
|
|
|
struct {
|
|
|
|
u32 st_name;
|
|
|
|
u8 st_info;
|
|
|
|
u8 st_other;
|
|
|
|
u16 st_shndx;
|
|
|
|
u64 st_value;
|
|
|
|
u64 st_size;
|
|
|
|
} sym;
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(std::addressof(sym)), m_debug_handle, module.sym_tab + j * sizeof(sym), sizeof(sym)))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the symbol is valid/STT_FUNC. */
|
|
|
|
if (sym.st_shndx == 0 || ((sym.st_shndx & 0xFF00) == 0xFF00)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((sym.st_info & 0xF) != 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the address. */
|
|
|
|
const uintptr_t func_start = module.start_address + sym.st_value;
|
|
|
|
if (func_start <= address && address < func_start + sym.st_size) {
|
|
|
|
/* Read the symbol name. */
|
|
|
|
const uintptr_t sym_address = module.str_tab + sym.st_name;
|
|
|
|
char sym_name[0x80];
|
|
|
|
if (R_FAILED(svc::ReadDebugProcessMemory(reinterpret_cast<uintptr_t>(sym_name), m_debug_handle, sym_address, sizeof(sym_name)))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure null-termination. */
|
|
|
|
sym_name[sizeof(sym_name) - 1] = '\x00';
|
|
|
|
|
|
|
|
/* Print the symbol. */
|
|
|
|
util::SNPrintf(m_address_str_buf, sizeof(m_address_str_buf), "%016lx (%s + 0x%lx) (%s + 0x%lx)", address, module.name, address - module.start_address, sym_name, address - func_start);
|
|
|
|
return m_address_str_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
util::SNPrintf(m_address_str_buf, sizeof(m_address_str_buf), "%016lx (%s + 0x%lx)", address, module.name, address - module.start_address);
|
2021-10-30 22:18:00 +01:00
|
|
|
return m_address_str_buf;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_address_str_buf;
|
2019-07-12 13:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|