-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLibraryResolver.cs
More file actions
140 lines (127 loc) · 2.9 KB
/
Copy pathLibraryResolver.cs
File metadata and controls
140 lines (127 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace BinaryNinja
{
public class LibraryResolver
{
private List<string> m_searchDirs = new List<string>();
public LibraryResolver()
{
this.AddSearchDir(
Directory.GetCurrentDirectory()
);
// Accept both names: BINARYNINJA_HOME is the historical variable, BINARYNINJA_BASE is
// the one documented in the README. Supporting both avoids a silent load failure when
// a user follows either instruction.
this.AddEnvironment(
"BINARYNINJA_HOME" ,
"BINARYNINJA_BASE"
);
}
public void AddEnvironment(params string[] environments)
{
foreach (string environment in environments)
{
string? dirname = Environment.GetEnvironmentVariable(environment);
if (!string.IsNullOrEmpty(dirname) && Directory.Exists(dirname))
{
this.AddSearchDir(dirname);
}
}
}
public void AddSearchDir(params string[] dirnames)
{
foreach (string dirname in dirnames)
{
if (!string.IsNullOrEmpty(dirname))
{
if (!this.m_searchDirs.Contains(dirname))
{
if (Directory.Exists(dirname))
{
this.m_searchDirs.Add(dirname);
}
}
}
}
}
public IntPtr ResolveDllImport(
string libraryName ,
Assembly assembly ,
DllImportSearchPath? searchPath
)
{
IntPtr handle = IntPtr.Zero;
string[] possibleNames = this.GetPossibleNames(libraryName);
foreach (string searchDir in this.m_searchDirs)
{
foreach (string possibleName in possibleNames)
{
string fullname = Path.Combine(searchDir, possibleName);
if (Path.Exists(fullname))
{
try
{
if (NativeLibrary.TryLoad(
fullname,
assembly,
searchPath,
out handle))
{
return handle;
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}
return IntPtr.Zero;
}
public string[] GetPossibleNames(string libraryName)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return new string[]
{
$"{libraryName}.dll"
};
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return new string[]
{
$"lib{libraryName}.so.1",
$"lib{libraryName}.so",
$"{libraryName}.so.1",
$"{libraryName}.so"
};
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return new string[]
{
$"lib{libraryName}.dylib",
$"{libraryName}.dylib",
$"lib{libraryName}.so.1",
$"lib{libraryName}.so",
$"{libraryName}.so.1",
$"{libraryName}.so"
};
}
else
{
return new string[]
{
libraryName
};
}
}
}
}