Skip to content

Commit

Permalink
fix: Load dlopen and dlsym from libc first
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnt2910 committed Dec 15, 2023
1 parent efd1742 commit bf4fbe7
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions MemoryModule/Linux/GlibcInterop/RtldGlobal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,39 @@ private RtldGlobal(byte* memory)

private static RtldGlobal GetInstance()
{
var libdl = dlopen("libdl.so");
var ptr = dlsym(libdl, "_rtld_global");
IntPtr libdl;
IntPtr ptr;

try
{
// Recent glibc updates have removed libdl.so from glibc.
// https://unix.stackexchange.com/questions/700097/unable-to-load-shared-library-libdl-so-or-one-of-its-dependencies
libdl = dlopen("libc.so");
ptr = dlsym(libdl, "_rtld_global");
}
catch (DllNotFoundException)
{
libdl = dlopen_dl("libdl.so");
ptr = dlsym_dl(libdl, "_rtld_global");
}

return new RtldGlobal((byte*)ptr);
}

[DllImport("dl")]
[DllImport("libc")]
private static extern IntPtr dlopen([MarshalAs(UnmanagedType.LPStr)] string name, int mode = 0x01);

[DllImport("dl")]
[DllImport("libc")]
private static extern IntPtr dlsym(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name);

[DllImport("libc")]
private static extern void _dl_get_tls_static_info(out UIntPtr size, out UIntPtr align);

[DllImport("dl")]
private static extern IntPtr dlopen_dl([MarshalAs(UnmanagedType.LPStr)] string name, int mode = 0x01);

[DllImport("dl")]
private static extern IntPtr dlsym_dl(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string name);
}

//[StructLayout(LayoutKind.Sequential)]
Expand Down

0 comments on commit bf4fbe7

Please sign in to comment.