1 /* ltdl.c -- system independent dlopen wrapper
3 Copyright (C) 1998, 1999, 2000, 2004, 2005, 2006,
4 2007, 2008 Free Software Foundation, Inc.
5 Written by Thomas Tanner, 1998
7 NOTE: The canonical source of this file is maintained with the
8 GNU Libtool package. Report bugs to bug-libtool@gnu.org.
10 GNU Libltdl is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 As a special exception to the GNU Lesser General Public License,
16 if you distribute this file as part of a program or library that
17 is built using GNU Libtool, you may include this file under the
18 same distribution terms that you use for the rest of that program.
20 GNU Libltdl is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with GNU Libltdl; see the file COPYING.LIB. If not, a
27 copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
28 or obtained by writing to the Free Software Foundation, Inc.,
29 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include "lt__private.h"
33 #include "lt_system.h"
34 #include "lt_dlloader.h"
37 /* --- MANIFEST CONSTANTS --- */
40 /* Standard libltdl search path environment variable name */
41 #undef LTDL_SEARCHPATH_VAR
42 #define LTDL_SEARCHPATH_VAR "LTDL_LIBRARY_PATH"
44 /* Standard libtool archive file extension. */
46 #define LT_ARCHIVE_EXT ".la"
48 /* max. filename length */
49 #if !defined(LT_FILENAME_MAX)
50 # define LT_FILENAME_MAX 1024
53 #if !defined(LT_LIBEXT)
54 # define LT_LIBEXT "a"
57 /* This is the maximum symbol size that won't require malloc/free */
58 #undef LT_SYMBOL_LENGTH
59 #define LT_SYMBOL_LENGTH 128
61 /* This accounts for the _LTX_ separator */
62 #undef LT_SYMBOL_OVERHEAD
63 #define LT_SYMBOL_OVERHEAD 5
65 /* Various boolean flags can be stored in the flags field of an
67 #define LT_DLIS_RESIDENT(handle) ((handle)->info.is_resident)
68 #define LT_DLIS_SYMGLOBAL(handle) ((handle)->info.is_symglobal)
69 #define LT_DLIS_SYMLOCAL(handle) ((handle)->info.is_symlocal)
72 static const char objdir[] = LT_OBJDIR;
73 static const char archive_ext[] = LT_ARCHIVE_EXT;
74 static const char libext[] = LT_LIBEXT;
75 #if defined(LT_MODULE_EXT)
76 static const char shlib_ext[] = LT_MODULE_EXT;
78 #if defined(LT_DLSEARCH_PATH)
79 static const char sys_dlsearch_path[] = LT_DLSEARCH_PATH;
85 /* --- DYNAMIC MODULE LOADING --- */
88 /* The type of a function used at each iteration of foreach_dirinpath(). */
89 typedef int foreach_callback_func (char *filename, void *data1,
91 /* foreachfile_callback itself calls a function of this type: */
92 typedef int file_worker_func (const char *filename, void *data);
95 static int foreach_dirinpath (const char *search_path,
96 const char *base_name,
97 foreach_callback_func *func,
98 void *data1, void *data2);
99 static int find_file_callback (char *filename, void *data1,
101 static int find_handle_callback (char *filename, void *data,
103 static int foreachfile_callback (char *filename, void *data1,
107 static int canonicalize_path (const char *path, char **pcanonical);
108 static int argzize_path (const char *path,
109 char **pargz, size_t *pargz_len);
110 static FILE *find_file (const char *search_path,
111 const char *base_name, char **pdir);
112 static lt_dlhandle *find_handle (const char *search_path,
113 const char *base_name,
116 static int find_module (lt_dlhandle *handle, const char *dir,
117 const char *libdir, const char *dlname,
118 const char *old_name, int installed,
120 static int has_library_ext (const char *filename);
121 static int load_deplibs (lt_dlhandle handle, char *deplibs);
122 static int trim (char **dest, const char *str);
123 static int try_dlopen (lt_dlhandle *handle,
124 const char *filename, const char *ext,
126 static int tryall_dlopen (lt_dlhandle *handle,
127 const char *filename,
129 const lt_dlvtable *vtable);
130 static int unload_deplibs (lt_dlhandle handle);
131 static int lt_argz_insert (char **pargz, size_t *pargz_len,
132 char *before, const char *entry);
133 static int lt_argz_insertinorder (char **pargz, size_t *pargz_len,
135 static int lt_argz_insertdir (char **pargz, size_t *pargz_len,
136 const char *dirnam, struct dirent *dp);
137 static int lt_dlpath_insertdir (char **ppath, char *before,
139 static int list_files_by_dir (const char *dirnam,
140 char **pargz, size_t *pargz_len);
141 static int file_not_found (void);
143 #ifdef HAVE_LIBDLLOADER
144 static int loader_init_callback (lt_dlhandle handle);
145 #endif /* HAVE_LIBDLLOADER */
147 static int loader_init (lt_get_vtable *vtable_func,
150 static char *user_search_path= 0;
151 static lt_dlhandle handles = 0;
152 static int initialized = 0;
154 /* Our memory failure callback sets the error message to be passed back
155 up to the client, so we must be careful to return from mallocation
156 callers if allocation fails (as this callback returns!!). */
158 lt__alloc_die_callback (void)
160 LT__SETERROR (NO_MEMORY);
163 #ifdef HAVE_LIBDLLOADER
164 /* This function is called to initialise each preloaded module loader,
165 and hook it into the list of loaders to be used when attempting to
166 dlopen an application module. */
168 loader_init_callback (lt_dlhandle handle)
170 lt_get_vtable *vtable_func = (lt_get_vtable *) lt_dlsym (handle, "get_vtable");
171 return loader_init (vtable_func, 0);
173 #endif /* HAVE_LIBDLLOADER */
176 loader_init (lt_get_vtable *vtable_func, lt_user_data data)
178 const lt_dlvtable *vtable = 0;
183 vtable = (*vtable_func) (data);
186 /* lt_dlloader_add will LT__SETERROR if it fails. */
187 errors += lt_dlloader_add (vtable);
189 assert (errors || vtable);
191 if ((!errors) && vtable->dlloader_init)
193 if ((*vtable->dlloader_init) (vtable->dlloader_data))
195 LT__SETERROR (INIT_LOADER);
203 /* Bootstrap the loader loading with the preopening loader. */
204 #define get_vtable preopen_LTX_get_vtable
205 #define preloaded_symbols LT_CONC3(lt_, LTDLOPEN, _LTX_preloaded_symbols)
208 LT_SCOPE const lt_dlvtable * get_vtable (lt_user_data data);
210 #ifdef HAVE_LIBDLLOADER
211 extern lt_dlsymlist preloaded_symbols;
214 /* Initialize libltdl. */
220 /* Initialize only at first call. */
221 if (++initialized == 1)
223 lt__alloc_die = lt__alloc_die_callback;
225 user_search_path = 0; /* empty search path */
227 /* First set up the statically loaded preload module loader, so
228 we can use it to preopen the other loaders we linked in at
230 errors += loader_init (get_vtable, 0);
232 /* Now open all the preloaded module loaders, so the application
233 can use _them_ to lt_dlopen its own modules. */
234 #ifdef HAVE_LIBDLLOADER
237 errors += lt_dlpreload (&preloaded_symbols);
242 errors += lt_dlpreload_open (LT_STR(LTDLOPEN), loader_init_callback);
244 #endif /* HAVE_LIBDLLOADER */
247 #ifdef LT_DEBUG_LOADERS
257 /* shut down libltdl */
258 lt_dlloader *loader = 0;
259 lt_dlhandle handle = handles;
264 LT__SETERROR (SHUTDOWN);
269 /* shut down only at last call. */
270 if (--initialized == 0)
274 while (handles && LT_DLIS_RESIDENT (handles))
276 handles = handles->next;
279 /* close all modules */
280 for (level = 1; handle; ++level)
282 lt_dlhandle cur = handles;
283 int saw_nonresident = 0;
287 lt_dlhandle tmp = cur;
289 if (!LT_DLIS_RESIDENT (tmp))
292 if (tmp->info.ref_count <= level)
294 if (lt_dlclose (tmp))
298 /* Make sure that the handle pointed to by 'cur' still exists.
299 lt_dlclose recursively closes dependent libraries which removes
300 them from the linked list. One of these might be the one
301 pointed to by 'cur'. */
304 for (tmp = handles; tmp; tmp = tmp->next)
313 /* done if only resident modules are left */
314 if (!saw_nonresident)
318 /* When removing loaders, we can only find out failure by testing
319 the error string, so avoid a spurious one from an earlier
324 /* close all loaders */
325 for (loader = (lt_dlloader *) lt_dlloader_next (NULL); loader;)
327 lt_dlloader *next = (lt_dlloader *) lt_dlloader_next (loader);
328 lt_dlvtable *vtable = (lt_dlvtable *) lt_dlloader_get (loader);
330 if ((vtable = lt_dlloader_remove ((char *) vtable->name)))
336 /* ignore errors due to resident modules */
346 FREE(user_search_path);
354 /* Try VTABLE or, if VTABLE is NULL, all available loaders for FILENAME.
355 If the library is not successfully loaded, return non-zero. Otherwise,
356 the dlhandle is stored at the address given in PHANDLE. */
358 tryall_dlopen (lt_dlhandle *phandle, const char *filename,
359 lt_dladvise advise, const lt_dlvtable *vtable)
361 lt_dlhandle handle = handles;
362 const char * saved_error = 0;
365 #ifdef LT_DEBUG_LOADERS
366 fprintf (stderr, "tryall_dlopen (%s, %s)\n",
367 filename ? filename : "(null)",
368 vtable ? vtable->name : "(ALL)");
371 LT__GETERROR (saved_error);
373 /* check whether the module was already opened */
374 for (;handle; handle = handle->next)
376 if ((handle->info.filename == filename) /* dlopen self: 0 == 0 */
377 || (handle->info.filename && filename
378 && streq (handle->info.filename, filename)))
386 ++handle->info.ref_count;
394 /* Comment out the check of file permissions using access.
395 This call seems to always return -1 with error EACCES.
397 /* We need to catch missing file errors early so that
398 file_not_found() can detect what happened.
399 if (access (filename, R_OK) != 0)
401 LT__SETERROR (FILE_NOT_FOUND);
406 handle->info.filename = lt__strdup (filename);
407 if (!handle->info.filename)
415 handle->info.filename = 0;
419 lt_dlloader loader = lt_dlloader_next (0);
420 const lt_dlvtable *loader_vtable;
425 loader_vtable = vtable;
427 loader_vtable = lt_dlloader_get (loader);
429 #ifdef LT_DEBUG_LOADERS
430 fprintf (stderr, "Calling %s->module_open (%s)\n",
431 (loader_vtable && loader_vtable->name) ? loader_vtable->name : "(null)",
432 filename ? filename : "(null)");
434 handle->module = (*loader_vtable->module_open) (loader_vtable->dlloader_data,
436 #ifdef LT_DEBUG_LOADERS
437 fprintf (stderr, " Result: %s\n",
438 handle->module ? "Success" : "Failed");
441 if (handle->module != 0)
445 handle->info.is_resident = advise->is_resident;
446 handle->info.is_symglobal = advise->is_symglobal;
447 handle->info.is_symlocal = advise->is_symlocal;
452 while (!vtable && (loader = lt_dlloader_next (loader)));
454 /* If VTABLE was given but couldn't open the module, or VTABLE wasn't
455 given but we exhausted all loaders without opening the module, bail
457 if ((vtable && !handle->module)
458 || (!vtable && !loader))
460 FREE (handle->info.filename);
465 handle->vtable = loader_vtable;
468 LT__SETERRORSTR (saved_error);
476 tryall_dlopen_module (lt_dlhandle *handle, const char *prefix,
477 const char *dirname, const char *dlname,
482 size_t filename_len = 0;
483 size_t dirname_len = LT_STRLEN (dirname);
488 #if defined(LT_DIRSEP_CHAR)
489 /* Only canonicalized names (i.e. with DIRSEP chars already converted)
490 should make it into this function: */
491 assert (strchr (dirname, LT_DIRSEP_CHAR) == 0);
495 if (dirname[dirname_len -1] == '/')
497 filename_len = dirname_len + 1 + LT_STRLEN (dlname);
499 /* Allocate memory, and combine DIRNAME and MODULENAME into it.
500 The PREFIX (if any) is handled below. */
501 filename = MALLOC (char, filename_len + 1);
505 sprintf (filename, "%.*s/%s", (int) dirname_len, dirname, dlname);
507 /* Now that we have combined DIRNAME and MODULENAME, if there is
508 also a PREFIX to contend with, simply recurse with the arguments
509 shuffled. Otherwise, attempt to open FILENAME as a module. */
512 error += tryall_dlopen_module (handle, (const char *) 0,
513 prefix, filename, advise);
515 else if (tryall_dlopen (handle, filename, advise, 0) != 0)
525 find_module (lt_dlhandle *handle, const char *dir, const char *libdir,
526 const char *dlname, const char *old_name, int installed,
529 /* Try to open the old library first; if it was dlpreopened,
530 we want the preopened version of it, even if a dlopenable
531 module is available. */
532 if (old_name && tryall_dlopen (handle, old_name,
533 advise, lt_dlloader_find ("lt_preopen") ) == 0)
538 /* Try to open the dynamic library. */
541 /* try to open the installed module */
542 if (installed && libdir)
544 if (tryall_dlopen_module (handle, (const char *) 0,
545 libdir, dlname, advise) == 0)
549 /* try to open the not-installed module */
552 if (tryall_dlopen_module (handle, dir, objdir,
553 dlname, advise) == 0)
557 /* maybe it was moved to another directory */
559 if (dir && (tryall_dlopen_module (handle, (const char *) 0,
560 dir, dlname, advise) == 0))
570 canonicalize_path (const char *path, char **pcanonical)
574 assert (path && *path);
577 canonical = MALLOC (char, 1+ LT_STRLEN (path));
584 for (src = 0; path[src] != LT_EOS_CHAR; ++src)
586 /* Path separators are not copied to the beginning or end of
587 the destination, or if another separator would follow
589 if (path[src] == LT_PATHSEP_CHAR)
592 || (path[1+ src] == LT_PATHSEP_CHAR)
593 || (path[1+ src] == LT_EOS_CHAR))
597 /* Anything other than a directory separator is copied verbatim. */
598 if ((path[src] != '/')
599 #if defined(LT_DIRSEP_CHAR)
600 && (path[src] != LT_DIRSEP_CHAR)
604 canonical[dest++] = path[src];
606 /* Directory separators are converted and copied only if they are
607 not at the end of a path -- i.e. before a path separator or
609 else if ((path[1+ src] != LT_PATHSEP_CHAR)
610 && (path[1+ src] != LT_EOS_CHAR)
611 #if defined(LT_DIRSEP_CHAR)
612 && (path[1+ src] != LT_DIRSEP_CHAR)
614 && (path[1+ src] != '/'))
616 canonical[dest++] = '/';
620 /* Add an end-of-string marker at the end. */
621 canonical[dest] = LT_EOS_CHAR;
624 /* Assign new value. */
625 *pcanonical = canonical;
631 argzize_path (const char *path, char **pargz, size_t *pargz_len)
639 if ((error = argz_create_sep (path, LT_PATHSEP_CHAR, pargz, pargz_len)))
644 LT__SETERROR (NO_MEMORY);
647 LT__SETERROR (UNKNOWN);
657 /* Repeatedly call FUNC with each LT_PATHSEP_CHAR delimited element
658 of SEARCH_PATH and references to DATA1 and DATA2, until FUNC returns
659 non-zero or all elements are exhausted. If BASE_NAME is non-NULL,
660 it is appended to each SEARCH_PATH element before FUNC is called. */
662 foreach_dirinpath (const char *search_path, const char *base_name,
663 foreach_callback_func *func, void *data1, void *data2)
666 size_t filenamesize = 0;
667 size_t lenbase = LT_STRLEN (base_name);
673 if (!search_path || !*search_path)
675 LT__SETERROR (FILE_NOT_FOUND);
679 if (canonicalize_path (search_path, &canonical) != 0)
682 if (argzize_path (canonical, &argz, &argz_len) != 0)
687 while ((dir_name = argz_next (argz, argz_len, dir_name)))
689 size_t lendir = LT_STRLEN (dir_name);
691 if (1+ lendir + lenbase >= filenamesize)
694 filenamesize = 1+ lendir + 1+ lenbase; /* "/d" + '/' + "f" + '\0' */
695 filename = MALLOC (char, filenamesize);
700 assert (filenamesize > lendir);
701 strcpy (filename, dir_name);
703 if (base_name && *base_name)
705 if (filename[lendir -1] != '/')
706 filename[lendir++] = '/';
707 strcpy (filename +lendir, base_name);
710 if ((result = (*func) (filename, data1, data2)))
725 /* If FILEPATH can be opened, store the name of the directory component
726 in DATA1, and the opened FILE* structure address in DATA2. Otherwise
727 DATA1 is unchanged, but DATA2 is set to a pointer to NULL. */
729 find_file_callback (char *filename, void *data1, void *data2)
731 char **pdir = (char **) data1;
732 FILE **pfile = (FILE **) data2;
735 assert (filename && *filename);
739 if ((*pfile = fopen (filename, LT_READTEXT_MODE)))
741 char *dirend = strrchr (filename, '/');
743 if (dirend > filename)
744 *dirend = LT_EOS_CHAR;
747 *pdir = lt__strdup (filename);
748 is_done = (*pdir == 0) ? -1 : 1;
755 find_file (const char *search_path, const char *base_name, char **pdir)
759 foreach_dirinpath (search_path, base_name, find_file_callback, pdir, &file);
765 find_handle_callback (char *filename, void *data, void *data2)
767 lt_dlhandle *phandle = (lt_dlhandle *) data;
768 int notfound = access (filename, R_OK);
769 lt_dladvise advise = (lt_dladvise) data2;
771 /* Bail out if file cannot be read... */
775 /* Try to dlopen the file, but do not continue searching in any
777 if (tryall_dlopen (phandle, filename, advise, 0) != 0)
783 /* If HANDLE was found return it, otherwise return 0. If HANDLE was
784 found but could not be opened, *HANDLE will be set to 0. */
786 find_handle (const char *search_path, const char *base_name,
787 lt_dlhandle *phandle, lt_dladvise advise)
792 if (!foreach_dirinpath (search_path, base_name, find_handle_callback,
799 #if !defined(LTDL_DLOPEN_DEPLIBS)
801 load_deplibs (lt_dlhandle handle, char * LT__UNUSED deplibs)
803 handle->depcount = 0;
807 #else /* defined(LTDL_DLOPEN_DEPLIBS) */
809 load_deplibs (lt_dlhandle handle, char *deplibs)
811 char *p, *save_search_path = 0;
817 handle->depcount = 0;
825 if (user_search_path)
827 save_search_path = lt__strdup (user_search_path);
828 if (!save_search_path)
832 /* extract search paths and count deplibs */
836 if (!isspace ((unsigned char) *p))
839 while (*end && !isspace((unsigned char) *end))
844 if (strncmp(p, "-L", 2) == 0 || strncmp(p, "-R", 2) == 0)
847 *end = 0; /* set a temporary string terminator */
848 if (lt_dladdsearchdir(p+2))
874 names = MALLOC (char *, depcount);
878 /* now only extract the actual deplibs */
883 if (isspace ((unsigned char) *p))
890 while (*end && !isspace ((unsigned char) *end))
895 if (strncmp(p, "-L", 2) != 0 && strncmp(p, "-R", 2) != 0)
899 *end = 0; /* set a temporary string terminator */
900 if (strncmp(p, "-l", 2) == 0)
902 size_t name_len = 3+ /* "lib" */ LT_STRLEN (p + 2);
903 name = MALLOC (char, 1+ name_len);
905 sprintf (name, "lib%s", p+2);
908 name = lt__strdup(p);
913 names[depcount++] = name;
920 /* load the deplibs (in reverse order)
921 At this stage, don't worry if the deplibs do not load correctly,
922 they may already be statically linked into the loading application
923 for instance. There will be a more enlightening error message
924 later on if the loaded module cannot resolve all of its symbols. */
927 lt_dlhandle cur = handle;
930 cur->deplibs = MALLOC (lt_dlhandle, depcount);
934 for (i = 0; i < depcount; ++i)
936 cur->deplibs[j] = lt_dlopenext(names[depcount-1-i]);
943 cur->depcount = j; /* Number of successfully loaded deplibs */
948 for (i = 0; i < depcount; ++i)
955 /* restore the old search path */
956 if (save_search_path) {
957 MEMREASSIGN (user_search_path, save_search_path);
962 #endif /* defined(LTDL_DLOPEN_DEPLIBS) */
965 unload_deplibs (lt_dlhandle handle)
969 lt_dlhandle cur = handle;
973 for (i = 0; i < cur->depcount; ++i)
975 if (!LT_DLIS_RESIDENT (cur->deplibs[i]))
977 errors += lt_dlclose (cur->deplibs[i]);
987 trim (char **dest, const char *str)
989 /* remove the leading and trailing "'" from str
990 and store the result in dest */
991 const char *end = strrchr (str, '\'');
992 size_t len = LT_STRLEN (str);
1000 if (len > 3 && str[0] == '\'')
1002 tmp = MALLOC (char, end - str);
1006 memcpy(tmp, &str[1], (end - str) - 1);
1007 tmp[(end - str) - 1] = LT_EOS_CHAR;
1018 /* Read the .la file FILE. */
1020 parse_dotla_file(FILE *file, char **dlname, char **libdir, char **deplibs,
1021 char **old_name, int *installed)
1024 size_t line_len = LT_FILENAME_MAX;
1025 char * line = MALLOC (char, line_len);
1029 LT__SETERROR (FILE_NOT_FOUND);
1033 while (!feof (file))
1035 line[line_len-2] = '\0';
1036 if (!fgets (line, (int) line_len, file))
1041 /* Handle the case where we occasionally need to read a line
1042 that is ssize_ter than the initial buffer size.
1043 Behave even if the file contains NUL bytes due to corruption. */
1044 while (line[line_len-2] != '\0' && line[line_len-2] != '\n' && !feof (file))
1046 line = REALLOC (char, line, line_len *2);
1052 line[line_len * 2 - 2] = '\0';
1053 if (!fgets (&line[line_len -1], (int) line_len +1, file))
1060 if (line[0] == '\n' || line[0] == '#')
1066 #define STR_DLNAME "dlname="
1067 if (strncmp (line, STR_DLNAME, sizeof (STR_DLNAME) - 1) == 0)
1069 errors += trim (dlname, &line[sizeof (STR_DLNAME) - 1]);
1072 #undef STR_OLD_LIBRARY
1073 #define STR_OLD_LIBRARY "old_library="
1074 else if (strncmp (line, STR_OLD_LIBRARY,
1075 sizeof (STR_OLD_LIBRARY) - 1) == 0)
1077 errors += trim (old_name, &line[sizeof (STR_OLD_LIBRARY) - 1]);
1080 #define STR_LIBDIR "libdir="
1081 else if (strncmp (line, STR_LIBDIR, sizeof (STR_LIBDIR) - 1) == 0)
1083 errors += trim (libdir, &line[sizeof(STR_LIBDIR) - 1]);
1086 #undef STR_DL_DEPLIBS
1087 #define STR_DL_DEPLIBS "dependency_libs="
1088 else if (strncmp (line, STR_DL_DEPLIBS,
1089 sizeof (STR_DL_DEPLIBS) - 1) == 0)
1091 errors += trim (deplibs, &line[sizeof (STR_DL_DEPLIBS) - 1]);
1093 else if (streq (line, "installed=yes\n"))
1097 else if (streq (line, "installed=no\n"))
1102 #undef STR_LIBRARY_NAMES
1103 #define STR_LIBRARY_NAMES "library_names="
1104 else if (!*dlname && strncmp (line, STR_LIBRARY_NAMES,
1105 sizeof (STR_LIBRARY_NAMES) - 1) == 0)
1108 errors += trim (dlname, &line[sizeof (STR_LIBRARY_NAMES) - 1]);
1111 && (last_libname = strrchr (*dlname, ' ')) != 0)
1113 last_libname = lt__strdup (last_libname + 1);
1119 MEMREASSIGN (*dlname, last_libname);
1132 /* Try to open FILENAME as a module. */
1134 try_dlopen (lt_dlhandle *phandle, const char *filename, const char *ext,
1137 const char * saved_error = 0;
1138 char * archive_name = 0;
1139 char * canonical = 0;
1140 char * base_name = 0;
1145 lt_dlhandle newhandle;
1148 assert (*phandle == 0);
1150 #ifdef LT_DEBUG_LOADERS
1151 fprintf (stderr, "try_dlopen (%s, %s)\n",
1152 filename ? filename : "(null)",
1153 ext ? ext : "(null)");
1156 LT__GETERROR (saved_error);
1161 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1165 newhandle = *phandle;
1167 /* lt_dlclose()ing yourself is very bad! Disallow it. */
1168 newhandle->info.is_resident = 1;
1170 if (tryall_dlopen (&newhandle, 0, advise, 0) != 0)
1176 goto register_handle;
1179 assert (filename && *filename);
1183 attempt = MALLOC (char, LT_STRLEN (filename) + LT_STRLEN (ext) + 1);
1187 sprintf(attempt, "%s%s", filename, ext);
1191 attempt = lt__strdup (filename);
1196 /* Doing this immediately allows internal functions to safely
1197 assume only canonicalized paths are passed. */
1198 if (canonicalize_path (attempt, &canonical) != 0)
1204 /* If the canonical module name is a path (relative or absolute)
1205 then split it into a directory part and a name part. */
1206 base_name = strrchr (canonical, '/');
1209 size_t dirlen = (1+ base_name) - canonical;
1211 dir = MALLOC (char, 1+ dirlen);
1218 strncpy (dir, canonical, dirlen);
1219 dir[dirlen] = LT_EOS_CHAR;
1224 MEMREASSIGN (base_name, canonical);
1226 assert (base_name && *base_name);
1228 ext = strrchr (base_name, '.');
1231 ext = base_name + LT_STRLEN (base_name);
1234 /* extract the module name from the file name */
1235 name = MALLOC (char, ext - base_name + 1);
1242 /* canonicalize the module name */
1245 for (i = 0; i < ext - base_name; ++i)
1247 if (isalnum ((unsigned char)(base_name[i])))
1249 name[i] = base_name[i];
1256 name[ext - base_name] = LT_EOS_CHAR;
1259 /* Before trawling through the filesystem in search of a module,
1260 check whether we are opening a preloaded module. */
1263 const lt_dlvtable *vtable = lt_dlloader_find ("lt_preopen");
1267 /* name + "." + libext + NULL */
1268 archive_name = MALLOC (char, LT_STRLEN (name) + LT_STRLEN (libext) + 2);
1269 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1271 if ((*phandle == NULL) || (archive_name == NULL))
1276 newhandle = *phandle;
1278 /* Preloaded modules are always named according to their old
1280 sprintf (archive_name, "%s.%s", name, libext);
1282 if (tryall_dlopen (&newhandle, archive_name, advise, vtable) == 0)
1284 goto register_handle;
1287 /* If we're still here, there was no matching preloaded module,
1288 so put things back as we found them, and continue searching. */
1294 /* If we are allowing only preloaded modules, and we didn't find
1295 anything yet, give up on the search here. */
1296 if (advise && advise->try_preload_only)
1301 /* Check whether we are opening a libtool module (.la extension). */
1302 if (ext && streq (ext, archive_ext))
1304 /* this seems to be a libtool module */
1307 char * old_name = 0;
1311 /* if we can't find the installed flag, it is probably an
1312 installed libtool archive, produced with an old version
1316 /* Now try to open the .la file. If there is no directory name
1317 component, try to find it first in user_search_path and then other
1318 prescribed paths. Otherwise (or in any case if the module was not
1319 yet found) try opening just the module name as passed. */
1322 const char *search_path = user_search_path;
1325 file = find_file (user_search_path, base_name, &dir);
1329 search_path = getenv (LTDL_SEARCHPATH_VAR);
1331 file = find_file (search_path, base_name, &dir);
1334 #if defined(LT_MODULE_PATH_VAR)
1337 search_path = getenv (LT_MODULE_PATH_VAR);
1339 file = find_file (search_path, base_name, &dir);
1342 #if defined(LT_DLSEARCH_PATH)
1343 if (!file && *sys_dlsearch_path)
1345 file = find_file (sys_dlsearch_path, base_name, &dir);
1351 file = fopen (attempt, LT_READTEXT_MODE);
1354 /* If we didn't find the file by now, it really isn't there. Set
1355 the status flag, and bail out. */
1358 LT__SETERROR (FILE_NOT_FOUND);
1363 /* read the .la file */
1364 if (parse_dotla_file(file, &dlname, &libdir, &deplibs,
1365 &old_name, &installed) != 0)
1370 /* allocate the handle */
1371 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1387 if (load_deplibs (*phandle, deplibs) == 0)
1389 newhandle = *phandle;
1390 /* find_module may replace newhandle */
1391 if (find_module (&newhandle, dir, libdir, dlname, old_name,
1394 unload_deplibs (*phandle);
1414 if (*phandle != newhandle)
1416 unload_deplibs (*phandle);
1421 /* not a libtool module */
1422 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1429 newhandle = *phandle;
1431 /* If the module has no directory name component, try to find it
1432 first in user_search_path and then other prescribed paths.
1433 Otherwise (or in any case if the module was not yet found) try
1434 opening just the module name as passed. */
1435 if ((dir || (!find_handle (user_search_path, base_name,
1437 && !find_handle (getenv (LTDL_SEARCHPATH_VAR), base_name,
1439 #if defined(LT_MODULE_PATH_VAR)
1440 && !find_handle (getenv (LT_MODULE_PATH_VAR), base_name,
1443 #if defined(LT_DLSEARCH_PATH)
1444 && !find_handle (sys_dlsearch_path, base_name,
1449 if (tryall_dlopen (&newhandle, attempt, advise, 0) != 0)
1464 MEMREASSIGN (*phandle, newhandle);
1466 if ((*phandle)->info.ref_count == 0)
1468 (*phandle)->info.ref_count = 1;
1469 MEMREASSIGN ((*phandle)->info.name, name);
1471 (*phandle)->next = handles;
1475 LT__SETERRORSTR (saved_error);
1481 if (!canonical) /* was MEMREASSIGNed */
1484 FREE (archive_name);
1490 /* If the last error messge store was `FILE_NOT_FOUND', then return
1493 file_not_found (void)
1495 const char *error = 0;
1497 LT__GETERROR (error);
1498 if (error == LT__STRERROR (FILE_NOT_FOUND))
1505 /* Unless FILENAME already bears a suitable library extension, then
1508 has_library_ext (const char *filename)
1514 ext = (char *) strrchr (filename, '.');
1516 if (ext && ((streq (ext, archive_ext))
1517 #if defined(LT_MODULE_EXT)
1518 || (streq (ext, shlib_ext))
1529 /* Initialise and configure a user lt_dladvise opaque object. */
1532 lt_dladvise_init (lt_dladvise *padvise)
1534 lt_dladvise advise = (lt_dladvise) lt__zalloc (sizeof (struct lt__advise));
1536 return (advise ? 0 : 1);
1540 lt_dladvise_destroy (lt_dladvise *padvise)
1548 lt_dladvise_ext (lt_dladvise *padvise)
1550 assert (padvise && *padvise);
1551 (*padvise)->try_ext = 1;
1556 lt_dladvise_resident (lt_dladvise *padvise)
1558 assert (padvise && *padvise);
1559 (*padvise)->is_resident = 1;
1564 lt_dladvise_local (lt_dladvise *padvise)
1566 assert (padvise && *padvise);
1567 (*padvise)->is_symlocal = 1;
1572 lt_dladvise_global (lt_dladvise *padvise)
1574 assert (padvise && *padvise);
1575 (*padvise)->is_symglobal = 1;
1580 lt_dladvise_preload (lt_dladvise *padvise)
1582 assert (padvise && *padvise);
1583 (*padvise)->try_preload_only = 1;
1587 /* Libtool-1.5.x interface for loading a new module named FILENAME. */
1589 lt_dlopen (const char *filename)
1591 return lt_dlopenadvise (filename, NULL);
1595 /* If FILENAME has an ARCHIVE_EXT or MODULE_EXT extension, try to
1596 open the FILENAME as passed. Otherwise try appending ARCHIVE_EXT,
1597 and if a file is still not found try again with MODULE_EXT appended
1600 lt_dlopenext (const char *filename)
1602 lt_dlhandle handle = 0;
1605 if (!lt_dladvise_init (&advise) && !lt_dladvise_ext (&advise))
1606 handle = lt_dlopenadvise (filename, advise);
1608 lt_dladvise_destroy (&advise);
1614 lt_dlopenadvise (const char *filename, lt_dladvise advise)
1616 lt_dlhandle handle = 0;
1619 /* Can't have symbols hidden and visible at the same time! */
1620 if (advise && advise->is_symlocal && advise->is_symglobal)
1622 LT__SETERROR (CONFLICTING_FLAGS);
1629 || has_library_ext (filename))
1631 /* Just incase we missed a code path in try_dlopen() that reports
1632 an error, but forgot to reset handle... */
1633 if (try_dlopen (&handle, filename, NULL, advise) != 0)
1638 else if (filename && *filename)
1641 /* First try appending ARCHIVE_EXT. */
1642 errors += try_dlopen (&handle, filename, archive_ext, advise);
1644 /* If we found FILENAME, stop searching -- whether we were able to
1645 load the file as a module or not. If the file exists but loading
1646 failed, it is better to return an error message here than to
1647 report FILE_NOT_FOUND when the alternatives (foo.so etc) are not
1648 in the module search path. */
1649 if (handle || ((errors > 0) && !file_not_found ()))
1652 #if defined(LT_MODULE_EXT)
1653 /* Try appending SHLIB_EXT. */
1654 errors = try_dlopen (&handle, filename, shlib_ext, advise);
1656 /* As before, if the file was found but loading failed, return now
1657 with the current error message. */
1658 if (handle || ((errors > 0) && !file_not_found ()))
1663 /* Still here? Then we really did fail to locate any of the file
1665 LT__SETERROR (FILE_NOT_FOUND);
1671 lt_argz_insert (char **pargz, size_t *pargz_len, char *before,
1676 /* Prior to Sep 8, 2005, newlib had a bug where argz_insert(pargz,
1677 pargz_len, NULL, entry) failed with EINVAL. */
1679 error = argz_insert (pargz, pargz_len, before, entry);
1681 error = argz_append (pargz, pargz_len, entry, 1 + strlen (entry));
1688 LT__SETERROR (NO_MEMORY);
1691 LT__SETERROR (UNKNOWN);
1701 lt_argz_insertinorder (char **pargz, size_t *pargz_len, const char *entry)
1707 assert (entry && *entry);
1710 while ((before = argz_next (*pargz, *pargz_len, before)))
1712 int cmp = strcmp (entry, before);
1715 if (cmp == 0) return 0; /* No duplicates! */
1718 return lt_argz_insert (pargz, pargz_len, before, entry);
1722 lt_argz_insertdir (char **pargz, size_t *pargz_len, const char *dirnam,
1728 size_t end_offset = 0;
1736 dir_len = LT_STRLEN (dirnam);
1737 end = dp->d_name + D_NAMLEN(dp);
1739 /* Ignore version numbers. */
1742 for (p = end; p -1 > dp->d_name; --p)
1743 if (strchr (".0123456789", p[-1]) == 0)
1750 /* Ignore filename extension. */
1753 for (p = end -1; p > dp->d_name; --p)
1761 /* Prepend the directory name. */
1762 end_offset = end - dp->d_name;
1763 buf_len = dir_len + 1+ end_offset;
1764 buf = MALLOC (char, 1+ buf_len);
1770 strcpy (buf, dirnam);
1772 strncat (buf, dp->d_name, end_offset);
1773 buf[buf_len] = LT_EOS_CHAR;
1775 /* Try to insert (in order) into ARGZ/ARGZ_LEN. */
1776 if (lt_argz_insertinorder (pargz, pargz_len, buf) != 0)
1785 list_files_by_dir (const char *dirnam, char **pargz, size_t *pargz_len)
1790 assert (dirnam && *dirnam);
1793 assert (dirnam[LT_STRLEN(dirnam) -1] != '/');
1795 dirp = opendir (dirnam);
1798 struct dirent *dp = 0;
1800 while ((dp = readdir (dirp)))
1801 if (dp->d_name[0] != '.')
1802 if (lt_argz_insertdir (pargz, pargz_len, dirnam, dp))
1817 /* If there are any files in DIRNAME, call the function passed in
1818 DATA1 (with the name of each file and DATA2 as arguments). */
1820 foreachfile_callback (char *dirname, void *data1, void *data2)
1822 file_worker_func *func = *(file_worker_func **) data1;
1826 size_t argz_len = 0;
1828 if (list_files_by_dir (dirname, &argz, &argz_len) != 0)
1835 while ((filename = argz_next (argz, argz_len, filename)))
1836 if ((is_done = (*func) (filename, data2)))
1847 /* Call FUNC for each unique extensionless file in SEARCH_PATH, along
1848 with DATA. The filenames passed to FUNC would be suitable for
1849 passing to lt_dlopenext. The extensions are stripped so that
1850 individual modules do not generate several entries (e.g. libfoo.la,
1851 libfoo.so, libfoo.so.1, libfoo.so.1.0.0). If SEARCH_PATH is NULL,
1852 then the same directories that lt_dlopen would search are examined. */
1854 lt_dlforeachfile (const char *search_path,
1855 int (*func) (const char *filename, void *data),
1859 file_worker_func **fpptr = &func;
1863 /* If a specific path was passed, search only the directories
1865 is_done = foreach_dirinpath (search_path, 0,
1866 foreachfile_callback, fpptr, data);
1870 /* Otherwise search the default paths. */
1871 is_done = foreach_dirinpath (user_search_path, 0,
1872 foreachfile_callback, fpptr, data);
1875 is_done = foreach_dirinpath (getenv(LTDL_SEARCHPATH_VAR), 0,
1876 foreachfile_callback, fpptr, data);
1879 #if defined(LT_MODULE_PATH_VAR)
1882 is_done = foreach_dirinpath (getenv(LT_MODULE_PATH_VAR), 0,
1883 foreachfile_callback, fpptr, data);
1886 #if defined(LT_DLSEARCH_PATH)
1887 if (!is_done && *sys_dlsearch_path)
1889 is_done = foreach_dirinpath (sys_dlsearch_path, 0,
1890 foreachfile_callback, fpptr, data);
1899 lt_dlclose (lt_dlhandle handle)
1901 lt_dlhandle cur, last;
1904 /* check whether the handle is valid */
1905 last = cur = handles;
1906 while (cur && handle != cur)
1914 LT__SETERROR (INVALID_HANDLE);
1920 cur->info.ref_count--;
1922 /* Note that even with resident modules, we must track the ref_count
1923 correctly incase the user decides to reset the residency flag
1924 later (even though the API makes no provision for that at the
1926 if (cur->info.ref_count <= 0 && !LT_DLIS_RESIDENT (cur))
1928 lt_user_data data = cur->vtable->dlloader_data;
1932 last->next = cur->next;
1936 handles = cur->next;
1939 errors += cur->vtable->module_close (data, cur->module);
1940 errors += unload_deplibs (handle);
1942 /* It is up to the callers to free the data itself. */
1943 FREE (cur->interface_data);
1945 FREE (cur->info.filename);
1946 FREE (cur->info.name);
1952 if (LT_DLIS_RESIDENT (handle))
1954 LT__SETERROR (CLOSE_RESIDENT_MODULE);
1963 lt_dlsym (lt_dlhandle place, const char *symbol)
1966 char lsym[LT_SYMBOL_LENGTH];
1974 LT__SETERROR (INVALID_HANDLE);
1982 LT__SETERROR (SYMBOL_NOT_FOUND);
1986 lensym = LT_STRLEN (symbol) + LT_STRLEN (handle->vtable->sym_prefix)
1987 + LT_STRLEN (handle->info.name);
1989 if (lensym + LT_SYMBOL_OVERHEAD < LT_SYMBOL_LENGTH)
1995 sym = MALLOC (char, lensym + LT_SYMBOL_OVERHEAD + 1);
1998 LT__SETERROR (BUFFER_OVERFLOW);
2003 data = handle->vtable->dlloader_data;
2004 if (handle->info.name)
2006 const char *saved_error;
2008 LT__GETERROR (saved_error);
2010 /* this is a libtool module */
2011 if (handle->vtable->sym_prefix)
2013 strcpy(sym, handle->vtable->sym_prefix);
2014 strcat(sym, handle->info.name);
2018 strcpy(sym, handle->info.name);
2021 strcat(sym, "_LTX_");
2022 strcat(sym, symbol);
2024 /* try "modulename_LTX_symbol" */
2025 address = handle->vtable->find_sym (data, handle->module, sym);
2034 LT__SETERRORSTR (saved_error);
2037 /* otherwise try "symbol" */
2038 if (handle->vtable->sym_prefix)
2040 strcpy(sym, handle->vtable->sym_prefix);
2041 strcat(sym, symbol);
2045 strcpy(sym, symbol);
2048 address = handle->vtable->find_sym (data, handle->module, sym);
2062 LT__GETERROR (error);
2063 LT__SETERRORSTR (0);
2065 return error ? error : NULL;
2069 lt_dlpath_insertdir (char **ppath, char *before, const char *dir)
2072 char *canonical = 0;
2074 size_t argz_len = 0;
2077 assert (dir && *dir);
2079 if (canonicalize_path (dir, &canonical) != 0)
2085 assert (canonical && *canonical);
2087 /* If *PPATH is empty, set it to DIR. */
2090 assert (!before); /* BEFORE cannot be set without PPATH. */
2091 assert (dir); /* Without DIR, don't call this function! */
2093 *ppath = lt__strdup (dir);
2100 assert (ppath && *ppath);
2102 if (argzize_path (*ppath, &argz, &argz_len) != 0)
2108 /* Convert BEFORE into an equivalent offset into ARGZ. This only works
2109 if *PPATH is already canonicalized, and hence does not change length
2110 with respect to ARGZ. We canonicalize each entry as it is added to
2111 the search path, and don't call this function with (uncanonicalized)
2112 user paths, so this is a fair assumption. */
2115 assert (*ppath <= before);
2116 assert ((int) (before - *ppath) <= (int) strlen (*ppath));
2118 before = before - *ppath + argz;
2121 if (lt_argz_insert (&argz, &argz_len, before, dir) != 0)
2127 argz_stringify (argz, argz_len, LT_PATHSEP_CHAR);
2128 MEMREASSIGN(*ppath, argz);
2138 lt_dladdsearchdir (const char *search_dir)
2142 if (search_dir && *search_dir)
2144 if (lt_dlpath_insertdir (&user_search_path, 0, search_dir) != 0)
2152 lt_dlinsertsearchdir (const char *before, const char *search_dir)
2158 if ((before < user_search_path)
2159 || (before >= user_search_path + LT_STRLEN (user_search_path)))
2161 LT__SETERROR (INVALID_POSITION);
2166 if (search_dir && *search_dir)
2168 if (lt_dlpath_insertdir (&user_search_path,
2169 (char *) before, search_dir) != 0)
2179 lt_dlsetsearchpath (const char *search_path)
2183 FREE (user_search_path);
2185 if (!search_path || !LT_STRLEN (search_path))
2190 if (canonicalize_path (search_path, &user_search_path) != 0)
2197 lt_dlgetsearchpath (void)
2199 const char *saved_path;
2201 saved_path = user_search_path;
2207 lt_dlmakeresident (lt_dlhandle handle)
2213 LT__SETERROR (INVALID_HANDLE);
2218 handle->info.is_resident = 1;
2225 lt_dlisresident (lt_dlhandle handle)
2229 LT__SETERROR (INVALID_HANDLE);
2233 return LT_DLIS_RESIDENT (handle);
2238 /* --- MODULE INFORMATION --- */
2241 const char *id_string;
2242 lt_dlhandle_interface *iface;
2246 lt_dlinterface_register (const char *id_string, lt_dlhandle_interface *iface)
2248 lt__interface_id *interface_id = (lt__interface_id *) lt__malloc (sizeof *interface_id);
2250 /* If lt__malloc fails, it will LT__SETERROR (NO_MEMORY), which
2251 can then be detected with lt_dlerror() if we return 0. */
2254 interface_id->id_string = lt__strdup (id_string);
2255 if (!interface_id->id_string)
2256 FREE (interface_id);
2258 interface_id->iface = iface;
2261 return (lt_dlinterface_id) interface_id;
2264 void lt_dlinterface_free (lt_dlinterface_id key)
2266 lt__interface_id *interface_id = (lt__interface_id *)key;
2267 FREE (interface_id->id_string);
2268 FREE (interface_id);
2272 lt_dlcaller_set_data (lt_dlinterface_id key, lt_dlhandle handle, void *data)
2275 void *stale = (void *) 0;
2276 lt_dlhandle cur = handle;
2279 if (cur->interface_data)
2280 while (cur->interface_data[n_elements].key)
2283 for (i = 0; i < n_elements; ++i)
2285 if (cur->interface_data[i].key == key)
2287 stale = cur->interface_data[i].data;
2292 /* Ensure that there is enough room in this handle's interface_data
2293 array to accept a new element (and an empty end marker). */
2294 if (i == n_elements)
2296 lt_interface_data *temp
2297 = REALLOC (lt_interface_data, cur->interface_data, 2+ n_elements);
2305 cur->interface_data = temp;
2307 /* We only need this if we needed to allocate a new interface_data. */
2308 cur->interface_data[i].key = key;
2309 cur->interface_data[1+ i].key = 0;
2312 cur->interface_data[i].data = data;
2319 lt_dlcaller_get_data (lt_dlinterface_id key, lt_dlhandle handle)
2321 void *result = (void *) 0;
2322 lt_dlhandle cur = handle;
2324 /* Locate the index of the element with a matching KEY. */
2325 if (cur->interface_data)
2328 for (i = 0; cur->interface_data[i].key; ++i)
2330 if (cur->interface_data[i].key == key)
2332 result = cur->interface_data[i].data;
2342 lt_dlgetinfo (lt_dlhandle handle)
2346 LT__SETERROR (INVALID_HANDLE);
2350 return &(handle->info);
2355 lt_dlhandle_iterate (lt_dlinterface_id iface, lt_dlhandle place)
2357 lt_dlhandle handle = place;
2358 lt__interface_id *iterator = (lt__interface_id *) iface;
2360 assert (iface); /* iface is a required argument */
2365 handle = handle->next;
2367 /* advance while the interface check fails */
2368 while (handle && iterator->iface
2369 && ((*iterator->iface) (handle, iterator->id_string) != 0))
2371 handle = handle->next;
2379 lt_dlhandle_fetch (lt_dlinterface_id iface, const char *module_name)
2381 lt_dlhandle handle = 0;
2383 assert (iface); /* iface is a required argument */
2385 while ((handle = lt_dlhandle_iterate (iface, handle)))
2387 lt_dlhandle cur = handle;
2388 if (cur && cur->info.name && streq (cur->info.name, module_name))
2397 lt_dlhandle_map (lt_dlinterface_id iface,
2398 int (*func) (lt_dlhandle handle, void *data), void *data)
2400 lt__interface_id *iterator = (lt__interface_id *) iface;
2401 lt_dlhandle cur = handles;
2403 assert (iface); /* iface is a required argument */
2409 /* advance while the interface check fails */
2410 while (cur && iterator->iface
2411 && ((*iterator->iface) (cur, iterator->id_string) != 0))
2416 if ((errorcode = (*func) (cur, data)) != 0)