From 7f5e6c8401a726ea4e01c2a545117fa198665ece Mon Sep 17 00:00:00 2001 From: itsmeft24 <57544858+itsmeft24@users.noreply.github.com> Date: Mon, 18 Jul 2022 22:40:28 -0400 Subject: [PATCH] Fixes an error where symbols with two adjacent Js would not decompress, handles symbols ending with Z1ZXZ2Z template arguments, and adds anonymous namespace parsing + strips away ghs_thunk before demangling. --- Program.cs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Program.cs b/Program.cs index 40712ce..9c8f4e6 100644 --- a/Program.cs +++ b/Program.cs @@ -183,6 +183,10 @@ static void DemangleAll(StreamReader r, string fileName) /* e.g. "h__Fi" => "h(int)" */ static string Demangle(string name) { + if (name.Contains("__ghs_thunk__")) { + name = name.Substring(25); + } + name = Decompress(name); /* This demangle method has basically turned into a hand-written @@ -295,7 +299,13 @@ static string Decompress(string name) int end = name.IndexOf('J', start + 1); - if (end != -1) + /* if two Js are right next to each other, then just append one J and move forward 2 characters. */ + if (end == start + 1) + { + result += "J"; + index = start + 2; + } + else if (end != -1) { bool valid = true; @@ -457,7 +467,7 @@ private static string ReadTemplateArguments(string name, out string remainder) if (args.Count > 0) result += ", "; - string type, val; + string type, val = ""; if (remainder.StartsWith("X", StringComparison.Ordinal)) { @@ -476,7 +486,7 @@ private static string ReadTemplateArguments(string name, out string remainder) else { /* */ - type = ReadType(args, remainder, out remainder).Replace("#", " #"); + type = ReadType(args, remainder, out remainder); if (remainder.StartsWith("L", StringComparison.Ordinal)) { @@ -494,12 +504,16 @@ private static string ReadTemplateArguments(string name, out string remainder) if (!remainder.StartsWith("_")) throw new InvalidDataException("Unexpected character after template parameter length \"" + remainder[0] + "\". Expected \"_\"."); + type = type.Replace("#", " #"); remainder = remainder.Substring(1); val = remainder.Substring(0, len); remainder = remainder.Substring(len); } else - throw new InvalidDataException("Unknown template parameter encoding: \"" + remainder[0] + "\"."); + { + val = type.Replace("#", ""); + type = "class #"; + } } } else @@ -697,6 +711,16 @@ static string ReadString(string name, out string remainder) if (len == 0 || name.Length < len) throw new InvalidDataException("Bad string length \"" + len.ToString(CultureInfo.InvariantCulture) + "\"."); + /* if the mangled string starts with __N_, and the number following is equal to the length of the string -9, then it refers to an anonymous namespace.*/ + if (name.Length > 4) + { + if (name.Substring(0, 4) == "__N_") + { + remainder = name.Substring(len); + return ""; + } + } + remainder = name.Substring(len); return DemangleTemplate(name.Substring(0, len)); }