xref: /haiku/src/system/libroot/posix/dlfcn.c (revision f75a7bf508f3156d63a14f8fd77c5e0ca4d08c42)
1 /*
2  * Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT 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 status_t sStatus;
18 	// Note, this is not thread-safe
19 
20 
21 void *
22 dlopen(char const *name, int mode)
23 {
24 // TODO: According to the standard multiple dlopen() invocations for the same
25 // file will cause the object to be loaded once only. That is we should load
26 // the object as a library, not an add-on.
27 	status_t status;
28 
29 	if (name == NULL)
30 		name = MAGIC_APP_NAME;
31 
32 	status = __gRuntimeLoader->load_add_on(name, mode);
33 	sStatus = status;
34 
35 	if (status < B_OK)
36 		return NULL;
37 
38 	return (void *)status;
39 }
40 
41 
42 void *
43 dlsym(void *handle, char const *name)
44 {
45 	status_t status;
46 	void *location;
47 
48 	status = get_image_symbol((image_id)handle, name, B_SYMBOL_TYPE_ANY, &location);
49 	sStatus = status;
50 
51 	if (status < B_OK)
52 		return NULL;
53 
54 	return location;
55 }
56 
57 
58 int
59 dlclose(void *handle)
60 {
61 	return unload_add_on((image_id)handle);
62 }
63 
64 
65 char *
66 dlerror(void)
67 {
68 	if (sStatus < B_OK)
69 		return strerror(sStatus);
70 
71 	return NULL;
72 }
73 
74 
75 // __libc_dl*** wrappers
76 // We use a mixed glibc / bsd libc, and glibc wants these
77 void *__libc_dlopen(const char *name);
78 void *__libc_dlsym(void *handle, const char *name);
79 void __libc_dlclose(void *handle);
80 
81 void *
82 __libc_dlopen(const char *name)
83 {
84 	return dlopen(name, 0);
85 }
86 
87 
88 void *
89 __libc_dlsym(void *handle, const char *name)
90 {
91 	return dlsym(handle, name);
92 }
93 
94 
95 void
96 __libc_dlclose(void *handle)
97 {
98 	dlclose(handle);
99 }
100