xref: /openbsd-src/lib/libc/dlfcn/dlfcn_stubs.c (revision 4eced852f4647b762f7fdc2001aeeeaf25762d86)
1 /*	$OpenBSD: dlfcn_stubs.c,v 1.18 2020/10/09 16:31:03 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/types.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <link.h>
33 #include <dlfcn.h>
34 #include <tib.h>
35 
36 #include "init.h"
37 
38 static int dlerror_ret;
39 
40 void *
dlopen(const char * libname,int how)41 dlopen(const char *libname, int how)
42 {
43 	if (_dl_cb != NULL && _dl_cb->dlopen != NULL)
44 		return _dl_cb->dlopen(libname, how);
45 	return NULL;
46 }
47 
48 int
dlclose(void * handle)49 dlclose(void *handle)
50 {
51 	if (_dl_cb != NULL && _dl_cb->dlclose != NULL)
52 		return _dl_cb->dlclose(handle);
53 	dlerror_ret = 1;
54 	return -1;
55 }
56 
57 void *
dlsym(void * handle,const char * name)58 dlsym(void *handle, const char *name)
59 {
60 	if (_dl_cb != NULL && _dl_cb->dlsym != NULL)
61 		return _dl_cb->dlsym(handle, name);
62 	dlerror_ret = 1;
63 	return NULL;
64 }
65 
66 int
dlctl(void * handle,int command,void * data)67 dlctl(void *handle, int command, void *data)
68 {
69 	if (_dl_cb != NULL && _dl_cb->dlctl != NULL)
70 		return _dl_cb->dlctl(handle, command, data);
71 	dlerror_ret = 1;
72 	return -1;
73 }
74 DEF_WEAK(dlctl);
75 
76 char *
dlerror(void)77 dlerror(void)
78 {
79 	if (_dl_cb != NULL && _dl_cb->dlerror != NULL)
80 		return _dl_cb->dlerror();
81 	if (dlerror_ret) {
82 		dlerror_ret = 0;
83 		return _dl_cb == NULL ? "No dynamic linker" :
84 		    "Incompatible dynamic linker";
85 	}
86 	return NULL;
87 }
88 
89 int
dl_iterate_phdr(int (* callback)(struct dl_phdr_info *,size_t,void *),void * data)90 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *),
91 	void *data)
92 {
93 	if (_dl_cb != NULL && _dl_cb->dl_iterate_phdr != NULL)
94 		return _dl_cb->dl_iterate_phdr(callback, data);
95 #ifndef PIC
96 	if (_static_phdr_info.dlpi_phdr != NULL)
97 		return callback(&_static_phdr_info, sizeof(_static_phdr_info),
98 		    data);
99 #endif /* !PIC */
100 	return -1;
101 }
102 DEF_WEAK(dl_iterate_phdr);
103 
104 int
dladdr(const void * addr,struct dl_info * info)105 dladdr(const void *addr, struct dl_info *info)
106 {
107 	if (_dl_cb != NULL && _dl_cb->dladdr != NULL)
108 		return _dl_cb->dladdr(addr, info);
109 	dlerror_ret = 1;
110 	return 0;
111 }
112 DEF_WEAK(dladdr);
113 
114 #if 0
115 /* Thread Local Storage argument structure */
116 typedef struct {
117 	unsigned long int ti_module;
118 	unsigned long int ti_offset;
119 } tls_index;
120 
121 void	*__tls_get_addr(tls_index *) __attribute__((weak));
122 #ifdef __i386
123 void	*___tls_get_addr(tls_index *) __attribute__((weak, __regparm__(1)));
124 #endif
125 
126 #if defined(__amd64) || defined(__i386) || defined(__sparc64)
127 void *
128 __tls_get_addr(tls_index *ti)
129 {
130 	return NULL;
131 }
132 
133 #ifdef __i386
134 __attribute__((__regparm__(1))) void *
135 ___tls_get_addr(tls_index *ti)
136 {
137 	return NULL;
138 }
139 #endif /* __i386 */
140 #endif /* arch with TLS support enabled */
141 #endif
142