diff options
| author | 2022-04-11 18:40:34 +0200 | |
|---|---|---|
| committer | 2022-04-11 18:40:34 +0200 | |
| commit | b86cfe159fe8fd4256a83db6fc4913a83ac37f00 (patch) | |
| tree | 8b49faf932128e9b281e7789bbbaa3eb5a3905af /src/core/arm/arm_interface.cpp | |
| parent | Merge pull request #8171 from tech-ticks/skyline-improvements (diff) | |
| parent | core: extract symbol reading (diff) | |
| download | yuzu-b86cfe159fe8fd4256a83db6fc4913a83ac37f00.tar.gz yuzu-b86cfe159fe8fd4256a83db6fc4913a83ac37f00.tar.xz yuzu-b86cfe159fe8fd4256a83db6fc4913a83ac37f00.zip | |
Merge pull request #8180 from liamwhite/symbols
core: extract symbol reading
Diffstat (limited to 'src/core/arm/arm_interface.cpp')
| -rw-r--r-- | src/core/arm/arm_interface.cpp | 141 |
1 files changed, 12 insertions, 129 deletions
diff --git a/src/core/arm/arm_interface.cpp b/src/core/arm/arm_interface.cpp index 0951e1976..08bf1201d 100644 --- a/src/core/arm/arm_interface.cpp +++ b/src/core/arm/arm_interface.cpp | |||
| @@ -8,134 +8,13 @@ | |||
| 8 | #include "common/common_types.h" | 8 | #include "common/common_types.h" |
| 9 | #include "common/logging/log.h" | 9 | #include "common/logging/log.h" |
| 10 | #include "core/arm/arm_interface.h" | 10 | #include "core/arm/arm_interface.h" |
| 11 | #include "core/arm/symbols.h" | ||
| 11 | #include "core/core.h" | 12 | #include "core/core.h" |
| 13 | #include "core/hle/kernel/k_process.h" | ||
| 12 | #include "core/loader/loader.h" | 14 | #include "core/loader/loader.h" |
| 13 | #include "core/memory.h" | 15 | #include "core/memory.h" |
| 14 | 16 | ||
| 15 | namespace Core { | 17 | namespace Core { |
| 16 | namespace { | ||
| 17 | |||
| 18 | constexpr u64 ELF_DYNAMIC_TAG_NULL = 0; | ||
| 19 | constexpr u64 ELF_DYNAMIC_TAG_STRTAB = 5; | ||
| 20 | constexpr u64 ELF_DYNAMIC_TAG_SYMTAB = 6; | ||
| 21 | constexpr u64 ELF_DYNAMIC_TAG_SYMENT = 11; | ||
| 22 | |||
| 23 | enum class ELFSymbolType : u8 { | ||
| 24 | None = 0, | ||
| 25 | Object = 1, | ||
| 26 | Function = 2, | ||
| 27 | Section = 3, | ||
| 28 | File = 4, | ||
| 29 | Common = 5, | ||
| 30 | TLS = 6, | ||
| 31 | }; | ||
| 32 | |||
| 33 | enum class ELFSymbolBinding : u8 { | ||
| 34 | Local = 0, | ||
| 35 | Global = 1, | ||
| 36 | Weak = 2, | ||
| 37 | }; | ||
| 38 | |||
| 39 | enum class ELFSymbolVisibility : u8 { | ||
| 40 | Default = 0, | ||
| 41 | Internal = 1, | ||
| 42 | Hidden = 2, | ||
| 43 | Protected = 3, | ||
| 44 | }; | ||
| 45 | |||
| 46 | struct ELFSymbol { | ||
| 47 | u32 name_index; | ||
| 48 | union { | ||
| 49 | u8 info; | ||
| 50 | |||
| 51 | BitField<0, 4, ELFSymbolType> type; | ||
| 52 | BitField<4, 4, ELFSymbolBinding> binding; | ||
| 53 | }; | ||
| 54 | ELFSymbolVisibility visibility; | ||
| 55 | u16 sh_index; | ||
| 56 | u64 value; | ||
| 57 | u64 size; | ||
| 58 | }; | ||
| 59 | static_assert(sizeof(ELFSymbol) == 0x18, "ELFSymbol has incorrect size."); | ||
| 60 | |||
| 61 | using Symbols = std::vector<std::pair<ELFSymbol, std::string>>; | ||
| 62 | |||
| 63 | Symbols GetSymbols(VAddr text_offset, Core::Memory::Memory& memory) { | ||
| 64 | const auto mod_offset = text_offset + memory.Read32(text_offset + 4); | ||
| 65 | |||
| 66 | if (mod_offset < text_offset || (mod_offset & 0b11) != 0 || | ||
| 67 | memory.Read32(mod_offset) != Common::MakeMagic('M', 'O', 'D', '0')) { | ||
| 68 | return {}; | ||
| 69 | } | ||
| 70 | |||
| 71 | const auto dynamic_offset = memory.Read32(mod_offset + 0x4) + mod_offset; | ||
| 72 | |||
| 73 | VAddr string_table_offset{}; | ||
| 74 | VAddr symbol_table_offset{}; | ||
| 75 | u64 symbol_entry_size{}; | ||
| 76 | |||
| 77 | VAddr dynamic_index = dynamic_offset; | ||
| 78 | while (true) { | ||
| 79 | const u64 tag = memory.Read64(dynamic_index); | ||
| 80 | const u64 value = memory.Read64(dynamic_index + 0x8); | ||
| 81 | dynamic_index += 0x10; | ||
| 82 | |||
| 83 | if (tag == ELF_DYNAMIC_TAG_NULL) { | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | |||
| 87 | if (tag == ELF_DYNAMIC_TAG_STRTAB) { | ||
| 88 | string_table_offset = value; | ||
| 89 | } else if (tag == ELF_DYNAMIC_TAG_SYMTAB) { | ||
| 90 | symbol_table_offset = value; | ||
| 91 | } else if (tag == ELF_DYNAMIC_TAG_SYMENT) { | ||
| 92 | symbol_entry_size = value; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | if (string_table_offset == 0 || symbol_table_offset == 0 || symbol_entry_size == 0) { | ||
| 97 | return {}; | ||
| 98 | } | ||
| 99 | |||
| 100 | const auto string_table_address = text_offset + string_table_offset; | ||
| 101 | const auto symbol_table_address = text_offset + symbol_table_offset; | ||
| 102 | |||
| 103 | Symbols out; | ||
| 104 | |||
| 105 | VAddr symbol_index = symbol_table_address; | ||
| 106 | while (symbol_index < string_table_address) { | ||
| 107 | ELFSymbol symbol{}; | ||
| 108 | memory.ReadBlock(symbol_index, &symbol, sizeof(ELFSymbol)); | ||
| 109 | |||
| 110 | VAddr string_offset = string_table_address + symbol.name_index; | ||
| 111 | std::string name; | ||
| 112 | for (u8 c = memory.Read8(string_offset); c != 0; c = memory.Read8(++string_offset)) { | ||
| 113 | name += static_cast<char>(c); | ||
| 114 | } | ||
| 115 | |||
| 116 | symbol_index += symbol_entry_size; | ||
| 117 | out.push_back({symbol, name}); | ||
| 118 | } | ||
| 119 | |||
| 120 | return out; | ||
| 121 | } | ||
| 122 | |||
| 123 | std::optional<std::string> GetSymbolName(const Symbols& symbols, VAddr func_address) { | ||
| 124 | const auto iter = | ||
| 125 | std::find_if(symbols.begin(), symbols.end(), [func_address](const auto& pair) { | ||
| 126 | const auto& symbol = pair.first; | ||
| 127 | const auto end_address = symbol.value + symbol.size; | ||
| 128 | return func_address >= symbol.value && func_address < end_address; | ||
| 129 | }); | ||
| 130 | |||
| 131 | if (iter == symbols.end()) { | ||
| 132 | return std::nullopt; | ||
| 133 | } | ||
| 134 | |||
| 135 | return iter->second; | ||
| 136 | } | ||
| 137 | |||
| 138 | } // Anonymous namespace | ||
| 139 | 18 | ||
| 140 | constexpr u64 SEGMENT_BASE = 0x7100000000ull; | 19 | constexpr u64 SEGMENT_BASE = 0x7100000000ull; |
| 141 | 20 | ||
| @@ -169,9 +48,11 @@ std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContex | |||
| 169 | return {}; | 48 | return {}; |
| 170 | } | 49 | } |
| 171 | 50 | ||
| 172 | std::map<std::string, Symbols> symbols; | 51 | std::map<std::string, Symbols::Symbols> symbols; |
| 173 | for (const auto& module : modules) { | 52 | for (const auto& module : modules) { |
| 174 | symbols.insert_or_assign(module.second, GetSymbols(module.first, memory)); | 53 | symbols.insert_or_assign(module.second, |
| 54 | Symbols::GetSymbols(module.first, system.Memory(), | ||
| 55 | system.CurrentProcess()->Is64BitProcess())); | ||
| 175 | } | 56 | } |
| 176 | 57 | ||
| 177 | for (auto& entry : out) { | 58 | for (auto& entry : out) { |
| @@ -193,7 +74,7 @@ std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktraceFromContex | |||
| 193 | 74 | ||
| 194 | const auto symbol_set = symbols.find(entry.module); | 75 | const auto symbol_set = symbols.find(entry.module); |
| 195 | if (symbol_set != symbols.end()) { | 76 | if (symbol_set != symbols.end()) { |
| 196 | const auto symbol = GetSymbolName(symbol_set->second, entry.offset); | 77 | const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset); |
| 197 | if (symbol.has_value()) { | 78 | if (symbol.has_value()) { |
| 198 | // TODO(DarkLordZach): Add demangling of symbol names. | 79 | // TODO(DarkLordZach): Add demangling of symbol names. |
| 199 | entry.name = *symbol; | 80 | entry.name = *symbol; |
| @@ -225,9 +106,11 @@ std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const { | |||
| 225 | return {}; | 106 | return {}; |
| 226 | } | 107 | } |
| 227 | 108 | ||
| 228 | std::map<std::string, Symbols> symbols; | 109 | std::map<std::string, Symbols::Symbols> symbols; |
| 229 | for (const auto& module : modules) { | 110 | for (const auto& module : modules) { |
| 230 | symbols.insert_or_assign(module.second, GetSymbols(module.first, memory)); | 111 | symbols.insert_or_assign(module.second, |
| 112 | Symbols::GetSymbols(module.first, system.Memory(), | ||
| 113 | system.CurrentProcess()->Is64BitProcess())); | ||
| 231 | } | 114 | } |
| 232 | 115 | ||
| 233 | for (auto& entry : out) { | 116 | for (auto& entry : out) { |
| @@ -249,7 +132,7 @@ std::vector<ARM_Interface::BacktraceEntry> ARM_Interface::GetBacktrace() const { | |||
| 249 | 132 | ||
| 250 | const auto symbol_set = symbols.find(entry.module); | 133 | const auto symbol_set = symbols.find(entry.module); |
| 251 | if (symbol_set != symbols.end()) { | 134 | if (symbol_set != symbols.end()) { |
| 252 | const auto symbol = GetSymbolName(symbol_set->second, entry.offset); | 135 | const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset); |
| 253 | if (symbol.has_value()) { | 136 | if (symbol.has_value()) { |
| 254 | // TODO(DarkLordZach): Add demangling of symbol names. | 137 | // TODO(DarkLordZach): Add demangling of symbol names. |
| 255 | entry.name = *symbol; | 138 | entry.name = *symbol; |