Skip to content

Instantly share code, notes, and snippets.

@lemonmojo
Created February 15, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemonmojo/41eb516de317dbb63ec05e4b09a9ed01 to your computer and use it in GitHub Desktop.
Save lemonmojo/41eb516de317dbb63ec05e4b09a9ed01 to your computer and use it in GitHub Desktop.
// Here's my AOT Utils helper class
public static class AOTUtils
{
private const string LIBRARY_INTERNAL = "__Internal";
private const string LIBRARY_SYSTEM = "/usr/lib/libSystem.dylib";
private enum RTLD
{
RTLD_LAZY = 0x1,
RTLD_NOW = 0x2,
RTLD_LOCAL = 0x4,
RTLD_GLOBAL = 0x8,
RTLD_NOLOAD = 0x10,
RTLD_NODELETE = 0x80,
RTLD_FIRST = 0x100
}
[DllImport (LIBRARY_SYSTEM)]
private static extern int dlclose (IntPtr handle);
[DllImport (LIBRARY_SYSTEM)]
private static extern IntPtr dlerror ();
[DllImport (LIBRARY_SYSTEM)]
private static extern IntPtr dlopen (string path, int mode);
[DllImport (LIBRARY_SYSTEM)]
private static extern IntPtr dlsym (IntPtr handle, string symbol);
[DllImport (LIBRARY_INTERNAL)]
private static extern void mono_aot_register_module (IntPtr aot_info);
public static IntPtr GetAOTInfo (string dylibPath)
{
IntPtr dylib = dlopen (dylibPath, (int)RTLD.RTLD_NOW);
IntPtr dylibErr = dlerror ();
if (dylib == IntPtr.Zero ||
dylibErr != IntPtr.Zero) {
if (dylib != IntPtr.Zero) {
dlclose (dylib);
}
throw new Exception ("Failed to load dylib");
}
IntPtr aotInfo = dlsym (dylib, "mono_aot_file_info");
if (aotInfo == IntPtr.Zero) {
dlclose (dylib);
throw new Exception ("Failed to load AOT info");
}
//dlclose (dylib);
return aotInfo;
}
public static void RegisterAOTInfo (IntPtr aotInfo)
{
mono_aot_register_module (aotInfo);
}
}
// And here's how I call it
Assembly LoadPlugin (string dll)
{
byte [] dllBytes = File.ReadAllBytes (dll);
Assembly pluginAssembly = Assembly.Load (dllBytes);
string dylib = dll.Replace (".dll", ".dll.dylib");
if (File.Exists (dylib)) {
IntPtr aotInfo = AOTUtils.GetAOTInfo (dylib);
if (aotInfo != IntPtr.Zero) {
AOTUtils.RegisterAOTInfo (aotInfo);
}
}
return pluginAssembly;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment