File tree Expand file tree Collapse file tree 10 files changed +50
-1
lines changed Expand file tree Collapse file tree 10 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -219,6 +219,9 @@ module Bytecode = struct
219
219
| exception _ -> None
220
220
| obj -> Some obj
221
221
222
+ let does_symbol_exist ~bytecode_or_asm_symbol =
223
+ Option. is_some (unsafe_get_global_value ~bytecode_or_asm_symbol )
224
+
222
225
let finish (ic , _filename , _digest ) =
223
226
close_in ic
224
227
end
Original file line number Diff line number Diff line change @@ -62,6 +62,10 @@ let unsafe_get_global_value ~bytecode_or_asm_symbol =
62
62
if is_native then N. unsafe_get_global_value ~bytecode_or_asm_symbol
63
63
else B. unsafe_get_global_value ~bytecode_or_asm_symbol
64
64
65
+ let does_symbol_exist ~bytecode_or_asm_symbol =
66
+ if is_native then N. does_symbol_exist ~bytecode_or_asm_symbol
67
+ else B. does_symbol_exist ~bytecode_or_asm_symbol
68
+
65
69
let adapt_filename file =
66
70
if is_native then N. adapt_filename file
67
71
else B. adapt_filename file
Original file line number Diff line number Diff line change @@ -166,6 +166,10 @@ val unsafe_get_global_value : bytecode_or_asm_symbol:string -> Obj.t option
166
166
The accessible values are those in the main program and those provided by
167
167
previous calls to [loadfile].
168
168
169
+ ** This function may only be used to retrieve the addresses of symbols
170
+ that are valid OCaml values. It cannot be used to retrieve e.g. code
171
+ pointers. **
172
+
169
173
This function is deemed "unsafe" as there is no type safety provided.
170
174
171
175
When executing in bytecode, this function uses [Symtable]. As a cautionary
@@ -178,3 +182,8 @@ val unsafe_get_global_value : bytecode_or_asm_symbol:string -> Obj.t option
178
182
client's version of [Symtable]). This is why we can't use [Dynlink] from the
179
183
toplevel interactive loop, in particular.
180
184
*)
185
+
186
+ (* * Like [unsafe_get_global_value], but only tests whether the given symbol
187
+ exists, and in native code may be used for any symbol (whether or not such
188
+ symbol points at a valid OCaml value). *)
189
+ val does_symbol_exist : bytecode_or_asm_symbol :string -> bool
Original file line number Diff line number Diff line change @@ -402,6 +402,12 @@ module Make (P : Dynlink_platform_intf.S) = struct
402
402
P. unsafe_get_global_value ~bytecode_or_asm_symbol
403
403
)
404
404
405
+ let does_symbol_exist ~bytecode_or_asm_symbol =
406
+ with_lock (fun _ ->
407
+ (* The bytecode implementation reads the global symtable *)
408
+ P. does_symbol_exist ~bytecode_or_asm_symbol
409
+ )
410
+
405
411
let is_native = P. is_native
406
412
let adapt_filename = P. adapt_filename
407
413
end
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ module Make (_ : Dynlink_platform_intf.S) : sig
24
24
val loadfile : string -> unit
25
25
val loadfile_private : string -> unit
26
26
val unsafe_get_global_value : bytecode_or_asm_symbol :string -> Obj .t option
27
+ val does_symbol_exist : bytecode_or_asm_symbol :string -> bool
27
28
val adapt_filename : string -> string
28
29
val set_allowed_units : string list -> unit
29
30
val allow_only : string list -> unit
Original file line number Diff line number Diff line change @@ -78,5 +78,7 @@ module type S = sig
78
78
79
79
val unsafe_get_global_value : bytecode_or_asm_symbol :string -> Obj .t option
80
80
81
+ val does_symbol_exist : bytecode_or_asm_symbol :string -> bool
82
+
81
83
val finish : handle -> unit
82
84
end
Original file line number Diff line number Diff line change @@ -53,6 +53,9 @@ module Native = struct
53
53
= " caml_sys_exit" " caml_natdynlink_globals_inited"
54
54
external ndl_loadsym : string -> Obj .t
55
55
= " caml_sys_exit" " caml_natdynlink_loadsym"
56
+ external ndl_existssym : string -> bool
57
+ = " caml_sys_exit" " caml_natdynlink_existssym"
58
+ [@@ noalloc]
56
59
57
60
module Unit_header = struct
58
61
type t = Cmxs_format .dynunit
@@ -161,6 +164,9 @@ module Native = struct
161
164
| exception _ -> None
162
165
| obj -> Some obj
163
166
167
+ let does_symbol_exist ~bytecode_or_asm_symbol =
168
+ ndl_existssym bytecode_or_asm_symbol
169
+
164
170
let finish _handle = ()
165
171
end
166
172
Original file line number Diff line number Diff line change @@ -204,7 +204,16 @@ CAMLprim value caml_natdynlink_loadsym(value symbol)
204
204
CAMLparam1 (symbol );
205
205
CAMLlocal1 (sym );
206
206
207
+ /* Note that this can only be used for symbols which are valid OCaml
208
+ values, otherwise a naked pointer would be returned. */
209
+
207
210
sym = (value ) caml_globalsym (String_val (symbol ));
208
211
if (!sym ) caml_failwith (String_val (symbol ));
209
212
CAMLreturn (sym );
210
213
}
214
+
215
+ CAMLprim value caml_natdynlink_existssym (value symbol )
216
+ {
217
+ void * sym = caml_globalsym (String_val (symbol ));
218
+ return sym != NULL ? Val_true : Val_false ;
219
+ }
Original file line number Diff line number Diff line change @@ -221,7 +221,16 @@ CAMLprim value caml_natdynlink_loadsym(value symbol)
221
221
CAMLparam1 (symbol );
222
222
CAMLlocal1 (sym );
223
223
224
+ /* Note that this can only be used for symbols which are valid OCaml
225
+ values, otherwise a naked pointer would be returned. */
226
+
224
227
sym = (value ) caml_globalsym (String_val (symbol ));
225
228
if (!sym ) caml_failwith (String_val (symbol ));
226
229
CAMLreturn (sym );
227
230
}
231
+
232
+ CAMLprim value caml_natdynlink_existssym (value symbol )
233
+ {
234
+ void * sym = caml_globalsym (String_val (symbol ));
235
+ return sym != NULL ? Val_true : Val_false ;
236
+ }
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ let lookup sym =
28
28
Dynlink. unsafe_get_global_value ~bytecode_or_asm_symbol: sym
29
29
30
30
let need_symbol sym =
31
- Option. is_none (Dynlink. unsafe_get_global_value ~bytecode_or_asm_symbol: sym)
31
+ not (Dynlink. does_symbol_exist ~bytecode_or_asm_symbol: sym)
32
32
33
33
let dll_run dll entry =
34
34
match (try Result (Obj. magic (ndl_run_toplevel dll entry))
You can’t perform that action at this time.
0 commit comments