Skip to content

Commit

Permalink
Fix the demangler, map parsing and update mapdas!
Browse files Browse the repository at this point in the history
  • Loading branch information
intns committed Dec 9, 2021
1 parent f079c59 commit 906073b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 19 deletions.
10 changes: 5 additions & 5 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void MapTree_Click(object sender, EventArgs ea)
// OutputPath is the path where we dump everything
private void ExportFile(MAP file, string targetPath, string outputPath)
{
DebugLog.Text += $"Writing to {outputPath}... ";
DebugLog.Text += $"Writing to {outputPath}...\n";
foreach (MAPParsing.TextEntry node in file._TextSymbols.FindAll(x => x._Path == targetPath))
{
string function = node._SymbolDemangled;
Expand Down Expand Up @@ -182,10 +182,10 @@ private void ExportFilesystem_Click(object o, EventArgs e)
{
if (!Directory.Exists(dialog.FileName + "/" + Path.GetDirectoryName(node.Text)))
{
Directory.CreateDirectory(dialog.FileName + "/" + Path.GetDirectoryName(node.Text));
Directory.CreateDirectory(dialog.FileName + "/" + Path.GetDirectoryName(node.Text.Replace(":", "")));
}

ExportFile(_FilesOpen[_SelectedFile], node.Text, dialog.FileName + "/" + node.Text);
ExportFile(_FilesOpen[_SelectedFile], node.Text, dialog.FileName + "/" + node.Text.Replace(":", ""));
DebugLog.SelectionStart = DebugLog.Text.Length;
DebugLog.ScrollToCaret();

Expand Down Expand Up @@ -760,7 +760,9 @@ private void FileToolStripMenuItem_Click(object sender, EventArgs e)
}
}

DebugLog.Text += "Opening Map File... ";
MAP newFile = new MAP(File.ReadAllLines(dialog.FileName));

_FilesOpen.Add(newFile);

/* Populate tree with text data
Expand All @@ -773,7 +775,6 @@ private void FileToolStripMenuItem_Click(object sender, EventArgs e)
* - (PARAMETERS)
*/

DebugLog.Text += "Opening Map File... ";
Update();
MapTree.BeginUpdate();

Expand Down Expand Up @@ -836,7 +837,6 @@ private void FileToolStripMenuItem_Click(object sender, EventArgs e)

MapTree.Nodes.Add(parent);
MapTree.EndUpdate();

DebugLog.Text += "Done!\n";

_SelectedFile = _FilesOpen.Count;
Expand Down
12 changes: 11 additions & 1 deletion Helpers/Demangler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ static void DemangleTemplate(StringStream input, StringBuilder output)
output.Append(", ");
break;
}
case '\0':
{
end = true;
break;
}
}
} while (!end);

Expand Down Expand Up @@ -190,6 +195,11 @@ static void DemangleType(StringStream input, StringBuilder output)
{
c = input.Read();

if (c == '\0')
{
end = true;
}

switch (c)
{
case 'C':
Expand Down Expand Up @@ -315,7 +325,7 @@ static void DemangleType(StringStream input, StringBuilder output)
StringBuilder prms = new StringBuilder(500);
StringBuilder ret = new StringBuilder(500);

while (input.Peek() != '_')
for (char nc = input.Peek(); nc != '_' && nc != '\0'; nc = input.Peek())
{
if (prms.Length > 0)
{
Expand Down
82 changes: 69 additions & 13 deletions Helpers/MAPParsing.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using arookas;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

namespace mapdas
Expand Down Expand Up @@ -42,6 +44,8 @@ public static List<TextEntry> ParseTextSection(MAP file)
return null;
}

bool updatedMap = segmentLines[0].Contains("File");

List<TextEntry> symbols = new List<TextEntry>();
foreach (string line in segmentLines)
{
Expand All @@ -54,23 +58,66 @@ public static List<TextEntry> ParseTextSection(MAP file)
// st = start
// si = size
// addr = address
// offs = offset
// t = type
// fun_n = function name
// fol_n = folder name
// fil_n = file name

// folder & used
Match fu_sym = Regex.Match(line, @"\s(?<st>[a-f0-9]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[a-f0-9]+)\s+(?<t>\d+)\s+(?<fun_n>[^ ]+)\s+(?<fol_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
// unused & folder
Match uf_sym = Regex.Match(line, @"\s(?<st>[A-Z]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[.]+)\s+(?<fun_n>[^ ]+)\s+(?<fol_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
// unused & no folder
Match unf_sym = Regex.Match(line, @"\s(?<st>[A-Z]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[.]+)\s+(?<fun_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
if (!fu_sym.Success && !uf_sym.Success && !unf_sym.Success)
Match symbol = null;
if (!updatedMap)
{
continue;
// folder & used
Match fu_sym = Regex.Match(line, @"\s(?<st>[a-f0-9]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[a-f0-9]+)\s+(?<t>\d+)\s+(?<fun_n>[^ ]+)\s+(?<fol_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
// unused & folder
Match uf_sym = Regex.Match(line, @"\s(?<st>[A-Z]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[.]+)\s+(?<fun_n>[^ ]+)\s+(?<fol_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
// unused & no folder
Match unf_sym = Regex.Match(line, @"\s(?<st>[A-Z]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[.]+)\s+(?<fun_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
if (!fu_sym.Success && !uf_sym.Success && !unf_sym.Success)
{
continue;
}

if (fu_sym.Success)
{
symbol = fu_sym;
}
else if (uf_sym.Success)
{
symbol = uf_sym;
}
else if (unf_sym.Success)
{
symbol = unf_sym;
}
}
else // Updated map, thanks wowjinxy for showing me these
{
// used & folder
Match fu_sym = Regex.Match(line, @"\s(?<st>[a-f0-9]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[a-f0-9]+)\s+(?<offs>[a-f0-9]+)\s+(?<t>\d+)\s+(?<fun_n>[^ ]+)\s+(?<fol_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
// unused & folder
Match uf_sym = Regex.Match(line, @"\s(?<st>[A-Z]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[.]+)\s+(?<offs>[a-f0-9]+)\s+(?<fun_n>[^ ]+)\s+(?<fol_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
// unused & no folder
Match unf_sym = Regex.Match(line, @"\s(?<st>[A-Z]+)\s+(?<si>[a-f0-9]+)\s+(?<addr>[.]+)\s+(?<offs>[a-f0-9]+)\s+(?<fun_n>[^ ]+)?\s(?<fil_n>[^ ]+)", RegexOptions.Compiled);
if (!fu_sym.Success && !uf_sym.Success && !unf_sym.Success)
{
continue;
}

if (fu_sym.Success)
{
symbol = fu_sym;
}
else if (uf_sym.Success)
{
symbol = uf_sym;
}
else if (unf_sym.Success)
{
symbol = unf_sym;
}
}

Match symbol = fu_sym.Success ? fu_sym : uf_sym.Success ? uf_sym : unf_sym;
TextEntry newTextSymbol = new TextEntry
{
_Address = symbol.Groups["addr"].Value,
Expand All @@ -81,12 +128,21 @@ public static List<TextEntry> ParseTextSection(MAP file)
_SymbolDemangled = Demangler.Demangle(symbol.Groups["fun_n"].Value),
};

string folder = symbol.Groups["fol_n"].Value.Replace(".o", "").Replace(".a", "").Replace('.', '/');
if (!folder.EndsWith("/"))
// If the .o file uses an absolute path we just recreate the entire directory
// because fuck it.
if (!symbol.Groups["fil_n"].Value.Contains(":\\"))
{
string folder = symbol.Groups["fol_n"].Value.Replace(".o", "").Replace(".a", "").Replace('.', '/');
if (!folder.EndsWith("/"))
{
folder += "/";
}
newTextSymbol._Path = folder + symbol.Groups["fil_n"].Value.Replace(".o", ".cpp");
}
else
{
folder += "/";
newTextSymbol._Path = symbol.Groups["fil_n"].Value.Replace(".o", ".cpp");
}
newTextSymbol._Path = folder + symbol.Groups["fil_n"].Value.Replace(".o", ".cpp");

symbols.Add(newTextSymbol);
}
Expand Down

0 comments on commit 906073b

Please sign in to comment.