From bf4fbe7e8e71c6ca29c4665e3f15017013e79ee3 Mon Sep 17 00:00:00 2001 From: Trung <57174311+trungnt2910@users.noreply.github.com> Date: Sat, 16 Dec 2023 00:31:55 +0700 Subject: [PATCH] fix: Load dlopen and dlsym from libc first --- MemoryModule/Linux/GlibcInterop/RtldGlobal.cs | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/MemoryModule/Linux/GlibcInterop/RtldGlobal.cs b/MemoryModule/Linux/GlibcInterop/RtldGlobal.cs index 6af4065..b3ee048 100644 --- a/MemoryModule/Linux/GlibcInterop/RtldGlobal.cs +++ b/MemoryModule/Linux/GlibcInterop/RtldGlobal.cs @@ -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)]