Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / libltdl / loaders / dld_link.c
CommitLineData
8d138742
CE
1/* loader-dld_link.c -- dynamic linking with dld
2
3 Copyright (C) 1998, 1999, 2000, 2004, 2006,
4 2007, 2008 Free Software Foundation, Inc.
5 Written by Thomas Tanner, 1998
6
7 NOTE: The canonical source of this file is maintained with the
8 GNU Libtool package. Report bugs to bug-libtool@gnu.org.
9
10GNU Libltdl is free software; you can redistribute it and/or
11modify it under the terms of the GNU Lesser General Public
12License as published by the Free Software Foundation; either
13version 2 of the License, or (at your option) any later version.
14
15As a special exception to the GNU Lesser General Public License,
16if you distribute this file as part of a program or library that
17is built using GNU Libtool, you may include this file under the
18same distribution terms that you use for the rest of that program.
19
20GNU Libltdl is distributed in the hope that it will be useful,
21but WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23GNU Lesser General Public License for more details.
24
25You should have received a copy of the GNU Lesser General Public
26License along with GNU Libltdl; see the file COPYING.LIB. If not, a
27copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
28or obtained by writing to the Free Software Foundation, Inc.,
2951 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30*/
31
32#include "lt__private.h"
33#include "lt_dlloader.h"
34
35/* Use the preprocessor to rename non-static symbols to avoid namespace
36 collisions when the loader code is statically linked into libltdl.
37 Use the "<module_name>_LTX_" prefix so that the symbol addresses can
38 be fetched from the preloaded symbol list by lt_dlsym(): */
39#define get_vtable dld_link_LTX_get_vtable
40
41LT_BEGIN_C_DECLS
42LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
43LT_END_C_DECLS
44
45
46/* Boilerplate code to set up the vtable for hooking this loader into
47 libltdl's loader list: */
48static int vl_exit (lt_user_data loader_data);
49static lt_module vm_open (lt_user_data loader_data, const char *filename,
50 lt_dladvise advise);
51static int vm_close (lt_user_data loader_data, lt_module module);
52static void * vm_sym (lt_user_data loader_data, lt_module module,
53 const char *symbolname);
54
55static lt_dlvtable *vtable = 0;
56
57/* Return the vtable for this loader, only the name and sym_prefix
58 attributes (plus the virtual function implementations, obviously)
59 change between loaders. */
60lt_dlvtable *
61get_vtable (lt_user_data loader_data)
62{
63 if (!vtable)
64 {
65 vtable = lt__zalloc (sizeof *vtable);
66 }
67
68 if (vtable && !vtable->name)
69 {
70 vtable->name = "lt_dld_link";
71 vtable->module_open = vm_open;
72 vtable->module_close = vm_close;
73 vtable->find_sym = vm_sym;
74 vtable->dlloader_exit = vl_exit;
75 vtable->dlloader_data = loader_data;
76 vtable->priority = LT_DLLOADER_APPEND;
77 }
78
79 if (vtable && (vtable->dlloader_data != loader_data))
80 {
81 LT__SETERROR (INIT_LOADER);
82 return 0;
83 }
84
85 return vtable;
86}
87
88
89
90/* --- IMPLEMENTATION --- */
91
92
93#if defined(HAVE_DLD_H)
94# include <dld.h>
95#endif
96
97/* A function called through the vtable when this loader is no
98 longer needed by the application. */
99static int
100vl_exit (lt_user_data LT__UNUSED loader_data)
101{
102 vtable = NULL;
103 return 0;
104}
105
106/* A function called through the vtable to open a module with this
107 loader. Returns an opaque representation of the newly opened
108 module for processing with this loader's other vtable functions. */
109static lt_module
110vm_open (lt_user_data LT__UNUSED loader_data, const char *filename,
111 lt_dladvise LT__UNUSED advise)
112{
113 lt_module module = lt__strdup (filename);
114
115 if (dld_link (filename) != 0)
116 {
117 LT__SETERROR (CANNOT_OPEN);
118 FREE (module);
119 }
120
121 return module;
122}
123
124/* A function called through the vtable when a particular module
125 should be unloaded. */
126static int
127vm_close (lt_user_data LT__UNUSED loader_data, lt_module module)
128{
129 int errors = 0;
130
131 if (dld_unlink_by_file ((char*)(module), 1) != 0)
132 {
133 LT__SETERROR (CANNOT_CLOSE);
134 ++errors;
135 }
136 else
137 {
138 FREE (module);
139 }
140
141 return errors;
142}
143
144/* A function called through the vtable to get the address of
145 a symbol loaded from a particular module. */
146static void *
147vm_sym (lt_user_data LT__UNUSED loader_data, lt_module LT__UNUSED module,
148 const char *name)
149{
150 void *address = dld_get_func (name);
151
152 if (!address)
153 {
154 LT__SETERROR (SYMBOL_NOT_FOUND);
155 }
156
157 return address;
158}