From 31f39f56464adfa40902634f6d1912b1b2caaf6b Mon Sep 17 00:00:00 2001 From: Thog Date: Wed, 8 Jan 2020 00:49:14 +0100 Subject: [PATCH] Use structs for elf symbols decoding --- .../HOS/Kernel/Process/HleProcessDebugger.cs | 67 +++++++++---------- Ryujinx.HLE/Loaders/Elf/ElfSymbol.cs | 8 +-- Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs | 17 +++++ Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs | 12 ++++ 4 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs create mode 100644 Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs diff --git a/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs b/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs index 06e60a6a75..75f52851de 100644 --- a/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs +++ b/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs @@ -1,9 +1,12 @@ using ARMeilleure.Memory; +using Ryujinx.Common; using Ryujinx.HLE.HOS.Diagnostics.Demangler; using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.Loaders.Elf; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading; @@ -87,9 +90,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process break; } - // Note: This is the return address, we need to subtract one instruction - // worth of bytes to get the branch instruction address. - AppendTrace(_owner.CpuMemory.ReadInt32(framePointer + 4) - 4); + AppendTrace(_owner.CpuMemory.ReadInt32(framePointer + 4)); framePointer = _owner.CpuMemory.ReadInt32(framePointer); } @@ -107,9 +108,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process break; } - // Note: This is the return address, we need to subtract one instruction - // worth of bytes to get the branch instruction address. - AppendTrace(_owner.CpuMemory.ReadInt64(framePointer + 8) - 4); + AppendTrace(_owner.CpuMemory.ReadInt64(framePointer + 8)); framePointer = _owner.CpuMemory.ReadInt64(framePointer); } @@ -133,9 +132,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Process ElfSymbol symbol = image.Symbols[middle]; - long endAddr = symbol.Value + symbol.Size; + ulong endAddr = symbol.Value + symbol.Size; - if ((ulong)address >= (ulong)symbol.Value && (ulong)address < (ulong)endAddr) + if ((ulong)address >= symbol.Value && (ulong)address < endAddr) { name = symbol.Name; @@ -326,40 +325,40 @@ namespace Ryujinx.HLE.HOS.Kernel.Process private ElfSymbol GetSymbol64(MemoryManager memory, long address, long strTblAddr) { - int nameIndex = memory.ReadInt32(address + 0); - int info = memory.ReadByte (address + 4); - int other = memory.ReadByte (address + 5); - int shIdx = memory.ReadInt16(address + 6); - long value = memory.ReadInt64(address + 8); - long size = memory.ReadInt64(address + 16); - - string name = string.Empty; - - for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;) + using (BinaryReader inputStream = new BinaryReader(new MemoryStream(memory.ReadBytes(address, Unsafe.SizeOf())))) { - name += (char)chr; - } + ElfSymbol64 sym = inputStream.ReadStruct(); - return new ElfSymbol(name, info, other, shIdx, value, size); + uint nameIndex = sym.NameOffset; + + string name = string.Empty; + + for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;) + { + name += (char)chr; + } + + return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size); + } } private ElfSymbol GetSymbol32(MemoryManager memory, long address, long strTblAddr) { - int nameIndex = memory.ReadInt32(address + 0); - int info = memory.ReadByte (address + 4); - int other = memory.ReadByte (address + 5); - int shIdx = memory.ReadByte (address + 12); - long value = memory.ReadByte (address + 13); - long size = memory.ReadInt16(address + 14); - - string name = string.Empty; - - for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;) + using (BinaryReader inputStream = new BinaryReader(new MemoryStream(memory.ReadBytes(address, Unsafe.SizeOf())))) { - name += (char)chr; - } + ElfSymbol32 sym = inputStream.ReadStruct(); - return new ElfSymbol(name, info, other, shIdx, value, size); + uint nameIndex = sym.NameOffset; + + string name = string.Empty; + + for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;) + { + name += (char)chr; + } + + return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size); + } } } } \ No newline at end of file diff --git a/Ryujinx.HLE/Loaders/Elf/ElfSymbol.cs b/Ryujinx.HLE/Loaders/Elf/ElfSymbol.cs index b655816f14..9961afe195 100644 --- a/Ryujinx.HLE/Loaders/Elf/ElfSymbol.cs +++ b/Ryujinx.HLE/Loaders/Elf/ElfSymbol.cs @@ -17,16 +17,16 @@ namespace Ryujinx.HLE.Loaders.Elf Binding == ElfSymbolBinding.StbWeak; public int ShIdx { get; private set; } - public long Value { get; private set; } - public long Size { get; private set; } + public ulong Value { get; private set; } + public ulong Size { get; private set; } public ElfSymbol( string name, int info, int other, int shIdx, - long value, - long size) + ulong value, + ulong size) { Name = name; Type = (ElfSymbolType)(info & 0xf); diff --git a/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs b/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs new file mode 100644 index 0000000000..31beceddd1 --- /dev/null +++ b/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs @@ -0,0 +1,17 @@ +namespace Ryujinx.HLE.Loaders.Elf +{ + struct ElfSymbol32 + { + public uint NameOffset; + public uint ValueAddress; + public uint Size; + public char Info; + public char Other; + public ushort SectionIndex; + + public string GetName() + { + + } + } +} diff --git a/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs b/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs new file mode 100644 index 0000000000..662de1ff1b --- /dev/null +++ b/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs @@ -0,0 +1,12 @@ +namespace Ryujinx.HLE.Loaders.Elf +{ + struct ElfSymbol64 + { + public uint NameOffset; + public char Info; + public char Other; + public ushort SectionIndex; + public ulong ValueAddress; + public ulong Size; + } +}