move tlsdesc and internal dl function declarations to dynlink.h
[musl] / src / ldso / dlerror.c
1 #include <dlfcn.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include "pthread_impl.h"
5 #include "dynlink.h"
6 #include "libc.h"
7
8 char *dlerror()
9 {
10         pthread_t self = __pthread_self();
11         if (!self->dlerror_flag) return 0;
12         self->dlerror_flag = 0;
13         char *s = self->dlerror_buf;
14         if (s == (void *)-1)
15                 return "Dynamic linker failed to allocate memory for error message";
16         else
17                 return s;
18 }
19
20 void __dl_thread_cleanup(void)
21 {
22         pthread_t self = __pthread_self();
23         if (self->dlerror_buf != (void *)-1)
24                 free(self->dlerror_buf);
25 }
26
27 hidden void __dl_vseterr(const char *fmt, va_list ap)
28 {
29         va_list ap2;
30         va_copy(ap2, ap);
31         pthread_t self = __pthread_self();
32         if (self->dlerror_buf != (void *)-1)
33                 free(self->dlerror_buf);
34         size_t len = vsnprintf(0, 0, fmt, ap2);
35         va_end(ap2);
36         char *buf = malloc(len+1);
37         if (buf) {
38                 vsnprintf(buf, len+1, fmt, ap);
39         } else {
40                 buf = (void *)-1;       
41         }
42         self->dlerror_buf = buf;
43         self->dlerror_flag = 1;
44 }
45
46 hidden void __dl_seterr(const char *fmt, ...)
47 {
48         va_list ap;
49         va_start(ap, fmt);
50         __dl_vseterr(fmt, ap);
51         va_end(ap);
52 }
53
54 static int stub_invalid_handle(void *h)
55 {
56         __dl_seterr("Invalid library handle %p", (void *)h);
57         return 1;
58 }
59
60 weak_alias(stub_invalid_handle, __dl_invalid_handle);