-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltdl.c
4067 lines (3335 loc) · 85.7 KB
/
ltdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////
// $Id: ltdl.c 10209 2011-02-24 22:05:47Z sshwarts $
//
// NOTE: The ltdl library comes from the Libtool package. Bochs uses
// ltdl and libtool to build and load plugins. The libtool
// documentation describes how to copy ltdl.c and ltdl.h into your
// distribution, so it is clearly legal to do so.
/////////////////////////////////////////////////////////////////////////
/* ltdl.c -- system independent dlopen wrapper
Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
Originally by Thomas Tanner <[email protected]>
This file is part of GNU Libtool.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
As a special exception to the GNU Lesser General Public License,
if you distribute this file as part of a program or library that
is built using GNU libtool, you may include it under the same
distribution terms that you use for the rest of that program.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// ltdlconf.h added for Bochs
#include "ltdlconf.h"
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_STDIO_H
# include <stdio.h>
#endif
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#else
# if HAVE_STRINGS_H
# include <strings.h>
# endif
#endif
#if HAVE_CTYPE_H
# include <ctype.h>
#endif
#if HAVE_MALLOC_H
# include <malloc.h>
#endif
#if HAVE_MEMORY_H
# include <memory.h>
#endif
#if HAVE_ERRNO_H
# include <errno.h>
#endif
#if HAVE_DIRENT_H
# include <dirent.h>
# define LT_D_NAMLEN(dirent) (strlen((dirent)->d_name))
#else
# define dirent direct
# define LT_D_NAMLEN(dirent) ((dirent)->d_namlen)
# if HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# if HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#if HAVE_ARGZ_H
# include <argz.h>
#endif
#if HAVE_ASSERT_H
# include <assert.h>
#else
# ifdef __GNUC__
# warning using my own assert
# endif
# define assert(cond) while (!(cond)) { fprintf (stderr, "Assert failed at %s:%d: '%s'\n", __FILE__, __LINE__, #cond); abort(); }
#endif
#define LTDEBUG_PRINTF(x) /* debug output disabled */
//#define LTDEBUG_PRINTF(x) do{ printf("LT_DEBUG: "); printf x; } while (0)
#include "ltdl.h"
/* --- WINDOWS SUPPORT --- */
#ifdef DLL_EXPORT
# define LT_GLOBAL_DATA __declspec(dllexport)
#else
# define LT_GLOBAL_DATA
#endif
/* fopen() mode flags for reading a text file */
#undef LT_READTEXT_MODE
#ifdef __WINDOWS__
# define LT_READTEXT_MODE "rt"
#else
# define LT_READTEXT_MODE "r"
#endif
/* --- MANIFEST CONSTANTS --- */
/* Standard libltdl search path environment variable name */
#undef LTDL_SEARCHPATH_VAR
#define LTDL_SEARCHPATH_VAR "LTDL_LIBRARY_PATH"
/* Standard libtool archive file extension. */
#undef LTDL_ARCHIVE_EXT
#define LTDL_ARCHIVE_EXT ".la"
/* max. filename length */
#ifndef LT_FILENAME_MAX
# define LT_FILENAME_MAX 1024
#endif
/* This is the maximum symbol size that won't require malloc/free */
#undef LT_SYMBOL_LENGTH
#define LT_SYMBOL_LENGTH 128
/* This accounts for the _LTX_ separator */
#undef LT_SYMBOL_OVERHEAD
#define LT_SYMBOL_OVERHEAD 5
/* --- MEMORY HANDLING --- */
/* These are the functions used internally. In addition to making
use of the associated function pointers above, they also perform
error handling. */
static char *lt_estrdup LT_PARAMS((const char *str));
static lt_ptr lt_emalloc LT_PARAMS((size_t size));
static lt_ptr lt_erealloc LT_PARAMS((lt_ptr addr, size_t size));
static lt_ptr rpl_realloc LT_PARAMS((lt_ptr ptr, size_t size));
/* These are the pointers that can be changed by the caller: */
LT_GLOBAL_DATA lt_ptr (*lt_dlmalloc) LT_PARAMS((size_t size))
= (lt_ptr (*) LT_PARAMS((size_t))) malloc;
LT_GLOBAL_DATA lt_ptr (*lt_dlrealloc) LT_PARAMS((lt_ptr ptr, size_t size))
= (lt_ptr (*) LT_PARAMS((lt_ptr, size_t))) rpl_realloc;
LT_GLOBAL_DATA void (*lt_dlfree) LT_PARAMS((lt_ptr ptr))
= (void (*) LT_PARAMS((lt_ptr))) free;
/* The following macros reduce the amount of typing needed to cast
assigned memory. */
#define LT_DLMALLOC(tp, n) ((tp *) lt_dlmalloc ((n) * sizeof(tp)))
#define LT_DLREALLOC(tp, p, n) ((tp *) rpl_realloc ((p), (n) * sizeof(tp)))
#define LT_DLFREE(p) \
LT_STMT_START { if (p) (p) = (lt_dlfree (p), (lt_ptr) 0); } LT_STMT_END
#define LT_EMALLOC(tp, n) ((tp *) lt_emalloc ((n) * sizeof(tp)))
#define LT_EREALLOC(tp, p, n) ((tp *) lt_erealloc ((p), (n) * sizeof(tp)))
#define LT_DLMEM_REASSIGN(p, q) LT_STMT_START { \
if ((p) != (q)) { lt_dlfree (p); (p) = (q); (q) = 0; } \
} LT_STMT_END
/* --- REPLACEMENT FUNCTIONS --- */
#undef strdup
#define strdup rpl_strdup
static char *strdup LT_PARAMS((const char *str));
char *
strdup(str)
const char *str;
{
char *tmp = 0;
if (str)
{
tmp = LT_DLMALLOC (char, 1+ strlen (str));
if (tmp)
{
strcpy(tmp, str);
}
}
return tmp;
}
#if ! HAVE_STRCMP
#undef strcmp
#define strcmp rpl_strcmp
static int strcmp LT_PARAMS((const char *str1, const char *str2));
int
strcmp (str1, str2)
const char *str1;
const char *str2;
{
if (str1 == str2)
return 0;
if (str1 == 0)
return -1;
if (str2 == 0)
return 1;
for (;*str1 && *str2; ++str1, ++str2)
{
if (*str1 != *str2)
break;
}
return (int)(*str1 - *str2);
}
#endif
#if ! HAVE_STRCHR
# if HAVE_INDEX
# define strchr index
# else
# define strchr rpl_strchr
static const char *strchr LT_PARAMS((const char *str, int ch));
const char*
strchr(str, ch)
const char *str;
int ch;
{
const char *p;
for (p = str; *p != (char)ch && *p != LT_EOS_CHAR; ++p)
/*NOWORK*/;
return (*p == (char)ch) ? p : 0;
}
# endif
#endif /* !HAVE_STRCHR */
#if ! HAVE_STRRCHR
# if HAVE_RINDEX
# define strrchr rindex
# else
# define strrchr rpl_strrchr
static const char *strrchr LT_PARAMS((const char *str, int ch));
const char*
strrchr(str, ch)
const char *str;
int ch;
{
const char *p, *q = 0;
for (p = str; *p != LT_EOS_CHAR; ++p)
{
if (*p == (char) ch)
{
q = p;
}
}
return q;
}
# endif
#endif
/* NOTE: Neither bcopy nor the memcpy implementation below can
reliably handle copying in overlapping areas of memory. Use
memmove (for which there is a fallback implmentation below)
if you need that behaviour. */
#if ! HAVE_MEMCPY
# if HAVE_BCOPY
# define memcpy(dest, src, size) bcopy (src, dest, size)
# else
# define memcpy rpl_memcpy
static lt_ptr memcpy LT_PARAMS((lt_ptr dest, const lt_ptr src, size_t size));
lt_ptr
memcpy (dest, src, size)
lt_ptr dest;
const lt_ptr src;
size_t size;
{
size_t i = 0;
for (i = 0; i < size; ++i)
{
dest[i] = src[i];
}
return dest;
}
# endif /* !HAVE_BCOPY */
#endif /* !HAVE_MEMCPY */
#if ! HAVE_MEMMOVE
# define memmove rpl_memmove
static lt_ptr memmove LT_PARAMS((lt_ptr dest, const lt_ptr src, size_t size));
lt_ptr
memmove (dest, src, size)
lt_ptr dest;
const lt_ptr src;
size_t size;
{
size_t i;
if (dest < src)
for (i = 0; i < size; ++i)
{
dest[i] = src[i];
}
else if (dest > src)
for (i = size -1; i >= 0; --i)
{
dest[i] = src[i];
}
return dest;
}
#endif /* !HAVE_MEMMOVE */
/* According to Alexandre Oliva <[email protected]>,
``realloc is not entirely portable''
In any case we want to use the allocator supplied by the user without
burdening them with an lt_dlrealloc function pointer to maintain.
Instead implement our own version (with known boundary conditions)
using lt_dlmalloc and lt_dlfree. */
#undef realloc
#define realloc rpl_realloc
lt_ptr
realloc (ptr, size)
lt_ptr ptr;
size_t size;
{
if (size <= 0)
{
/* For zero or less bytes, free the original memory */
if (ptr != 0)
{
lt_dlfree (ptr);
}
return (lt_ptr) 0;
}
else if (ptr == 0)
{
/* Allow reallocation of a NULL pointer. */
return lt_dlmalloc (size);
}
else
{
/* Allocate a new block, copy and free the old block. */
lt_ptr mem = lt_dlmalloc (size);
if (mem)
{
memcpy (mem, ptr, size);
lt_dlfree (ptr);
}
/* Note that the contents of PTR are not damaged if there is
insufficient memory to realloc. */
return mem;
}
}
#if ! HAVE_ARGZ_APPEND
# define argz_append rpl_argz_append
static error_t argz_append LT_PARAMS((char **pargz, size_t *pargz_len,
const char *buf, size_t buf_len));
error_t
argz_append (pargz, pargz_len, buf, buf_len)
char **pargz;
size_t *pargz_len;
const char *buf;
size_t buf_len;
{
size_t argz_len;
char *argz;
assert (pargz);
assert (pargz_len);
assert ((*pargz && *pargz_len) || (!*pargz && !*pargz_len));
/* If nothing needs to be appended, no more work is required. */
if (buf_len == 0)
return 0;
/* Ensure there is enough room to append BUF_LEN. */
argz_len = *pargz_len + buf_len;
argz = LT_DLREALLOC (char, *pargz, argz_len);
if (!argz)
return ENOMEM;
/* Copy characters from BUF after terminating '\0' in ARGZ. */
memcpy (argz + *pargz_len, buf, buf_len);
/* Assign new values. */
*pargz = argz;
*pargz_len = argz_len;
return 0;
}
#endif /* !HAVE_ARGZ_APPEND */
#if ! HAVE_ARGZ_CREATE_SEP
# define argz_create_sep rpl_argz_create_sep
static error_t argz_create_sep LT_PARAMS((const char *str, int delim,
char **pargz, size_t *pargz_len));
error_t
argz_create_sep (str, delim, pargz, pargz_len)
const char *str;
int delim;
char **pargz;
size_t *pargz_len;
{
size_t argz_len;
char *argz = 0;
assert (str);
assert (pargz);
assert (pargz_len);
/* Make a copy of STR, but replacing each occurence of
DELIM with '\0'. */
argz_len = 1+ LT_STRLEN (str);
if (argz_len)
{
const char *p;
char *q;
argz = LT_DLMALLOC (char, argz_len);
if (!argz)
return ENOMEM;
for (p = str, q = argz; *p != LT_EOS_CHAR; ++p)
{
if (*p == delim)
{
/* Ignore leading delimiters, and fold consecutive
delimiters in STR into a single '\0' in ARGZ. */
if ((q > argz) && (q[-1] != LT_EOS_CHAR))
*q++ = LT_EOS_CHAR;
else
--argz_len;
}
else
*q++ = *p;
}
/* Copy terminating LT_EOS_CHAR. */
*q = *p;
}
/* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory. */
if (!argz_len)
LT_DLFREE (argz);
/* Assign new values. */
*pargz = argz;
*pargz_len = argz_len;
return 0;
}
#endif /* !HAVE_ARGZ_CREATE_SEP */
#if ! HAVE_ARGZ_INSERT
# define argz_insert rpl_argz_insert
static error_t argz_insert LT_PARAMS((char **pargz, size_t *pargz_len,
char *before, const char *entry));
error_t
argz_insert (pargz, pargz_len, before, entry)
char **pargz;
size_t *pargz_len;
char *before;
const char *entry;
{
assert (pargz);
assert (pargz_len);
assert (entry && *entry);
/* Either PARGZ/PARGZ_LEN is empty and BEFORE is NULL,
or BEFORE points into an address within the ARGZ vector. */
assert ((!*pargz && !*pargz_len && !before)
|| ((*pargz <= before) && (before < (*pargz + *pargz_len))));
/* No BEFORE address indicates ENTRY should be inserted after the
current last element. */
if (!before)
return argz_append (pargz, pargz_len, entry, 1+ LT_STRLEN (entry));
/* This probably indicates a programmer error, but to preserve
semantics, scan back to the start of an entry if BEFORE points
into the middle of it. */
while ((before >= *pargz) && (before[-1] != LT_EOS_CHAR))
--before;
{
size_t entry_len = 1+ LT_STRLEN (entry);
size_t argz_len = *pargz_len + entry_len;
size_t offset = before - *pargz;
char *argz = LT_DLREALLOC (char, *pargz, argz_len);
if (!argz)
return ENOMEM;
/* Make BEFORE point to the equivalent offset in ARGZ that it
used to have in *PARGZ incase realloc() moved the block. */
before = argz + offset;
/* Move the ARGZ entries starting at BEFORE up into the new
space at the end -- making room to copy ENTRY into the
resulting gap. */
memmove (before + entry_len, before, *pargz_len - offset);
memcpy (before, entry, entry_len);
/* Assign new values. */
*pargz = argz;
*pargz_len = argz_len;
}
return 0;
}
#endif /* !HAVE_ARGZ_INSERT */
#if ! HAVE_ARGZ_NEXT
# define argz_next rpl_argz_next
static char *argz_next LT_PARAMS((char *argz, size_t argz_len,
const char *entry));
char *
argz_next (argz, argz_len, entry)
char *argz;
size_t argz_len;
const char *entry;
{
assert ((argz && argz_len) || (!argz && !argz_len));
if (entry)
{
/* Either ARGZ/ARGZ_LEN is empty, or ENTRY points into an address
within the ARGZ vector. */
assert ((!argz && !argz_len)
|| ((argz <= entry) && (entry < (argz + argz_len))));
/* Move to the char immediately after the terminating
'\0' of ENTRY. */
entry = 1+ strchr (entry, LT_EOS_CHAR);
/* Return either the new ENTRY, or else NULL if ARGZ is
exhausted. */
return (entry >= argz + argz_len) ? 0 : (char *) entry;
}
else
{
/* This should probably be flagged as a programmer error,
since starting an argz_next loop with the iterator set
to ARGZ is safer. To preserve semantics, handle the NULL
case by returning the start of ARGZ (if any). */
if (argz_len > 0)
return argz;
else
return 0;
}
}
#endif /* !HAVE_ARGZ_NEXT */
#if ! HAVE_ARGZ_STRINGIFY
# define argz_stringify rpl_argz_stringify
static void argz_stringify LT_PARAMS((char *argz, size_t argz_len,
int sep));
void
argz_stringify (argz, argz_len, sep)
char *argz;
size_t argz_len;
int sep;
{
assert ((argz && argz_len) || (!argz && !argz_len));
if (sep)
{
--argz_len; /* don't stringify the terminating EOS */
while (--argz_len > 0)
{
if (argz[argz_len] == LT_EOS_CHAR)
argz[argz_len] = sep;
}
}
}
#endif /* !HAVE_ARGZ_STRINGIFY */
/* --- TYPE DEFINITIONS -- */
/* This type is used for the array of caller data sets in each handler. */
typedef struct {
lt_dlcaller_id key;
lt_ptr data;
} lt_caller_data;
/* --- OPAQUE STRUCTURES DECLARED IN LTDL.H --- */
/* Extract the diagnostic strings from the error table macro in the same
order as the enumerated indices in ltdl.h. */
static const char *lt_dlerror_strings[] =
{
lt_dlerror_names_list
0
};
/* This structure is used for the list of registered loaders. */
struct lt_dlloader {
struct lt_dlloader *next;
const char *loader_name; /* identifying name for each loader */
const char *sym_prefix; /* prefix for symbols */
lt_module_open *module_open;
lt_module_close *module_close;
lt_find_sym *find_sym;
lt_dlloader_exit *dlloader_exit;
lt_user_data dlloader_data;
};
struct lt_dlhandle_struct {
struct lt_dlhandle_struct *next;
lt_dlloader *loader; /* dlopening interface */
lt_dlinfo info;
int depcount; /* number of dependencies */
lt_dlhandle *deplibs; /* dependencies */
lt_module module; /* system module handle */
lt_ptr system; /* system specific data */
lt_caller_data *caller_data; /* per caller associated data */
int flags; /* various boolean stats */
};
/* Various boolean flags can be stored in the flags field of an
lt_dlhandle_struct... */
#define LT_DLGET_FLAG(handle, flag) (((handle)->flags & (flag)) == (flag))
#define LT_DLSET_FLAG(handle, flag) ((handle)->flags |= (flag))
#define LT_DLRESIDENT_FLAG (0x01 << 0)
/* ...add more flags here... */
#define LT_DLIS_RESIDENT(handle) LT_DLGET_FLAG(handle, LT_DLRESIDENT_FLAG)
#define LT_DLSTRERROR(name) lt_dlerror_strings[LT_CONC(LT_ERROR_,name)]
static const char objdir[] = LTDL_OBJDIR;
static const char archive_ext[] = LTDL_ARCHIVE_EXT;
#ifdef LTDL_SHLIB_EXT
static const char shlib_ext[] = LTDL_SHLIB_EXT;
#endif
#ifdef LTDL_SYSSEARCHPATH
static const char sys_search_path[] = LTDL_SYSSEARCHPATH;
#endif
/* --- MUTEX LOCKING --- */
/* Macros to make it easier to run the lock functions only if they have
been registered. The reason for the complicated lock macro is to
ensure that the stored error message from the last error is not
accidentally erased if the current function doesn't generate an
error of its own. */
#define LT_DLMUTEX_LOCK() LT_STMT_START { \
if (lt_dlmutex_lock_func) (*lt_dlmutex_lock_func)(); \
} LT_STMT_END
#define LT_DLMUTEX_UNLOCK() LT_STMT_START { \
if (lt_dlmutex_unlock_func) (*lt_dlmutex_unlock_func)();\
} LT_STMT_END
#define LT_DLMUTEX_SETERROR(errormsg) LT_STMT_START { \
if (lt_dlmutex_seterror_func) \
(*lt_dlmutex_seterror_func) (errormsg); \
else lt_dllast_error = (errormsg); } LT_STMT_END
#define LT_DLMUTEX_GETERROR(errormsg) LT_STMT_START { \
if (lt_dlmutex_seterror_func) \
(errormsg) = (*lt_dlmutex_geterror_func) (); \
else (errormsg) = lt_dllast_error; } LT_STMT_END
/* The mutex functions stored here are global, and are necessarily the
same for all threads that wish to share access to libltdl. */
static lt_dlmutex_lock *lt_dlmutex_lock_func = 0;
static lt_dlmutex_unlock *lt_dlmutex_unlock_func = 0;
static lt_dlmutex_seterror *lt_dlmutex_seterror_func = 0;
static lt_dlmutex_geterror *lt_dlmutex_geterror_func = 0;
static const char *lt_dllast_error = 0;
/* Either set or reset the mutex functions. Either all the arguments must
be valid functions, or else all can be NULL to turn off locking entirely.
The registered functions should be manipulating a static global lock
from the lock() and unlock() callbacks, which needs to be reentrant. */
int
lt_dlmutex_register (lock, unlock, seterror, geterror)
lt_dlmutex_lock *lock;
lt_dlmutex_unlock *unlock;
lt_dlmutex_seterror *seterror;
lt_dlmutex_geterror *geterror;
{
lt_dlmutex_unlock *old_unlock = unlock;
int errors = 0;
/* Lock using the old lock() callback, if any. */
LT_DLMUTEX_LOCK ();
if ((lock && unlock && seterror && geterror)
|| !(lock || unlock || seterror || geterror))
{
lt_dlmutex_lock_func = lock;
lt_dlmutex_unlock_func = unlock;
lt_dlmutex_geterror_func = geterror;
}
else
{
LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_MUTEX_ARGS));
++errors;
}
/* Use the old unlock() callback we saved earlier, if any. Otherwise
record any errors using internal storage. */
if (old_unlock)
(*old_unlock) ();
/* Return the number of errors encountered during the execution of
this function. */
return errors;
}
/* --- ERROR HANDLING --- */
static const char **user_error_strings = 0;
static int errorcount = LT_ERROR_MAX;
int
lt_dladderror (diagnostic)
const char *diagnostic;
{
int errindex = 0;
int result = -1;
const char **temp = (const char **) 0;
assert (diagnostic);
LT_DLMUTEX_LOCK ();
errindex = errorcount - LT_ERROR_MAX;
temp = LT_EREALLOC (const char *, user_error_strings, 1 + errindex);
if (temp)
{
user_error_strings = temp;
user_error_strings[errindex] = diagnostic;
result = errorcount++;
}
LT_DLMUTEX_UNLOCK ();
return result;
}
int
lt_dlseterror (errindex)
int errindex;
{
int errors = 0;
LT_DLMUTEX_LOCK ();
if (errindex >= errorcount || errindex < 0)
{
/* Ack! Error setting the error message! */
LT_DLMUTEX_SETERROR (LT_DLSTRERROR (INVALID_ERRORCODE));
++errors;
}
else if (errindex < LT_ERROR_MAX)
{
/* No error setting the error message! */
LT_DLMUTEX_SETERROR (lt_dlerror_strings[errindex]);
}
else
{
/* No error setting the error message! */
LT_DLMUTEX_SETERROR (user_error_strings[errindex - LT_ERROR_MAX]);
}
LT_DLMUTEX_UNLOCK ();
return errors;
}
lt_ptr
lt_emalloc (size)
size_t size;
{
lt_ptr mem = lt_dlmalloc (size);
if (size && !mem)
LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY));
return mem;
}
lt_ptr
lt_erealloc (addr, size)
lt_ptr addr;
size_t size;
{
lt_ptr mem = realloc (addr, size);
if (size && !mem)
LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY));
return mem;
}
char *
lt_estrdup (str)
const char *str;
{
char *dup = strdup (str);
if (LT_STRLEN (str) && !dup)
LT_DLMUTEX_SETERROR (LT_DLSTRERROR (NO_MEMORY));
return dup;
}
/* --- DLOPEN() INTERFACE LOADER --- */
/* The Cygwin dlopen implementation prints a spurious error message to
stderr if its call to LoadLibrary() fails for any reason. We can
mitigate this by not using the Cygwin implementation, and falling
back to our own LoadLibrary() wrapper. */
#if HAVE_LIBDL && !defined(__CYGWIN__)
/* dynamic linking with dlopen/dlsym */
#if HAVE_DLFCN_H
# include <dlfcn.h>
#endif
#if HAVE_SYS_DL_H
# include <sys/dl.h>
#endif
#ifdef RTLD_GLOBAL
# define LT_GLOBAL RTLD_GLOBAL
#else
# ifdef DL_GLOBAL
# define LT_GLOBAL DL_GLOBAL
# endif
#endif /* !RTLD_GLOBAL */
#ifndef LT_GLOBAL
# define LT_GLOBAL 0
#endif /* !LT_GLOBAL */
/* We may have to define LT_LAZY_OR_NOW in the command line if we
find out it does not work in some platform. */
#ifndef LT_LAZY_OR_NOW
# ifdef RTLD_LAZY
# define LT_LAZY_OR_NOW RTLD_LAZY
# else
# ifdef DL_LAZY
# define LT_LAZY_OR_NOW DL_LAZY
# endif
# endif /* !RTLD_LAZY */
#endif
#ifndef LT_LAZY_OR_NOW
# ifdef RTLD_NOW
# define LT_LAZY_OR_NOW RTLD_NOW
# else
# ifdef DL_NOW
# define LT_LAZY_OR_NOW DL_NOW
# endif
# endif /* !RTLD_NOW */
#endif
#ifndef LT_LAZY_OR_NOW
# define LT_LAZY_OR_NOW 0
#endif /* !LT_LAZY_OR_NOW */
#if HAVE_DLERROR
# define DLERROR(arg) dlerror ()
#else
# define DLERROR(arg) LT_DLSTRERROR (arg)
#endif
static lt_module
sys_dl_open (loader_data, filename)
lt_user_data loader_data;
const char *filename;
{
lt_module module = dlopen (filename, LT_GLOBAL | LT_LAZY_OR_NOW);
if (!module)
{
LT_DLMUTEX_SETERROR (DLERROR (CANNOT_OPEN));
}
return module;
}
static int
sys_dl_close (loader_data, module)
lt_user_data loader_data;
lt_module module;
{
int errors = 0;
if (dlclose (module) != 0)
{
LT_DLMUTEX_SETERROR (DLERROR (CANNOT_CLOSE));
++errors;
}
return errors;
}
static lt_ptr
sys_dl_sym (loader_data, module, symbol)
lt_user_data loader_data;