2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.HLE.HOS.Diagnostics.Demangler;
|
2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Memory;
|
2021-05-21 01:27:16 +02:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.HLE.Loaders.Elf;
|
2020-12-02 00:23:43 +01:00
|
|
|
using Ryujinx.Memory;
|
2018-11-28 23:18:09 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Process
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
class HleProcessDebugger
|
|
|
|
{
|
|
|
|
private const int Mod0 = 'M' << 0 | 'O' << 8 | 'D' << 16 | '0' << 24;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KProcess _owner;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
private class Image
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
public ulong BaseAddress { get; }
|
2021-05-21 01:27:16 +02:00
|
|
|
public ulong Size { get; }
|
|
|
|
public ulong EndAddress => BaseAddress + Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
public ElfSymbol[] Symbols { get; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
public Image(ulong baseAddress, ulong size, ElfSymbol[] symbols)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BaseAddress = baseAddress;
|
2021-05-21 01:27:16 +02:00
|
|
|
Size = size;
|
2018-12-06 12:16:24 +01:00
|
|
|
Symbols = symbols;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private List<Image> _images;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private int _loaded;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public HleProcessDebugger(KProcess owner)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_owner = owner;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_images = new List<Image>();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
public string GetGuestStackTrace(KThread thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
EnsureLoaded();
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
var context = thread.Context;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
StringBuilder trace = new StringBuilder();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
trace.AppendLine($"Process: {_owner.Name}, PID: {_owner.Pid}");
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
void AppendTrace(ulong address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2021-05-21 01:27:16 +02:00
|
|
|
if(AnalyzePointer(out PointerInfo info, address, thread))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2021-05-21 01:27:16 +02:00
|
|
|
trace.AppendLine($" 0x{address:x16}\t{info.ImageDisplay}\t{info.SubDisplay}");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-21 01:27:16 +02:00
|
|
|
trace.AppendLine($" 0x{address:x16}");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:06:26 +01:00
|
|
|
if (context.IsAarch32)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
ulong framePointer = context.GetX(11);
|
2020-01-12 12:06:26 +01:00
|
|
|
|
|
|
|
while (framePointer != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-01-12 12:06:26 +01:00
|
|
|
if ((framePointer & 3) != 0 ||
|
|
|
|
!_owner.CpuMemory.IsMapped(framePointer) ||
|
|
|
|
!_owner.CpuMemory.IsMapped(framePointer + 4))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
AppendTrace(_owner.CpuMemory.Read<uint>(framePointer + 4));
|
2020-01-12 12:06:26 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
framePointer = _owner.CpuMemory.Read<uint>(framePointer);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2020-01-12 12:06:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
ulong framePointer = context.GetX(29);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-01-12 12:06:26 +01:00
|
|
|
while (framePointer != 0)
|
|
|
|
{
|
|
|
|
if ((framePointer & 7) != 0 ||
|
|
|
|
!_owner.CpuMemory.IsMapped(framePointer) ||
|
|
|
|
!_owner.CpuMemory.IsMapped(framePointer + 8))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
AppendTrace(_owner.CpuMemory.Read<ulong>(framePointer + 8));
|
2020-01-12 12:06:26 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
framePointer = _owner.CpuMemory.Read<ulong>(framePointer);
|
2020-01-12 12:06:26 +01:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 04:37:54 +01:00
|
|
|
return trace.ToString();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
public string GetCpuRegisterPrintout(KThread thread)
|
|
|
|
{
|
|
|
|
EnsureLoaded();
|
|
|
|
|
|
|
|
var context = thread.Context;
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
string GetReg(int x)
|
|
|
|
{
|
2022-05-31 21:29:35 +02:00
|
|
|
var v = x == 32 ? context.Pc : context.GetX(x);
|
2021-05-21 01:27:16 +02:00
|
|
|
if (!AnalyzePointer(out PointerInfo info, v, thread))
|
|
|
|
{
|
|
|
|
return $"0x{v:x16}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(info.ImageName))
|
|
|
|
{
|
|
|
|
return $"0x{v:x16} ({info.ImageDisplay})\t=> {info.SubDisplay}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $"0x{v:x16} ({info.SpDisplay})";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= 28; i++)
|
|
|
|
{
|
|
|
|
sb.AppendLine($"\tX[{i:d2}]:\t{GetReg(i)}");
|
|
|
|
}
|
|
|
|
sb.AppendLine($"\tFP:\t{GetReg(29)}");
|
|
|
|
sb.AppendLine($"\tLR:\t{GetReg(30)}");
|
|
|
|
sb.AppendLine($"\tSP:\t{GetReg(31)}");
|
|
|
|
sb.AppendLine($"\tPC:\t{GetReg(32)}");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool TryGetSubName(Image image, ulong address, out ElfSymbol symbol)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
address -= image.BaseAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int left = 0;
|
|
|
|
int right = image.Symbols.Length - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (left <= right)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int size = right - left;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int middle = left + (size >> 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
symbol = image.Symbols[middle];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-01-12 12:06:26 +01:00
|
|
|
ulong endAddr = symbol.Value + symbol.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
if (address >= symbol.Value && address < endAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
if (address < symbol.Value)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
right = middle - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
left = middle + 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
symbol = default;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PointerInfo
|
|
|
|
{
|
|
|
|
public string ImageName;
|
|
|
|
public string SubName;
|
|
|
|
|
|
|
|
public ulong Offset;
|
|
|
|
public ulong SubOffset;
|
|
|
|
|
|
|
|
public string ImageDisplay => $"{ImageName}:0x{Offset:x4}";
|
|
|
|
public string SubDisplay => SubOffset == 0 ? SubName : $"{SubName}:0x{SubOffset:x4}";
|
|
|
|
public string SpDisplay => SubOffset == 0 ? "SP" : $"SP:-0x{SubOffset:x4}";
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool AnalyzePointer(out PointerInfo info, ulong address, KThread thread)
|
|
|
|
{
|
|
|
|
if (AnalyzePointerFromImages(out info, address))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AnalyzePointerFromStack(out info, address, thread))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
private bool AnalyzePointerFromImages(out PointerInfo info, ulong address)
|
|
|
|
{
|
|
|
|
info = default;
|
|
|
|
|
|
|
|
Image image = GetImage(address, out int imageIndex);
|
|
|
|
|
|
|
|
if (image == null)
|
|
|
|
{
|
|
|
|
// Value isn't a pointer to a known image...
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Offset = address - image.BaseAddress;
|
|
|
|
|
|
|
|
// Try to find what this pointer is referring to
|
|
|
|
if (TryGetSubName(image, address, out ElfSymbol symbol))
|
|
|
|
{
|
|
|
|
info.SubName = symbol.Name;
|
|
|
|
|
|
|
|
// Demangle string if possible
|
|
|
|
if (info.SubName.StartsWith("_Z"))
|
|
|
|
{
|
|
|
|
info.SubName = Demangler.Parse(info.SubName);
|
|
|
|
}
|
|
|
|
info.SubOffset = info.Offset - symbol.Value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.SubName = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
info.ImageName = GetGuessedNsoNameFromIndex(imageIndex);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool AnalyzePointerFromStack(out PointerInfo info, ulong address, KThread thread)
|
|
|
|
{
|
|
|
|
info = default;
|
|
|
|
|
|
|
|
ulong sp = thread.Context.GetX(31);
|
|
|
|
var memoryInfo = _owner.MemoryManager.QueryMemory(address);
|
|
|
|
MemoryState memoryState = memoryInfo.State;
|
|
|
|
|
|
|
|
if (!memoryState.HasFlag(MemoryState.Stack)) // Is this pointer within the stack?
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.SubOffset = address - sp;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
private Image GetImage(ulong address, out int index)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_images)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
for (index = _images.Count - 1; index >= 0; index--)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2021-05-21 01:27:16 +02:00
|
|
|
if (address >= _images[index].BaseAddress && address < _images[index].EndAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _images[index];
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private string GetGuessedNsoNameFromIndex(int index)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((uint)index > 11)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (index == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "rtld";
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (index == 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "main";
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (index == GetImagesCount() - 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return "sdk";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return "subsdk" + (index - 2);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetImagesCount()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_images)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _images.Count;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureLoaded()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (Interlocked.CompareExchange(ref _loaded, 1, 0) == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
ScanMemoryForTextSegments();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ScanMemoryForTextSegments()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong oldAddress = 0;
|
|
|
|
ulong address = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (address >= oldAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info = _owner.MemoryManager.QueryMemory(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State == MemoryState.Reserved)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
if (info.State == MemoryState.CodeStatic && info.Permission == KMemoryPermission.ReadAndExecute)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2021-05-21 01:27:16 +02:00
|
|
|
LoadMod0Symbols(_owner.CpuMemory, info.Address, info.Size);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
oldAddress = address;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
address = info.Address + info.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:16 +02:00
|
|
|
private void LoadMod0Symbols(IVirtualMemoryManager memory, ulong textOffset, ulong textSize)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
ulong mod0Offset = textOffset + memory.Read<uint>(textOffset + 4);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
Dictionary<ElfDynamicTag, ulong> dynamic = new Dictionary<ElfDynamicTag, ulong>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
int mod0Magic = memory.Read<int>(mod0Offset + 0x0);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (mod0Magic != Mod0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
ulong dynamicOffset = memory.Read<uint>(mod0Offset + 0x4) + mod0Offset;
|
|
|
|
ulong bssStartOffset = memory.Read<uint>(mod0Offset + 0x8) + mod0Offset;
|
|
|
|
ulong bssEndOffset = memory.Read<uint>(mod0Offset + 0xc) + mod0Offset;
|
|
|
|
ulong ehHdrStartOffset = memory.Read<uint>(mod0Offset + 0x10) + mod0Offset;
|
|
|
|
ulong ehHdrEndOffset = memory.Read<uint>(mod0Offset + 0x14) + mod0Offset;
|
|
|
|
ulong modObjOffset = memory.Read<uint>(mod0Offset + 0x18) + mod0Offset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
bool isAArch32 = memory.Read<ulong>(dynamicOffset) > 0xFFFFFFFF || memory.Read<ulong>(dynamicOffset + 0x10) > 0xFFFFFFFF;
|
2020-01-12 12:06:26 +01:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
ulong tagVal;
|
|
|
|
ulong value;
|
2020-01-12 12:06:26 +01:00
|
|
|
|
|
|
|
if (isAArch32)
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
tagVal = memory.Read<uint>(dynamicOffset + 0);
|
|
|
|
value = memory.Read<uint>(dynamicOffset + 4);
|
2020-01-12 12:06:26 +01:00
|
|
|
|
|
|
|
dynamicOffset += 0x8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
tagVal = memory.Read<ulong>(dynamicOffset + 0);
|
|
|
|
value = memory.Read<ulong>(dynamicOffset + 8);
|
2020-01-12 12:06:26 +01:00
|
|
|
|
|
|
|
dynamicOffset += 0x10;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ElfDynamicTag tag = (ElfDynamicTag)tagVal;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (tag == ElfDynamicTag.DT_NULL)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
dynamic[tag] = value;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out ulong strTab) ||
|
|
|
|
!dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out ulong symTab) ||
|
|
|
|
!dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out ulong symEntSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
ulong strTblAddr = textOffset + strTab;
|
|
|
|
ulong symTblAddr = textOffset + symTab;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
List<ElfSymbol> symbols = new List<ElfSymbol>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
while (symTblAddr < strTblAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-01-12 12:06:26 +01:00
|
|
|
ElfSymbol sym = isAArch32 ? GetSymbol32(memory, symTblAddr, strTblAddr) : GetSymbol64(memory, symTblAddr, strTblAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
symbols.Add(sym);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
symTblAddr += symEntSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_images)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2021-05-21 01:27:16 +02:00
|
|
|
_images.Add(new Image(textOffset, textSize, symbols.OrderBy(x => x.Value).ToArray()));
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
private ElfSymbol GetSymbol64(IVirtualMemoryManager memory, ulong address, ulong strTblAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
ElfSymbol64 sym = memory.Read<ElfSymbol64>(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
uint nameIndex = sym.NameOffset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
string name = string.Empty;
|
2020-01-12 12:06:26 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
|
|
|
|
{
|
|
|
|
name += (char)chr;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2020-05-04 00:54:50 +02:00
|
|
|
|
|
|
|
return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
|
2020-01-12 12:06:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
private ElfSymbol GetSymbol32(IVirtualMemoryManager memory, ulong address, ulong strTblAddr)
|
2020-01-12 12:06:26 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
ElfSymbol32 sym = memory.Read<ElfSymbol32>(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
uint nameIndex = sym.NameOffset;
|
2020-01-12 12:06:26 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
string name = string.Empty;
|
2020-01-12 12:06:26 +01:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
|
|
|
|
{
|
|
|
|
name += (char)chr;
|
2020-01-12 12:06:26 +01:00
|
|
|
}
|
2020-05-04 00:54:50 +02:00
|
|
|
|
|
|
|
return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-21 01:27:16 +02:00
|
|
|
}
|