dlfcn.c revision 33180
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 *	$Id$
27 */
28
29/*
30 * Trampolines to services provided by the dynamic linker.
31 */
32
33#include <sys/types.h>
34#include <nlist.h>		/* XXX - Required by link.h */
35#include <dlfcn.h>
36#include <link.h>
37#include <stddef.h>
38
39/*
40 * These variables are set by code in crt0.o.  For compatibility with
41 * old executables, they must be common, not extern.
42 */
43struct ld_entry	*__ldso_entry;		/* Entry points to dynamic linker */
44int		 __ldso_version;	/* Dynamic linker version number */
45
46void *
47dlopen(name, mode)
48	const char	*name;
49	int		 mode;
50{
51	if (__ldso_entry == NULL)
52		return NULL;
53	return (__ldso_entry->dlopen)(name, mode);
54}
55
56int
57dlclose(fd)
58	void		*fd;
59{
60	if (__ldso_entry == NULL)
61		return -1;
62	return (__ldso_entry->dlclose)(fd);
63}
64
65void *
66dlsym(fd, name)
67	void		*fd;
68	const char	*name;
69{
70	if (__ldso_entry == NULL)
71		return NULL;
72	if (__ldso_version >= LDSO_VERSION_HAS_DLSYM3) {
73		void *retaddr = *(&fd - 1);  /* XXX - ABI/machine dependent */
74		return (__ldso_entry->dlsym3)(fd, name, retaddr);
75	} else
76		return (__ldso_entry->dlsym)(fd, name);
77}
78
79
80const char *
81dlerror()
82{
83	if (__ldso_entry == NULL)
84		return "Service unavailable";
85	return (__ldso_entry->dlerror)();
86}
87
88int
89dladdr(addr, dlip)
90	const void	*addr;
91	Dl_info		*dlip;
92{
93	if (__ldso_entry == NULL || __ldso_version < LDSO_VERSION_HAS_DLADDR)
94		return 0;
95	return (__ldso_entry->dladdr)(addr, dlip);
96}
97