-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathExecSymbolLookup.java
More file actions
76 lines (65 loc) · 2.31 KB
/
Copy pathExecSymbolLookup.java
File metadata and controls
76 lines (65 loc) · 2.31 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
package javaforce.ffm;
import java.util.*;
import java.lang.foreign.*;
import java.lang.invoke.*;
import static java.lang.foreign.ValueLayout.*;
import javaforce.*;
/** Executable Symbol Lookup.
*
* Find symbols in the main executable.
*
* @author pquiring
*/
public class ExecSymbolLookup implements SymbolLookup {
private MemorySegment handle; //executable handle
private MethodHandle getsymbol; //GetProcAddress() or dlsym()
private Arena arena;
private static final int RTLD_LAZY = 1;
private static final int RTLD_NOW = 2;
private static final int RTLD_GLOBAL = 0x100;
private static boolean debug = false;
public boolean init() {
if (debug) JFLog.log("ExecSymbolLookup init");
try {
arena = Arena.global();
if (JF.isWindows()) {
SymbolLookup kernel32 = SymbolLookup.libraryLookup("kernel32", arena);
FFM.setSymbolLookup(kernel32);
MethodHandle GetModuleHandle = FFM.getFunction("GetModuleHandleA", FFM.getFunctionDesciptor(ADDRESS, ADDRESS));
getsymbol = FFM.getFunction("GetProcAddress", FFM.getFunctionDesciptor(ADDRESS, ADDRESS, ADDRESS));
try {
handle = (MemorySegment)GetModuleHandle.invokeExact(MemorySegment.NULL);
} catch (Throwable t) {
JFLog.log(t);
}
} else {
MethodHandle dlopen = FFM.getFunction("dlopen", FFM.getFunctionDesciptor(ADDRESS, ADDRESS, JAVA_INT));
if (debug) JFLog.log("dlopen=" + dlopen);
getsymbol = FFM.getFunction("dlsym", FFM.getFunctionDesciptor(ADDRESS, ADDRESS, ADDRESS));
if (debug) JFLog.log("getsymbol=" + getsymbol);
try {
handle = (MemorySegment)dlopen.invokeExact(MemorySegment.NULL, RTLD_NOW | RTLD_GLOBAL);
if (debug) JFLog.log("handle=" + handle);
} catch (Throwable t) {
JFLog.log(t);
}
}
FFM.setSymbolLookup(this);
return true;
} catch (Throwable t) {
JFLog.log(t);
return false;
}
}
public Optional<MemorySegment> find(String name) {
try {
if (debug) JFLog.log("lookup:name=" + name);
MemorySegment sym = (MemorySegment)getsymbol.invokeExact(handle, arena.allocateFrom(name));
if (debug) JFLog.log("lookup:sym=" + sym);
return Optional.of(sym);
} catch (Throwable t) {
JFLog.log(t);
return null;
}
}
}