dlfcn.c revision 103213
1/*-
2 * Copyright (c) 1998 John D. Polstra
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libc/gen/dlfcn.c 103213 2002-09-11 05:05:48Z mike $");
29
30/*
31 * Linkage to services provided by the dynamic linker.  These are
32 * implemented differently in ELF and a.out, because the dynamic
33 * linkers have different interfaces.
34 */
35#ifdef __ELF__
36
37#include <dlfcn.h>
38#include <stddef.h>
39
40static const char sorry[] = "Service unavailable";
41
42/*
43 * For ELF, the dynamic linker directly resolves references to its
44 * services to functions inside the dynamic linker itself.  These
45 * weak-symbol stubs are necessary so that "ld" won't complain about
46 * undefined symbols.  The stubs are executed only when the program is
47 * linked statically, or when a given service isn't implemented in the
48 * dynamic linker.  They must return an error if called, and they must
49 * be weak symbols so that the dynamic linker can override them.
50 */
51
52#pragma weak _rtld_error
53void
54_rtld_error(const char *fmt, ...)
55{
56}
57
58#pragma weak dladdr
59int
60dladdr(const void *addr, Dl_info *dlip)
61{
62	_rtld_error(sorry);
63	return 0;
64}
65
66#pragma weak dlclose
67int
68dlclose(void *handle)
69{
70	_rtld_error(sorry);
71	return -1;
72}
73
74#pragma weak dlerror
75const char *
76dlerror(void)
77{
78	return sorry;
79}
80
81#pragma weak dllockinit
82void
83dllockinit(void *context,
84	   void *(*lock_create)(void *context),
85	   void (*rlock_acquire)(void *lock),
86	   void (*wlock_acquire)(void *lock),
87	   void (*lock_release)(void *lock),
88	   void (*lock_destroy)(void *lock),
89	   void (*context_destroy)(void *context))
90{
91	if (context_destroy != NULL)
92		context_destroy(context);
93}
94
95#pragma weak dlopen
96void *
97dlopen(const char *name, int mode)
98{
99	_rtld_error(sorry);
100	return NULL;
101}
102
103#pragma weak dlsym
104void *
105dlsym(void * __restrict handle, const char * __restrict name)
106{
107	_rtld_error(sorry);
108	return NULL;
109}
110
111#else /* a.out format */
112
113#include <sys/types.h>
114#include <nlist.h>		/* XXX - Required by link.h */
115#include <dlfcn.h>
116#include <link.h>
117#include <stddef.h>
118
119/*
120 * For a.out, entry to the dynamic linker is via these trampolines.
121 * They enter the dynamic linker through the ld_entry struct that was
122 * passed back from the dynamic linker at startup time.
123 */
124
125/* GCC is needed because we use its __builtin_return_address construct. */
126
127#ifndef __GNUC__
128#error "GCC is needed to compile this file"
129#endif
130
131/*
132 * These variables are set by code in crt0.o.  For compatibility with
133 * old executables, they must be common, not extern.
134 */
135struct ld_entry	*__ldso_entry;		/* Entry points to dynamic linker */
136int		 __ldso_version;	/* Dynamic linker version number */
137
138int
139dladdr(const void *addr, Dl_info *dlip)
140{
141	if (__ldso_entry == NULL || __ldso_version < LDSO_VERSION_HAS_DLADDR)
142		return 0;
143	return (__ldso_entry->dladdr)(addr, dlip);
144}
145
146int
147dlclose(void *handle)
148{
149	if (__ldso_entry == NULL)
150		return -1;
151	return (__ldso_entry->dlclose)(handle);
152}
153
154const char *
155dlerror(void)
156{
157	if (__ldso_entry == NULL)
158		return "Service unavailable";
159	return (__ldso_entry->dlerror)();
160}
161
162void *
163dlopen(const char *name, int mode)
164{
165	if (__ldso_entry == NULL)
166		return NULL;
167	return (__ldso_entry->dlopen)(name, mode);
168}
169
170void *
171dlsym(void * __restrict handle, const char * __restrict name)
172{
173	if (__ldso_entry == NULL)
174		return NULL;
175	if (__ldso_version >= LDSO_VERSION_HAS_DLSYM3) {
176		void *retaddr = __builtin_return_address(0); /* __GNUC__ only */
177		return (__ldso_entry->dlsym3)(handle, name, retaddr);
178	} else
179		return (__ldso_entry->dlsym)(handle, name);
180}
181
182#endif /* __ELF__ */
183