Skip to content

Commit

Permalink
Added initial Linux support to InlineExportHelper.
Browse files Browse the repository at this point in the history
Relates to #209
  • Loading branch information
PathogenDavid committed Aug 26, 2021
1 parent e01b85c commit 2625bff
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions Biohazrd.Utilities/InlineExportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public sealed class InlineExportHelper : TransformationBase
private volatile int NextTrampolineId = 0;
private readonly CppCodeWriter Writer;

public bool __ItaniumExportMode = false;

public InlineExportHelper(OutputSession session, string filePath)
=> Writer = session.Open<CppCodeWriter>(filePath);

Expand Down Expand Up @@ -159,15 +161,21 @@ protected override TranslatedLibrary PostTransformLibrary(TranslatedLibrary libr
if (FunctionsExportedViaFunctionPointer.Count > 0)
{
// Instruct the linker to export the symbols
foreach (TranslatedFunction function in FunctionsExportedViaFunctionPointer)
{ Writer.WriteLine($"#pragma comment(linker, \"/export:{function.MangledName}\")"); }
if (!__ItaniumExportMode)
{
foreach (TranslatedFunction function in FunctionsExportedViaFunctionPointer)
{ Writer.WriteLine($"#pragma comment(linker, \"/export:{function.MangledName}\")"); }
}

Writer.EnsureSeparation();

// Add dummy reference to the inline functions so they will exist for the linker
Writer.WriteLine($"namespace {dummyNamespaceName}");
using (Writer.Block())
{
if (__ItaniumExportMode)
{ Writer.WriteLineLeftAdjusted("#pragma GCC visibility push(hidden)"); }

for (int i = 0; i < FunctionsExportedViaFunctionPointer.Count; i++)
{
TranslatedFunction function = FunctionsExportedViaFunctionPointer[i];
Expand Down Expand Up @@ -212,6 +220,9 @@ protected override TranslatedLibrary PostTransformLibrary(TranslatedLibrary libr
WriteOutNamespaceAndType(functionDecl);
Writer.WriteLine($"{functionDecl};");
}

if (__ItaniumExportMode)
{ Writer.WriteLineLeftAdjusted("#pragma GCC visibility pop"); }
}
}

Expand Down Expand Up @@ -241,6 +252,11 @@ protected override TranslatedLibrary PostTransformLibrary(TranslatedLibrary libr
Writer.WriteLine($"extern \"C\" namespace {dummyNamespaceName}");
using (Writer.Block())
{
//TODO: We should use a call strategy for Itanium instead
// See https://github.com/InfectedLibraries/Biohazrd/issues/209
if (__ItaniumExportMode)
{ Writer.WriteLineLeftAdjusted("#pragma GCC visibility push(default)"); }

foreach (TranslatedFunction function in FunctionsExportedViaTrampoline)
{
Debug.Assert(function.Declaration is FunctionDecl);
Expand All @@ -258,7 +274,8 @@ protected override TranslatedLibrary PostTransformLibrary(TranslatedLibrary libr
{ Writer.Include(function.File.FilePath); }

Writer.EnsureSeparation();
Writer.Write("__declspec(dllexport) ");
if (!__ItaniumExportMode)
{ Writer.Write("__declspec(dllexport) "); }

// Write return type
if (functionDecl is CXXConstructorDecl)
Expand Down Expand Up @@ -317,6 +334,9 @@ protected override TranslatedLibrary PostTransformLibrary(TranslatedLibrary libr

Writer.WriteLine("); }");
}

if (__ItaniumExportMode)
{ Writer.WriteLineLeftAdjusted("#pragma GCC visibility pop"); }
}
}

Expand Down

0 comments on commit 2625bff

Please sign in to comment.