Import code from my Subversion repository
[black_box_cellml.git] / libltdl / loaders / dlopen.c
blob7e058164b418beff428d6f7244646c71461c3b4b
1 /* loader-dlopen.c -- dynamic linking with dlopen/dlsym
2 Copyright (C) 1998, 1999, 2000, 2004 Free Software Foundation, Inc.
3 Originally by Thomas Tanner <tanner@ffii.org>
5 NOTE: The canonical source of this file is maintained with the
6 GNU Libtool package. Report bugs to bug-libtool@gnu.org.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 As a special exception to the GNU Lesser General Public License,
14 if you distribute this file as part of a program or library that
15 is built using GNU libtool, you may include it under the same
16 distribution terms that you use for the rest of that program.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 02110-1301 USA
30 #include "lt_dlloader.h"
31 #include "lt__private.h"
33 /* Use the preprocessor to rename non-static symbols to avoid namespace
34 collisions when the loader code is statically linked into libltdl.
35 Use the "<module_name>_LTX_" prefix so that the symbol addresses can
36 be fetched from the preloaded symbol list by lt_dlsym(): */
37 #define get_vtable dlopen_LTX_get_vtable
39 LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
42 /* Boilerplate code to set up the vtable for hooking this loader into
43 libltdl's loader list: */
44 static lt_module vm_open (lt_user_data loader_data, const char *filename);
45 static int vm_close (lt_user_data loader_data, lt_module module);
46 static void * vm_sym (lt_user_data loader_data, lt_module module,
47 const char *symbolname);
49 /* Return the vtable for this loader, only the name and sym_prefix
50 attributes (plus the virtual function implementations, obviously)
51 change between loaders. */
52 lt_dlvtable *
53 get_vtable (lt_user_data loader_data)
55 static lt_dlvtable *vtable = 0;
57 if (!vtable)
59 vtable = lt__zalloc (sizeof *vtable);
62 if (vtable && !vtable->name)
64 vtable->name = "lt_dlopen";
65 #if defined(DLSYM_USCORE)
66 vtable->sym_prefix = "_";
67 #endif
68 vtable->module_open = vm_open;
69 vtable->module_close = vm_close;
70 vtable->find_sym = vm_sym;
71 vtable->dlloader_data = loader_data;
72 vtable->priority = LT_DLLOADER_PREPEND;
75 if (vtable && (vtable->dlloader_data != loader_data))
77 LT__SETERROR (INIT_LOADER);
78 return 0;
81 return vtable;
86 /* --- IMPLEMENTATION --- */
89 #if defined(HAVE_DLFCN_H)
90 # include <dlfcn.h>
91 #endif
93 #if defined(HAVE_SYS_DL_H)
94 # include <sys/dl.h>
95 #endif
98 /* We may have to define LT_LAZY_OR_NOW in the command line if we
99 find out it does not work in some platform. */
100 #if !defined(LT_LAZY_OR_NOW)
101 # if defined(RTLD_LAZY)
102 # define LT_LAZY_OR_NOW RTLD_LAZY
103 # else
104 # if defined(DL_LAZY)
105 # define LT_LAZY_OR_NOW DL_LAZY
106 # endif
107 # endif /* !RTLD_LAZY */
108 #endif
109 #if !defined(LT_LAZY_OR_NOW)
110 # if defined(RTLD_NOW)
111 # define LT_LAZY_OR_NOW RTLD_NOW
112 # else
113 # if defined(DL_NOW)
114 # define LT_LAZY_OR_NOW DL_NOW
115 # endif
116 # endif /* !RTLD_NOW */
117 #endif
118 #if !defined(LT_LAZY_OR_NOW)
119 # define LT_LAZY_OR_NOW 0
120 #endif /* !LT_LAZY_OR_NOW */
122 #if defined(HAVE_DLERROR)
123 # define DLERROR(arg) dlerror ()
124 #else
125 # define DLERROR(arg) LT__STRERROR (arg)
126 #endif
128 #define DL__SETERROR(errorcode) \
129 LT__SETERRORSTR (DLERROR (errorcode))
131 /* A function called through the vtable to open a module with this
132 loader. Returns an opaque representation of the newly opened
133 module for processing with this loader's other vtable functions. */
134 static lt_module
135 vm_open (lt_user_data loader_data, const char *filename)
137 lt_module module = dlopen (filename, LT_LAZY_OR_NOW);
139 if (!module)
141 DL__SETERROR (CANNOT_OPEN);
144 return module;
148 /* A function called through the vtable when a particular module
149 should be unloaded. */
150 static int
151 vm_close (lt_user_data loader_data, lt_module module)
153 int errors = 0;
155 if (dlclose (module) != 0)
157 DL__SETERROR (CANNOT_CLOSE);
158 ++errors;
161 return errors;
165 /* A function called through the vtable to get the address of
166 a symbol loaded from a particular module. */
167 static void *
168 vm_sym (lt_user_data loader_data, lt_module module, const char *name)
170 void *address = dlsym (module, name);
172 if (!address)
174 DL__SETERROR (SYMBOL_NOT_FOUND);
177 return address;