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);
997 if (!end || end == 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 longer 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) + 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 message stored 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)
1510 const char * ext = 0;
1514 ext = 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;
1618 const char * saved_error = 0;
1620 LT__GETERROR (saved_error);
1622 /* Can't have symbols hidden and visible at the same time! */
1623 if (advise && advise->is_symlocal && advise->is_symglobal)
1625 LT__SETERROR (CONFLICTING_FLAGS);
1632 || has_library_ext (filename))
1634 /* Just incase we missed a code path in try_dlopen() that reports
1635 an error, but forgot to reset handle... */
1636 if (try_dlopen (&handle, filename, NULL, advise) != 0)
1641 else if (filename && *filename)
1644 /* First try appending ARCHIVE_EXT. */
1645 errors += try_dlopen (&handle, filename, archive_ext, advise);
1647 /* If we found FILENAME, stop searching -- whether we were able to
1648 load the file as a module or not. If the file exists but loading
1649 failed, it is better to return an error message here than to
1650 report FILE_NOT_FOUND when the alternatives (foo.so etc) are not
1651 in the module search path. */
1652 if (handle || ((errors > 0) && !file_not_found ()))
1655 #if defined(LT_MODULE_EXT)
1656 /* Try appending SHLIB_EXT. */
1657 LT__SETERRORSTR (saved_error);
1658 errors = try_dlopen (&handle, filename, shlib_ext, advise);
1660 /* As before, if the file was found but loading failed, return now
1661 with the current error message. */
1662 if (handle || ((errors > 0) && !file_not_found ()))
1667 /* Still here? Then we really did fail to locate any of the file
1669 LT__SETERROR (FILE_NOT_FOUND);
1675 lt_argz_insert (char **pargz, size_t *pargz_len, char *before,
1680 /* Prior to Sep 8, 2005, newlib had a bug where argz_insert(pargz,
1681 pargz_len, NULL, entry) failed with EINVAL. */
1683 error = argz_insert (pargz, pargz_len, before, entry);
1685 error = argz_append (pargz, pargz_len, entry, 1 + strlen (entry));
1692 LT__SETERROR (NO_MEMORY);
1695 LT__SETERROR (UNKNOWN);
1705 lt_argz_insertinorder (char **pargz, size_t *pargz_len, const char *entry)
1711 assert (entry && *entry);
1714 while ((before = argz_next (*pargz, *pargz_len, before)))
1716 int cmp = strcmp (entry, before);
1719 if (cmp == 0) return 0; /* No duplicates! */
1722 return lt_argz_insert (pargz, pargz_len, before, entry);
1726 lt_argz_insertdir (char **pargz, size_t *pargz_len, const char *dirnam,
1732 size_t end_offset = 0;
1740 dir_len = LT_STRLEN (dirnam);
1741 end = dp->d_name + D_NAMLEN(dp);
1743 /* Ignore version numbers. */
1746 for (p = end; p -1 > dp->d_name; --p)
1747 if (strchr (".0123456789", p[-1]) == 0)
1754 /* Ignore filename extension. */
1757 for (p = end -1; p > dp->d_name; --p)
1765 /* Prepend the directory name. */
1766 end_offset = end - dp->d_name;
1767 buf_len = dir_len + 1+ end_offset;
1768 buf = MALLOC (char, 1+ buf_len);
1774 strcpy (buf, dirnam);
1776 strncat (buf, dp->d_name, end_offset);
1777 buf[buf_len] = LT_EOS_CHAR;
1779 /* Try to insert (in order) into ARGZ/ARGZ_LEN. */
1780 if (lt_argz_insertinorder (pargz, pargz_len, buf) != 0)
1789 list_files_by_dir (const char *dirnam, char **pargz, size_t *pargz_len)
1794 assert (dirnam && *dirnam);
1797 assert (dirnam[LT_STRLEN(dirnam) -1] != '/');
1799 dirp = opendir (dirnam);
1802 struct dirent *dp = 0;
1804 while ((dp = readdir (dirp)))
1805 if (dp->d_name[0] != '.')
1806 if (lt_argz_insertdir (pargz, pargz_len, dirnam, dp))
1821 /* If there are any files in DIRNAME, call the function passed in
1822 DATA1 (with the name of each file and DATA2 as arguments). */
1824 foreachfile_callback (char *dirname, void *data1, void *data2)
1826 file_worker_func *func = *(file_worker_func **) data1;
1830 size_t argz_len = 0;
1832 if (list_files_by_dir (dirname, &argz, &argz_len) != 0)
1839 while ((filename = argz_next (argz, argz_len, filename)))
1840 if ((is_done = (*func) (filename, data2)))
1851 /* Call FUNC for each unique extensionless file in SEARCH_PATH, along
1852 with DATA. The filenames passed to FUNC would be suitable for
1853 passing to lt_dlopenext. The extensions are stripped so that
1854 individual modules do not generate several entries (e.g. libfoo.la,
1855 libfoo.so, libfoo.so.1, libfoo.so.1.0.0). If SEARCH_PATH is NULL,
1856 then the same directories that lt_dlopen would search are examined. */
1858 lt_dlforeachfile (const char *search_path,
1859 int (*func) (const char *filename, void *data),
1863 file_worker_func **fpptr = &func;
1867 /* If a specific path was passed, search only the directories
1869 is_done = foreach_dirinpath (search_path, 0,
1870 foreachfile_callback, fpptr, data);
1874 /* Otherwise search the default paths. */
1875 is_done = foreach_dirinpath (user_search_path, 0,
1876 foreachfile_callback, fpptr, data);
1879 is_done = foreach_dirinpath (getenv(LTDL_SEARCHPATH_VAR), 0,
1880 foreachfile_callback, fpptr, data);
1883 #if defined(LT_MODULE_PATH_VAR)
1886 is_done = foreach_dirinpath (getenv(LT_MODULE_PATH_VAR), 0,
1887 foreachfile_callback, fpptr, data);
1890 #if defined(LT_DLSEARCH_PATH)
1891 if (!is_done && *sys_dlsearch_path)
1893 is_done = foreach_dirinpath (sys_dlsearch_path, 0,
1894 foreachfile_callback, fpptr, data);
1903 lt_dlclose (lt_dlhandle handle)
1905 lt_dlhandle cur, last;
1908 /* check whether the handle is valid */
1909 last = cur = handles;
1910 while (cur && handle != cur)
1918 LT__SETERROR (INVALID_HANDLE);
1924 cur->info.ref_count--;
1926 /* Note that even with resident modules, we must track the ref_count
1927 correctly incase the user decides to reset the residency flag
1928 later (even though the API makes no provision for that at the
1930 if (cur->info.ref_count <= 0 && !LT_DLIS_RESIDENT (cur))
1932 lt_user_data data = cur->vtable->dlloader_data;
1936 last->next = cur->next;
1940 handles = cur->next;
1943 errors += cur->vtable->module_close (data, cur->module);
1944 errors += unload_deplibs (handle);
1946 /* It is up to the callers to free the data itself. */
1947 FREE (cur->interface_data);
1949 FREE (cur->info.filename);
1950 FREE (cur->info.name);
1956 if (LT_DLIS_RESIDENT (handle))
1958 LT__SETERROR (CLOSE_RESIDENT_MODULE);
1967 lt_dlsym (lt_dlhandle place, const char *symbol)
1970 char lsym[LT_SYMBOL_LENGTH];
1978 LT__SETERROR (INVALID_HANDLE);
1986 LT__SETERROR (SYMBOL_NOT_FOUND);
1990 lensym = LT_STRLEN (symbol) + LT_STRLEN (handle->vtable->sym_prefix)
1991 + LT_STRLEN (handle->info.name);
1993 if (lensym + LT_SYMBOL_OVERHEAD < LT_SYMBOL_LENGTH)
1999 sym = MALLOC (char, lensym + LT_SYMBOL_OVERHEAD + 1);
2002 LT__SETERROR (BUFFER_OVERFLOW);
2007 data = handle->vtable->dlloader_data;
2008 if (handle->info.name)
2010 const char *saved_error;
2012 LT__GETERROR (saved_error);
2014 /* this is a libtool module */
2015 if (handle->vtable->sym_prefix)
2017 strcpy(sym, handle->vtable->sym_prefix);
2018 strcat(sym, handle->info.name);
2022 strcpy(sym, handle->info.name);
2025 strcat(sym, "_LTX_");
2026 strcat(sym, symbol);
2028 /* try "modulename_LTX_symbol" */
2029 address = handle->vtable->find_sym (data, handle->module, sym);
2038 LT__SETERRORSTR (saved_error);
2041 /* otherwise try "symbol" */
2042 if (handle->vtable->sym_prefix)
2044 strcpy(sym, handle->vtable->sym_prefix);
2045 strcat(sym, symbol);
2049 strcpy(sym, symbol);
2052 address = handle->vtable->find_sym (data, handle->module, sym);
2066 LT__GETERROR (error);
2067 LT__SETERRORSTR (0);
2073 lt_dlpath_insertdir (char **ppath, char *before, const char *dir)
2076 char *canonical = 0;
2078 size_t argz_len = 0;
2081 assert (dir && *dir);
2083 if (canonicalize_path (dir, &canonical) != 0)
2089 assert (canonical && *canonical);
2091 /* If *PPATH is empty, set it to DIR. */
2094 assert (!before); /* BEFORE cannot be set without PPATH. */
2095 assert (dir); /* Without DIR, don't call this function! */
2097 *ppath = lt__strdup (dir);
2104 assert (ppath && *ppath);
2106 if (argzize_path (*ppath, &argz, &argz_len) != 0)
2112 /* Convert BEFORE into an equivalent offset into ARGZ. This only works
2113 if *PPATH is already canonicalized, and hence does not change length
2114 with respect to ARGZ. We canonicalize each entry as it is added to
2115 the search path, and don't call this function with (uncanonicalized)
2116 user paths, so this is a fair assumption. */
2119 assert (*ppath <= before);
2120 assert ((int) (before - *ppath) <= (int) strlen (*ppath));
2122 before = before - *ppath + argz;
2125 if (lt_argz_insert (&argz, &argz_len, before, dir) != 0)
2131 argz_stringify (argz, argz_len, LT_PATHSEP_CHAR);
2132 MEMREASSIGN(*ppath, argz);
2142 lt_dladdsearchdir (const char *search_dir)
2146 if (search_dir && *search_dir)
2148 if (lt_dlpath_insertdir (&user_search_path, 0, search_dir) != 0)
2156 lt_dlinsertsearchdir (const char *before, const char *search_dir)
2162 if ((before < user_search_path)
2163 || (before >= user_search_path + LT_STRLEN (user_search_path)))
2165 LT__SETERROR (INVALID_POSITION);
2170 if (search_dir && *search_dir)
2172 if (lt_dlpath_insertdir (&user_search_path,
2173 (char *) before, search_dir) != 0)
2183 lt_dlsetsearchpath (const char *search_path)
2187 FREE (user_search_path);
2189 if (!search_path || !LT_STRLEN (search_path))
2194 if (canonicalize_path (search_path, &user_search_path) != 0)
2201 lt_dlgetsearchpath (void)
2203 const char *saved_path;
2205 saved_path = user_search_path;
2211 lt_dlmakeresident (lt_dlhandle handle)
2217 LT__SETERROR (INVALID_HANDLE);
2222 handle->info.is_resident = 1;
2229 lt_dlisresident (lt_dlhandle handle)
2233 LT__SETERROR (INVALID_HANDLE);
2237 return LT_DLIS_RESIDENT (handle);
2242 /* --- MODULE INFORMATION --- */
2245 const char *id_string;
2246 lt_dlhandle_interface *iface;
2250 lt_dlinterface_register (const char *id_string, lt_dlhandle_interface *iface)
2252 lt__interface_id *interface_id = (lt__interface_id *) lt__malloc (sizeof *interface_id);
2254 /* If lt__malloc fails, it will LT__SETERROR (NO_MEMORY), which
2255 can then be detected with lt_dlerror() if we return 0. */
2258 interface_id->id_string = lt__strdup (id_string);
2259 if (!interface_id->id_string)
2260 FREE (interface_id);
2262 interface_id->iface = iface;
2265 return (lt_dlinterface_id) interface_id;
2268 void lt_dlinterface_free (lt_dlinterface_id key)
2270 lt__interface_id *interface_id = (lt__interface_id *)key;
2271 FREE (interface_id->id_string);
2272 FREE (interface_id);
2276 lt_dlcaller_set_data (lt_dlinterface_id key, lt_dlhandle handle, void *data)
2279 void *stale = (void *) 0;
2280 lt_dlhandle cur = handle;
2283 if (cur->interface_data)
2284 while (cur->interface_data[n_elements].key)
2287 for (i = 0; i < n_elements; ++i)
2289 if (cur->interface_data[i].key == key)
2291 stale = cur->interface_data[i].data;
2296 /* Ensure that there is enough room in this handle's interface_data
2297 array to accept a new element (and an empty end marker). */
2298 if (i == n_elements)
2300 lt_interface_data *temp
2301 = REALLOC (lt_interface_data, cur->interface_data, 2+ n_elements);
2309 cur->interface_data = temp;
2311 /* We only need this if we needed to allocate a new interface_data. */
2312 cur->interface_data[i].key = key;
2313 cur->interface_data[1+ i].key = 0;
2316 cur->interface_data[i].data = data;
2323 lt_dlcaller_get_data (lt_dlinterface_id key, lt_dlhandle handle)
2325 void *result = (void *) 0;
2326 lt_dlhandle cur = handle;
2328 /* Locate the index of the element with a matching KEY. */
2329 if (cur->interface_data)
2332 for (i = 0; cur->interface_data[i].key; ++i)
2334 if (cur->interface_data[i].key == key)
2336 result = cur->interface_data[i].data;
2346 lt_dlgetinfo (lt_dlhandle handle)
2350 LT__SETERROR (INVALID_HANDLE);
2354 return &(handle->info);
2359 lt_dlhandle_iterate (lt_dlinterface_id iface, lt_dlhandle place)
2361 lt_dlhandle handle = place;
2362 lt__interface_id *iterator = (lt__interface_id *) iface;
2364 assert (iface); /* iface is a required argument */
2369 handle = handle->next;
2371 /* advance while the interface check fails */
2372 while (handle && iterator->iface
2373 && ((*iterator->iface) (handle, iterator->id_string) != 0))
2375 handle = handle->next;
2383 lt_dlhandle_fetch (lt_dlinterface_id iface, const char *module_name)
2385 lt_dlhandle handle = 0;
2387 assert (iface); /* iface is a required argument */
2389 while ((handle = lt_dlhandle_iterate (iface, handle)))
2391 lt_dlhandle cur = handle;
2392 if (cur && cur->info.name && streq (cur->info.name, module_name))
2401 lt_dlhandle_map (lt_dlinterface_id iface,
2402 int (*func) (lt_dlhandle handle, void *data), void *data)
2404 lt__interface_id *iterator = (lt__interface_id *) iface;
2405 lt_dlhandle cur = handles;
2407 assert (iface); /* iface is a required argument */
2413 /* advance while the interface check fails */
2414 while (cur && iterator->iface
2415 && ((*iterator->iface) (cur, iterator->id_string) != 0))
2420 if ((errorcode = (*func) (cur, data)) != 0)