xref: /haiku/src/system/libroot/posix/dlfcn.c (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2 ** Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 **
5 ** Copyright 2002, Manuel J. Petit. All rights reserved.
6 ** Distributed under the terms of the NewOS License.
7 */
8 
9 
10 #include <libroot_private.h>
11 #include <user_runtime.h>
12 
13 #include <dlfcn.h>
14 #include <string.h>
15 
16 
17 static struct rld_export const *sRuntimeLinker;
18 static status_t sStatus;
19 	// Note, this is not thread-safe
20 
21 
22 void *
23 dlopen(char const *name, int mode)
24 {
25 	status_t status;
26 
27 	if (name == NULL)
28 		name = MAGIC_APP_NAME;
29 
30 	status = sRuntimeLinker->load_add_on(name, mode);
31 	sStatus = status;
32 
33 	if (status < B_OK)
34 		return NULL;
35 
36 	return (void *)status;
37 }
38 
39 
40 void *
41 dlsym(void *handle, char const *name)
42 {
43 	status_t status;
44 	void *location;
45 
46 	status = sRuntimeLinker->get_image_symbol((image_id)handle, name, B_SYMBOL_TYPE_ANY, &location);
47 	sStatus = status;
48 
49 	if (status < B_OK)
50 		return NULL;
51 
52 	return location;
53 }
54 
55 
56 int
57 dlclose(void *handle)
58 {
59 	return sRuntimeLinker->unload_add_on((image_id)handle);
60 }
61 
62 
63 char *
64 dlerror(void)
65 {
66 	if (sStatus < B_OK)
67 		return strerror(sStatus);
68 
69 	return NULL;
70 }
71 
72 
73 void
74 __init_dlfcn(const struct uspace_program_args *args)
75 {
76 	sRuntimeLinker = args->rld_export;
77 }
78