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 #if !defined(LT_LIBPREFIX)
58 # define LT_LIBPREFIX "lib"
61 /* This is the maximum symbol size that won't require malloc/free */
62 #undef LT_SYMBOL_LENGTH
63 #define LT_SYMBOL_LENGTH 128
65 /* This accounts for the _LTX_ separator */
66 #undef LT_SYMBOL_OVERHEAD
67 #define LT_SYMBOL_OVERHEAD 5
69 /* Various boolean flags can be stored in the flags field of an
71 #define LT_DLIS_RESIDENT(handle) ((handle)->info.is_resident)
72 #define LT_DLIS_SYMGLOBAL(handle) ((handle)->info.is_symglobal)
73 #define LT_DLIS_SYMLOCAL(handle) ((handle)->info.is_symlocal)
76 static const char objdir[] = LT_OBJDIR;
77 static const char archive_ext[] = LT_ARCHIVE_EXT;
78 static const char libext[] = LT_LIBEXT;
79 static const char libprefix[] = LT_LIBPREFIX;
80 #if defined(LT_MODULE_EXT)
81 static const char shlib_ext[] = LT_MODULE_EXT;
83 #if defined(LT_DLSEARCH_PATH)
84 static const char sys_dlsearch_path[] = LT_DLSEARCH_PATH;
90 /* --- DYNAMIC MODULE LOADING --- */
93 /* The type of a function used at each iteration of foreach_dirinpath(). */
94 typedef int foreach_callback_func (char *filename, void *data1,
96 /* foreachfile_callback itself calls a function of this type: */
97 typedef int file_worker_func (const char *filename, void *data);
100 static int foreach_dirinpath (const char *search_path,
101 const char *base_name,
102 foreach_callback_func *func,
103 void *data1, void *data2);
104 static int find_file_callback (char *filename, void *data1,
106 static int find_handle_callback (char *filename, void *data,
108 static int foreachfile_callback (char *filename, void *data1,
112 static int canonicalize_path (const char *path, char **pcanonical);
113 static int argzize_path (const char *path,
114 char **pargz, size_t *pargz_len);
115 static FILE *find_file (const char *search_path,
116 const char *base_name, char **pdir);
117 static lt_dlhandle *find_handle (const char *search_path,
118 const char *base_name,
121 static int find_module (lt_dlhandle *handle, const char *dir,
122 const char *libdir, const char *dlname,
123 const char *old_name, int installed,
125 static int has_library_ext (const char *filename);
126 static int load_deplibs (lt_dlhandle handle, char *deplibs);
127 static int trim (char **dest, const char *str);
128 static int try_dlopen (lt_dlhandle *handle,
129 const char *filename, const char *ext,
131 static int tryall_dlopen (lt_dlhandle *handle,
132 const char *filename,
134 const lt_dlvtable *vtable);
135 static int unload_deplibs (lt_dlhandle handle);
136 static int lt_argz_insert (char **pargz, size_t *pargz_len,
137 char *before, const char *entry);
138 static int lt_argz_insertinorder (char **pargz, size_t *pargz_len,
140 static int lt_argz_insertdir (char **pargz, size_t *pargz_len,
141 const char *dirnam, struct dirent *dp);
142 static int lt_dlpath_insertdir (char **ppath, char *before,
144 static int list_files_by_dir (const char *dirnam,
145 char **pargz, size_t *pargz_len);
146 static int file_not_found (void);
148 #ifdef HAVE_LIBDLLOADER
149 static int loader_init_callback (lt_dlhandle handle);
150 #endif /* HAVE_LIBDLLOADER */
152 static int loader_init (lt_get_vtable *vtable_func,
155 static char *user_search_path= 0;
156 static lt_dlhandle handles = 0;
157 static int initialized = 0;
159 /* Our memory failure callback sets the error message to be passed back
160 up to the client, so we must be careful to return from mallocation
161 callers if allocation fails (as this callback returns!!). */
163 lt__alloc_die_callback (void)
165 LT__SETERROR (NO_MEMORY);
168 #ifdef HAVE_LIBDLLOADER
169 /* This function is called to initialise each preloaded module loader,
170 and hook it into the list of loaders to be used when attempting to
171 dlopen an application module. */
173 loader_init_callback (lt_dlhandle handle)
175 lt_get_vtable *vtable_func = (lt_get_vtable *) lt_dlsym (handle, "get_vtable");
176 return loader_init (vtable_func, 0);
178 #endif /* HAVE_LIBDLLOADER */
181 loader_init (lt_get_vtable *vtable_func, lt_user_data data)
183 const lt_dlvtable *vtable = 0;
188 vtable = (*vtable_func) (data);
191 /* lt_dlloader_add will LT__SETERROR if it fails. */
192 errors += lt_dlloader_add (vtable);
194 assert (errors || vtable);
196 if ((!errors) && vtable->dlloader_init)
198 if ((*vtable->dlloader_init) (vtable->dlloader_data))
200 LT__SETERROR (INIT_LOADER);
208 /* Bootstrap the loader loading with the preopening loader. */
209 #define get_vtable preopen_LTX_get_vtable
210 #define preloaded_symbols LT_CONC3(lt_, LTDLOPEN, _LTX_preloaded_symbols)
213 LT_SCOPE const lt_dlvtable * get_vtable (lt_user_data data);
215 #ifdef HAVE_LIBDLLOADER
216 extern LT_DLSYM_CONST lt_dlsymlist preloaded_symbols[];
219 /* Initialize libltdl. */
225 /* Initialize only at first call. */
226 if (++initialized == 1)
228 lt__alloc_die = lt__alloc_die_callback;
230 user_search_path = 0; /* empty search path */
232 /* First set up the statically loaded preload module loader, so
233 we can use it to preopen the other loaders we linked in at
235 errors += loader_init (get_vtable, 0);
237 /* Now open all the preloaded module loaders, so the application
238 can use _them_ to lt_dlopen its own modules. */
239 #ifdef HAVE_LIBDLLOADER
242 errors += lt_dlpreload (preloaded_symbols);
247 errors += lt_dlpreload_open (LT_STR(LTDLOPEN), loader_init_callback);
249 #endif /* HAVE_LIBDLLOADER */
252 #ifdef LT_DEBUG_LOADERS
262 /* shut down libltdl */
263 lt_dlloader *loader = 0;
264 lt_dlhandle handle = handles;
269 LT__SETERROR (SHUTDOWN);
274 /* shut down only at last call. */
275 if (--initialized == 0)
279 while (handles && LT_DLIS_RESIDENT (handles))
281 handles = handles->next;
284 /* close all modules */
285 for (level = 1; handle; ++level)
287 lt_dlhandle cur = handles;
288 int saw_nonresident = 0;
292 lt_dlhandle tmp = cur;
294 if (!LT_DLIS_RESIDENT (tmp))
297 if (tmp->info.ref_count <= level)
299 if (lt_dlclose (tmp))
303 /* Make sure that the handle pointed to by 'cur' still exists.
304 lt_dlclose recursively closes dependent libraries which removes
305 them from the linked list. One of these might be the one
306 pointed to by 'cur'. */
309 for (tmp = handles; tmp; tmp = tmp->next)
318 /* done if only resident modules are left */
319 if (!saw_nonresident)
323 /* When removing loaders, we can only find out failure by testing
324 the error string, so avoid a spurious one from an earlier
329 /* close all loaders */
330 for (loader = (lt_dlloader *) lt_dlloader_next (NULL); loader;)
332 lt_dlloader *next = (lt_dlloader *) lt_dlloader_next (loader);
333 lt_dlvtable *vtable = (lt_dlvtable *) lt_dlloader_get (loader);
335 if ((vtable = lt_dlloader_remove ((char *) vtable->name)))
341 /* ignore errors due to resident modules */
351 FREE(user_search_path);
359 /* Try VTABLE or, if VTABLE is NULL, all available loaders for FILENAME.
360 If the library is not successfully loaded, return non-zero. Otherwise,
361 the dlhandle is stored at the address given in PHANDLE. */
363 tryall_dlopen (lt_dlhandle *phandle, const char *filename,
364 lt_dladvise advise, const lt_dlvtable *vtable)
366 lt_dlhandle handle = handles;
367 const char * saved_error = 0;
370 #ifdef LT_DEBUG_LOADERS
371 fprintf (stderr, "tryall_dlopen (%s, %s)\n",
372 filename ? filename : "(null)",
373 vtable ? vtable->name : "(ALL)");
376 LT__GETERROR (saved_error);
378 /* check whether the module was already opened */
379 for (;handle; handle = handle->next)
381 if ((handle->info.filename == filename) /* dlopen self: 0 == 0 */
382 || (handle->info.filename && filename
383 && streq (handle->info.filename, filename)))
391 ++handle->info.ref_count;
399 /* Comment out the check of file permissions using access.
400 This call seems to always return -1 with error EACCES.
402 /* We need to catch missing file errors early so that
403 file_not_found() can detect what happened.
404 if (access (filename, R_OK) != 0)
406 LT__SETERROR (FILE_NOT_FOUND);
411 handle->info.filename = lt__strdup (filename);
412 if (!handle->info.filename)
420 handle->info.filename = 0;
424 lt_dlloader loader = lt_dlloader_next (0);
425 const lt_dlvtable *loader_vtable;
430 loader_vtable = vtable;
432 loader_vtable = lt_dlloader_get (loader);
434 #ifdef LT_DEBUG_LOADERS
435 fprintf (stderr, "Calling %s->module_open (%s)\n",
436 (loader_vtable && loader_vtable->name) ? loader_vtable->name : "(null)",
437 filename ? filename : "(null)");
439 handle->module = (*loader_vtable->module_open) (loader_vtable->dlloader_data,
441 #ifdef LT_DEBUG_LOADERS
442 fprintf (stderr, " Result: %s\n",
443 handle->module ? "Success" : "Failed");
446 if (handle->module != 0)
450 handle->info.is_resident = advise->is_resident;
451 handle->info.is_symglobal = advise->is_symglobal;
452 handle->info.is_symlocal = advise->is_symlocal;
457 while (!vtable && (loader = lt_dlloader_next (loader)));
459 /* If VTABLE was given but couldn't open the module, or VTABLE wasn't
460 given but we exhausted all loaders without opening the module, bail
462 if ((vtable && !handle->module)
463 || (!vtable && !loader))
465 FREE (handle->info.filename);
470 handle->vtable = loader_vtable;
473 LT__SETERRORSTR (saved_error);
481 tryall_dlopen_module (lt_dlhandle *handle, const char *prefix,
482 const char *dirname, const char *dlname,
487 size_t filename_len = 0;
488 size_t dirname_len = LT_STRLEN (dirname);
493 #if defined(LT_DIRSEP_CHAR)
494 /* Only canonicalized names (i.e. with DIRSEP chars already converted)
495 should make it into this function: */
496 assert (strchr (dirname, LT_DIRSEP_CHAR) == 0);
500 if (dirname[dirname_len -1] == '/')
502 filename_len = dirname_len + 1 + LT_STRLEN (dlname);
504 /* Allocate memory, and combine DIRNAME and MODULENAME into it.
505 The PREFIX (if any) is handled below. */
506 filename = MALLOC (char, filename_len + 1);
510 sprintf (filename, "%.*s/%s", (int) dirname_len, dirname, dlname);
512 /* Now that we have combined DIRNAME and MODULENAME, if there is
513 also a PREFIX to contend with, simply recurse with the arguments
514 shuffled. Otherwise, attempt to open FILENAME as a module. */
517 error += tryall_dlopen_module (handle, (const char *) 0,
518 prefix, filename, advise);
520 else if (tryall_dlopen (handle, filename, advise, 0) != 0)
530 find_module (lt_dlhandle *handle, const char *dir, const char *libdir,
531 const char *dlname, const char *old_name, int installed,
534 /* Try to open the old library first; if it was dlpreopened,
535 we want the preopened version of it, even if a dlopenable
536 module is available. */
537 if (old_name && tryall_dlopen (handle, old_name,
538 advise, lt_dlloader_find ("lt_preopen") ) == 0)
543 /* Try to open the dynamic library. */
546 /* try to open the installed module */
547 if (installed && libdir)
549 if (tryall_dlopen_module (handle, (const char *) 0,
550 libdir, dlname, advise) == 0)
554 /* try to open the not-installed module */
557 if (tryall_dlopen_module (handle, dir, objdir,
558 dlname, advise) == 0)
562 /* maybe it was moved to another directory */
564 if (dir && (tryall_dlopen_module (handle, (const char *) 0,
565 dir, dlname, advise) == 0))
575 canonicalize_path (const char *path, char **pcanonical)
579 assert (path && *path);
582 canonical = MALLOC (char, 1+ LT_STRLEN (path));
589 for (src = 0; path[src] != LT_EOS_CHAR; ++src)
591 /* Path separators are not copied to the beginning or end of
592 the destination, or if another separator would follow
594 if (path[src] == LT_PATHSEP_CHAR)
597 || (path[1+ src] == LT_PATHSEP_CHAR)
598 || (path[1+ src] == LT_EOS_CHAR))
602 /* Anything other than a directory separator is copied verbatim. */
603 if ((path[src] != '/')
604 #if defined(LT_DIRSEP_CHAR)
605 && (path[src] != LT_DIRSEP_CHAR)
609 canonical[dest++] = path[src];
611 /* Directory separators are converted and copied only if they are
612 not at the end of a path -- i.e. before a path separator or
614 else if ((path[1+ src] != LT_PATHSEP_CHAR)
615 && (path[1+ src] != LT_EOS_CHAR)
616 #if defined(LT_DIRSEP_CHAR)
617 && (path[1+ src] != LT_DIRSEP_CHAR)
619 && (path[1+ src] != '/'))
621 canonical[dest++] = '/';
625 /* Add an end-of-string marker at the end. */
626 canonical[dest] = LT_EOS_CHAR;
629 /* Assign new value. */
630 *pcanonical = canonical;
636 argzize_path (const char *path, char **pargz, size_t *pargz_len)
644 if ((error = argz_create_sep (path, LT_PATHSEP_CHAR, pargz, pargz_len)))
649 LT__SETERROR (NO_MEMORY);
652 LT__SETERROR (UNKNOWN);
662 /* Repeatedly call FUNC with each LT_PATHSEP_CHAR delimited element
663 of SEARCH_PATH and references to DATA1 and DATA2, until FUNC returns
664 non-zero or all elements are exhausted. If BASE_NAME is non-NULL,
665 it is appended to each SEARCH_PATH element before FUNC is called. */
667 foreach_dirinpath (const char *search_path, const char *base_name,
668 foreach_callback_func *func, void *data1, void *data2)
671 size_t filenamesize = 0;
672 size_t lenbase = LT_STRLEN (base_name);
678 if (!search_path || !*search_path)
680 LT__SETERROR (FILE_NOT_FOUND);
684 if (canonicalize_path (search_path, &canonical) != 0)
687 if (argzize_path (canonical, &argz, &argz_len) != 0)
692 while ((dir_name = argz_next (argz, argz_len, dir_name)))
694 size_t lendir = LT_STRLEN (dir_name);
696 if (1+ lendir + lenbase >= filenamesize)
699 filenamesize = 1+ lendir + 1+ lenbase; /* "/d" + '/' + "f" + '\0' */
700 filename = MALLOC (char, filenamesize);
705 assert (filenamesize > lendir);
706 strcpy (filename, dir_name);
708 if (base_name && *base_name)
710 if (filename[lendir -1] != '/')
711 filename[lendir++] = '/';
712 strcpy (filename +lendir, base_name);
715 if ((result = (*func) (filename, data1, data2)))
730 /* If FILEPATH can be opened, store the name of the directory component
731 in DATA1, and the opened FILE* structure address in DATA2. Otherwise
732 DATA1 is unchanged, but DATA2 is set to a pointer to NULL. */
734 find_file_callback (char *filename, void *data1, void *data2)
736 char **pdir = (char **) data1;
737 FILE **pfile = (FILE **) data2;
740 assert (filename && *filename);
744 if ((*pfile = fopen (filename, LT_READTEXT_MODE)))
746 char *dirend = strrchr (filename, '/');
748 if (dirend > filename)
749 *dirend = LT_EOS_CHAR;
752 *pdir = lt__strdup (filename);
753 is_done = (*pdir == 0) ? -1 : 1;
760 find_file (const char *search_path, const char *base_name, char **pdir)
764 foreach_dirinpath (search_path, base_name, find_file_callback, pdir, &file);
770 find_handle_callback (char *filename, void *data, void *data2)
772 lt_dlhandle *phandle = (lt_dlhandle *) data;
773 int notfound = access (filename, R_OK);
774 lt_dladvise advise = (lt_dladvise) data2;
776 /* Bail out if file cannot be read... */
780 /* Try to dlopen the file, but do not continue searching in any
782 if (tryall_dlopen (phandle, filename, advise, 0) != 0)
788 /* If HANDLE was found return it, otherwise return 0. If HANDLE was
789 found but could not be opened, *HANDLE will be set to 0. */
791 find_handle (const char *search_path, const char *base_name,
792 lt_dlhandle *phandle, lt_dladvise advise)
797 if (!foreach_dirinpath (search_path, base_name, find_handle_callback,
804 #if !defined(LTDL_DLOPEN_DEPLIBS)
806 load_deplibs (lt_dlhandle handle, char * LT__UNUSED deplibs)
808 handle->depcount = 0;
812 #else /* defined(LTDL_DLOPEN_DEPLIBS) */
814 load_deplibs (lt_dlhandle handle, char *deplibs)
816 char *p, *save_search_path = 0;
822 handle->depcount = 0;
830 if (user_search_path)
832 save_search_path = lt__strdup (user_search_path);
833 if (!save_search_path)
837 /* extract search paths and count deplibs */
841 if (!isspace ((unsigned char) *p))
844 while (*end && !isspace((unsigned char) *end))
849 if (strncmp(p, "-L", 2) == 0 || strncmp(p, "-R", 2) == 0)
852 *end = 0; /* set a temporary string terminator */
853 if (lt_dladdsearchdir(p+2))
879 names = MALLOC (char *, depcount);
883 /* now only extract the actual deplibs */
888 if (isspace ((unsigned char) *p))
895 while (*end && !isspace ((unsigned char) *end))
900 if (strncmp(p, "-L", 2) != 0 && strncmp(p, "-R", 2) != 0)
904 *end = 0; /* set a temporary string terminator */
905 if (strncmp(p, "-l", 2) == 0)
907 size_t name_len = 3+ /* "lib" */ LT_STRLEN (p + 2);
908 name = MALLOC (char, 1+ name_len);
910 sprintf (name, "lib%s", p+2);
913 name = lt__strdup(p);
918 names[depcount++] = name;
925 /* load the deplibs (in reverse order)
926 At this stage, don't worry if the deplibs do not load correctly,
927 they may already be statically linked into the loading application
928 for instance. There will be a more enlightening error message
929 later on if the loaded module cannot resolve all of its symbols. */
932 lt_dlhandle cur = handle;
935 cur->deplibs = MALLOC (lt_dlhandle, depcount);
939 for (i = 0; i < depcount; ++i)
941 cur->deplibs[j] = lt_dlopenext(names[depcount-1-i]);
948 cur->depcount = j; /* Number of successfully loaded deplibs */
953 for (i = 0; i < depcount; ++i)
960 /* restore the old search path */
961 if (save_search_path) {
962 MEMREASSIGN (user_search_path, save_search_path);
967 #endif /* defined(LTDL_DLOPEN_DEPLIBS) */
970 unload_deplibs (lt_dlhandle handle)
974 lt_dlhandle cur = handle;
978 for (i = 0; i < cur->depcount; ++i)
980 if (!LT_DLIS_RESIDENT (cur->deplibs[i]))
982 errors += lt_dlclose (cur->deplibs[i]);
992 trim (char **dest, const char *str)
994 /* remove the leading and trailing "'" from str
995 and store the result in dest */
996 const char *end = strrchr (str, '\'');
997 size_t len = LT_STRLEN (str);
1002 if (!end || end == str)
1005 if (len > 3 && str[0] == '\'')
1007 tmp = MALLOC (char, end - str);
1011 memcpy(tmp, &str[1], (end - str) - 1);
1012 tmp[(end - str) - 1] = LT_EOS_CHAR;
1023 /* Read the .la file FILE. */
1025 parse_dotla_file(FILE *file, char **dlname, char **libdir, char **deplibs,
1026 char **old_name, int *installed)
1029 size_t line_len = LT_FILENAME_MAX;
1030 char * line = MALLOC (char, line_len);
1034 LT__SETERROR (FILE_NOT_FOUND);
1038 while (!feof (file))
1040 line[line_len-2] = '\0';
1041 if (!fgets (line, (int) line_len, file))
1046 /* Handle the case where we occasionally need to read a line
1047 that is longer than the initial buffer size.
1048 Behave even if the file contains NUL bytes due to corruption. */
1049 while (line[line_len-2] != '\0' && line[line_len-2] != '\n' && !feof (file))
1051 line = REALLOC (char, line, line_len *2);
1057 line[line_len * 2 - 2] = '\0';
1058 if (!fgets (&line[line_len -1], (int) line_len +1, file))
1065 if (line[0] == '\n' || line[0] == '#')
1071 #define STR_DLNAME "dlname="
1072 if (strncmp (line, STR_DLNAME, sizeof (STR_DLNAME) - 1) == 0)
1074 errors += trim (dlname, &line[sizeof (STR_DLNAME) - 1]);
1077 #undef STR_OLD_LIBRARY
1078 #define STR_OLD_LIBRARY "old_library="
1079 else if (strncmp (line, STR_OLD_LIBRARY,
1080 sizeof (STR_OLD_LIBRARY) - 1) == 0)
1082 errors += trim (old_name, &line[sizeof (STR_OLD_LIBRARY) - 1]);
1085 /* Windows native tools do not understand the POSIX paths we store
1088 #define STR_LIBDIR "libdir="
1089 else if (strncmp (line, STR_LIBDIR, sizeof (STR_LIBDIR) - 1) == 0)
1091 errors += trim (libdir, &line[sizeof(STR_LIBDIR) - 1]);
1093 /* Disallow following unix-style paths on MinGW. */
1094 if (*libdir && (**libdir == '/' || **libdir == '\\'))
1099 #undef STR_DL_DEPLIBS
1100 #define STR_DL_DEPLIBS "dependency_libs="
1101 else if (strncmp (line, STR_DL_DEPLIBS,
1102 sizeof (STR_DL_DEPLIBS) - 1) == 0)
1104 errors += trim (deplibs, &line[sizeof (STR_DL_DEPLIBS) - 1]);
1106 else if (streq (line, "installed=yes\n"))
1110 else if (streq (line, "installed=no\n"))
1115 #undef STR_LIBRARY_NAMES
1116 #define STR_LIBRARY_NAMES "library_names="
1117 else if (!*dlname && strncmp (line, STR_LIBRARY_NAMES,
1118 sizeof (STR_LIBRARY_NAMES) - 1) == 0)
1121 errors += trim (dlname, &line[sizeof (STR_LIBRARY_NAMES) - 1]);
1124 && (last_libname = strrchr (*dlname, ' ')) != 0)
1126 last_libname = lt__strdup (last_libname + 1);
1132 MEMREASSIGN (*dlname, last_libname);
1145 /* Try to open FILENAME as a module. */
1147 try_dlopen (lt_dlhandle *phandle, const char *filename, const char *ext,
1150 const char * saved_error = 0;
1151 char * archive_name = 0;
1152 char * canonical = 0;
1153 char * base_name = 0;
1158 lt_dlhandle newhandle;
1161 assert (*phandle == 0);
1163 #ifdef LT_DEBUG_LOADERS
1164 fprintf (stderr, "try_dlopen (%s, %s)\n",
1165 filename ? filename : "(null)",
1166 ext ? ext : "(null)");
1169 LT__GETERROR (saved_error);
1174 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1178 newhandle = *phandle;
1180 /* lt_dlclose()ing yourself is very bad! Disallow it. */
1181 newhandle->info.is_resident = 1;
1183 if (tryall_dlopen (&newhandle, 0, advise, 0) != 0)
1189 goto register_handle;
1192 assert (filename && *filename);
1196 attempt = MALLOC (char, LT_STRLEN (filename) + LT_STRLEN (ext) + 1);
1200 sprintf(attempt, "%s%s", filename, ext);
1204 attempt = lt__strdup (filename);
1209 /* Doing this immediately allows internal functions to safely
1210 assume only canonicalized paths are passed. */
1211 if (canonicalize_path (attempt, &canonical) != 0)
1217 /* If the canonical module name is a path (relative or absolute)
1218 then split it into a directory part and a name part. */
1219 base_name = strrchr (canonical, '/');
1222 size_t dirlen = (1+ base_name) - canonical;
1224 dir = MALLOC (char, 1+ dirlen);
1231 strncpy (dir, canonical, dirlen);
1232 dir[dirlen] = LT_EOS_CHAR;
1237 MEMREASSIGN (base_name, canonical);
1239 assert (base_name && *base_name);
1241 ext = strrchr (base_name, '.');
1244 ext = base_name + LT_STRLEN (base_name);
1247 /* extract the module name from the file name */
1248 name = MALLOC (char, ext - base_name + 1);
1255 /* canonicalize the module name */
1258 for (i = 0; i < ext - base_name; ++i)
1260 if (isalnum ((unsigned char)(base_name[i])))
1262 name[i] = base_name[i];
1269 name[ext - base_name] = LT_EOS_CHAR;
1272 /* Before trawling through the filesystem in search of a module,
1273 check whether we are opening a preloaded module. */
1276 const lt_dlvtable *vtable = lt_dlloader_find ("lt_preopen");
1280 /* libprefix + name + "." + libext + NULL */
1281 archive_name = MALLOC (char, strlen (libprefix) + LT_STRLEN (name) + strlen (libext) + 2);
1282 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1284 if ((*phandle == NULL) || (archive_name == NULL))
1289 newhandle = *phandle;
1291 /* Preloaded modules are always named according to their old
1293 if (strncmp(name, "lib", 3) == 0)
1295 sprintf (archive_name, "%s%s.%s", libprefix, name + 3, libext);
1299 sprintf (archive_name, "%s.%s", name, libext);
1302 if (tryall_dlopen (&newhandle, archive_name, advise, vtable) == 0)
1304 goto register_handle;
1307 /* If we're still here, there was no matching preloaded module,
1308 so put things back as we found them, and continue searching. */
1314 /* If we are allowing only preloaded modules, and we didn't find
1315 anything yet, give up on the search here. */
1316 if (advise && advise->try_preload_only)
1321 /* Check whether we are opening a libtool module (.la extension). */
1322 if (ext && streq (ext, archive_ext))
1324 /* this seems to be a libtool module */
1327 char * old_name = 0;
1331 /* if we can't find the installed flag, it is probably an
1332 installed libtool archive, produced with an old version
1336 /* Now try to open the .la file. If there is no directory name
1337 component, try to find it first in user_search_path and then other
1338 prescribed paths. Otherwise (or in any case if the module was not
1339 yet found) try opening just the module name as passed. */
1342 const char *search_path = user_search_path;
1345 file = find_file (user_search_path, base_name, &dir);
1349 search_path = getenv (LTDL_SEARCHPATH_VAR);
1351 file = find_file (search_path, base_name, &dir);
1354 #if defined(LT_MODULE_PATH_VAR)
1357 search_path = getenv (LT_MODULE_PATH_VAR);
1359 file = find_file (search_path, base_name, &dir);
1362 #if defined(LT_DLSEARCH_PATH)
1363 if (!file && *sys_dlsearch_path)
1365 file = find_file (sys_dlsearch_path, base_name, &dir);
1371 file = fopen (attempt, LT_READTEXT_MODE);
1374 /* If we didn't find the file by now, it really isn't there. Set
1375 the status flag, and bail out. */
1378 LT__SETERROR (FILE_NOT_FOUND);
1383 /* read the .la file */
1384 if (parse_dotla_file(file, &dlname, &libdir, &deplibs,
1385 &old_name, &installed) != 0)
1390 /* allocate the handle */
1391 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1407 if (load_deplibs (*phandle, deplibs) == 0)
1409 newhandle = *phandle;
1410 /* find_module may replace newhandle */
1411 if (find_module (&newhandle, dir, libdir, dlname, old_name,
1414 unload_deplibs (*phandle);
1434 if (*phandle != newhandle)
1436 unload_deplibs (*phandle);
1441 /* not a libtool module */
1442 *phandle = (lt_dlhandle) lt__zalloc (sizeof (struct lt__handle));
1449 newhandle = *phandle;
1451 /* If the module has no directory name component, try to find it
1452 first in user_search_path and then other prescribed paths.
1453 Otherwise (or in any case if the module was not yet found) try
1454 opening just the module name as passed. */
1455 if ((dir || (!find_handle (user_search_path, base_name,
1457 && !find_handle (getenv (LTDL_SEARCHPATH_VAR), base_name,
1459 #if defined(LT_MODULE_PATH_VAR)
1460 && !find_handle (getenv (LT_MODULE_PATH_VAR), base_name,
1463 #if defined(LT_DLSEARCH_PATH)
1464 && !find_handle (sys_dlsearch_path, base_name,
1469 if (tryall_dlopen (&newhandle, attempt, advise, 0) != 0)
1484 MEMREASSIGN (*phandle, newhandle);
1486 if ((*phandle)->info.ref_count == 0)
1488 (*phandle)->info.ref_count = 1;
1489 MEMREASSIGN ((*phandle)->info.name, name);
1491 (*phandle)->next = handles;
1495 LT__SETERRORSTR (saved_error);
1501 if (!canonical) /* was MEMREASSIGNed */
1504 FREE (archive_name);
1510 /* If the last error message stored was `FILE_NOT_FOUND', then return
1513 file_not_found (void)
1515 const char *error = 0;
1517 LT__GETERROR (error);
1518 if (error == LT__STRERROR (FILE_NOT_FOUND))
1525 /* Unless FILENAME already bears a suitable library extension, then
1528 has_library_ext (const char *filename)
1530 const char * ext = 0;
1534 ext = strrchr (filename, '.');
1536 if (ext && ((streq (ext, archive_ext))
1537 #if defined(LT_MODULE_EXT)
1538 || (streq (ext, shlib_ext))
1549 /* Initialise and configure a user lt_dladvise opaque object. */
1552 lt_dladvise_init (lt_dladvise *padvise)
1554 lt_dladvise advise = (lt_dladvise) lt__zalloc (sizeof (struct lt__advise));
1556 return (advise ? 0 : 1);
1560 lt_dladvise_destroy (lt_dladvise *padvise)
1568 lt_dladvise_ext (lt_dladvise *padvise)
1570 assert (padvise && *padvise);
1571 (*padvise)->try_ext = 1;
1576 lt_dladvise_resident (lt_dladvise *padvise)
1578 assert (padvise && *padvise);
1579 (*padvise)->is_resident = 1;
1584 lt_dladvise_local (lt_dladvise *padvise)
1586 assert (padvise && *padvise);
1587 (*padvise)->is_symlocal = 1;
1592 lt_dladvise_global (lt_dladvise *padvise)
1594 assert (padvise && *padvise);
1595 (*padvise)->is_symglobal = 1;
1600 lt_dladvise_preload (lt_dladvise *padvise)
1602 assert (padvise && *padvise);
1603 (*padvise)->try_preload_only = 1;
1607 /* Libtool-1.5.x interface for loading a new module named FILENAME. */
1609 lt_dlopen (const char *filename)
1611 return lt_dlopenadvise (filename, NULL);
1615 /* If FILENAME has an ARCHIVE_EXT or MODULE_EXT extension, try to
1616 open the FILENAME as passed. Otherwise try appending ARCHIVE_EXT,
1617 and if a file is still not found try again with MODULE_EXT appended
1620 lt_dlopenext (const char *filename)
1622 lt_dlhandle handle = 0;
1625 if (!lt_dladvise_init (&advise) && !lt_dladvise_ext (&advise))
1626 handle = lt_dlopenadvise (filename, advise);
1628 lt_dladvise_destroy (&advise);
1634 lt_dlopenadvise (const char *filename, lt_dladvise advise)
1636 lt_dlhandle handle = 0;
1638 const char * saved_error = 0;
1640 LT__GETERROR (saved_error);
1642 /* Can't have symbols hidden and visible at the same time! */
1643 if (advise && advise->is_symlocal && advise->is_symglobal)
1645 LT__SETERROR (CONFLICTING_FLAGS);
1652 || has_library_ext (filename))
1654 /* Just incase we missed a code path in try_dlopen() that reports
1655 an error, but forgot to reset handle... */
1656 if (try_dlopen (&handle, filename, NULL, advise) != 0)
1661 else if (filename && *filename)
1664 /* First try appending ARCHIVE_EXT. */
1665 errors += try_dlopen (&handle, filename, archive_ext, advise);
1667 /* If we found FILENAME, stop searching -- whether we were able to
1668 load the file as a module or not. If the file exists but loading
1669 failed, it is better to return an error message here than to
1670 report FILE_NOT_FOUND when the alternatives (foo.so etc) are not
1671 in the module search path. */
1672 if (handle || ((errors > 0) && !file_not_found ()))
1675 #if defined(LT_MODULE_EXT)
1676 /* Try appending SHLIB_EXT. */
1677 LT__SETERRORSTR (saved_error);
1678 errors = try_dlopen (&handle, filename, shlib_ext, advise);
1680 /* As before, if the file was found but loading failed, return now
1681 with the current error message. */
1682 if (handle || ((errors > 0) && !file_not_found ()))
1687 /* Still here? Then we really did fail to locate any of the file
1689 LT__SETERROR (FILE_NOT_FOUND);
1695 lt_argz_insert (char **pargz, size_t *pargz_len, char *before,
1700 /* Prior to Sep 8, 2005, newlib had a bug where argz_insert(pargz,
1701 pargz_len, NULL, entry) failed with EINVAL. */
1703 error = argz_insert (pargz, pargz_len, before, entry);
1705 error = argz_append (pargz, pargz_len, entry, 1 + strlen (entry));
1712 LT__SETERROR (NO_MEMORY);
1715 LT__SETERROR (UNKNOWN);
1725 lt_argz_insertinorder (char **pargz, size_t *pargz_len, const char *entry)
1731 assert (entry && *entry);
1734 while ((before = argz_next (*pargz, *pargz_len, before)))
1736 int cmp = strcmp (entry, before);
1739 if (cmp == 0) return 0; /* No duplicates! */
1742 return lt_argz_insert (pargz, pargz_len, before, entry);
1746 lt_argz_insertdir (char **pargz, size_t *pargz_len, const char *dirnam,
1752 size_t end_offset = 0;
1760 dir_len = LT_STRLEN (dirnam);
1761 end = dp->d_name + D_NAMLEN(dp);
1763 /* Ignore version numbers. */
1766 for (p = end; p -1 > dp->d_name; --p)
1767 if (strchr (".0123456789", p[-1]) == 0)
1774 /* Ignore filename extension. */
1777 for (p = end -1; p > dp->d_name; --p)
1785 /* Prepend the directory name. */
1786 end_offset = end - dp->d_name;
1787 buf_len = dir_len + 1+ end_offset;
1788 buf = MALLOC (char, 1+ buf_len);
1794 strcpy (buf, dirnam);
1796 strncat (buf, dp->d_name, end_offset);
1797 buf[buf_len] = LT_EOS_CHAR;
1799 /* Try to insert (in order) into ARGZ/ARGZ_LEN. */
1800 if (lt_argz_insertinorder (pargz, pargz_len, buf) != 0)
1809 list_files_by_dir (const char *dirnam, char **pargz, size_t *pargz_len)
1814 assert (dirnam && *dirnam);
1817 assert (dirnam[LT_STRLEN(dirnam) -1] != '/');
1819 dirp = opendir (dirnam);
1822 struct dirent *dp = 0;
1824 while ((dp = readdir (dirp)))
1825 if (dp->d_name[0] != '.')
1826 if (lt_argz_insertdir (pargz, pargz_len, dirnam, dp))
1841 /* If there are any files in DIRNAME, call the function passed in
1842 DATA1 (with the name of each file and DATA2 as arguments). */
1844 foreachfile_callback (char *dirname, void *data1, void *data2)
1846 file_worker_func *func = *(file_worker_func **) data1;
1850 size_t argz_len = 0;
1852 if (list_files_by_dir (dirname, &argz, &argz_len) != 0)
1859 while ((filename = argz_next (argz, argz_len, filename)))
1860 if ((is_done = (*func) (filename, data2)))
1871 /* Call FUNC for each unique extensionless file in SEARCH_PATH, along
1872 with DATA. The filenames passed to FUNC would be suitable for
1873 passing to lt_dlopenext. The extensions are stripped so that
1874 individual modules do not generate several entries (e.g. libfoo.la,
1875 libfoo.so, libfoo.so.1, libfoo.so.1.0.0). If SEARCH_PATH is NULL,
1876 then the same directories that lt_dlopen would search are examined. */
1878 lt_dlforeachfile (const char *search_path,
1879 int (*func) (const char *filename, void *data),
1883 file_worker_func **fpptr = &func;
1887 /* If a specific path was passed, search only the directories
1889 is_done = foreach_dirinpath (search_path, 0,
1890 foreachfile_callback, fpptr, data);
1894 /* Otherwise search the default paths. */
1895 is_done = foreach_dirinpath (user_search_path, 0,
1896 foreachfile_callback, fpptr, data);
1899 is_done = foreach_dirinpath (getenv(LTDL_SEARCHPATH_VAR), 0,
1900 foreachfile_callback, fpptr, data);
1903 #if defined(LT_MODULE_PATH_VAR)
1906 is_done = foreach_dirinpath (getenv(LT_MODULE_PATH_VAR), 0,
1907 foreachfile_callback, fpptr, data);
1910 #if defined(LT_DLSEARCH_PATH)
1911 if (!is_done && *sys_dlsearch_path)
1913 is_done = foreach_dirinpath (sys_dlsearch_path, 0,
1914 foreachfile_callback, fpptr, data);
1923 lt_dlclose (lt_dlhandle handle)
1925 lt_dlhandle cur, last;
1928 /* check whether the handle is valid */
1929 last = cur = handles;
1930 while (cur && handle != cur)
1938 LT__SETERROR (INVALID_HANDLE);
1944 cur->info.ref_count--;
1946 /* Note that even with resident modules, we must track the ref_count
1947 correctly incase the user decides to reset the residency flag
1948 later (even though the API makes no provision for that at the
1950 if (cur->info.ref_count <= 0 && !LT_DLIS_RESIDENT (cur))
1952 lt_user_data data = cur->vtable->dlloader_data;
1956 last->next = cur->next;
1960 handles = cur->next;
1963 errors += cur->vtable->module_close (data, cur->module);
1964 errors += unload_deplibs (handle);
1966 /* It is up to the callers to free the data itself. */
1967 FREE (cur->interface_data);
1969 FREE (cur->info.filename);
1970 FREE (cur->info.name);
1976 if (LT_DLIS_RESIDENT (handle))
1978 LT__SETERROR (CLOSE_RESIDENT_MODULE);
1987 lt_dlsym (lt_dlhandle place, const char *symbol)
1990 char lsym[LT_SYMBOL_LENGTH];
1998 LT__SETERROR (INVALID_HANDLE);
2006 LT__SETERROR (SYMBOL_NOT_FOUND);
2010 lensym = LT_STRLEN (symbol) + LT_STRLEN (handle->vtable->sym_prefix)
2011 + LT_STRLEN (handle->info.name);
2013 if (lensym + LT_SYMBOL_OVERHEAD < LT_SYMBOL_LENGTH)
2019 sym = MALLOC (char, lensym + LT_SYMBOL_OVERHEAD + 1);
2022 LT__SETERROR (BUFFER_OVERFLOW);
2027 data = handle->vtable->dlloader_data;
2028 if (handle->info.name)
2030 const char *saved_error;
2032 LT__GETERROR (saved_error);
2034 /* this is a libtool module */
2035 if (handle->vtable->sym_prefix)
2037 strcpy(sym, handle->vtable->sym_prefix);
2038 strcat(sym, handle->info.name);
2042 strcpy(sym, handle->info.name);
2045 strcat(sym, "_LTX_");
2046 strcat(sym, symbol);
2048 /* try "modulename_LTX_symbol" */
2049 address = handle->vtable->find_sym (data, handle->module, sym);
2058 LT__SETERRORSTR (saved_error);
2061 /* otherwise try "symbol" */
2062 if (handle->vtable->sym_prefix)
2064 strcpy(sym, handle->vtable->sym_prefix);
2065 strcat(sym, symbol);
2069 strcpy(sym, symbol);
2072 address = handle->vtable->find_sym (data, handle->module, sym);
2086 LT__GETERROR (error);
2087 LT__SETERRORSTR (0);
2093 lt_dlpath_insertdir (char **ppath, char *before, const char *dir)
2096 char *canonical = 0;
2098 size_t argz_len = 0;
2101 assert (dir && *dir);
2103 if (canonicalize_path (dir, &canonical) != 0)
2109 assert (canonical && *canonical);
2111 /* If *PPATH is empty, set it to DIR. */
2114 assert (!before); /* BEFORE cannot be set without PPATH. */
2115 assert (dir); /* Without DIR, don't call this function! */
2117 *ppath = lt__strdup (dir);
2124 assert (ppath && *ppath);
2126 if (argzize_path (*ppath, &argz, &argz_len) != 0)
2132 /* Convert BEFORE into an equivalent offset into ARGZ. This only works
2133 if *PPATH is already canonicalized, and hence does not change length
2134 with respect to ARGZ. We canonicalize each entry as it is added to
2135 the search path, and don't call this function with (uncanonicalized)
2136 user paths, so this is a fair assumption. */
2139 assert (*ppath <= before);
2140 assert ((int) (before - *ppath) <= (int) strlen (*ppath));
2142 before = before - *ppath + argz;
2145 if (lt_argz_insert (&argz, &argz_len, before, dir) != 0)
2151 argz_stringify (argz, argz_len, LT_PATHSEP_CHAR);
2152 MEMREASSIGN(*ppath, argz);
2162 lt_dladdsearchdir (const char *search_dir)
2166 if (search_dir && *search_dir)
2168 if (lt_dlpath_insertdir (&user_search_path, 0, search_dir) != 0)
2176 lt_dlinsertsearchdir (const char *before, const char *search_dir)
2182 if ((before < user_search_path)
2183 || (before >= user_search_path + LT_STRLEN (user_search_path)))
2185 LT__SETERROR (INVALID_POSITION);
2190 if (search_dir && *search_dir)
2192 if (lt_dlpath_insertdir (&user_search_path,
2193 (char *) before, search_dir) != 0)
2203 lt_dlsetsearchpath (const char *search_path)
2207 FREE (user_search_path);
2209 if (!search_path || !LT_STRLEN (search_path))
2214 if (canonicalize_path (search_path, &user_search_path) != 0)
2221 lt_dlgetsearchpath (void)
2223 const char *saved_path;
2225 saved_path = user_search_path;
2231 lt_dlmakeresident (lt_dlhandle handle)
2237 LT__SETERROR (INVALID_HANDLE);
2242 handle->info.is_resident = 1;
2249 lt_dlisresident (lt_dlhandle handle)
2253 LT__SETERROR (INVALID_HANDLE);
2257 return LT_DLIS_RESIDENT (handle);
2262 /* --- MODULE INFORMATION --- */
2265 const char *id_string;
2266 lt_dlhandle_interface *iface;
2270 lt_dlinterface_register (const char *id_string, lt_dlhandle_interface *iface)
2272 lt__interface_id *interface_id = (lt__interface_id *) lt__malloc (sizeof *interface_id);
2274 /* If lt__malloc fails, it will LT__SETERROR (NO_MEMORY), which
2275 can then be detected with lt_dlerror() if we return 0. */
2278 interface_id->id_string = lt__strdup (id_string);
2279 if (!interface_id->id_string)
2280 FREE (interface_id);
2282 interface_id->iface = iface;
2285 return (lt_dlinterface_id) interface_id;
2288 void lt_dlinterface_free (lt_dlinterface_id key)
2290 lt__interface_id *interface_id = (lt__interface_id *)key;
2291 FREE (interface_id->id_string);
2292 FREE (interface_id);
2296 lt_dlcaller_set_data (lt_dlinterface_id key, lt_dlhandle handle, void *data)
2299 void *stale = (void *) 0;
2300 lt_dlhandle cur = handle;
2303 if (cur->interface_data)
2304 while (cur->interface_data[n_elements].key)
2307 for (i = 0; i < n_elements; ++i)
2309 if (cur->interface_data[i].key == key)
2311 stale = cur->interface_data[i].data;
2316 /* Ensure that there is enough room in this handle's interface_data
2317 array to accept a new element (and an empty end marker). */
2318 if (i == n_elements)
2320 lt_interface_data *temp
2321 = REALLOC (lt_interface_data, cur->interface_data, 2+ n_elements);
2329 cur->interface_data = temp;
2331 /* We only need this if we needed to allocate a new interface_data. */
2332 cur->interface_data[i].key = key;
2333 cur->interface_data[1+ i].key = 0;
2336 cur->interface_data[i].data = data;
2343 lt_dlcaller_get_data (lt_dlinterface_id key, lt_dlhandle handle)
2345 void *result = (void *) 0;
2346 lt_dlhandle cur = handle;
2348 /* Locate the index of the element with a matching KEY. */
2349 if (cur->interface_data)
2352 for (i = 0; cur->interface_data[i].key; ++i)
2354 if (cur->interface_data[i].key == key)
2356 result = cur->interface_data[i].data;
2366 lt_dlgetinfo (lt_dlhandle handle)
2370 LT__SETERROR (INVALID_HANDLE);
2374 return &(handle->info);
2379 lt_dlhandle_iterate (lt_dlinterface_id iface, lt_dlhandle place)
2381 lt_dlhandle handle = place;
2382 lt__interface_id *iterator = (lt__interface_id *) iface;
2384 assert (iface); /* iface is a required argument */
2389 handle = handle->next;
2391 /* advance while the interface check fails */
2392 while (handle && iterator->iface
2393 && ((*iterator->iface) (handle, iterator->id_string) != 0))
2395 handle = handle->next;
2403 lt_dlhandle_fetch (lt_dlinterface_id iface, const char *module_name)
2405 lt_dlhandle handle = 0;
2407 assert (iface); /* iface is a required argument */
2409 while ((handle = lt_dlhandle_iterate (iface, handle)))
2411 lt_dlhandle cur = handle;
2412 if (cur && cur->info.name && streq (cur->info.name, module_name))
2421 lt_dlhandle_map (lt_dlinterface_id iface,
2422 int (*func) (lt_dlhandle handle, void *data), void *data)
2424 lt__interface_id *iterator = (lt__interface_id *) iface;
2425 lt_dlhandle cur = handles;
2427 assert (iface); /* iface is a required argument */
2433 /* advance while the interface check fails */
2434 while (cur && iterator->iface
2435 && ((*iterator->iface) (cur, iterator->id_string) != 0))
2440 if ((errorcode = (*func) (cur, data)) != 0)