1 #include "HalideRuntime.h"
2 
3 extern "C" {
4 
5 void *dlopen(const char *, int);
6 void *dlsym(void *, const char *);
7 
8 #define RTLD_DEFAULT ((void *)-2)
9 
10 #define RTLD_LAZY 0x1
11 #define RTLD_LOCAL 0x4
12 
halide_default_get_symbol(const char * name)13 WEAK void *halide_default_get_symbol(const char *name) {
14     return dlsym(RTLD_DEFAULT, name);
15 }
16 
halide_default_load_library(const char * name)17 WEAK void *halide_default_load_library(const char *name) {
18     return dlopen(name, RTLD_LAZY | RTLD_LOCAL);
19 }
20 
halide_default_get_library_symbol(void * lib,const char * name)21 WEAK void *halide_default_get_library_symbol(void *lib, const char *name) {
22     // We want our semantics to be such that if lib is NULL, this call
23     // is equivalent to halide_get_symbol.
24     if (lib == NULL) {
25         lib = RTLD_DEFAULT;
26     }
27     return dlsym(lib, name);
28 }
29 
30 }  // extern "C"
31 
32 namespace Halide {
33 namespace Runtime {
34 namespace Internal {
35 
36 WEAK halide_get_symbol_t custom_get_symbol = halide_default_get_symbol;
37 WEAK halide_load_library_t custom_load_library = halide_default_load_library;
38 WEAK halide_get_library_symbol_t custom_get_library_symbol = halide_default_get_library_symbol;
39 
40 }  // namespace Internal
41 }  // namespace Runtime
42 }  // namespace Halide
43 
44 extern "C" {
45 
halide_set_custom_get_symbol(halide_get_symbol_t f)46 WEAK halide_get_symbol_t halide_set_custom_get_symbol(halide_get_symbol_t f) {
47     halide_get_symbol_t result = custom_get_symbol;
48     custom_get_symbol = f;
49     return result;
50 }
51 
halide_set_custom_load_library(halide_load_library_t f)52 WEAK halide_load_library_t halide_set_custom_load_library(halide_load_library_t f) {
53     halide_load_library_t result = custom_load_library;
54     custom_load_library = f;
55     return result;
56 }
57 
halide_set_custom_get_library_symbol(halide_get_library_symbol_t f)58 WEAK halide_get_library_symbol_t halide_set_custom_get_library_symbol(halide_get_library_symbol_t f) {
59     halide_get_library_symbol_t result = custom_get_library_symbol;
60     custom_get_library_symbol = f;
61     return result;
62 }
63 
halide_get_symbol(const char * name)64 WEAK void *halide_get_symbol(const char *name) {
65     return custom_get_symbol(name);
66 }
67 
halide_load_library(const char * name)68 WEAK void *halide_load_library(const char *name) {
69     return custom_load_library(name);
70 }
71 
halide_get_library_symbol(void * lib,const char * name)72 WEAK void *halide_get_library_symbol(void *lib, const char *name) {
73     return custom_get_library_symbol(lib, name);
74 }
75 
76 }  // extern "C"
77