Cthulhu  0.2.10
Cthulhu compiler collection
library.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "os/os.h"
4 
5 #include "base/panic.h"
6 
7 #include <dlfcn.h>
8 #include <errno.h>
9 
11 {
12  return dlopen(path, RTLD_NOW);
13 }
14 
16 {
17  return dlclose(lib);
18 }
19 
20 // casting a object pointer to a function pointer is unspecified behavior.
21 // gnu warns on it, but this is a posix platform, so its well defined.
22 #if defined(__GNUC__) && !defined(__clang__)
23 # pragma GCC diagnostic push
24 # pragma GCC diagnostic ignored "-Wpedantic"
25 #endif
26 
27 CT_LOCAL void *impl_library_symbol(os_library_impl_t lib, const char *name)
28 {
29  // clear any previous errors
30  dlerror();
31 
32  void *addr = (void*)dlsym(lib, name);
33 
34  const char *error = dlerror();
35  if (error != NULL)
36  {
37  return NULL;
38  }
39 
40  return addr;
41 }
42 
43 #if defined(__GNUC__) && !defined(__clang__)
44 # pragma GCC diagnostic pop
45 #endif
#define CT_LOCAL
Definition: compiler.h:166
CT_LOCAL os_library_impl_t impl_library_open(const char *path)
Definition: library.c:10
CT_LOCAL bool impl_library_close(os_library_impl_t lib)
Definition: library.c:15
CT_LOCAL void * impl_library_symbol(os_library_impl_t lib, const char *name)
Definition: library.c:27
void * os_library_impl_t
Definition: posix.h:10