Skip to content

Commit

Permalink
Debugger: WS - Added ELF symbol support
Browse files Browse the repository at this point in the history
  • Loading branch information
SourMesen committed Sep 22, 2024
1 parent fa37284 commit 864a7dd
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
6 changes: 6 additions & 0 deletions UI/Config/IntegrationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public bool IsMemoryTypeImportEnabled(MemoryType memType)
case MemoryType.PceMemory:
case MemoryType.SmsMemory:
case MemoryType.GbaMemory:
case MemoryType.WsMemory:
case MemoryType.SnesVideoRam:
case MemoryType.SnesSpriteRam:
case MemoryType.SnesCgRam:
Expand All @@ -61,6 +62,8 @@ public bool IsMemoryTypeImportEnabled(MemoryType memType)
case MemoryType.GbaVideoRam:
case MemoryType.GbaSpriteRam:
case MemoryType.GbaPaletteRam:
case MemoryType.WsBootRom:
case MemoryType.WsPort:
return ImportOtherLabels;

case MemoryType.SnesPrgRom:
Expand All @@ -74,6 +77,7 @@ public bool IsMemoryTypeImportEnabled(MemoryType memType)
case MemoryType.GbBootRom:
case MemoryType.GbaPrgRom:
case MemoryType.GbaBootRom:
case MemoryType.WsPrgRom:
return ImportPrgRomLabels;

case MemoryType.SnesWorkRam:
Expand All @@ -95,6 +99,7 @@ public bool IsMemoryTypeImportEnabled(MemoryType memType)
case MemoryType.BsxPsRam:
case MemoryType.GbaIntWorkRam:
case MemoryType.GbaExtWorkRam:
case MemoryType.WsWorkRam:
return ImportWorkRamLabels;

case MemoryType.SnesSaveRam:
Expand All @@ -104,6 +109,7 @@ public bool IsMemoryTypeImportEnabled(MemoryType memType)
case MemoryType.SmsCartRam:
case MemoryType.BsxMemoryPack:
case MemoryType.GbaSaveRam:
case MemoryType.WsCartRam:
return ImportSaveRamLabels;

default:
Expand Down
107 changes: 95 additions & 12 deletions UI/Debugger/Integration/ElfImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,46 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Mesen.Debugger.Integration;

public class ElfImporter
public abstract class ElfImporter
{
public static void Import(string path, bool showResult, CpuType cpuType)
{
switch(cpuType) {
case CpuType.Ws: new ElfImporterWs().PrivateImport(path, showResult, cpuType); break;
case CpuType.Gba: new ElfImporterGba().PrivateImport(path, showResult, cpuType); break;
}
}

protected abstract bool TryGetSymbolInfo(SymbolEntry<uint> symbol, int romSize, [MaybeNullWhen(false)] out ElfSymbolInfo symbolInfo);

private void PrivateImport(string path, bool showResult, CpuType cpuType)
{
try {
MemoryType memType = cpuType.ToMemoryType();

Dictionary<AddressInfo, CodeLabel> labels = new();
HashSet<string> usedLabels = new();
IELF elf = ELFReader.Load(path);
int romSize = DebugApi.GetMemorySize(cpuType.GetPrgRomMemoryType());

if(elf.TryGetSection(".symtab", out ISection section)) {
if(section is ISymbolTable symbols) {
foreach(SymbolEntry<uint> symbol in symbols.Entries) {
if(!string.IsNullOrWhiteSpace(symbol.Name) && symbol.Type != SymbolType.File && symbol.Type != SymbolType.Section && symbol.Type != SymbolType.Object && symbol.PointedSection != null) {
uint value = symbol.Value & ~(uint)0x01;
AddressInfo relAddr = new AddressInfo() { Address = (int)value, Type = memType };
AddressInfo absAddr = DebugApi.GetAbsoluteAddress(relAddr);
AddressInfo labelAddr = absAddr.Type != MemoryType.None ? absAddr : relAddr;
if(!string.IsNullOrWhiteSpace(symbol.Name) && symbol.Type != SymbolType.File && symbol.Type != SymbolType.Section && symbol.PointedSection != null) {
if(!TryGetSymbolInfo(symbol, romSize, out ElfSymbolInfo? symbolInfo)) {
continue;
}

string name = symbolInfo.Name;
AddressInfo labelAddr = symbolInfo.Address;

if(!ConfigManager.Config.Debug.Integration.IsMemoryTypeImportEnabled(labelAddr.Type)) {
continue;
Expand All @@ -42,12 +57,6 @@ public static void Import(string path, bool showResult, CpuType cpuType)
continue;
}

string name = symbol.Name switch {
"$a" => "arm_" + relAddr.Address.ToString("X7"),
"$d" => "data_" + relAddr.Address.ToString("X7"),
"$t" => "thumb_" + relAddr.Address.ToString("X7"),
_ => symbol.Name
};

//Demangle and replace any invalid characters with underscores
name = LabelManager.InvalidLabelRegex.Replace(Demangle(name), "_");
Expand Down Expand Up @@ -134,3 +143,77 @@ private static string Demangle(string name)
return name;
}
}

public class ElfSymbolInfo
{
public AddressInfo Address;
public string Name = "";
}

public class ElfImporterWs : ElfImporter
{
protected override bool TryGetSymbolInfo(SymbolEntry<uint> symbol, int romSize, [MaybeNullWhen(false)] out ElfSymbolInfo symbolInfo)
{
symbolInfo = null;

if(symbol.Name.EndsWith("$") || symbol.Name.EndsWith("&") || symbol.Name.EndsWith("!")) {
return false;
}

AddressInfo absAddr = new();
uint addr = symbol.PointedSection.LoadAddress;
if((addr & 0x80000000) != 0) {
absAddr.Address = (int)((addr & 0xFFFF) | ((addr & 0x7FF00000) >> 4)) & (romSize - 1);
absAddr.Type = MemoryType.WsPrgRom;
} else if(addr <= 0xFFFF) {
absAddr.Address = (int)addr;
absAddr.Type = MemoryType.WsWorkRam;
} else if((addr & 0xF0000) == 0x10000) {
absAddr.Address = (int)((addr & 0xFFFF) | ((addr & 0xF00000) >> 4));
absAddr.Type = MemoryType.WsCartRam;
}

symbolInfo = new() {
Address = absAddr,
Name = symbol.Name
};

return true;
}
}

public class ElfImporterGba : ElfImporter
{
protected override bool TryGetSymbolInfo(SymbolEntry<uint> symbol, int romSize, [MaybeNullWhen(false)] out ElfSymbolInfo symbolInfo)
{
symbolInfo = null;

if(symbol.Type == SymbolType.Object) {
return false;
}

uint value = symbol.Value & ~(uint)0x01;
AddressInfo relAddr = new AddressInfo() { Address = (int)value, Type = MemoryType.GbaMemory };
AddressInfo absAddr = DebugApi.GetAbsoluteAddress(relAddr);

if(absAddr.Type == MemoryType.None) {
return false;
}

AddressInfo labelAddr = absAddr;

string name = symbol.Name switch {
"$a" => "arm_" + relAddr.Address.ToString("X7"),
"$d" => "data_" + relAddr.Address.ToString("X7"),
"$t" => "thumb_" + relAddr.Address.ToString("X7"),
_ => symbol.Name
};

symbolInfo = new() {
Name = name,
Address = absAddr
};

return true;
}
}

0 comments on commit 864a7dd

Please sign in to comment.