Browse Source

Always use the mangled names as reported by Clang

The names in the binaries themselves are sometimes different than those Clang reports. However, it seems that the reported ones are the actually correct for invocation. For example, the name passed to dlsym() (for variables) must not be prepended with an underscore (https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html). Another example, again on macOS, is with extern C functions which sometimes also receive an _ at the front.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1480/head
Dimitar Dobrev 6 years ago
parent
commit
341fadc655
  1. 49
      src/AST/SymbolContext.cs
  2. 7
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 8
      src/Generator/Passes/FindSymbolsPass.cs
  4. 3
      src/Generator/Passes/GenerateSymbolsPass.cs

49
src/AST/SymbolContext.cs

@ -92,48 +92,11 @@ namespace CppSharp.AST
} }
} }
public bool FindSymbol(ref string symbol) public bool FindLibraryBySymbol(string symbol, out NativeLibrary library) =>
{ Symbols.TryGetValue(symbol, out library) ||
if (FindLibraryBySymbol(symbol, out _)) Symbols.TryGetValue("_" + symbol, out library) ||
return true; Symbols.TryGetValue(symbol.TrimStart('_'), out library) ||
Symbols.TryGetValue("_imp_" + symbol, out library) ||
string alternativeSymbol; Symbols.TryGetValue("__imp_" + symbol, out library);
// Check for C symbols with a leading underscore.
alternativeSymbol = "_" + symbol;
if (FindLibraryBySymbol(alternativeSymbol, out _))
{
symbol = alternativeSymbol;
return true;
}
alternativeSymbol = symbol.TrimStart('_');
if (FindLibraryBySymbol(alternativeSymbol, out _))
{
symbol = alternativeSymbol;
return true;
}
alternativeSymbol = "_imp_" + symbol;
if (FindLibraryBySymbol(alternativeSymbol, out _))
{
symbol = alternativeSymbol;
return true;
}
alternativeSymbol = "__imp_" + symbol;
if (FindLibraryBySymbol("__imp_" + symbol, out _))
{
symbol = alternativeSymbol;
return true;
}
return false;
}
public bool FindLibraryBySymbol(string symbol, out NativeLibrary library)
{
return Symbols.TryGetValue(symbol, out library);
}
} }
} }

7
src/Generator/Generators/CSharp/CSharpSources.cs

@ -3395,12 +3395,6 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
NativeLibrary library; NativeLibrary library;
Context.Symbols.FindLibraryBySymbol(((IMangledDecl) declaration).Mangled, out library); Context.Symbols.FindLibraryBySymbol(((IMangledDecl) declaration).Mangled, out library);
var targetTriple = Context.ParserOptions.TargetTriple;
if (library == null && targetTriple.IsMacOS())
// the symbol name passed to dlsym() must NOT be prepended with an underscore
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html
Context.Symbols.FindLibraryBySymbol('_' + ((IMangledDecl) declaration).Mangled, out library);
if (library != null) if (library != null)
libName = Path.GetFileNameWithoutExtension(library.FileName); libName = Path.GetFileNameWithoutExtension(library.FileName);
@ -3411,6 +3405,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
if (libName == null) if (libName == null)
libName = declaration.TranslationUnit.Module.SharedLibraryName; libName = declaration.TranslationUnit.Module.SharedLibraryName;
var targetTriple = Context.ParserOptions.TargetTriple;
if (Options.GenerateInternalImports) if (Options.GenerateInternalImports)
libName = "__Internal"; libName = "__Internal";
else if (targetTriple.IsWindows() && else if (targetTriple.IsWindows() &&

8
src/Generator/Passes/FindSymbolsPass.cs

@ -93,15 +93,11 @@ namespace CppSharp.Passes
private bool VisitMangledDeclaration(IMangledDecl mangledDecl) private bool VisitMangledDeclaration(IMangledDecl mangledDecl)
{ {
var symbol = mangledDecl.Mangled; if (!Context.Symbols.FindLibraryBySymbol(mangledDecl.Mangled, out _))
if (!Context.Symbols.FindSymbol(ref symbol))
{ {
Diagnostics.Warning("Symbol not found: {0}", symbol); Diagnostics.Warning("Symbol not found: {0}", mangledDecl.Mangled);
return false; return false;
} }
mangledDecl.Mangled = symbol;
return true; return true;
} }

3
src/Generator/Passes/GenerateSymbolsPass.cs

@ -114,7 +114,6 @@ namespace CppSharp.Passes
private bool NeedsSymbol(Function function) private bool NeedsSymbol(Function function)
{ {
var mangled = function.Mangled;
var method = function as Method; var method = function as Method;
bool isInImplicitSpecialization; bool isInImplicitSpecialization;
var declarationContext = function.Namespace; var declarationContext = function.Namespace;
@ -136,7 +135,7 @@ namespace CppSharp.Passes
(!method.IsConstructor || !((Class) method.Namespace).IsAbstract))) && (!method.IsConstructor || !((Class) method.Namespace).IsAbstract))) &&
// we cannot handle nested anonymous types // we cannot handle nested anonymous types
(!(function.Namespace is Class) || !string.IsNullOrEmpty(function.Namespace.OriginalName)) && (!(function.Namespace is Class) || !string.IsNullOrEmpty(function.Namespace.OriginalName)) &&
!Context.Symbols.FindSymbol(ref mangled); !Context.Symbols.FindLibraryBySymbol(function.Mangled, out _);
} }
private SymbolsCodeGenerator GetSymbolsCodeGenerator(Module module) private SymbolsCodeGenerator GetSymbolsCodeGenerator(Module module)

Loading…
Cancel
Save