1 /* ltdl.c -- system independent dlopen wrapper
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 Originally by Thomas Tanner <tanner@ffii.org>
4 This file is part of GNU Libtool.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 As a special exception to the GNU Lesser General Public License,
12 if you distribute this file as part of a program or library that
13 is built using GNU libtool, you may include it under the same
14 distribution terms that you use for the rest of that program.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
76 #undef LT_USE_POSIX_DIRENT
81 # define LT_USE_POSIX_DIRENT
82 # endif /* HAVE_DIRENT_H */
83 # endif /* HAVE_READDIR */
84 # endif /* HAVE_OPENDIR */
85 #endif /* HAVE_CLOSEDIR */
88 #undef LT_USE_WINDOWS_DIRENT_EMULATION
89 #ifndef LT_USE_POSIX_DIRENT
91 # define LT_USE_WINDOWS_DIRENT_EMULATION
92 # endif /* __WINDOWS__ */
93 #endif /* LT_USE_POSIX_DIRENT */
96 #ifdef LT_USE_POSIX_DIRENT
98 # define LT_D_NAMLEN(dirent) (strlen((dirent)->d_name))
100 # ifdef LT_USE_WINDOWS_DIRENT_EMULATION
101 # define LT_D_NAMLEN(dirent) (strlen((dirent)->d_name))
103 # define dirent direct
104 # define LT_D_NAMLEN(dirent) ((dirent)->d_namlen)
106 # include <sys/ndir.h>
109 # include <sys/dir.h>
124 # define assert(arg) ((void) 0)
130 # include <dmalloc.h>
136 /* --- WINDOWS SUPPORT --- */
140 # define LT_GLOBAL_DATA __declspec(dllexport)
142 # define LT_GLOBAL_DATA
145 /* fopen() mode flags for reading a text file */
146 #undef LT_READTEXT_MODE
148 # define LT_READTEXT_MODE "rt"
150 # define LT_READTEXT_MODE "r"
153 #ifdef LT_USE_WINDOWS_DIRENT_EMULATION
157 #define dirent lt_dirent
169 WIN32_FIND_DATA Win32FindData
;
171 struct dirent file_info
;
174 #endif /* LT_USE_WINDOWS_DIRENT_EMULATION */
177 /* --- MANIFEST CONSTANTS --- */
180 /* Standard libltdl search path environment variable name */
181 #undef LTDL_SEARCHPATH_VAR
182 #define LTDL_SEARCHPATH_VAR "LTDL_LIBRARY_PATH"
184 /* Standard libtool archive file extension. */
185 #undef LTDL_ARCHIVE_EXT
186 #define LTDL_ARCHIVE_EXT ".la"
188 /* max. filename length */
189 #ifndef LT_FILENAME_MAX
190 # define LT_FILENAME_MAX 1024
193 /* This is the maximum symbol size that won't require malloc/free */
194 #undef LT_SYMBOL_LENGTH
195 #define LT_SYMBOL_LENGTH 128
197 /* This accounts for the _LTX_ separator */
198 #undef LT_SYMBOL_OVERHEAD
199 #define LT_SYMBOL_OVERHEAD 5
204 /* --- MEMORY HANDLING --- */
207 /* These are the functions used internally. In addition to making
208 use of the associated function pointers above, they also perform
210 static char *lt_estrdup
LT_PARAMS((const char *str
));
211 static lt_ptr lt_emalloc
LT_PARAMS((size_t size
));
212 static lt_ptr lt_erealloc
LT_PARAMS((lt_ptr addr
, size_t size
));
214 /* static lt_ptr rpl_realloc LT_PARAMS((lt_ptr ptr, size_t size)); */
215 #define rpl_realloc realloc
217 /* These are the pointers that can be changed by the caller: */
218 LT_GLOBAL_DATA
lt_ptr (*lt_dlmalloc
) LT_PARAMS((size_t size
))
219 = (lt_ptr (*) LT_PARAMS((size_t))) malloc
;
220 LT_GLOBAL_DATA
lt_ptr (*lt_dlrealloc
) LT_PARAMS((lt_ptr ptr
, size_t size
))
221 = (lt_ptr (*) LT_PARAMS((lt_ptr
, size_t))) rpl_realloc
;
222 LT_GLOBAL_DATA
void (*lt_dlfree
) LT_PARAMS((lt_ptr ptr
))
223 = (void (*) LT_PARAMS((lt_ptr
))) free
;
225 /* The following macros reduce the amount of typing needed to cast
229 #define LT_DLMALLOC(tp, n) ((tp *) xmalloc ((n) * sizeof(tp)))
230 #define LT_DLREALLOC(tp, p, n) ((tp *) xrealloc ((p), (n) * sizeof(tp)))
231 #define LT_DLFREE(p) \
232 LT_STMT_START { if (p) (p) = (xfree (p), (lt_ptr) 0); } LT_STMT_END
234 #define LT_EMALLOC(tp, n) ((tp *) xmalloc ((n) * sizeof(tp)))
235 #define LT_EREALLOC(tp, p, n) ((tp *) xrealloc ((p), (n) * sizeof(tp)))
239 #define LT_DLMALLOC(tp, n) ((tp *) lt_dlmalloc ((n) * sizeof(tp)))
240 #define LT_DLREALLOC(tp, p, n) ((tp *) rpl_realloc ((p), (n) * sizeof(tp)))
241 #define LT_DLFREE(p) \
242 LT_STMT_START { if (p) (p) = (lt_dlfree (p), (lt_ptr) 0); } LT_STMT_END
244 #define LT_EMALLOC(tp, n) ((tp *) lt_emalloc ((n) * sizeof(tp)))
245 #define LT_EREALLOC(tp, p, n) ((tp *) lt_erealloc ((p), (n) * sizeof(tp)))
249 #define LT_DLMEM_REASSIGN(p, q) LT_STMT_START { \
250 if ((p) != (q)) { if (p) lt_dlfree (p); (p) = (q); (q) = 0; } \
254 /* --- REPLACEMENT FUNCTIONS --- */
258 #define strdup rpl_strdup
260 static char *strdup
LT_PARAMS((const char *str
));
270 tmp
= LT_DLMALLOC (char, 1+ strlen (str
));
284 #define strcmp rpl_strcmp
286 static int strcmp
LT_PARAMS((const char *str1
, const char *str2
));
300 for (;*str1
&& *str2
; ++str1
, ++str2
)
306 return (int)(*str1
- *str2
);
314 # define strchr index
316 # define strchr rpl_strchr
318 static const char *strchr
LT_PARAMS((const char *str
, int ch
));
327 for (p
= str
; *p
!= (char)ch
&& *p
!= LT_EOS_CHAR
; ++p
)
330 return (*p
== (char)ch
) ? p
: 0;
334 #endif /* !HAVE_STRCHR */
340 # define strrchr rindex
342 # define strrchr rpl_strrchr
344 static const char *strrchr
LT_PARAMS((const char *str
, int ch
));
351 const char *p
, *q
= 0;
353 for (p
= str
; *p
!= LT_EOS_CHAR
; ++p
)
367 /* NOTE: Neither bcopy nor the memcpy implementation below can
368 reliably handle copying in overlapping areas of memory. Use
369 memmove (for which there is a fallback implmentation below)
370 if you need that behaviour. */
374 # define memcpy(dest, src, size) bcopy (src, dest, size)
376 # define memcpy rpl_memcpy
378 static lt_ptr memcpy
LT_PARAMS((lt_ptr dest
, const lt_ptr src
, size_t size
));
381 memcpy (dest
, src
, size
)
388 for (i
= 0; i
< size
; ++i
)
396 # endif /* !HAVE_BCOPY */
397 #endif /* !HAVE_MEMCPY */
400 # define memmove rpl_memmove
402 static lt_ptr memmove
LT_PARAMS((lt_ptr dest
, const lt_ptr src
, size_t size
));
405 memmove (dest
, src
, size
)
413 for (i
= 0; i
< size
; ++i
)
418 for (i
= size
-1; i
>= 0; --i
)
426 #endif /* !HAVE_MEMMOVE */
428 #ifdef LT_USE_WINDOWS_DIRENT_EMULATION
430 static void closedir
LT_PARAMS((DIR *entry
));
436 assert(entry
!= (DIR *) NULL
);
437 FindClose(entry
->hSearch
);
438 lt_dlfree((lt_ptr
)entry
);
442 static DIR * opendir
LT_PARAMS((const char *path
));
448 char file_specification
[LT_FILENAME_MAX
];
451 assert(path
!= (char *) NULL
);
452 (void) strncpy(file_specification
,path
,LT_FILENAME_MAX
-1);
453 (void) strcat(file_specification
,"\\");
454 entry
= LT_DLMALLOC (DIR,sizeof(DIR));
455 if (entry
!= (DIR *) 0)
457 entry
->firsttime
= TRUE
;
458 entry
->hSearch
= FindFirstFile(file_specification
,&entry
->Win32FindData
);
460 if (entry
->hSearch
== INVALID_HANDLE_VALUE
)
462 (void) strcat(file_specification
,"\\*.*");
463 entry
->hSearch
= FindFirstFile(file_specification
,&entry
->Win32FindData
);
464 if (entry
->hSearch
== INVALID_HANDLE_VALUE
)
474 static struct dirent
*readdir
LT_PARAMS((DIR *entry
));
476 static struct dirent
*readdir(entry
)
482 if (entry
== (DIR *) 0)
483 return((struct dirent
*) 0);
484 if (!entry
->firsttime
)
486 status
= FindNextFile(entry
->hSearch
,&entry
->Win32FindData
);
488 return((struct dirent
*) 0);
490 entry
->firsttime
= FALSE
;
491 (void) strncpy(entry
->file_info
.d_name
,entry
->Win32FindData
.cFileName
,
493 entry
->file_info
.d_namlen
= strlen(entry
->file_info
.d_name
);
494 return(&entry
->file_info
);
497 #endif /* LT_USE_WINDOWS_DIRENT_EMULATION */
499 /* According to Alexandre Oliva <oliva@lsd.ic.unicamp.br>,
500 ``realloc is not entirely portable''
501 In any case we want to use the allocator supplied by the user without
502 burdening them with an lt_dlrealloc function pointer to maintain.
503 Instead implement our own version (with known boundary conditions)
504 using lt_dlmalloc and lt_dlfree. */
507 #define realloc rpl_realloc
510 /* You can't (re)define realloc unless you also (re)define malloc.
511 Right now, this code uses the size of the *destination* to decide
512 how much to copy. That's not right, but you can't know the size
513 of the source unless you know enough about, or wrote malloc. So
514 this code is disabled... */
523 /* For zero or less bytes, free the original memory */
533 /* Allow reallocation of a NULL pointer. */
534 return lt_dlmalloc (size
);
538 /* Allocate a new block, copy and free the old block. */
539 lt_ptr mem
= lt_dlmalloc (size
);
543 memcpy (mem
, ptr
, size
);
547 /* Note that the contents of PTR are not damaged if there is
548 insufficient memory to realloc. */
555 #if ! HAVE_ARGZ_APPEND
556 # define argz_append rpl_argz_append
558 static error_t argz_append
LT_PARAMS((char **pargz
, size_t *pargz_len
,
559 const char *buf
, size_t buf_len
));
562 argz_append (pargz
, pargz_len
, buf
, buf_len
)
573 assert ((*pargz
&& *pargz_len
) || (!*pargz
&& !*pargz_len
));
575 /* If nothing needs to be appended, no more work is required. */
579 /* Ensure there is enough room to append BUF_LEN. */
580 argz_len
= *pargz_len
+ buf_len
;
581 argz
= LT_DLREALLOC (char, *pargz
, argz_len
);
585 /* Copy characters from BUF after terminating '\0' in ARGZ. */
586 memcpy (argz
+ *pargz_len
, buf
, buf_len
);
588 /* Assign new values. */
590 *pargz_len
= argz_len
;
594 #endif /* !HAVE_ARGZ_APPEND */
597 #if ! HAVE_ARGZ_CREATE_SEP
598 # define argz_create_sep rpl_argz_create_sep
600 static error_t argz_create_sep
LT_PARAMS((const char *str
, int delim
,
601 char **pargz
, size_t *pargz_len
));
604 argz_create_sep (str
, delim
, pargz
, pargz_len
)
617 /* Make a copy of STR, but replacing each occurence of
619 argz_len
= 1+ LT_STRLEN (str
);
625 argz
= LT_DLMALLOC (char, argz_len
);
629 for (p
= str
, q
= argz
; *p
!= LT_EOS_CHAR
; ++p
)
633 /* Ignore leading delimiters, and fold consecutive
634 delimiters in STR into a single '\0' in ARGZ. */
635 if ((q
> argz
) && (q
[-1] != LT_EOS_CHAR
))
643 /* Copy terminating LT_EOS_CHAR. */
647 /* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory. */
651 /* Assign new values. */
653 *pargz_len
= argz_len
;
657 #endif /* !HAVE_ARGZ_CREATE_SEP */
660 #if ! HAVE_ARGZ_INSERT
661 # define argz_insert rpl_argz_insert
663 static error_t argz_insert
LT_PARAMS((char **pargz
, size_t *pargz_len
,
664 char *before
, const char *entry
));
667 argz_insert (pargz
, pargz_len
, before
, entry
)
675 assert (entry
&& *entry
);
677 /* No BEFORE address indicates ENTRY should be inserted after the
678 current last element. */
680 return argz_append (pargz
, pargz_len
, entry
, 1+ LT_STRLEN (entry
));
682 /* This probably indicates a programmer error, but to preserve
683 semantics, scan back to the start of an entry if BEFORE points
684 into the middle of it. */
685 while ((before
>= *pargz
) && (before
[-1] != LT_EOS_CHAR
))
689 size_t entry_len
= 1+ LT_STRLEN (entry
);
690 size_t argz_len
= *pargz_len
+ entry_len
;
691 size_t offset
= before
- *pargz
;
692 char *argz
= LT_DLREALLOC (char, *pargz
, argz_len
);
697 /* Make BEFORE point to the equivalent offset in ARGZ that it
698 used to have in *PARGZ incase realloc() moved the block. */
699 before
= argz
+ offset
;
701 /* Move the ARGZ entries starting at BEFORE up into the new
702 space at the end -- making room to copy ENTRY into the
704 memmove (before
+ entry_len
, before
, *pargz_len
- offset
);
705 memcpy (before
, entry
, entry_len
);
707 /* Assign new values. */
709 *pargz_len
= argz_len
;
714 #endif /* !HAVE_ARGZ_INSERT */
718 # define argz_next rpl_argz_next
720 static char *argz_next
LT_PARAMS((char *argz
, size_t argz_len
,
724 argz_next (argz
, argz_len
, entry
)
729 assert ((argz
&& argz_len
) || (!argz
&& !argz_len
));
733 /* Either ARGZ/ARGZ_LEN is empty, or ENTRY points into an address
734 within the ARGZ vector. */
735 assert ((!argz
&& !argz_len
)
736 || ((argz
<= entry
) && (entry
< (argz
+ argz_len
))));
738 /* Move to the char immediately after the terminating
740 entry
= 1+ strchr (entry
, LT_EOS_CHAR
);
742 /* Return either the new ENTRY, or else NULL if ARGZ is
744 return (entry
>= argz
+ argz_len
) ? 0 : (char *) entry
;
748 /* This should probably be flagged as a programmer error,
749 since starting an argz_next loop with the iterator set
750 to ARGZ is safer. To preserve semantics, handle the NULL
751 case by returning the start of ARGZ (if any). */
758 #endif /* !HAVE_ARGZ_NEXT */
762 #if ! HAVE_ARGZ_STRINGIFY
763 # define argz_stringify rpl_argz_stringify
765 static void argz_stringify
LT_PARAMS((char *argz
, size_t argz_len
,
769 argz_stringify (argz
, argz_len
, sep
)
774 assert ((argz
&& argz_len
) || (!argz
&& !argz_len
));
778 --argz_len
; /* don't stringify the terminating EOS */
779 while (--argz_len
> 0)
781 if (argz
[argz_len
] == LT_EOS_CHAR
)
782 argz
[argz_len
] = sep
;
786 #endif /* !HAVE_ARGZ_STRINGIFY */
791 /* --- TYPE DEFINITIONS -- */
794 /* This type is used for the array of caller data sets in each handler. */
803 /* --- OPAQUE STRUCTURES DECLARED IN LTDL.H --- */
806 /* Extract the diagnostic strings from the error table macro in the same
807 order as the enumerated indices in ltdl.h. */
809 static const char *lt_dlerror_strings
[] =
811 #define LT_ERROR(name, diagnostic) (diagnostic),
818 /* This structure is used for the list of registered loaders. */
820 struct lt_dlloader
*next
;
821 const char *loader_name
; /* identifying name for each loader */
822 const char *sym_prefix
; /* prefix for symbols */
823 lt_module_open
*module_open
;
824 lt_module_close
*module_close
;
825 lt_find_sym
*find_sym
;
826 lt_dlloader_exit
*dlloader_exit
;
827 lt_user_data dlloader_data
;
830 struct lt_dlhandle_struct
{
831 struct lt_dlhandle_struct
*next
;
832 lt_dlloader
*loader
; /* dlopening interface */
834 int depcount
; /* number of dependencies */
835 lt_dlhandle
*deplibs
; /* dependencies */
836 lt_module module
; /* system module handle */
837 lt_ptr system
; /* system specific data */
838 lt_caller_data
*caller_data
; /* per caller associated data */
839 int flags
; /* various boolean stats */
842 /* Various boolean flags can be stored in the flags field of an
843 lt_dlhandle_struct... */
844 #define LT_DLGET_FLAG(handle, flag) (((handle)->flags & (flag)) == (flag))
845 #define LT_DLSET_FLAG(handle, flag) ((handle)->flags |= (flag))
847 #define LT_DLRESIDENT_FLAG (0x01 << 0)
848 /* ...add more flags here... */
850 #define LT_DLIS_RESIDENT(handle) LT_DLGET_FLAG(handle, LT_DLRESIDENT_FLAG)
853 #define LT_DLSTRERROR(name) lt_dlerror_strings[LT_CONC(LT_ERROR_,name)]
855 static const char objdir
[] = LTDL_OBJDIR
;
856 static const char archive_ext
[] = LTDL_ARCHIVE_EXT
;
857 #ifdef LTDL_SHLIB_EXT
858 static const char shlib_ext
[] = LTDL_SHLIB_EXT
;
860 #ifdef LTDL_SYSSEARCHPATH
861 static const char sys_search_path
[] = LTDL_SYSSEARCHPATH
;
867 /* --- MUTEX LOCKING --- */
870 /* Macros to make it easier to run the lock functions only if they have
871 been registered. The reason for the complicated lock macro is to
872 ensure that the stored error message from the last error is not
873 accidentally erased if the current function doesn't generate an
875 #define LT_DLMUTEX_LOCK() LT_STMT_START { \
876 if (lt_dlmutex_lock_func) (*lt_dlmutex_lock_func)(); \
878 #define LT_DLMUTEX_UNLOCK() LT_STMT_START { \
879 if (lt_dlmutex_unlock_func) (*lt_dlmutex_unlock_func)();\
881 #define LT_DLMUTEX_SETERROR(errormsg) LT_STMT_START { \
882 if (lt_dlmutex_seterror_func) \
883 (*lt_dlmutex_seterror_func) (errormsg); \
884 else lt_dllast_error = (errormsg); } LT_STMT_END
885 #define LT_DLMUTEX_GETERROR(errormsg) LT_STMT_START { \
886 if (lt_dlmutex_seterror_func) \
887 (errormsg) = (*lt_dlmutex_geterror_func) (); \
888 else (errormsg) = lt_dllast_error; } LT_STMT_END
890 /* The mutex functions stored here are global, and are necessarily the
891 same for all threads that wish to share access to libltdl. */
892 static lt_dlmutex_lock
*lt_dlmutex_lock_func
= 0;
893 static lt_dlmutex_unlock
*lt_dlmutex_unlock_func
= 0;
894 static lt_dlmutex_seterror
*lt_dlmutex_seterror_func
= 0;
895 static lt_dlmutex_geterror
*lt_dlmutex_geterror_func
= 0;
896 static const char *lt_dllast_error
= 0;
899 /* Either set or reset the mutex functions. Either all the arguments must
900 be valid functions, or else all can be NULL to turn off locking entirely.
901 The registered functions should be manipulating a static global lock
902 from the lock() and unlock() callbacks, which needs to be reentrant. */
904 lt_dlmutex_register (lock
, unlock
, seterror
, geterror
)
905 lt_dlmutex_lock
*lock
;
906 lt_dlmutex_unlock
*unlock
;
907 lt_dlmutex_seterror
*seterror
;
908 lt_dlmutex_geterror
*geterror
;
910 lt_dlmutex_unlock
*old_unlock
= unlock
;
913 /* Lock using the old lock() callback, if any. */
916 if ((lock
&& unlock
&& seterror
&& geterror
)
917 || !(lock
|| unlock
|| seterror
|| geterror
))
919 lt_dlmutex_lock_func
= lock
;
920 lt_dlmutex_unlock_func
= unlock
;
921 lt_dlmutex_geterror_func
= geterror
;
925 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_MUTEX_ARGS
));
929 /* Use the old unlock() callback we saved earlier, if any. Otherwise
930 record any errors using internal storage. */
934 /* Return the number of errors encountered during the execution of
942 /* --- ERROR HANDLING --- */
945 static const char **user_error_strings
= 0;
946 static int errorcount
= LT_ERROR_MAX
;
949 lt_dladderror (diagnostic
)
950 const char *diagnostic
;
954 const char **temp
= (const char **) 0;
960 errindex
= errorcount
- LT_ERROR_MAX
;
961 temp
= LT_EREALLOC (const char *, user_error_strings
, 1 + errindex
);
964 user_error_strings
= temp
;
965 user_error_strings
[errindex
] = diagnostic
;
966 result
= errorcount
++;
969 LT_DLMUTEX_UNLOCK ();
975 lt_dlseterror (errindex
)
982 if (errindex
>= errorcount
|| errindex
< 0)
984 /* Ack! Error setting the error message! */
985 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_ERRORCODE
));
988 else if (errindex
< LT_ERROR_MAX
)
990 /* No error setting the error message! */
991 LT_DLMUTEX_SETERROR (lt_dlerror_strings
[errindex
]);
995 /* No error setting the error message! */
996 LT_DLMUTEX_SETERROR (user_error_strings
[errindex
- LT_ERROR_MAX
]);
999 LT_DLMUTEX_UNLOCK ();
1008 lt_ptr mem
= lt_dlmalloc (size
);
1010 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY
));
1015 lt_erealloc (addr
, size
)
1019 lt_ptr mem
= realloc (addr
, size
);
1021 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY
));
1029 char *copy
= strdup (str
);
1030 if (LT_STRLEN (str
) && !copy
)
1031 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY
));
1038 /* --- DLOPEN() INTERFACE LOADER --- */
1043 /* dynamic linking with dlopen/dlsym */
1050 # include <sys/dl.h>
1054 # define LT_GLOBAL RTLD_GLOBAL
1057 # define LT_GLOBAL DL_GLOBAL
1059 #endif /* !RTLD_GLOBAL */
1061 # define LT_GLOBAL 0
1062 #endif /* !LT_GLOBAL */
1064 /* We may have to define LT_LAZY_OR_NOW in the command line if we
1065 find out it does not work in some platform. */
1066 #ifndef LT_LAZY_OR_NOW
1068 # define LT_LAZY_OR_NOW RTLD_LAZY
1071 # define LT_LAZY_OR_NOW DL_LAZY
1073 # endif /* !RTLD_LAZY */
1075 #ifndef LT_LAZY_OR_NOW
1077 # define LT_LAZY_OR_NOW RTLD_NOW
1080 # define LT_LAZY_OR_NOW DL_NOW
1082 # endif /* !RTLD_NOW */
1084 #ifndef LT_LAZY_OR_NOW
1085 # define LT_LAZY_OR_NOW 0
1086 #endif /* !LT_LAZY_OR_NOW */
1089 # define DLERROR(arg) dlerror ()
1091 # define DLERROR(arg) LT_DLSTRERROR (arg)
1095 sys_dl_open (loader_data
, filename
)
1096 lt_user_data loader_data
;
1097 const char *filename
;
1099 lt_module module
= dlopen (filename
, LT_GLOBAL
| LT_LAZY_OR_NOW
);
1103 LT_DLMUTEX_SETERROR (DLERROR (CANNOT_OPEN
));
1110 sys_dl_close (loader_data
, module
)
1111 lt_user_data loader_data
;
1116 if (dlclose (module
) != 0)
1118 LT_DLMUTEX_SETERROR (DLERROR (CANNOT_CLOSE
));
1126 sys_dl_sym (loader_data
, module
, symbol
)
1127 lt_user_data loader_data
;
1131 lt_ptr address
= dlsym (module
, symbol
);
1135 LT_DLMUTEX_SETERROR (DLERROR (SYMBOL_NOT_FOUND
));
1141 static struct lt_user_dlloader sys_dl
=
1148 sys_dl_open
, sys_dl_close
, sys_dl_sym
, 0, 0 };
1151 #endif /* HAVE_LIBDL */
1155 /* --- SHL_LOAD() INTERFACE LOADER --- */
1159 /* dynamic linking with shl_load (HP-UX) (comments from gmodule) */
1165 /* some flags are missing on some systems, so we provide
1166 * harmless defaults.
1169 * BIND_IMMEDIATE - Resolve symbol references when the library is loaded.
1170 * BIND_DEFERRED - Delay code symbol resolution until actual reference.
1173 * BIND_FIRST - Place the library at the head of the symbol search
1175 * BIND_NONFATAL - The default BIND_IMMEDIATE behavior is to treat all
1176 * unsatisfied symbols as fatal. This flag allows
1177 * binding of unsatisfied code symbols to be deferred
1179 * [Perl: For certain libraries, like DCE, deferred
1180 * binding often causes run time problems. Adding
1181 * BIND_NONFATAL to BIND_IMMEDIATE still allows
1182 * unresolved references in situations like this.]
1183 * BIND_NOSTART - Do not call the initializer for the shared library
1184 * when the library is loaded, nor on a future call to
1186 * BIND_VERBOSE - Print verbose messages concerning possible
1187 * unsatisfied symbols.
1189 * hp9000s700/hp9000s800:
1190 * BIND_RESTRICTED - Restrict symbols visible by the library to those
1191 * present at library load time.
1192 * DYNAMIC_PATH - Allow the loader to dynamically search for the
1193 * library specified by the path argument.
1196 #ifndef DYNAMIC_PATH
1197 # define DYNAMIC_PATH 0
1199 #ifndef BIND_RESTRICTED
1200 # define BIND_RESTRICTED 0
1203 #define LT_BIND_FLAGS (BIND_IMMEDIATE | BIND_NONFATAL | DYNAMIC_PATH)
1206 sys_shl_open (loader_data
, filename
)
1207 lt_user_data loader_data
;
1208 const char *filename
;
1210 static shl_t self
= (shl_t
) 0;
1211 lt_module module
= shl_load (filename
, LT_BIND_FLAGS
, 0L);
1213 /* Since searching for a symbol against a NULL module handle will also
1214 look in everything else that was already loaded and exported with
1215 the -E compiler flag, we always cache a handle saved before any
1216 modules are loaded. */
1220 shl_findsym (&self
, "main", TYPE_UNDEFINED
, &address
);
1229 module
= shl_load (filename
, LT_BIND_FLAGS
, 0L);
1233 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_OPEN
));
1241 sys_shl_close (loader_data
, module
)
1242 lt_user_data loader_data
;
1247 if (module
&& (shl_unload ((shl_t
) (module
)) != 0))
1249 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_CLOSE
));
1257 sys_shl_sym (loader_data
, module
, symbol
)
1258 lt_user_data loader_data
;
1264 /* sys_shl_open should never return a NULL module handle */
1265 if (module
== (lt_module
) 0)
1267 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_HANDLE
));
1269 else if (!shl_findsym((shl_t
*) &module
, symbol
, TYPE_UNDEFINED
, &address
))
1273 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SYMBOL_NOT_FOUND
));
1280 static struct lt_user_dlloader sys_shl
= {
1281 0, sys_shl_open
, sys_shl_close
, sys_shl_sym
, 0, 0
1284 #endif /* HAVE_SHL_LOAD */
1289 /* --- LOADLIBRARY() INTERFACE LOADER --- */
1293 /* dynamic linking for Win32 */
1295 #include <windows.h>
1297 /* Forward declaration; required to implement handle search below. */
1298 static lt_dlhandle handles
;
1301 sys_wll_open (loader_data
, filename
)
1302 lt_user_data loader_data
;
1303 const char *filename
;
1306 lt_module module
= 0;
1307 const char *errormsg
= 0;
1308 char *searchname
= 0;
1310 char self_name_buf
[MAX_PATH
];
1314 /* Get the name of main module */
1316 GetModuleFileName (NULL
, self_name_buf
, sizeof (self_name_buf
));
1317 filename
= ext
= self_name_buf
;
1321 ext
= strrchr (filename
, '.');
1326 /* FILENAME already has an extension. */
1327 searchname
= lt_estrdup (filename
);
1331 /* Append a `.' to stop Windows from adding an
1332 implicit `.dll' extension. */
1333 searchname
= LT_EMALLOC (char, 2+ LT_STRLEN (filename
));
1335 sprintf (searchname
, "%s.", filename
);
1342 char wpath
[MAX_PATH
];
1343 cygwin_conv_to_full_win32_path(searchname
, wpath
);
1344 module
= LoadLibrary(wpath
);
1347 module
= LoadLibrary (searchname
);
1349 LT_DLFREE (searchname
);
1351 /* libltdl expects this function to fail if it is unable
1352 to physically load the library. Sadly, LoadLibrary
1353 will search the loaded libraries for a match and return
1354 one of them if the path search load fails.
1356 We check whether LoadLibrary is returning a handle to
1357 an already loaded module, and simulate failure if we
1369 if (cur
->module
== module
)
1376 LT_DLMUTEX_UNLOCK ();
1380 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_OPEN
));
1388 sys_wll_close (loader_data
, module
)
1389 lt_user_data loader_data
;
1394 if (FreeLibrary(module
) == 0)
1396 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_CLOSE
));
1404 sys_wll_sym (loader_data
, module
, symbol
)
1405 lt_user_data loader_data
;
1409 lt_ptr address
= GetProcAddress (module
, symbol
);
1413 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SYMBOL_NOT_FOUND
));
1419 static struct lt_user_dlloader sys_wll
= {
1420 0, sys_wll_open
, sys_wll_close
, sys_wll_sym
, 0, 0
1423 #endif /* __WINDOWS__ */
1428 /* --- LOAD_ADD_ON() INTERFACE LOADER --- */
1433 /* dynamic linking for BeOS */
1435 #include <kernel/image.h>
1438 sys_bedl_open (loader_data
, filename
)
1439 lt_user_data loader_data
;
1440 const char *filename
;
1446 image
= load_add_on (filename
);
1452 if (get_next_image_info (0, &cookie
, &info
) == B_OK
)
1453 image
= load_add_on (info
.name
);
1458 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_OPEN
));
1462 return (lt_module
) image
;
1466 sys_bedl_close (loader_data
, module
)
1467 lt_user_data loader_data
;
1472 if (unload_add_on ((image_id
) module
) != B_OK
)
1474 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_CLOSE
));
1482 sys_bedl_sym (loader_data
, module
, symbol
)
1483 lt_user_data loader_data
;
1488 image_id image
= (image_id
) module
;
1490 if (get_image_symbol (image
, symbol
, B_SYMBOL_TYPE_ANY
, address
) != B_OK
)
1492 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SYMBOL_NOT_FOUND
));
1499 static struct lt_user_dlloader sys_bedl
= {
1500 0, sys_bedl_open
, sys_bedl_close
, sys_bedl_sym
, 0, 0
1503 #endif /* __BEOS__ */
1508 /* --- DLD_LINK() INTERFACE LOADER --- */
1513 /* dynamic linking with dld */
1520 sys_dld_open (loader_data
, filename
)
1521 lt_user_data loader_data
;
1522 const char *filename
;
1524 lt_module module
= strdup (filename
);
1526 if (dld_link (filename
) != 0)
1528 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_OPEN
));
1537 sys_dld_close (loader_data
, module
)
1538 lt_user_data loader_data
;
1543 if (dld_unlink_by_file ((char*)(module
), 1) != 0)
1545 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CANNOT_CLOSE
));
1557 sys_dld_sym (loader_data
, module
, symbol
)
1558 lt_user_data loader_data
;
1562 lt_ptr address
= dld_get_func (symbol
);
1566 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SYMBOL_NOT_FOUND
));
1572 static struct lt_user_dlloader sys_dld
= {
1573 0, sys_dld_open
, sys_dld_close
, sys_dld_sym
, 0, 0
1576 #endif /* HAVE_DLD */
1581 /* --- DLPREOPEN() INTERFACE LOADER --- */
1584 /* emulate dynamic linking using preloaded_symbols */
1586 typedef struct lt_dlsymlists_t
1588 struct lt_dlsymlists_t
*next
;
1589 const lt_dlsymlist
*syms
;
1592 static const lt_dlsymlist
*default_preloaded_symbols
= 0;
1593 static lt_dlsymlists_t
*preloaded_symbols
= 0;
1596 presym_init (loader_data
)
1597 lt_user_data loader_data
;
1603 preloaded_symbols
= 0;
1604 if (default_preloaded_symbols
)
1606 errors
= lt_dlpreload (default_preloaded_symbols
);
1609 LT_DLMUTEX_UNLOCK ();
1615 presym_free_symlists ()
1617 lt_dlsymlists_t
*lists
;
1621 lists
= preloaded_symbols
;
1624 lt_dlsymlists_t
*tmp
= lists
;
1626 lists
= lists
->next
;
1629 preloaded_symbols
= 0;
1631 LT_DLMUTEX_UNLOCK ();
1637 presym_exit (loader_data
)
1638 lt_user_data loader_data
;
1640 presym_free_symlists ();
1645 presym_add_symlist (preloaded
)
1646 const lt_dlsymlist
*preloaded
;
1648 lt_dlsymlists_t
*tmp
;
1649 lt_dlsymlists_t
*lists
;
1654 lists
= preloaded_symbols
;
1657 if (lists
->syms
== preloaded
)
1661 lists
= lists
->next
;
1664 tmp
= LT_EMALLOC (lt_dlsymlists_t
, 1);
1667 memset (tmp
, 0, sizeof(lt_dlsymlists_t
));
1668 tmp
->syms
= preloaded
;
1669 tmp
->next
= preloaded_symbols
;
1670 preloaded_symbols
= tmp
;
1678 LT_DLMUTEX_UNLOCK ();
1683 presym_open (loader_data
, filename
)
1684 lt_user_data loader_data
;
1685 const char *filename
;
1687 lt_dlsymlists_t
*lists
;
1688 lt_module module
= (lt_module
) 0;
1691 lists
= preloaded_symbols
;
1695 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_SYMBOLS
));
1699 /* Can't use NULL as the reflective symbol header, as NULL is
1700 used to mark the end of the entire symbol list. Self-dlpreopened
1701 symbols follow this magic number, chosen to be an unlikely
1702 clash with a real module name. */
1705 filename
= "@PROGRAM@";
1710 const lt_dlsymlist
*syms
= lists
->syms
;
1714 if (!syms
->address
&& strcmp(syms
->name
, filename
) == 0)
1716 module
= (lt_module
) syms
;
1722 lists
= lists
->next
;
1725 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (FILE_NOT_FOUND
));
1728 LT_DLMUTEX_UNLOCK ();
1733 presym_close (loader_data
, module
)
1734 lt_user_data loader_data
;
1737 /* Just to silence gcc -Wall */
1743 presym_sym (loader_data
, module
, symbol
)
1744 lt_user_data loader_data
;
1748 lt_dlsymlist
*syms
= (lt_dlsymlist
*) module
;
1751 while (syms
->address
)
1753 if (strcmp(syms
->name
, symbol
) == 0)
1755 return syms
->address
;
1761 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SYMBOL_NOT_FOUND
));
1766 static struct lt_user_dlloader presym
= {
1767 0, presym_open
, presym_close
, presym_sym
, presym_exit
, 0
1774 /* --- DYNAMIC MODULE LOADING --- */
1777 /* The type of a function used at each iteration of foreach_dirinpath(). */
1778 typedef int foreach_callback_func
LT_PARAMS((char *filename
, lt_ptr data1
,
1781 static int foreach_dirinpath
LT_PARAMS((const char *search_path
,
1782 const char *base_name
,
1783 foreach_callback_func
*func
,
1784 lt_ptr data1
, lt_ptr data2
));
1786 static int find_file_callback
LT_PARAMS((char *filename
, lt_ptr data
,
1788 static int find_handle_callback
LT_PARAMS((char *filename
, lt_ptr data
,
1790 static int foreachfile_callback
LT_PARAMS((char *filename
, lt_ptr data1
,
1794 static int canonicalize_path
LT_PARAMS((const char *path
,
1795 char **pcanonical
));
1796 static int argzize_path
LT_PARAMS((const char *path
,
1798 size_t *pargz_len
));
1799 static FILE *find_file
LT_PARAMS((const char *search_path
,
1800 const char *base_name
,
1802 static lt_dlhandle
*find_handle
LT_PARAMS((const char *search_path
,
1803 const char *base_name
,
1804 lt_dlhandle
*handle
));
1805 static int find_module
LT_PARAMS((lt_dlhandle
*handle
,
1809 const char *old_name
,
1811 static int free_vars
LT_PARAMS((char *dlname
, char *oldname
,
1812 char *libdir
, char *deplibs
));
1813 static int load_deplibs
LT_PARAMS((lt_dlhandle handle
,
1815 static int trim
LT_PARAMS((char **dest
,
1817 static int try_dlopen
LT_PARAMS((lt_dlhandle
*handle
,
1818 const char *filename
));
1819 static int tryall_dlopen
LT_PARAMS((lt_dlhandle
*handle
,
1820 const char *filename
));
1821 static int unload_deplibs
LT_PARAMS((lt_dlhandle handle
));
1822 static int lt_argz_insert
LT_PARAMS((char **pargz
,
1825 const char *entry
));
1826 static int lt_argz_insertinorder
LT_PARAMS((char **pargz
,
1828 const char *entry
));
1829 static int lt_argz_insertdir
LT_PARAMS((char **pargz
,
1832 struct dirent
*dp
));
1833 static int lt_dlpath_insertdir
LT_PARAMS((char **ppath
,
1836 static int list_files_by_dir
LT_PARAMS((const char *dirnam
,
1838 size_t *pargz_len
));
1839 static int file_not_found
LT_PARAMS((void));
1841 static char *user_search_path
= 0;
1842 static lt_dlloader
*loaders
= 0;
1843 static lt_dlhandle handles
= 0;
1844 static int initialized
= 0;
1846 /* Initialize libltdl. */
1854 /* Initialize only at first call. */
1855 if (++initialized
== 1)
1858 user_search_path
= 0; /* empty search path */
1861 errors
+= lt_dlloader_add (lt_dlloader_next (0), &sys_dl
, "dlopen");
1864 errors
+= lt_dlloader_add (lt_dlloader_next (0), &sys_shl
, "dlopen");
1867 errors
+= lt_dlloader_add (lt_dlloader_next (0), &sys_wll
, "dlopen");
1870 errors
+= lt_dlloader_add (lt_dlloader_next (0), &sys_bedl
, "dlopen");
1873 errors
+= lt_dlloader_add (lt_dlloader_next (0), &sys_dld
, "dld");
1875 errors
+= lt_dlloader_add (lt_dlloader_next (0), &presym
, "dlpreload");
1877 if (presym_init (presym
.dlloader_data
))
1879 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INIT_LOADER
));
1882 else if (errors
!= 0)
1884 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (DLOPEN_NOT_SUPPORTED
));
1889 LT_DLMUTEX_UNLOCK ();
1895 lt_dlpreload (preloaded
)
1896 const lt_dlsymlist
*preloaded
;
1902 errors
= presym_add_symlist (preloaded
);
1906 presym_free_symlists();
1909 if (default_preloaded_symbols
)
1911 errors
= lt_dlpreload (default_preloaded_symbols
);
1913 LT_DLMUTEX_UNLOCK ();
1920 lt_dlpreload_default (preloaded
)
1921 const lt_dlsymlist
*preloaded
;
1924 default_preloaded_symbols
= preloaded
;
1925 LT_DLMUTEX_UNLOCK ();
1932 /* shut down libltdl */
1933 lt_dlloader
*loader
;
1941 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SHUTDOWN
));
1946 /* shut down only at last call. */
1947 if (--initialized
== 0)
1951 while (handles
&& LT_DLIS_RESIDENT (handles
))
1953 handles
= handles
->next
;
1956 /* close all modules */
1957 for (level
= 1; handles
; ++level
)
1959 lt_dlhandle cur
= handles
;
1960 int saw_nonresident
= 0;
1964 lt_dlhandle tmp
= cur
;
1966 if (!LT_DLIS_RESIDENT (tmp
))
1967 saw_nonresident
= 1;
1968 if (!LT_DLIS_RESIDENT (tmp
) && tmp
->info
.ref_count
<= level
)
1970 if (lt_dlclose (tmp
))
1976 /* done if only resident modules are left */
1977 if (!saw_nonresident
)
1981 /* close all loaders */
1984 lt_dlloader
*next
= loader
->next
;
1985 lt_user_data data
= loader
->dlloader_data
;
1986 if (loader
->dlloader_exit
&& loader
->dlloader_exit (data
))
1991 LT_DLMEM_REASSIGN (loader
, next
);
1997 LT_DLMUTEX_UNLOCK ();
2002 tryall_dlopen (handle
, filename
)
2003 lt_dlhandle
*handle
;
2004 const char *filename
;
2007 lt_dlloader
*loader
;
2008 const char *saved_error
;
2011 LT_DLMUTEX_GETERROR (saved_error
);
2017 /* check whether the module was already opened */
2020 /* try to dlopen the program itself? */
2021 if (!cur
->info
.filename
&& !filename
)
2026 if (cur
->info
.filename
&& filename
2027 && strcmp (cur
->info
.filename
, filename
) == 0)
2037 ++cur
->info
.ref_count
;
2045 /* Comment out the check of file permissions using access.
2046 This call seems to always return -1 with error EACCES.
2048 /* We need to catch missing file errors early so that
2049 file_not_found() can detect what happened.
2050 if (access (filename, R_OK) != 0)
2052 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (FILE_NOT_FOUND));
2057 cur
->info
.filename
= lt_estrdup (filename
);
2058 if (!cur
->info
.filename
)
2066 cur
->info
.filename
= 0;
2071 lt_user_data data
= loader
->dlloader_data
;
2073 cur
->module
= loader
->module_open (data
, filename
);
2075 if (cur
->module
!= 0)
2079 loader
= loader
->next
;
2084 LT_DLFREE (cur
->info
.filename
);
2089 cur
->loader
= loader
;
2090 LT_DLMUTEX_SETERROR (saved_error
);
2093 LT_DLMUTEX_UNLOCK ();
2099 tryall_dlopen_module (handle
, prefix
, dirname
, dlname
)
2100 lt_dlhandle
*handle
;
2102 const char *dirname
;
2107 size_t filename_len
= 0;
2108 size_t dirname_len
= LT_STRLEN (dirname
);
2113 #ifdef LT_DIRSEP_CHAR
2114 /* Only canonicalized names (i.e. with DIRSEP chars already converted)
2115 should make it into this function: */
2116 assert (strchr (dirname
, LT_DIRSEP_CHAR
) == 0);
2119 if (dirname_len
> 0)
2120 if (dirname
[dirname_len
-1] == '/')
2122 filename_len
= dirname_len
+ 1 + LT_STRLEN (dlname
);
2124 /* Allocate memory, and combine DIRNAME and MODULENAME into it.
2125 The PREFIX (if any) is handled below. */
2126 filename
= LT_EMALLOC (char, dirname_len
+ 1 + filename_len
+ 1);
2130 sprintf (filename
, "%.*s/%s", (int) dirname_len
, dirname
, dlname
);
2132 /* Now that we have combined DIRNAME and MODULENAME, if there is
2133 also a PREFIX to contend with, simply recurse with the arguments
2134 shuffled. Otherwise, attempt to open FILENAME as a module. */
2137 error
+= tryall_dlopen_module (handle
,
2138 (const char *) 0, prefix
, filename
);
2140 else if (tryall_dlopen (handle
, filename
) != 0)
2145 LT_DLFREE (filename
);
2150 find_module (handle
, dir
, libdir
, dlname
, old_name
, installed
)
2151 lt_dlhandle
*handle
;
2155 const char *old_name
;
2158 /* Try to open the old library first; if it was dlpreopened,
2159 we want the preopened version of it, even if a dlopenable
2160 module is available. */
2161 if (old_name
&& tryall_dlopen (handle
, old_name
) == 0)
2166 /* Try to open the dynamic library. */
2169 /* try to open the installed module */
2170 if (installed
&& libdir
)
2172 if (tryall_dlopen_module (handle
,
2173 (const char *) 0, libdir
, dlname
) == 0)
2177 /* try to open the not-installed module */
2180 if (tryall_dlopen_module (handle
, dir
, objdir
, dlname
) == 0)
2184 /* maybe it was moved to another directory */
2186 if (tryall_dlopen_module (handle
,
2187 (const char *) 0, dir
, dlname
) == 0)
2197 canonicalize_path (path
, pcanonical
)
2201 char *canonical
= 0;
2203 assert (path
&& *path
);
2204 assert (pcanonical
);
2206 canonical
= LT_EMALLOC (char, 1+ LT_STRLEN (path
));
2213 for (src
= 0; path
[src
] != LT_EOS_CHAR
; ++src
)
2215 /* Path separators are not copied to the beginning or end of
2216 the destination, or if another separator would follow
2218 if (path
[src
] == LT_PATHSEP_CHAR
)
2221 || (path
[1+ src
] == LT_PATHSEP_CHAR
)
2222 || (path
[1+ src
] == LT_EOS_CHAR
))
2226 /* Anything other than a directory separator is copied verbatim. */
2227 if ((path
[src
] != '/')
2228 #ifdef LT_DIRSEP_CHAR
2229 && (path
[src
] != LT_DIRSEP_CHAR
)
2233 canonical
[dest
++] = path
[src
];
2235 /* Directory separators are converted and copied only if they are
2236 not at the end of a path -- i.e. before a path separator or
2238 else if ((path
[1+ src
] != LT_PATHSEP_CHAR
)
2239 && (path
[1+ src
] != LT_EOS_CHAR
)
2240 #ifdef LT_DIRSEP_CHAR
2241 && (path
[1+ src
] != LT_DIRSEP_CHAR
)
2243 && (path
[1+ src
] != '/'))
2245 canonical
[dest
++] = '/';
2249 /* Add an end-of-string marker at the end. */
2250 canonical
[dest
] = LT_EOS_CHAR
;
2253 /* Assign new value. */
2254 *pcanonical
= canonical
;
2260 argzize_path (path
, pargz
, pargz_len
)
2271 if ((error
= argz_create_sep (path
, LT_PATHSEP_CHAR
, pargz
, pargz_len
)))
2276 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY
));
2279 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (UNKNOWN
));
2289 /* Repeatedly call FUNC with each LT_PATHSEP_CHAR delimited element
2290 of SEARCH_PATH and references to DATA1 and DATA2, until FUNC returns
2291 non-zero or all elements are exhausted. If BASE_NAME is non-NULL,
2292 it is appended to each SEARCH_PATH element before FUNC is called. */
2294 foreach_dirinpath (search_path
, base_name
, func
, data1
, data2
)
2295 const char *search_path
;
2296 const char *base_name
;
2297 foreach_callback_func
*func
;
2302 int filenamesize
= 0;
2303 size_t lenbase
= LT_STRLEN (base_name
);
2304 size_t argz_len
= 0;
2307 char *canonical
= 0;
2311 if (!search_path
|| !*search_path
)
2313 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (FILE_NOT_FOUND
));
2317 if (canonicalize_path (search_path
, &canonical
) != 0)
2320 if (argzize_path (canonical
, &argz
, &argz_len
) != 0)
2325 while ((dir_name
= argz_next (argz
, argz_len
, dir_name
)))
2327 size_t lendir
= LT_STRLEN (dir_name
);
2329 if (lendir
+1 +lenbase
>= filenamesize
)
2331 LT_DLFREE (filename
);
2332 filenamesize
= lendir
+1 +lenbase
+1; /* "/d" + '/' + "f" + '\0' */
2333 filename
= LT_EMALLOC (char, filenamesize
);
2338 assert (filenamesize
> lendir
);
2339 strcpy (filename
, dir_name
);
2341 if (base_name
&& *base_name
)
2343 if (filename
[lendir
-1] != '/')
2344 filename
[lendir
++] = '/';
2345 strcpy (filename
+lendir
, base_name
);
2348 if ((result
= (*func
) (filename
, data1
, data2
)))
2357 LT_DLFREE (canonical
);
2358 LT_DLFREE (filename
);
2360 LT_DLMUTEX_UNLOCK ();
2365 /* If FILEPATH can be opened, store the name of the directory component
2366 in DATA1, and the opened FILE* structure address in DATA2. Otherwise
2367 DATA1 is unchanged, but DATA2 is set to a pointer to NULL. */
2369 find_file_callback (filename
, data1
, data2
)
2374 char **pdir
= (char **) data1
;
2375 FILE **pfile
= (FILE **) data2
;
2378 assert (filename
&& *filename
);
2382 if ((*pfile
= fopen (filename
, LT_READTEXT_MODE
)))
2384 char *dirend
= strrchr (filename
, '/');
2386 if (dirend
> filename
)
2387 *dirend
= LT_EOS_CHAR
;
2390 *pdir
= lt_estrdup (filename
);
2391 is_done
= (*pdir
== 0) ? -1 : 1;
2398 find_file (search_path
, base_name
, pdir
)
2399 const char *search_path
;
2400 const char *base_name
;
2405 foreach_dirinpath (search_path
, base_name
, find_file_callback
, pdir
, &file
);
2411 find_handle_callback (filename
, data
, ignored
)
2416 lt_dlhandle
*handle
= (lt_dlhandle
*) data
;
2417 int notfound
= access (filename
, R_OK
);
2419 /* Bail out if file cannot be read... */
2423 /* Try to dlopen the file, but do not continue searching in any
2425 if (tryall_dlopen (handle
, filename
) != 0)
2431 /* If HANDLE was found return it, otherwise return 0. If HANDLE was
2432 found but could not be opened, *HANDLE will be set to 0. */
2433 static lt_dlhandle
*
2434 find_handle (search_path
, base_name
, handle
)
2435 const char *search_path
;
2436 const char *base_name
;
2437 lt_dlhandle
*handle
;
2442 if (!foreach_dirinpath (search_path
, base_name
, find_handle_callback
,
2450 load_deplibs (handle
, deplibs
)
2454 #if LTDL_DLOPEN_DEPLIBS
2455 char *p
, *save_search_path
= 0;
2462 handle
->depcount
= 0;
2464 #if LTDL_DLOPEN_DEPLIBS
2472 if (user_search_path
)
2474 save_search_path
= lt_estrdup (user_search_path
);
2475 if (!save_search_path
)
2479 /* extract search paths and count deplibs */
2483 if (!isspace ((int) *p
))
2486 while (*end
&& !isspace((int) *end
))
2491 if (strncmp(p
, "-L", 2) == 0 || strncmp(p
, "-R", 2) == 0)
2494 *end
= 0; /* set a temporary string terminator */
2495 if (lt_dladdsearchdir(p
+2))
2514 /* restore the old search path */
2515 LT_DLFREE (user_search_path
);
2516 user_search_path
= save_search_path
;
2518 LT_DLMUTEX_UNLOCK ();
2526 names
= LT_EMALLOC (char *, depcount
* sizeof (char*));
2530 /* now only extract the actual deplibs */
2535 if (isspace ((int) *p
))
2542 while (*end
&& !isspace ((int) *end
))
2547 if (strncmp(p
, "-L", 2) != 0 && strncmp(p
, "-R", 2) != 0)
2551 *end
= 0; /* set a temporary string terminator */
2552 if (strncmp(p
, "-l", 2) == 0)
2554 size_t name_len
= 3+ /* "lib" */ LT_STRLEN (p
+ 2);
2555 name
= LT_EMALLOC (char, 1+ name_len
);
2557 sprintf (name
, "lib%s", p
+2);
2560 name
= lt_estrdup(p
);
2565 names
[depcount
++] = name
;
2572 /* load the deplibs (in reverse order)
2573 At this stage, don't worry if the deplibs do not load correctly,
2574 they may already be statically linked into the loading application
2575 for instance. There will be a more enlightening error message
2576 later on if the loaded module cannot resolve all of its symbols. */
2581 handle
->deplibs
= (lt_dlhandle
*) LT_EMALLOC (lt_dlhandle
*, depcount
);
2582 if (!handle
->deplibs
)
2585 for (i
= 0; i
< depcount
; ++i
)
2587 handle
->deplibs
[j
] = lt_dlopenext(names
[depcount
-1-i
]);
2588 if (handle
->deplibs
[j
])
2594 handle
->depcount
= j
; /* Number of successfully loaded deplibs */
2599 for (i
= 0; i
< depcount
; ++i
)
2601 LT_DLFREE (names
[i
]);
2612 unload_deplibs (handle
)
2618 if (handle
->depcount
)
2620 for (i
= 0; i
< handle
->depcount
; ++i
)
2622 if (!LT_DLIS_RESIDENT (handle
->deplibs
[i
]))
2624 errors
+= lt_dlclose (handle
->deplibs
[i
]);
2637 /* remove the leading and trailing "'" from str
2638 and store the result in dest */
2639 const char *end
= strrchr (str
, '\'');
2640 size_t len
= LT_STRLEN (str
);
2645 if (len
> 3 && str
[0] == '\'')
2647 tmp
= LT_EMALLOC (char, end
- str
);
2651 strncpy(tmp
, &str
[1], (end
- str
) - 1);
2652 tmp
[len
-3] = LT_EOS_CHAR
;
2664 free_vars (dlname
, oldname
, libdir
, deplibs
)
2671 LT_DLFREE (oldname
);
2673 LT_DLFREE (deplibs
);
2679 try_dlopen (phandle
, filename
)
2680 lt_dlhandle
*phandle
;
2681 const char *filename
;
2683 const char * ext
= 0;
2684 const char * saved_error
= 0;
2685 char * canonical
= 0;
2686 char * base_name
= 0;
2690 lt_dlhandle newhandle
;
2693 assert (*phandle
== 0);
2695 LT_DLMUTEX_GETERROR (saved_error
);
2700 *phandle
= (lt_dlhandle
) LT_EMALLOC (struct lt_dlhandle_struct
, 1);
2704 memset (*phandle
, 0, sizeof(struct lt_dlhandle_struct
));
2705 newhandle
= *phandle
;
2707 /* lt_dlclose()ing yourself is very bad! Disallow it. */
2708 LT_DLSET_FLAG (*phandle
, LT_DLRESIDENT_FLAG
);
2710 if (tryall_dlopen (&newhandle
, 0) != 0)
2712 LT_DLFREE (*phandle
);
2716 goto register_handle
;
2719 assert (filename
&& *filename
);
2721 /* Doing this immediately allows internal functions to safely
2722 assume only canonicalized paths are passed. */
2723 if (canonicalize_path (filename
, &canonical
) != 0)
2729 /* If the canonical module name is a path (relative or absolute)
2730 then split it into a directory part and a name part. */
2731 base_name
= strrchr (canonical
, '/');
2734 size_t dirlen
= (1+ base_name
) - canonical
;
2736 dir
= LT_EMALLOC (char, 1+ dirlen
);
2743 strncpy (dir
, canonical
, dirlen
);
2744 dir
[dirlen
] = LT_EOS_CHAR
;
2749 LT_DLMEM_REASSIGN (base_name
, canonical
);
2751 assert (base_name
&& *base_name
);
2753 /* Check whether we are opening a libtool module (.la extension). */
2754 ext
= strrchr (base_name
, '.');
2755 if (ext
&& strcmp (ext
, archive_ext
) == 0)
2757 /* this seems to be a libtool module */
2760 char * old_name
= 0;
2766 /* if we can't find the installed flag, it is probably an
2767 installed libtool archive, produced with an old version
2771 /* extract the module name from the file name */
2772 name
= LT_EMALLOC (char, ext
- base_name
+ 1);
2779 /* canonicalize the module name */
2782 for (i
= 0; i
< ext
- base_name
; ++i
)
2784 if (isalnum ((int)(base_name
[i
])))
2786 name
[i
] = base_name
[i
];
2793 name
[ext
- base_name
] = LT_EOS_CHAR
;
2796 /* Now try to open the .la file. If there is no directory name
2797 component, try to find it first in user_search_path and then other
2798 prescribed paths. Otherwise (or in any case if the module was not
2799 yet found) try opening just the module name as passed. */
2802 const char *search_path
;
2805 search_path
= user_search_path
;
2807 file
= find_file (user_search_path
, base_name
, &dir
);
2808 LT_DLMUTEX_UNLOCK ();
2812 search_path
= getenv (LTDL_SEARCHPATH_VAR
);
2814 file
= find_file (search_path
, base_name
, &dir
);
2817 #ifdef LTDL_SHLIBPATH_VAR
2820 search_path
= getenv (LTDL_SHLIBPATH_VAR
);
2822 file
= find_file (search_path
, base_name
, &dir
);
2825 #ifdef LTDL_SYSSEARCHPATH
2826 if (!file
&& sys_search_path
)
2828 file
= find_file (sys_search_path
, base_name
, &dir
);
2834 file
= fopen (filename
, LT_READTEXT_MODE
);
2837 /* If we didn't find the file by now, it really isn't there. Set
2838 the status flag, and bail out. */
2841 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (FILE_NOT_FOUND
));
2846 line_len
= LT_FILENAME_MAX
;
2847 line
= LT_EMALLOC (char, line_len
);
2855 /* read the .la file */
2856 while (!feof (file
))
2858 if (!fgets (line
, (int) line_len
, file
))
2863 /* Handle the case where we occasionally need to read a line
2864 that is longer than the initial buffer size. */
2865 while ((line
[LT_STRLEN(line
) -1] != '\n') && (!feof (file
)))
2867 line
= LT_DLREALLOC (char, line
, line_len
*2);
2868 if (!fgets (&line
[line_len
-1], (int) line_len
+1, file
))
2875 if (line
[0] == '\n' || line
[0] == '#')
2881 #define STR_DLNAME "dlname="
2882 if (strncmp (line
, STR_DLNAME
, sizeof (STR_DLNAME
) - 1) == 0)
2884 errors
+= trim (&dlname
, &line
[sizeof (STR_DLNAME
) - 1]);
2887 #undef STR_OLD_LIBRARY
2888 #define STR_OLD_LIBRARY "old_library="
2889 else if (strncmp (line
, STR_OLD_LIBRARY
,
2890 sizeof (STR_OLD_LIBRARY
) - 1) == 0)
2892 errors
+= trim (&old_name
, &line
[sizeof (STR_OLD_LIBRARY
) - 1]);
2895 #define STR_LIBDIR "libdir="
2896 else if (strncmp (line
, STR_LIBDIR
, sizeof (STR_LIBDIR
) - 1) == 0)
2898 errors
+= trim (&libdir
, &line
[sizeof(STR_LIBDIR
) - 1]);
2901 #undef STR_DL_DEPLIBS
2902 #define STR_DL_DEPLIBS "dependency_libs="
2903 else if (strncmp (line
, STR_DL_DEPLIBS
,
2904 sizeof (STR_DL_DEPLIBS
) - 1) == 0)
2906 errors
+= trim (&deplibs
, &line
[sizeof (STR_DL_DEPLIBS
) - 1]);
2908 else if (strcmp (line
, "installed=yes\n") == 0)
2912 else if (strcmp (line
, "installed=no\n") == 0)
2917 #undef STR_LIBRARY_NAMES
2918 #define STR_LIBRARY_NAMES "library_names="
2919 else if (! dlname
&& strncmp (line
, STR_LIBRARY_NAMES
,
2920 sizeof (STR_LIBRARY_NAMES
) - 1) == 0)
2923 errors
+= trim (&dlname
, &line
[sizeof (STR_LIBRARY_NAMES
) - 1]);
2926 && (last_libname
= strrchr (dlname
, ' ')) != 0)
2928 last_libname
= lt_estrdup (last_libname
+ 1);
2934 LT_DLMEM_REASSIGN (dlname
, last_libname
);
2945 /* allocate the handle */
2946 *phandle
= (lt_dlhandle
) LT_EMALLOC (struct lt_dlhandle_struct
, 1);
2952 free_vars (dlname
, old_name
, libdir
, deplibs
);
2953 LT_DLFREE (*phandle
);
2959 memset (*phandle
, 0, sizeof(struct lt_dlhandle_struct
));
2960 if (load_deplibs (*phandle
, deplibs
) == 0)
2962 newhandle
= *phandle
;
2963 /* find_module may replace newhandle */
2964 if (find_module (&newhandle
, dir
, libdir
, dlname
, old_name
, installed
))
2966 unload_deplibs (*phandle
);
2975 free_vars (dlname
, old_name
, libdir
, deplibs
);
2978 LT_DLFREE (*phandle
);
2982 if (*phandle
!= newhandle
)
2984 unload_deplibs (*phandle
);
2989 /* not a libtool module */
2990 *phandle
= (lt_dlhandle
) LT_EMALLOC (struct lt_dlhandle_struct
, 1);
2997 memset (*phandle
, 0, sizeof (struct lt_dlhandle_struct
));
2998 newhandle
= *phandle
;
3000 /* If the module has no directory name component, try to find it
3001 first in user_search_path and then other prescribed paths.
3002 Otherwise (or in any case if the module was not yet found) try
3003 opening just the module name as passed. */
3004 if ((dir
|| (!find_handle (user_search_path
, base_name
, &newhandle
)
3005 && !find_handle (getenv (LTDL_SEARCHPATH_VAR
), base_name
,
3007 #ifdef LTDL_SHLIBPATH_VAR
3008 && !find_handle (getenv (LTDL_SHLIBPATH_VAR
), base_name
,
3011 #ifdef LTDL_SYSSEARCHPATH
3012 && !find_handle (sys_search_path
, base_name
, &newhandle
)
3016 if (tryall_dlopen (&newhandle
, filename
) != 0)
3024 LT_DLFREE (*phandle
);
3031 LT_DLMEM_REASSIGN (*phandle
, newhandle
);
3033 if ((*phandle
)->info
.ref_count
== 0)
3035 (*phandle
)->info
.ref_count
= 1;
3036 LT_DLMEM_REASSIGN ((*phandle
)->info
.name
, name
);
3039 (*phandle
)->next
= handles
;
3041 LT_DLMUTEX_UNLOCK ();
3044 LT_DLMUTEX_SETERROR (saved_error
);
3049 LT_DLFREE (canonical
);
3055 lt_dlopen (filename
)
3056 const char *filename
;
3058 lt_dlhandle handle
= 0;
3060 /* Just incase we missed a code path in try_dlopen() that reports
3061 an error, but forgets to reset handle... */
3062 if (try_dlopen (&handle
, filename
) != 0)
3068 /* If the last error messge store was `FILE_NOT_FOUND', then return
3073 const char *error
= 0;
3075 LT_DLMUTEX_GETERROR (error
);
3076 if (error
== LT_DLSTRERROR (FILE_NOT_FOUND
))
3082 /* If FILENAME has an ARCHIVE_EXT or SHLIB_EXT extension, try to
3083 open the FILENAME as passed. Otherwise try appending ARCHIVE_EXT,
3084 and if a file is still not found try again with SHLIB_EXT appended
3087 lt_dlopenext (filename
)
3088 const char *filename
;
3090 lt_dlhandle handle
= 0;
3098 return lt_dlopen (filename
);
3103 len
= LT_STRLEN (filename
);
3104 ext
= strrchr (filename
, '.');
3106 /* If FILENAME already bears a suitable extension, there is no need
3107 to try appending additional extensions. */
3108 if (ext
&& ((strcmp (ext
, archive_ext
) == 0)
3109 #ifdef LTDL_SHLIB_EXT
3110 || (strcmp (ext
, shlib_ext
) == 0)
3114 return lt_dlopen (filename
);
3117 /* First try appending ARCHIVE_EXT. */
3118 tmp
= LT_EMALLOC (char, len
+ LT_STRLEN (archive_ext
) + 1);
3122 strcpy (tmp
, filename
);
3123 strcat (tmp
, archive_ext
);
3124 errors
= try_dlopen (&handle
, tmp
);
3126 /* If we found FILENAME, stop searching -- whether we were able to
3127 load the file as a module or not. If the file exists but loading
3128 failed, it is better to return an error message here than to
3129 report FILE_NOT_FOUND when the alternatives (foo.so etc) are not
3130 in the module search path. */
3131 if (handle
|| ((errors
> 0) && !file_not_found ()))
3137 #ifdef LTDL_SHLIB_EXT
3138 /* Try appending SHLIB_EXT. */
3139 if (LT_STRLEN (shlib_ext
) > LT_STRLEN (archive_ext
))
3142 tmp
= LT_EMALLOC (char, len
+ LT_STRLEN (shlib_ext
) + 1);
3146 strcpy (tmp
, filename
);
3150 tmp
[len
] = LT_EOS_CHAR
;
3153 strcat(tmp
, shlib_ext
);
3154 errors
= try_dlopen (&handle
, tmp
);
3156 /* As before, if the file was found but loading failed, return now
3157 with the current error message. */
3158 if (handle
|| ((errors
> 0) && !file_not_found ()))
3165 /* Still here? Then we really did fail to locate any of the file
3167 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (FILE_NOT_FOUND
));
3174 lt_argz_insert (pargz
, pargz_len
, before
, entry
)
3182 if ((error
= argz_insert (pargz
, pargz_len
, before
, entry
)))
3187 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY
));
3190 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (UNKNOWN
));
3200 lt_argz_insertinorder (pargz
, pargz_len
, entry
)
3209 assert (entry
&& *entry
);
3212 while ((before
= argz_next (*pargz
, *pargz_len
, before
)))
3214 int cmp
= strcmp (entry
, before
);
3217 if (cmp
== 0) return 0; /* No duplicates! */
3220 return lt_argz_insert (pargz
, pargz_len
, before
, entry
);
3224 lt_argz_insertdir (pargz
, pargz_len
, dirnam
, dp
)
3233 size_t end_offset
= 0;
3241 dir_len
= LT_STRLEN (dirnam
);
3242 end
= dp
->d_name
+ LT_D_NAMLEN(dp
);
3244 /* Ignore version numbers. */
3247 for (p
= end
; p
-1 > dp
->d_name
; --p
)
3248 if (strchr (".0123456789", p
[-1]) == 0)
3255 /* Ignore filename extension. */
3258 for (p
= end
-1; p
> dp
->d_name
; --p
)
3266 /* Prepend the directory name. */
3267 end_offset
= end
- dp
->d_name
;
3268 buf_len
= dir_len
+ 1+ end_offset
;
3269 buf
= LT_EMALLOC (char, 1+ buf_len
);
3275 strcpy (buf
, dirnam
);
3277 strncat (buf
, dp
->d_name
, end_offset
);
3278 buf
[buf_len
] = LT_EOS_CHAR
;
3280 /* Try to insert (in order) into ARGZ/ARGZ_LEN. */
3281 if (lt_argz_insertinorder (pargz
, pargz_len
, buf
) != 0)
3290 list_files_by_dir (dirnam
, pargz
, pargz_len
)
3298 assert (dirnam
&& *dirnam
);
3301 assert (dirnam
[LT_STRLEN(dirnam
) -1] != '/');
3303 dirp
= opendir (dirnam
);
3306 struct dirent
*dp
= 0;
3308 while ((dp
= readdir (dirp
)))
3309 if (dp
->d_name
[0] != '.')
3310 if (lt_argz_insertdir (pargz
, pargz_len
, dirnam
, dp
))
3325 /* If there are any files in DIRNAME, call the function passed in
3326 DATA1 (with the name of each file and DATA2 as arguments). */
3328 foreachfile_callback (dirname
, data1
, data2
)
3333 int (*func
) LT_PARAMS((const char *filename
, lt_ptr data
))
3334 = (int (*) LT_PARAMS((const char *filename
, lt_ptr data
))) data1
;
3338 size_t argz_len
= 0;
3340 if (list_files_by_dir (dirname
, &argz
, &argz_len
) != 0)
3347 while ((filename
= argz_next (argz
, argz_len
, filename
)))
3348 if ((is_done
= (*func
) (filename
, data2
)))
3359 /* Call FUNC for each unique extensionless file in SEARCH_PATH, along
3360 with DATA. The filenames passed to FUNC would be suitable for
3361 passing to lt_dlopenext. The extensions are stripped so that
3362 individual modules do not generate several entries (e.g. libfoo.la,
3363 libfoo.so, libfoo.so.1, libfoo.so.1.0.0). If SEARCH_PATH is NULL,
3364 then the same directories that lt_dlopen would search are examined. */
3366 lt_dlforeachfile (search_path
, func
, data
)
3367 const char *search_path
;
3368 int (*func
) LT_PARAMS ((const char *filename
, lt_ptr data
));
3375 /* If a specific path was passed, search only the directories
3377 is_done
= foreach_dirinpath (search_path
, 0,
3378 foreachfile_callback
, func
, data
);
3382 /* Otherwise search the default paths. */
3383 is_done
= foreach_dirinpath (user_search_path
, 0,
3384 foreachfile_callback
, func
, data
);
3387 is_done
= foreach_dirinpath (getenv("LTDL_LIBRARY_PATH"), 0,
3388 foreachfile_callback
, func
, data
);
3391 #ifdef LTDL_SHLIBPATH_VAR
3394 is_done
= foreach_dirinpath (getenv(LTDL_SHLIBPATH_VAR
), 0,
3395 foreachfile_callback
, func
, data
);
3398 #ifdef LTDL_SYSSEARCHPATH
3401 is_done
= foreach_dirinpath (getenv(LTDL_SYSSEARCHPATH
), 0,
3402 foreachfile_callback
, func
, data
);
3414 lt_dlhandle cur
, last
;
3419 /* check whether the handle is valid */
3420 last
= cur
= handles
;
3421 while (cur
&& handle
!= cur
)
3429 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_HANDLE
));
3434 handle
->info
.ref_count
--;
3436 /* Note that even with resident modules, we must track the ref_count
3437 correctly incase the user decides to reset the residency flag
3438 later (even though the API makes no provision for that at the
3440 if (handle
->info
.ref_count
<= 0 && !LT_DLIS_RESIDENT (handle
))
3442 lt_user_data data
= handle
->loader
->dlloader_data
;
3444 if (handle
!= handles
)
3446 last
->next
= handle
->next
;
3450 handles
= handle
->next
;
3453 errors
+= handle
->loader
->module_close (data
, handle
->module
);
3454 errors
+= unload_deplibs(handle
);
3456 /* It is up to the callers to free the data itself. */
3457 LT_DLFREE (handle
->caller_data
);
3459 LT_DLFREE (handle
->info
.filename
);
3460 LT_DLFREE (handle
->info
.name
);
3466 if (LT_DLIS_RESIDENT (handle
))
3468 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (CLOSE_RESIDENT_MODULE
));
3473 LT_DLMUTEX_UNLOCK ();
3479 lt_dlsym (handle
, symbol
)
3484 char lsym
[LT_SYMBOL_LENGTH
];
3491 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_HANDLE
));
3497 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (SYMBOL_NOT_FOUND
));
3501 lensym
= LT_STRLEN (symbol
) + LT_STRLEN (handle
->loader
->sym_prefix
)
3502 + LT_STRLEN (handle
->info
.name
);
3504 if (lensym
+ LT_SYMBOL_OVERHEAD
< LT_SYMBOL_LENGTH
)
3510 sym
= LT_EMALLOC (char, lensym
+ LT_SYMBOL_OVERHEAD
+ 1);
3513 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (BUFFER_OVERFLOW
));
3518 data
= handle
->loader
->dlloader_data
;
3519 if (handle
->info
.name
)
3521 const char *saved_error
;
3523 LT_DLMUTEX_GETERROR (saved_error
);
3525 /* this is a libtool module */
3526 if (handle
->loader
->sym_prefix
)
3528 strcpy(sym
, handle
->loader
->sym_prefix
);
3529 strcat(sym
, handle
->info
.name
);
3533 strcpy(sym
, handle
->info
.name
);
3536 strcat(sym
, "_LTX_");
3537 strcat(sym
, symbol
);
3539 /* try "modulename_LTX_symbol" */
3540 address
= handle
->loader
->find_sym (data
, handle
->module
, sym
);
3549 LT_DLMUTEX_SETERROR (saved_error
);
3552 /* otherwise try "symbol" */
3553 if (handle
->loader
->sym_prefix
)
3555 strcpy(sym
, handle
->loader
->sym_prefix
);
3556 strcat(sym
, symbol
);
3560 strcpy(sym
, symbol
);
3563 address
= handle
->loader
->find_sym (data
, handle
->module
, sym
);
3577 LT_DLMUTEX_GETERROR (error
);
3578 LT_DLMUTEX_SETERROR (0);
3580 return error
? error
: LT_DLSTRERROR (UNKNOWN
);
3584 lt_dlpath_insertdir (ppath
, before
, dir
)
3590 char *canonical
= 0;
3592 size_t argz_len
= 0;
3595 assert (dir
&& *dir
);
3597 if (canonicalize_path (dir
, &canonical
) != 0)
3603 assert (canonical
&& *canonical
);
3605 /* If *PPATH is empty, set it to DIR. */
3608 assert (!before
); /* BEFORE cannot be set without PPATH. */
3609 assert (dir
); /* Without DIR, don't call this function! */
3611 *ppath
= lt_estrdup (dir
);
3618 assert (ppath
&& *ppath
);
3620 if (argzize_path (*ppath
, &argz
, &argz_len
) != 0)
3626 /* Convert BEFORE into an equivalent offset into ARGZ. This only works
3627 if *PPATH is already canonicalized, and hence does not change length
3628 with respect to ARGZ. We canonicalize each entry as it is added to
3629 the search path, and don't call this function with (uncanonicalized)
3630 user paths, so this is a fair assumption. */
3633 assert (*ppath
<= before
);
3634 assert (before
- *ppath
<= strlen (*ppath
));
3636 before
= before
- *ppath
+ argz
;
3639 if (lt_argz_insert (&argz
, &argz_len
, before
, dir
) != 0)
3645 argz_stringify (argz
, argz_len
, LT_PATHSEP_CHAR
);
3646 LT_DLMEM_REASSIGN (*ppath
, argz
);
3649 LT_DLFREE (canonical
);
3656 lt_dladdsearchdir (search_dir
)
3657 const char *search_dir
;
3661 if (search_dir
&& *search_dir
)
3664 if (lt_dlpath_insertdir (&user_search_path
, 0, search_dir
) != 0)
3666 LT_DLMUTEX_UNLOCK ();
3673 lt_dlinsertsearchdir (before
, search_dir
)
3675 const char *search_dir
;
3682 if ((before
< user_search_path
)
3683 || (before
>= user_search_path
+ LT_STRLEN (user_search_path
)))
3685 LT_DLMUTEX_UNLOCK ();
3686 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_POSITION
));
3689 LT_DLMUTEX_UNLOCK ();
3692 if (search_dir
&& *search_dir
)
3695 if (lt_dlpath_insertdir (&user_search_path
,
3696 (char *) before
, search_dir
) != 0)
3700 LT_DLMUTEX_UNLOCK ();
3707 lt_dlsetsearchpath (search_path
)
3708 const char *search_path
;
3713 LT_DLFREE (user_search_path
);
3714 LT_DLMUTEX_UNLOCK ();
3716 if (!search_path
|| !LT_STRLEN (search_path
))
3722 if (canonicalize_path (search_path
, &user_search_path
) != 0)
3724 LT_DLMUTEX_UNLOCK ();
3730 lt_dlgetsearchpath ()
3732 const char *saved_path
;
3735 saved_path
= user_search_path
;
3736 LT_DLMUTEX_UNLOCK ();
3742 lt_dlmakeresident (handle
)
3749 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_HANDLE
));
3754 LT_DLSET_FLAG (handle
, LT_DLRESIDENT_FLAG
);
3761 lt_dlisresident (handle
)
3766 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_HANDLE
));
3770 return LT_DLIS_RESIDENT (handle
);
3776 /* --- MODULE INFORMATION --- */
3779 lt_dlgetinfo (handle
)
3784 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_HANDLE
));
3788 return &(handle
->info
);
3792 lt_dlhandle_next (place
)
3795 return place
? place
->next
: handles
;
3799 lt_dlforeach (func
, data
)
3800 int (*func
) LT_PARAMS((lt_dlhandle handle
, lt_ptr data
));
3811 lt_dlhandle tmp
= cur
;
3814 if ((*func
) (tmp
, data
))
3821 LT_DLMUTEX_UNLOCK ();
3827 lt_dlcaller_register ()
3829 static lt_dlcaller_id last_caller_id
= 0;
3833 result
= ++last_caller_id
;
3834 LT_DLMUTEX_UNLOCK ();
3840 lt_dlcaller_set_data (key
, handle
, data
)
3846 lt_ptr stale
= (lt_ptr
) 0;
3849 /* This needs to be locked so that the caller data can be updated
3850 simultaneously by different threads. */
3853 if (handle
->caller_data
)
3854 while (handle
->caller_data
[n_elements
].key
)
3857 for (i
= 0; i
< n_elements
; ++i
)
3859 if (handle
->caller_data
[i
].key
== key
)
3861 stale
= handle
->caller_data
[i
].data
;
3866 /* Ensure that there is enough room in this handle's caller_data
3867 array to accept a new element (and an empty end marker). */
3868 if (i
== n_elements
)
3870 lt_caller_data
*temp
3871 = LT_DLREALLOC (lt_caller_data
, handle
->caller_data
, 2+ n_elements
);
3879 handle
->caller_data
= temp
;
3881 /* We only need this if we needed to allocate a new caller_data. */
3882 handle
->caller_data
[i
].key
= key
;
3883 handle
->caller_data
[1+ i
].key
= 0;
3886 handle
->caller_data
[i
].data
= data
;
3889 LT_DLMUTEX_UNLOCK ();
3895 lt_dlcaller_get_data (key
, handle
)
3899 lt_ptr result
= (lt_ptr
) 0;
3901 /* This needs to be locked so that the caller data isn't updated by
3902 another thread part way through this function. */
3905 /* Locate the index of the element with a matching KEY. */
3908 for (i
= 0; handle
->caller_data
[i
].key
; ++i
)
3910 if (handle
->caller_data
[i
].key
== key
)
3912 result
= handle
->caller_data
[i
].data
;
3918 LT_DLMUTEX_UNLOCK ();
3925 /* --- USER MODULE LOADER API --- */
3929 lt_dlloader_add (place
, dlloader
, loader_name
)
3931 const struct lt_user_dlloader
*dlloader
;
3932 const char *loader_name
;
3935 lt_dlloader
*node
= 0, *ptr
= 0;
3937 if ((dlloader
== 0) /* diagnose null parameters */
3938 || (dlloader
->module_open
== 0)
3939 || (dlloader
->module_close
== 0)
3940 || (dlloader
->find_sym
== 0))
3942 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_LOADER
));
3946 /* Create a new dlloader node with copies of the user callbacks. */
3947 node
= LT_EMALLOC (lt_dlloader
, 1);
3952 node
->loader_name
= loader_name
;
3953 node
->sym_prefix
= dlloader
->sym_prefix
;
3954 node
->dlloader_exit
= dlloader
->dlloader_exit
;
3955 node
->module_open
= dlloader
->module_open
;
3956 node
->module_close
= dlloader
->module_close
;
3957 node
->find_sym
= dlloader
->find_sym
;
3958 node
->dlloader_data
= dlloader
->dlloader_data
;
3963 /* If there are no loaders, NODE becomes the list! */
3968 /* If PLACE is not set, add NODE to the end of the
3970 for (ptr
= loaders
; ptr
->next
; ptr
= ptr
->next
)
3977 else if (loaders
== place
)
3979 /* If PLACE is the first loader, NODE goes first. */
3985 /* Find the node immediately preceding PLACE. */
3986 for (ptr
= loaders
; ptr
->next
!= place
; ptr
= ptr
->next
)
3991 if (ptr
->next
!= place
)
3993 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_LOADER
));
3998 /* Insert NODE between PTR and PLACE. */
4004 LT_DLMUTEX_UNLOCK ();
4010 lt_dlloader_remove (loader_name
)
4011 const char *loader_name
;
4013 lt_dlloader
*place
= lt_dlloader_find (loader_name
);
4019 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_LOADER
));
4025 /* Fail if there are any open modules which use this loader. */
4026 for (handle
= handles
; handle
; handle
= handle
->next
)
4028 if (handle
->loader
== place
)
4030 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (REMOVE_LOADER
));
4036 if (place
== loaders
)
4038 /* PLACE is the first loader in the list. */
4039 loaders
= loaders
->next
;
4043 /* Find the loader before the one being removed. */
4045 for (prev
= loaders
; prev
->next
; prev
= prev
->next
)
4047 if (!strcmp (prev
->next
->loader_name
, loader_name
))
4054 prev
->next
= prev
->next
->next
;
4057 if (place
->dlloader_exit
)
4059 errors
= place
->dlloader_exit (place
->dlloader_data
);
4065 LT_DLMUTEX_UNLOCK ();
4071 lt_dlloader_next (place
)
4077 next
= place
? place
->next
: loaders
;
4078 LT_DLMUTEX_UNLOCK ();
4084 lt_dlloader_name (place
)
4087 const char *name
= 0;
4092 name
= place
? place
->loader_name
: 0;
4093 LT_DLMUTEX_UNLOCK ();
4097 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_LOADER
));
4104 lt_dlloader_data (place
)
4107 lt_user_data
*data
= 0;
4112 data
= place
? &(place
->dlloader_data
) : 0;
4113 LT_DLMUTEX_UNLOCK ();
4117 LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_LOADER
));
4124 lt_dlloader_find (loader_name
)
4125 const char *loader_name
;
4127 lt_dlloader
*place
= 0;
4130 for (place
= loaders
; place
; place
= place
->next
)
4132 if (strcmp (place
->loader_name
, loader_name
) == 0)
4137 LT_DLMUTEX_UNLOCK ();