Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2304dc

Browse files
authored
Merge pull request#1425 from losttech/bugs/libdl.so.2
Try libdl.so.2 on Linux before libdl.so
2 parents1e5338f +483f192 commita2304dc

File tree

2 files changed

+121
-92
lines changed

2 files changed

+121
-92
lines changed

‎src/runtime/platform/LibDL.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#pragma warning disableIDE1006// Naming Styles (interface for native functions)
2+
usingSystem;
3+
usingSystem.Runtime.InteropServices;
4+
5+
namespacePython.Runtime.Platform
6+
{
7+
interfaceILibDL
8+
{
9+
IntPtrdlopen(stringfileName,intflags);
10+
IntPtrdlsym(IntPtrhandle,stringsymbol);
11+
intdlclose(IntPtrhandle);
12+
IntPtrdlerror();
13+
14+
intRTLD_NOW{get;}
15+
intRTLD_GLOBAL{get;}
16+
IntPtrRTLD_DEFAULT{get;}
17+
}
18+
19+
classLinuxLibDL:ILibDL
20+
{
21+
privateconststringNativeDll="libdl.so";
22+
23+
publicintRTLD_NOW=>0x2;
24+
publicintRTLD_GLOBAL=>0x100;
25+
publicIntPtrRTLD_DEFAULT=>IntPtr.Zero;
26+
27+
publicstaticILibDLGetInstance()
28+
{
29+
try
30+
{
31+
ILibDLlibdl2=newLinuxLibDL2();
32+
// call dlerror to ensure library is resolved
33+
libdl2.dlerror();
34+
returnlibdl2;
35+
}catch(DllNotFoundException)
36+
{
37+
returnnewLinuxLibDL();
38+
}
39+
}
40+
41+
IntPtrILibDL.dlopen(stringfileName,intflags)=>dlopen(fileName,flags);
42+
IntPtrILibDL.dlsym(IntPtrhandle,stringsymbol)=>dlsym(handle,symbol);
43+
intILibDL.dlclose(IntPtrhandle)=>dlclose(handle);
44+
IntPtrILibDL.dlerror()=>dlerror();
45+
46+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
47+
privatestaticexternIntPtrdlopen(stringfileName,intflags);
48+
49+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
50+
privatestaticexternIntPtrdlsym(IntPtrhandle,stringsymbol);
51+
52+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
53+
privatestaticexternintdlclose(IntPtrhandle);
54+
55+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
56+
privatestaticexternIntPtrdlerror();
57+
}
58+
59+
classLinuxLibDL2:ILibDL
60+
{
61+
privateconststringNativeDll="libdl.so.2";
62+
63+
publicintRTLD_NOW=>0x2;
64+
publicintRTLD_GLOBAL=>0x100;
65+
publicIntPtrRTLD_DEFAULT=>IntPtr.Zero;
66+
67+
IntPtrILibDL.dlopen(stringfileName,intflags)=>dlopen(fileName,flags);
68+
IntPtrILibDL.dlsym(IntPtrhandle,stringsymbol)=>dlsym(handle,symbol);
69+
intILibDL.dlclose(IntPtrhandle)=>dlclose(handle);
70+
IntPtrILibDL.dlerror()=>dlerror();
71+
72+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
73+
privatestaticexternIntPtrdlopen(stringfileName,intflags);
74+
75+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
76+
privatestaticexternIntPtrdlsym(IntPtrhandle,stringsymbol);
77+
78+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
79+
privatestaticexternintdlclose(IntPtrhandle);
80+
81+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
82+
privatestaticexternIntPtrdlerror();
83+
}
84+
85+
classMacLibDL:ILibDL
86+
{
87+
publicintRTLD_NOW=>0x2;
88+
publicintRTLD_GLOBAL=>0x8;
89+
conststringNativeDll="/usr/lib/libSystem.dylib";
90+
publicIntPtrRTLD_DEFAULT=>new(-2);
91+
92+
IntPtrILibDL.dlopen(stringfileName,intflags)=>dlopen(fileName,flags);
93+
IntPtrILibDL.dlsym(IntPtrhandle,stringsymbol)=>dlsym(handle,symbol);
94+
intILibDL.dlclose(IntPtrhandle)=>dlclose(handle);
95+
IntPtrILibDL.dlerror()=>dlerror();
96+
97+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
98+
privatestaticexternIntPtrdlopen(stringfileName,intflags);
99+
100+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
101+
privatestaticexternIntPtrdlsym(IntPtrhandle,stringsymbol);
102+
103+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
104+
privatestaticexternintdlclose(IntPtrhandle);
105+
106+
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
107+
privatestaticexternIntPtrdlerror();
108+
}
109+
}

‎src/runtime/platform/LibraryLoader.cs

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public static ILibraryLoader Instance
2828
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2929
_instance=newWindowsLoader();
3030
elseif(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
31-
_instance=newLinuxLoader();
31+
_instance=newPosixLoader(LinuxLibDL.GetInstance());
3232
elseif(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
33-
_instance=newDarwinLoader();
33+
_instance=newPosixLoader(newMacLibDL());
3434
else
3535
thrownewPlatformNotSupportedException(
3636
"This operating system is not supported"
@@ -42,87 +42,19 @@ public static ILibraryLoader Instance
4242
}
4343
}
4444

45-
classLinuxLoader:ILibraryLoader
45+
classPosixLoader:ILibraryLoader
4646
{
47-
privatestaticintRTLD_NOW=0x2;
48-
privatestaticintRTLD_GLOBAL=0x100;
49-
privatestaticIntPtrRTLD_DEFAULT=IntPtr.Zero;
50-
privateconststringNativeDll="libdl.so";
47+
privatereadonlyILibDLlibDL;
5148

52-
publicIntPtrLoad(stringdllToLoad)
53-
{
54-
ClearError();
55-
varres=dlopen(dllToLoad,RTLD_NOW|RTLD_GLOBAL);
56-
if(res==IntPtr.Zero)
57-
{
58-
varerr=GetError();
59-
thrownewDllNotFoundException($"Could not load{dllToLoad} with flags RTLD_NOW | RTLD_GLOBAL:{err}");
60-
}
61-
62-
returnres;
63-
}
64-
65-
publicvoidFree(IntPtrhandle)
66-
{
67-
dlclose(handle);
68-
}
69-
70-
publicIntPtrGetFunction(IntPtrdllHandle,stringname)
71-
{
72-
// look in the exe if dllHandle is NULL
73-
if(dllHandle==IntPtr.Zero)
74-
{
75-
dllHandle=RTLD_DEFAULT;
76-
}
77-
78-
ClearError();
79-
IntPtrres=dlsym(dllHandle,name);
80-
if(res==IntPtr.Zero)
81-
{
82-
varerr=GetError();
83-
thrownewMissingMethodException($"Failed to load symbol{name}:{err}");
84-
}
85-
returnres;
86-
}
87-
88-
voidClearError()
49+
publicPosixLoader(ILibDLlibDL)
8950
{
90-
dlerror();
91-
}
92-
93-
stringGetError()
94-
{
95-
varres=dlerror();
96-
if(res!=IntPtr.Zero)
97-
returnMarshal.PtrToStringAnsi(res);
98-
else
99-
returnnull;
51+
this.libDL=libDL??thrownewArgumentNullException(nameof(libDL));
10052
}
10153

102-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
103-
publicstaticexternIntPtrdlopen(stringfileName,intflags);
104-
105-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
106-
privatestaticexternIntPtrdlsym(IntPtrhandle,stringsymbol);
107-
108-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
109-
privatestaticexternintdlclose(IntPtrhandle);
110-
111-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
112-
privatestaticexternIntPtrdlerror();
113-
}
114-
115-
classDarwinLoader:ILibraryLoader
116-
{
117-
privatestaticintRTLD_NOW=0x2;
118-
privatestaticintRTLD_GLOBAL=0x8;
119-
privateconststringNativeDll="/usr/lib/libSystem.dylib";
120-
privatestaticIntPtrRTLD_DEFAULT=newIntPtr(-2);
121-
12254
publicIntPtrLoad(stringdllToLoad)
12355
{
12456
ClearError();
125-
varres=dlopen(dllToLoad,RTLD_NOW|RTLD_GLOBAL);
57+
varres=libDL.dlopen(dllToLoad,libDL.RTLD_NOW|libDL.RTLD_GLOBAL);
12658
if(res==IntPtr.Zero)
12759
{
12860
varerr=GetError();
@@ -134,19 +66,19 @@ public IntPtr Load(string dllToLoad)
13466

13567
publicvoidFree(IntPtrhandle)
13668
{
137-
dlclose(handle);
69+
libDL.dlclose(handle);
13870
}
13971

14072
publicIntPtrGetFunction(IntPtrdllHandle,stringname)
14173
{
14274
// look in the exe if dllHandle is NULL
14375
if(dllHandle==IntPtr.Zero)
14476
{
145-
dllHandle=RTLD_DEFAULT;
77+
dllHandle=libDL.RTLD_DEFAULT;
14678
}
14779

14880
ClearError();
149-
IntPtrres=dlsym(dllHandle,name);
81+
IntPtrres=libDL.dlsym(dllHandle,name);
15082
if(res==IntPtr.Zero)
15183
{
15284
varerr=GetError();
@@ -157,29 +89,17 @@ public IntPtr GetFunction(IntPtr dllHandle, string name)
15789

15890
voidClearError()
15991
{
160-
dlerror();
92+
libDL.dlerror();
16193
}
16294

16395
stringGetError()
16496
{
165-
varres=dlerror();
97+
varres=libDL.dlerror();
16698
if(res!=IntPtr.Zero)
16799
returnMarshal.PtrToStringAnsi(res);
168100
else
169101
returnnull;
170102
}
171-
172-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
173-
publicstaticexternIntPtrdlopen(StringfileName,intflags);
174-
175-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
176-
privatestaticexternIntPtrdlsym(IntPtrhandle,Stringsymbol);
177-
178-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
179-
privatestaticexternintdlclose(IntPtrhandle);
180-
181-
[DllImport(NativeDll,CallingConvention=CallingConvention.Cdecl)]
182-
privatestaticexternIntPtrdlerror();
183103
}
184104

185105
classWindowsLoader:ILibraryLoader

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp