infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
loader.c
Go to the documentation of this file.
1
32
33#if defined(INFIX_OS_WINDOWS)
34#include <windows.h>
35#else
36#include <dlfcn.h>
37#endif
38
51 if (path == nullptr)
52 return nullptr;
53
55 if (lib == nullptr) {
57 return nullptr;
58 }
59
60#if defined(INFIX_OS_WINDOWS)
61 lib->handle = LoadLibraryA(path);
62#else
63 // Use RTLD_LAZY for performance (resolve symbols only when they are first used).
64 // Use RTLD_GLOBAL to make symbols from this library available for resolution
65 // by other libraries that might be loaded later. This is important for complex
66 // dependency chains.
67 lib->handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
68#endif
69
70 if (lib->handle == nullptr) {
71#if defined(INFIX_OS_WINDOWS)
72 // On Windows, GetLastError() provides the specific error code.
74#else
75 // On POSIX, dlerror() returns a human-readable string.
77#endif
78 infix_free(lib);
79 return nullptr;
80 }
81 return lib;
82}
83
92 if (lib == nullptr)
93 return;
94
95 if (lib->handle) {
96#if defined(INFIX_OS_WINDOWS)
97 FreeLibrary((HMODULE)lib->handle);
98#else
99 dlclose(lib->handle);
100#endif
101 }
102 infix_free(lib);
103}
104
121c23_nodiscard void * infix_library_get_symbol(infix_library_t * lib, const char * symbol_name) {
122 if (lib == nullptr || lib->handle == nullptr || symbol_name == nullptr)
123 return nullptr;
124
125#if defined(INFIX_OS_WINDOWS)
126 return (void *)GetProcAddress((HMODULE)lib->handle, symbol_name);
127#else
128 return dlsym(lib->handle, symbol_name);
129#endif
130}
131
150 const char * symbol_name,
151 const char * type_signature,
152 void * buffer,
154 if (buffer == nullptr)
156
157 void * symbol_addr = infix_library_get_symbol(lib, symbol_name);
158 if (symbol_addr == nullptr) {
161 }
162
163 // Parse the signature to get the type's size.
164 infix_type * type = nullptr;
165 infix_arena_t * arena = nullptr;
166 infix_status status = infix_type_from_signature(&type, &arena, type_signature, registry);
167 if (status != INFIX_SUCCESS)
168 return status;
169
170 // Safely copy the data using the parsed size.
171 infix_memcpy(buffer, symbol_addr, type->size);
172
174 return INFIX_SUCCESS;
175}
176
197 const char * symbol_name,
198 const char * type_signature,
199 void * buffer,
201 if (buffer == nullptr)
203
204 void * symbol_addr = infix_library_get_symbol(lib, symbol_name);
205 if (symbol_addr == nullptr) {
208 }
209
210 infix_type * type = nullptr;
211 infix_arena_t * arena = nullptr;
212 infix_status status = infix_type_from_signature(&type, &arena, type_signature, registry);
213 if (status != INFIX_SUCCESS)
214 return status;
215
216 // Note: This assumes the memory page containing the global is writable.
217 // This is standard for data segments but could fail in unusual cases.
218 infix_memcpy(symbol_addr, buffer, type->size);
219
221 return INFIX_SUCCESS;
222}
infix_arena_t * arena
Definition 005_layouts.c:68
infix_registry_t * registry
Definition 008_registry_introspection.c:35
infix_status status
Definition 103_unions.c:66
#define c23_nodiscard
A compatibility macro for the C23 [[nodiscard]] attribute.
Definition compat_c23.h:113
@ INFIX_CODE_SYMBOL_NOT_FOUND
Definition infix.h:1352
@ INFIX_CODE_LIBRARY_LOAD_FAILED
Definition infix.h:1353
@ INFIX_CODE_OUT_OF_MEMORY
Definition infix.h:1330
@ INFIX_CATEGORY_ALLOCATION
Definition infix.h:1316
@ INFIX_CATEGORY_GENERAL
Definition infix.h:1315
c23_nodiscard infix_status infix_write_global(infix_library_t *lib, const char *symbol_name, const char *type_signature, void *buffer, infix_registry_t *registry)
Writes data from a buffer into an exported global variable in a library.
Definition loader.c:196
void infix_library_close(infix_library_t *lib)
Closes a dynamic library handle and frees associated resources.
Definition loader.c:91
c23_nodiscard void * infix_library_get_symbol(infix_library_t *lib, const char *symbol_name)
Retrieves the address of a symbol (function or variable) from a loaded library.
Definition loader.c:121
c23_nodiscard infix_library_t * infix_library_open(const char *path)
Opens a dynamic library and returns a handle to it.
Definition loader.c:50
c23_nodiscard infix_status infix_read_global(infix_library_t *lib, const char *symbol_name, const char *type_signature, void *buffer, infix_registry_t *registry)
Reads the value of an exported global variable from a library into a buffer.
Definition loader.c:149
c23_nodiscard infix_status infix_type_from_signature(infix_type **, infix_arena_t **, const char *, infix_registry_t *)
Parses a signature string representing a single data type.
Definition signature.c:1026
size_t size
Definition infix.h:214
infix_status
Enumerates the possible status codes returned by infix API functions.
Definition infix.h:389
@ INFIX_SUCCESS
Definition infix.h:390
@ INFIX_ERROR_INVALID_ARGUMENT
Definition infix.h:392
#define infix_free
A macro that can be defined to override the default free function.
Definition infix.h:330
void infix_arena_destroy(infix_arena_t *)
Destroys an arena and frees all memory allocated from it.
Definition arena.c:90
#define infix_memcpy
A macro that can be defined to override the default memcpy function.
Definition infix.h:335
#define infix_calloc
A macro that can be defined to override the default calloc function.
Definition infix.h:320
Internal data structures, function prototypes, and constants.
void _infix_set_error(infix_error_category_t category, infix_error_code_t code, size_t position)
Sets the thread-local error state with detailed information.
Definition error.c:155
void _infix_set_system_error(infix_error_category_t category, infix_error_code_t code, long system_code, const char *msg)
Sets the thread-local error state for a system-level error.
Definition error.c:232
Internal definition of a memory arena.
Definition infix_internals.h:146
Internal definition of a dynamic library handle.
Definition infix_internals.h:218
void * handle
Definition infix_internals.h:219
Internal definition of a named type registry.
Definition infix_internals.h:175
A semi-opaque structure that describes a C type.
Definition infix.h:211