-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
DynamicLinkLoader.java
168 lines (142 loc) · 6.62 KB
/
DynamicLinkLoader.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
* MACHINE GENERATED FILE, DO NOT EDIT
*/
package org.lwjgl.system.linux;
import javax.annotation.*;
import java.nio.*;
import org.lwjgl.system.*;
import static org.lwjgl.system.Checks.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
/** Native bindings to <dlfcn.h>. */
public class DynamicLinkLoader {
static { Library.initialize(); }
/** The {@code mode} argument to {@link #dlopen} contains one of the following. */
public static final int
RTLD_LAZY = 0x1,
RTLD_NOW = 0x2,
RTLD_BINDING_MASK = 0x3,
RTLD_NOLOAD = 0x4,
RTLD_DEEPBIND = 0x8;
/**
* If the following bit is set in the {@code mode} argument to {@link #dlopen}, the symbols of the loaded object and its dependencies are made visible as
* if the object were linked directly into the program.
*/
public static final int RTLD_GLOBAL = 0x100;
/**
* Unix98 demands the following flag which is the inverse to {@link #RTLD_GLOBAL}. The implementation does this by default and so we can define the value
* to zero.
*/
public static final int RTLD_LOCAL = 0;
/** Do not delete object when closed. */
public static final int RTLD_NODELETE = 0x1000;
protected DynamicLinkLoader() {
throw new UnsupportedOperationException();
}
// --- [ dlopen ] ---
/** Unsafe version of: {@link #dlopen} */
public static native long ndlopen(long filename, int mode);
/**
* Loads the dynamic library file named by the null-terminated string {@code filename} and returns an opaque "handle" for the dynamic library. If
* {@code filename} is {@code NULL}, then the returned handle is for the main program.
*
* @param filename the name of the dynamic library to open, or {@code NULL}
* @param mode a bitfield. One or more of:<br><table><tr><td>{@link #RTLD_LAZY}</td><td>{@link #RTLD_NOW}</td><td>{@link #RTLD_BINDING_MASK}</td><td>{@link #RTLD_NOLOAD}</td><td>{@link #RTLD_DEEPBIND}</td><td>{@link #RTLD_GLOBAL}</td></tr><tr><td>{@link #RTLD_LOCAL}</td><td>{@link #RTLD_NODELETE}</td></tr></table>
*/
@NativeType("void *")
public static long dlopen(@Nullable @NativeType("char const *") ByteBuffer filename, int mode) {
if (CHECKS) {
checkNT1Safe(filename);
}
return ndlopen(memAddressSafe(filename), mode);
}
/**
* Loads the dynamic library file named by the null-terminated string {@code filename} and returns an opaque "handle" for the dynamic library. If
* {@code filename} is {@code NULL}, then the returned handle is for the main program.
*
* @param filename the name of the dynamic library to open, or {@code NULL}
* @param mode a bitfield. One or more of:<br><table><tr><td>{@link #RTLD_LAZY}</td><td>{@link #RTLD_NOW}</td><td>{@link #RTLD_BINDING_MASK}</td><td>{@link #RTLD_NOLOAD}</td><td>{@link #RTLD_DEEPBIND}</td><td>{@link #RTLD_GLOBAL}</td></tr><tr><td>{@link #RTLD_LOCAL}</td><td>{@link #RTLD_NODELETE}</td></tr></table>
*/
@NativeType("void *")
public static long dlopen(@Nullable @NativeType("char const *") CharSequence filename, int mode) {
MemoryStack stack = stackGet(); int stackPointer = stack.getPointer();
try {
stack.nUTF8Safe(filename, true);
long filenameEncoded = filename == null ? NULL : stack.getPointerAddress();
return ndlopen(filenameEncoded, mode);
} finally {
stack.setPointer(stackPointer);
}
}
// --- [ dlerror ] ---
/** Unsafe version of: {@link #dlerror} */
public static native long ndlerror();
/**
* Returns a human readable string describing the most recent error that occurred from {@link #dlopen}, {@link #dlsym} or {@link #dlclose} since
* the last call to {@code dlerror()}. It returns {@code NULL} if no errors have occurred since initialization or since it was last called.
*/
@Nullable
@NativeType("char *")
public static String dlerror() {
long __result = ndlerror();
return memASCIISafe(__result);
}
// --- [ dlsym ] ---
/** Unsafe version of: {@link #dlsym} */
public static native long ndlsym(long handle, long name);
/**
* Takes a "handle" of a dynamic library returned by {@link #dlopen} and the null-terminated symbol name, returning the address where that symbol is loaded
* into memory. If the symbol is not found, in the specified library or any of the libraries that were automatically loaded by {@link #dlopen} when that
* library was loaded, {@code dlsym()} returns {@code NULL}.
*
* @param handle the dynamic library handle
* @param name the symbol name
*/
@NativeType("void *")
public static long dlsym(@NativeType("void *") long handle, @NativeType("char const *") ByteBuffer name) {
if (CHECKS) {
check(handle);
checkNT1(name);
}
return ndlsym(handle, memAddress(name));
}
/**
* Takes a "handle" of a dynamic library returned by {@link #dlopen} and the null-terminated symbol name, returning the address where that symbol is loaded
* into memory. If the symbol is not found, in the specified library or any of the libraries that were automatically loaded by {@link #dlopen} when that
* library was loaded, {@code dlsym()} returns {@code NULL}.
*
* @param handle the dynamic library handle
* @param name the symbol name
*/
@NativeType("void *")
public static long dlsym(@NativeType("void *") long handle, @NativeType("char const *") CharSequence name) {
if (CHECKS) {
check(handle);
}
MemoryStack stack = stackGet(); int stackPointer = stack.getPointer();
try {
stack.nASCII(name, true);
long nameEncoded = stack.getPointerAddress();
return ndlsym(handle, nameEncoded);
} finally {
stack.setPointer(stackPointer);
}
}
// --- [ dlclose ] ---
/** Unsafe version of: {@link #dlclose} */
public static native int ndlclose(long handle);
/**
* Decrements the reference count on the dynamic library handle handle. If the reference count drops to zero and no other loaded libraries use symbols in
* it, then the dynamic library is unloaded.
*
* @param handle the dynamic library to close
*/
public static int dlclose(@NativeType("void *") long handle) {
if (CHECKS) {
check(handle);
}
return ndlclose(handle);
}
}