forked from ivmai/bdwgc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyn_load.c
1580 lines (1411 loc) · 54.3 KB
/
dyn_load.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
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1997 by Silicon Graphics. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*/
#include "private/gc_priv.h"
/*
* This is incredibly OS specific code for tracking down data sections in
* dynamic libraries. There appears to be no way of doing this quickly
* without groveling through undocumented data structures. We would argue
* that this is a bug in the design of the dlopen interface. THIS CODE
* MAY BREAK IN FUTURE OS RELEASES. If this matters to you, don't hesitate
* to let your vendor know ...
*
* None of this is safe with dlclose and incremental collection.
* But then not much of anything is safe in the presence of dlclose.
*/
#if !defined(MACOS) && !defined(SN_TARGET_ORBIS) && !defined(SN_TARGET_PSP2) \
&& !defined(_WIN32_WCE) && !defined(__CC_ARM)
# include <sys/types.h>
#endif
/* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */
#undef GC_MUST_RESTORE_REDEFINED_DLOPEN
#if defined(GC_PTHREADS) && !defined(GC_NO_DLOPEN) \
&& !defined(GC_NO_THREAD_REDIRECTS) && !defined(GC_USE_LD_WRAP)
/* To support threads in Solaris, gc.h interposes on dlopen by */
/* defining "dlopen" to be "GC_dlopen", which is implemented below. */
/* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
/* real system dlopen() in their implementation. We first remove */
/* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
# undef dlopen
# define GC_MUST_RESTORE_REDEFINED_DLOPEN
#endif /* !GC_NO_DLOPEN */
/* A user-supplied routine (custom filter) that might be called to */
/* determine whether a DSO really needs to be scanned by the GC. */
/* 0 means no filter installed. May be unused on some platforms. */
/* FIXME: Add filter support for more platforms. */
STATIC GC_has_static_roots_func GC_has_static_roots = 0;
#if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
|| defined(CYGWIN32)) && !defined(PCR)
#if !defined(DARWIN) && !defined(SCO_ELF) && !defined(SOLARISDL) \
&& !defined(AIX) && !defined(DGUX) && !defined(IRIX5) && !defined(HPUX) \
&& !defined(CYGWIN32) && !defined(MSWIN32) && !defined(MSWINCE) \
&& !(defined(ALPHA) && defined(OSF1)) \
&& !(defined(FREEBSD) && defined(__ELF__)) \
&& !((defined(LINUX) || defined(NACL)) && defined(__ELF__)) \
&& !(defined(NETBSD) && defined(__ELF__)) \
&& !defined(HAIKU) && !defined(HURD) \
&& !(defined(OPENBSD) && (defined(__ELF__) || defined(M68K))) \
&& !defined(CPPCHECK)
# error We only know how to find data segments of dynamic libraries for above.
# error Additional SVR4 variants might not be too hard to add.
#endif
#include <stdio.h>
#ifdef SOLARISDL
# include <sys/elf.h>
# include <dlfcn.h>
# include <link.h>
#endif
#if defined(NETBSD)
# include <sys/param.h>
# include <dlfcn.h>
# include <machine/elf_machdep.h>
# define ELFSIZE ARCH_ELFSIZE
#endif
#if defined(OPENBSD)
# include <sys/param.h>
# if (OpenBSD >= 200519) && !defined(HAVE_DL_ITERATE_PHDR)
# define HAVE_DL_ITERATE_PHDR
# endif
#endif /* OPENBSD */
#if defined(SCO_ELF) || defined(DGUX) || defined(HURD) \
|| (defined(__ELF__) && (defined(LINUX) || defined(FREEBSD) \
|| defined(NACL) || defined(NETBSD) \
|| defined(OPENBSD)))
# include <stddef.h>
# if !defined(OPENBSD) && !defined(HOST_ANDROID)
/* OpenBSD does not have elf.h file; link.h below is sufficient. */
/* Exclude Android because linker.h below includes its own version. */
# include <elf.h>
# endif
# ifdef HOST_ANDROID
/* If you don't need the "dynamic loading" feature, you may build */
/* the collector with -D IGNORE_DYNAMIC_LOADING. */
# ifdef BIONIC_ELFDATA_REDEF_BUG
/* Workaround a problem in Bionic (as of Android 4.2) which has */
/* mismatching ELF_DATA definitions in sys/exec_elf.h and */
/* asm/elf.h included from linker.h file (similar to EM_ALPHA). */
# include <asm/elf.h>
# include <linux/elf-em.h>
# undef ELF_DATA
# undef EM_ALPHA
# endif
# include <link.h>
# if !defined(GC_DONT_DEFINE_LINK_MAP) && !(__ANDROID_API__ >= 21)
/* link_map and r_debug are defined in link.h of NDK r10+. */
/* bionic/linker/linker.h defines them too but the header */
/* itself is a C++ one starting from Android 4.3. */
struct link_map {
uintptr_t l_addr;
char* l_name;
uintptr_t l_ld;
struct link_map* l_next;
struct link_map* l_prev;
};
struct r_debug {
int32_t r_version;
struct link_map* r_map;
void (*r_brk)(void);
int32_t r_state;
uintptr_t r_ldbase;
};
# endif
# else
EXTERN_C_BEGIN /* Workaround missing extern "C" around _DYNAMIC */
/* symbol in link.h of some Linux hosts. */
# include <link.h>
EXTERN_C_END
# endif
#endif
/* Newer versions of GNU/Linux define this macro. We
* define it similarly for any ELF systems that don't. */
# ifndef ElfW
# if defined(FREEBSD)
# if __ELF_WORD_SIZE == 32
# define ElfW(type) Elf32_##type
# else
# define ElfW(type) Elf64_##type
# endif
# elif defined(NETBSD) || defined(OPENBSD)
# if ELFSIZE == 32
# define ElfW(type) Elf32_##type
# else
# define ElfW(type) Elf64_##type
# endif
# else
# if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
# define ElfW(type) Elf32_##type
# else
# define ElfW(type) Elf64_##type
# endif
# endif
# endif
#if defined(SOLARISDL) && !defined(USE_PROC_FOR_LIBRARIES)
EXTERN_C_BEGIN
extern ElfW(Dyn) _DYNAMIC;
EXTERN_C_END
STATIC struct link_map *
GC_FirstDLOpenedLinkMap(void)
{
ElfW(Dyn) *dp;
static struct link_map * cachedResult = 0;
static ElfW(Dyn) *dynStructureAddr = 0;
/* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
# ifdef SUNOS53_SHARED_LIB
/* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
/* up properly in dynamically linked .so's. This means we have */
/* to use its value in the set of original object files loaded */
/* at program startup. */
if( dynStructureAddr == 0 ) {
void* startupSyms = dlopen(0, RTLD_LAZY);
dynStructureAddr = (ElfW(Dyn)*)(word)dlsym(startupSyms, "_DYNAMIC");
}
# else
dynStructureAddr = &_DYNAMIC;
# endif
if (0 == COVERT_DATAFLOW(dynStructureAddr)) {
/* _DYNAMIC symbol not resolved. */
return(0);
}
if (cachedResult == 0) {
int tag;
for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
if (tag == DT_DEBUG) {
struct r_debug *rd = (struct r_debug *)dp->d_un.d_ptr;
if (rd != NULL) {
struct link_map *lm = rd->r_map;
if (lm != NULL)
cachedResult = lm->l_next; /* might be NULL */
}
break;
}
}
}
return cachedResult;
}
#endif /* SOLARISDL ... */
/* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
# ifdef GC_MUST_RESTORE_REDEFINED_DLOPEN
# define dlopen GC_dlopen
# endif
# if defined(SOLARISDL)
/* Add dynamic library data sections to the root set. */
# if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS) \
&& !defined(CPPCHECK)
# error Fix mutual exclusion with dlopen
# endif
# ifndef USE_PROC_FOR_LIBRARIES
GC_INNER void GC_register_dynamic_libraries(void)
{
struct link_map *lm;
for (lm = GC_FirstDLOpenedLinkMap(); lm != 0; lm = lm->l_next) {
ElfW(Ehdr) * e;
ElfW(Phdr) * p;
unsigned long offset;
char * start;
int i;
e = (ElfW(Ehdr) *) lm->l_addr;
p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
offset = ((unsigned long)(lm->l_addr));
for( i = 0; i < (int)e->e_phnum; i++, p++ ) {
switch( p->p_type ) {
case PT_LOAD:
{
if( !(p->p_flags & PF_W) ) break;
start = ((char *)(p->p_vaddr)) + offset;
GC_add_roots_inner(start, start + p->p_memsz, TRUE);
}
break;
default:
break;
}
}
}
}
# endif /* !USE_PROC ... */
# endif /* SOLARISDL */
#if defined(SCO_ELF) || defined(DGUX) || defined(HURD) \
|| (defined(__ELF__) && (defined(LINUX) || defined(FREEBSD) \
|| defined(NACL) || defined(NETBSD) \
|| defined(OPENBSD)))
#ifdef USE_PROC_FOR_LIBRARIES
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MAPS_BUF_SIZE (32*1024)
/* Sort an array of HeapSects by start address. */
/* Unfortunately at least some versions of */
/* Linux qsort end up calling malloc by way of sysconf, and hence can't */
/* be used in the collector. Hence we roll our own. Should be */
/* reasonably fast if the array is already mostly sorted, as we expect */
/* it to be. */
static void sort_heap_sects(struct HeapSect *base, size_t number_of_elements)
{
signed_word n = (signed_word)number_of_elements;
signed_word nsorted = 1;
while (nsorted < n) {
signed_word i;
while (nsorted < n &&
(word)base[nsorted-1].hs_start < (word)base[nsorted].hs_start)
++nsorted;
if (nsorted == n) break;
GC_ASSERT((word)base[nsorted-1].hs_start > (word)base[nsorted].hs_start);
i = nsorted - 1;
while (i >= 0 && (word)base[i].hs_start > (word)base[i+1].hs_start) {
struct HeapSect tmp = base[i];
base[i] = base[i+1];
base[i+1] = tmp;
--i;
}
GC_ASSERT((word)base[nsorted-1].hs_start < (word)base[nsorted].hs_start);
++nsorted;
}
}
STATIC void GC_register_map_entries(char *maps)
{
char *prot;
char *buf_ptr = maps;
ptr_t start, end;
unsigned int maj_dev;
ptr_t least_ha, greatest_ha;
unsigned i;
GC_ASSERT(I_HOLD_LOCK());
sort_heap_sects(GC_our_memory, GC_n_memory);
least_ha = GC_our_memory[0].hs_start;
greatest_ha = GC_our_memory[GC_n_memory-1].hs_start
+ GC_our_memory[GC_n_memory-1].hs_bytes;
for (;;) {
buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, &prot,
&maj_dev, 0);
if (NULL == buf_ptr)
break;
if (prot[1] == 'w') {
/* This is a writable mapping. Add it to */
/* the root set unless it is already otherwise */
/* accounted for. */
if ((word)start <= (word)GC_stackbottom
&& (word)end >= (word)GC_stackbottom) {
/* Stack mapping; discard */
continue;
}
# ifdef THREADS
/* This may fail, since a thread may already be */
/* unregistered, but its thread stack may still be there. */
/* That can fail because the stack may disappear while */
/* we're marking. Thus the marker is, and has to be */
/* prepared to recover from segmentation faults. */
if (GC_segment_is_thread_stack(start, end)) continue;
/* FIXME: NPTL squirrels */
/* away pointers in pieces of the stack segment that we */
/* don't scan. We work around this */
/* by treating anything allocated by libpthread as */
/* uncollectible, as we do in some other cases. */
/* A specifically identified problem is that */
/* thread stacks contain pointers to dynamic thread */
/* vectors, which may be reused due to thread caching. */
/* They may not be marked if the thread is still live. */
/* This specific instance should be addressed by */
/* INCLUDE_LINUX_THREAD_DESCR, but that doesn't quite */
/* seem to suffice. */
/* We currently trace entire thread stacks, if they are */
/* are currently cached but unused. This is */
/* very suboptimal for performance reasons. */
# endif
/* We no longer exclude the main data segment. */
if ((word)end <= (word)least_ha
|| (word)start >= (word)greatest_ha) {
/* The easy case; just trace entire segment */
GC_add_roots_inner(start, end, TRUE);
continue;
}
/* Add sections that don't belong to us. */
i = 0;
while ((word)(GC_our_memory[i].hs_start
+ GC_our_memory[i].hs_bytes) < (word)start)
++i;
GC_ASSERT(i < GC_n_memory);
if ((word)GC_our_memory[i].hs_start <= (word)start) {
start = GC_our_memory[i].hs_start
+ GC_our_memory[i].hs_bytes;
++i;
}
while (i < GC_n_memory
&& (word)GC_our_memory[i].hs_start < (word)end
&& (word)start < (word)end) {
if ((word)start < (word)GC_our_memory[i].hs_start)
GC_add_roots_inner(start,
GC_our_memory[i].hs_start, TRUE);
start = GC_our_memory[i].hs_start
+ GC_our_memory[i].hs_bytes;
++i;
}
if ((word)start < (word)end)
GC_add_roots_inner(start, end, TRUE);
} else if (prot[0] == '-' && prot[1] == '-' && prot[2] == '-') {
/* Even roots added statically might disappear partially */
/* (e.g. the roots added by INCLUDE_LINUX_THREAD_DESCR). */
GC_remove_roots_subregion(start, end);
}
}
}
GC_INNER void GC_register_dynamic_libraries(void)
{
char *maps = GC_get_maps();
if (NULL == maps)
ABORT("Failed to read /proc for library registration");
GC_register_map_entries(maps);
}
/* We now take care of the main data segment ourselves: */
GC_INNER GC_bool GC_register_main_static_data(void)
{
return FALSE;
}
# define HAVE_REGISTER_MAIN_STATIC_DATA
#else /* !USE_PROC_FOR_LIBRARIES */
/* The following is the preferred way to walk dynamic libraries */
/* for glibc 2.2.4+. Unfortunately, it doesn't work for older */
/* versions. Thanks to Jakub Jelinek for most of the code. */
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
|| (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)) \
|| defined(HOST_ANDROID) /* Are others OK here, too? */
# ifndef HAVE_DL_ITERATE_PHDR
# define HAVE_DL_ITERATE_PHDR
# endif
# ifdef HOST_ANDROID
/* Android headers might have no such definition for some targets. */
EXTERN_C_BEGIN
extern int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *,
size_t, void *),
void *data);
EXTERN_C_END
# endif
#endif /* __GLIBC__ >= 2 || HOST_ANDROID */
#if defined(__DragonFly__) || defined(__FreeBSD_kernel__) \
|| (defined(FREEBSD) && __FreeBSD__ >= 7)
/* On the FreeBSD system, any target system at major version 7 shall */
/* have dl_iterate_phdr; therefore, we need not make it weak as below. */
# ifndef HAVE_DL_ITERATE_PHDR
# define HAVE_DL_ITERATE_PHDR
# endif
# define DL_ITERATE_PHDR_STRONG
#elif defined(HAVE_DL_ITERATE_PHDR)
/* We have the header files for a glibc that includes dl_iterate_phdr.*/
/* It may still not be available in the library on the target system. */
/* Thus we also treat it as a weak symbol. */
EXTERN_C_BEGIN
# pragma weak dl_iterate_phdr
EXTERN_C_END
#endif
#if defined(HAVE_DL_ITERATE_PHDR)
# ifdef PT_GNU_RELRO
/* Instead of registering PT_LOAD sections directly, we keep them */
/* in a temporary list, and filter them by excluding PT_GNU_RELRO */
/* segments. Processing PT_GNU_RELRO sections with */
/* GC_exclude_static_roots instead would be superficially cleaner. But */
/* it runs into trouble if a client registers an overlapping segment, */
/* which unfortunately seems quite possible. */
# define MAX_LOAD_SEGS MAX_ROOT_SETS
static struct load_segment {
ptr_t start;
ptr_t end;
/* Room for a second segment if we remove a RELRO segment */
/* from the middle. */
ptr_t start2;
ptr_t end2;
} load_segs[MAX_LOAD_SEGS];
static int n_load_segs;
static GC_bool load_segs_overflow;
# endif /* PT_GNU_RELRO */
STATIC int GC_register_dynlib_callback(struct dl_phdr_info * info,
size_t size, void * ptr)
{
const ElfW(Phdr) * p;
ptr_t start, end;
int i;
/* Make sure struct dl_phdr_info is at least as big as we need. */
if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
+ sizeof (info->dlpi_phnum))
return -1;
p = info->dlpi_phdr;
for (i = 0; i < (int)info->dlpi_phnum; i++, p++) {
if (p->p_type == PT_LOAD) {
GC_has_static_roots_func callback = GC_has_static_roots;
if ((p->p_flags & PF_W) == 0) continue;
start = (ptr_t)p->p_vaddr + info->dlpi_addr;
end = start + p->p_memsz;
if (callback != 0 && !callback(info->dlpi_name, start, p->p_memsz))
continue;
# ifdef PT_GNU_RELRO
# if CPP_WORDSZ == 64
/* TODO: GC_push_all eventually does the correct */
/* rounding to the next multiple of ALIGNMENT, so, most */
/* probably, we should remove the corresponding assertion */
/* check in GC_add_roots_inner along with this code line. */
/* start pointer value may require aligning. */
start = (ptr_t)((word)start & ~(word)(sizeof(word) - 1));
# endif
if (n_load_segs >= MAX_LOAD_SEGS) {
if (!load_segs_overflow) {
WARN("Too many PT_LOAD segments;"
" registering as roots directly...\n", 0);
load_segs_overflow = TRUE;
}
GC_add_roots_inner(start, end, TRUE);
} else {
load_segs[n_load_segs].start = start;
load_segs[n_load_segs].end = end;
load_segs[n_load_segs].start2 = 0;
load_segs[n_load_segs].end2 = 0;
++n_load_segs;
}
# else
GC_add_roots_inner(start, end, TRUE);
# endif /* !PT_GNU_RELRO */
}
}
# ifdef PT_GNU_RELRO
p = info->dlpi_phdr;
for (i = 0; i < (int)info->dlpi_phnum; i++, p++) {
if (p->p_type == PT_GNU_RELRO) {
/* This entry is known to be constant and will eventually be */
/* remapped as read-only. However, the address range covered */
/* by this entry is typically a subset of a previously */
/* encountered "LOAD" segment, so we need to exclude it. */
int j;
start = (ptr_t)p->p_vaddr + info->dlpi_addr;
end = start + p->p_memsz;
for (j = n_load_segs; --j >= 0; ) {
if ((word)start >= (word)load_segs[j].start
&& (word)start < (word)load_segs[j].end) {
if (load_segs[j].start2 != 0) {
WARN("More than one GNU_RELRO segment per load one\n",0);
} else {
GC_ASSERT((word)end <= (word)load_segs[j].end);
/* Remove from the existing load segment */
load_segs[j].end2 = load_segs[j].end;
load_segs[j].end = start;
load_segs[j].start2 = end;
}
break;
}
if (0 == j && 0 == GC_has_static_roots)
WARN("Failed to find PT_GNU_RELRO segment"
" inside PT_LOAD region\n", 0);
/* No warning reported in case of the callback is present */
/* because most likely the segment has been excluded. */
}
}
}
# endif
*(int *)ptr = 1; /* Signal that we were called */
return 0;
}
/* Do we need to separately register the main static data segment? */
GC_INNER GC_bool GC_register_main_static_data(void)
{
# ifdef DL_ITERATE_PHDR_STRONG
/* If dl_iterate_phdr is not a weak symbol then don't test against */
/* zero (otherwise a compiler might issue a warning). */
return FALSE;
# else
return 0 == COVERT_DATAFLOW(dl_iterate_phdr);
# endif
}
/* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
STATIC GC_bool GC_register_dynamic_libraries_dl_iterate_phdr(void)
{
int did_something;
if (GC_register_main_static_data())
return FALSE;
# ifdef PT_GNU_RELRO
{
static GC_bool excluded_segs = FALSE;
n_load_segs = 0;
load_segs_overflow = FALSE;
if (!EXPECT(excluded_segs, TRUE)) {
GC_exclude_static_roots_inner((ptr_t)load_segs,
(ptr_t)load_segs + sizeof(load_segs));
excluded_segs = TRUE;
}
}
# endif
did_something = 0;
dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
if (did_something) {
# ifdef PT_GNU_RELRO
int i;
for (i = 0; i < n_load_segs; ++i) {
if ((word)load_segs[i].end > (word)load_segs[i].start) {
GC_add_roots_inner(load_segs[i].start, load_segs[i].end, TRUE);
}
if ((word)load_segs[i].end2 > (word)load_segs[i].start2) {
GC_add_roots_inner(load_segs[i].start2, load_segs[i].end2, TRUE);
}
}
# endif
} else {
ptr_t datastart, dataend;
# ifdef DATASTART_IS_FUNC
static ptr_t datastart_cached = (ptr_t)GC_WORD_MAX;
/* Evaluate DATASTART only once. */
if (datastart_cached == (ptr_t)GC_WORD_MAX) {
datastart_cached = DATASTART;
}
datastart = datastart_cached;
# else
datastart = DATASTART;
# endif
# ifdef DATAEND_IS_FUNC
{
static ptr_t dataend_cached = 0;
/* Evaluate DATAEND only once. */
if (dataend_cached == 0) {
dataend_cached = DATAEND;
}
dataend = dataend_cached;
}
# else
dataend = DATAEND;
# endif
if (NULL == *(char * volatile *)&datastart
|| (word)datastart > (word)dataend)
ABORT_ARG2("Wrong DATASTART/END pair",
": %p .. %p", (void *)datastart, (void *)dataend);
/* dl_iterate_phdr may forget the static data segment in */
/* statically linked executables. */
GC_add_roots_inner(datastart, dataend, TRUE);
# ifdef GC_HAVE_DATAREGION2
if ((word)DATASTART2 - 1U >= (word)DATAEND2) {
/* Subtract one to check also for NULL */
/* without a compiler warning. */
ABORT_ARG2("Wrong DATASTART/END2 pair",
": %p .. %p", (void *)DATASTART2, (void *)DATAEND2);
}
GC_add_roots_inner(DATASTART2, DATAEND2, TRUE);
# endif
}
return TRUE;
}
# define HAVE_REGISTER_MAIN_STATIC_DATA
#else /* !HAVE_DL_ITERATE_PHDR */
/* Dynamic loading code for Linux running ELF. Somewhat tested on
* Linux/x86, untested but hopefully should work on Linux/Alpha.
* This code was derived from the Solaris/ELF support. Thanks to
* whatever kind soul wrote that. - Patrick Bridges */
/* This doesn't necessarily work in all cases, e.g. with preloaded
* dynamic libraries. */
# if defined(NETBSD) || defined(OPENBSD)
# include <sys/exec_elf.h>
/* for compatibility with 1.4.x */
# ifndef DT_DEBUG
# define DT_DEBUG 21
# endif
# ifndef PT_LOAD
# define PT_LOAD 1
# endif
# ifndef PF_W
# define PF_W 2
# endif
# elif !defined(HOST_ANDROID)
# include <elf.h>
# endif
# ifndef HOST_ANDROID
# include <link.h>
# endif
#endif /* !HAVE_DL_ITERATE_PHDR */
EXTERN_C_BEGIN
#ifdef __GNUC__
# pragma weak _DYNAMIC
#endif
extern ElfW(Dyn) _DYNAMIC[];
EXTERN_C_END
STATIC struct link_map *
GC_FirstDLOpenedLinkMap(void)
{
static struct link_map *cachedResult = 0;
if (0 == COVERT_DATAFLOW(_DYNAMIC)) {
/* _DYNAMIC symbol not resolved. */
return(0);
}
if( cachedResult == 0 ) {
# if defined(NETBSD) && defined(RTLD_DI_LINKMAP)
# if defined(CPPCHECK)
# define GC_RTLD_DI_LINKMAP 2
# else
# define GC_RTLD_DI_LINKMAP RTLD_DI_LINKMAP
# endif
struct link_map *lm = NULL;
if (!dlinfo(RTLD_SELF, GC_RTLD_DI_LINKMAP, &lm) && lm != NULL) {
/* Now lm points link_map object of libgc. Since it */
/* might not be the first dynamically linked object, */
/* try to find it (object next to the main object). */
while (lm->l_prev != NULL) {
lm = lm->l_prev;
}
cachedResult = lm->l_next;
}
# else
ElfW(Dyn) *dp;
int tag;
for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
if (tag == DT_DEBUG) {
struct r_debug *rd = (struct r_debug *)dp->d_un.d_ptr;
/* d_ptr could be null if libs are linked statically. */
if (rd != NULL) {
struct link_map *lm = rd->r_map;
if (lm != NULL)
cachedResult = lm->l_next; /* might be NULL */
}
break;
}
}
# endif /* !NETBSD || !RTLD_DI_LINKMAP */
}
return cachedResult;
}
GC_INNER void GC_register_dynamic_libraries(void)
{
struct link_map *lm;
# ifdef HAVE_DL_ITERATE_PHDR
if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
return;
}
# endif
for (lm = GC_FirstDLOpenedLinkMap(); lm != 0; lm = lm->l_next)
{
ElfW(Ehdr) * e;
ElfW(Phdr) * p;
unsigned long offset;
char * start;
int i;
e = (ElfW(Ehdr) *) lm->l_addr;
# ifdef HOST_ANDROID
if (e == NULL)
continue;
# endif
p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
offset = ((unsigned long)(lm->l_addr));
for( i = 0; i < (int)e->e_phnum; i++, p++ ) {
switch( p->p_type ) {
case PT_LOAD:
{
if( !(p->p_flags & PF_W) ) break;
start = ((char *)(p->p_vaddr)) + offset;
GC_add_roots_inner(start, start + p->p_memsz, TRUE);
}
break;
default:
break;
}
}
}
}
#endif /* !USE_PROC_FOR_LIBRARIES */
#endif /* LINUX */
#if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
#include <sys/procfs.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <elf.h>
#include <errno.h>
#include <signal.h> /* Only for the following test. */
#ifndef _sigargs
# define IRIX6
#endif
/* We use /proc to track down all parts of the address space that are */
/* mapped by the process, and throw out regions we know we shouldn't */
/* worry about. This may also work under other SVR4 variants. */
GC_INNER void GC_register_dynamic_libraries(void)
{
static int fd = -1;
char buf[30];
static prmap_t * addr_map = 0;
static int current_sz = 0; /* Number of records currently in addr_map */
int needed_sz = 0; /* Required size of addr_map */
int i;
long flags;
ptr_t start;
ptr_t limit;
ptr_t heap_start = HEAP_START;
ptr_t heap_end = heap_start;
# ifdef SOLARISDL
# define MA_PHYS 0
# endif /* SOLARISDL */
if (fd < 0) {
(void)snprintf(buf, sizeof(buf), "/proc/%ld", (long)getpid());
buf[sizeof(buf) - 1] = '\0';
fd = open(buf, O_RDONLY);
if (fd < 0) {
ABORT("/proc open failed");
}
}
if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
ABORT_ARG2("/proc PIOCNMAP ioctl failed",
": fd = %d, errno = %d", fd, errno);
}
if (needed_sz >= current_sz) {
GC_scratch_recycle_no_gww(addr_map,
(size_t)current_sz * sizeof(prmap_t));
current_sz = needed_sz * 2 + 1;
/* Expansion, plus room for 0 record */
addr_map = (prmap_t *)GC_scratch_alloc(
(size_t)current_sz * sizeof(prmap_t));
if (addr_map == NULL)
ABORT("Insufficient memory for address map");
}
if (ioctl(fd, PIOCMAP, addr_map) < 0) {
ABORT_ARG3("/proc PIOCMAP ioctl failed",
": errcode= %d, needed_sz= %d, addr_map= %p",
errno, needed_sz, (void *)addr_map);
};
if (GC_n_heap_sects > 0) {
heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
+ GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
if ((word)heap_end < (word)GC_scratch_last_end_ptr)
heap_end = GC_scratch_last_end_ptr;
}
for (i = 0; i < needed_sz; i++) {
flags = addr_map[i].pr_mflags;
if ((flags & (MA_BREAK | MA_STACK | MA_PHYS
| MA_FETCHOP | MA_NOTCACHED)) != 0) goto irrelevant;
if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
goto irrelevant;
/* The latter test is empirically useless in very old Irix */
/* versions. Other than the */
/* main data and stack segments, everything appears to be */
/* mapped readable, writable, executable, and shared(!!). */
/* This makes no sense to me. - HB */
start = (ptr_t)(addr_map[i].pr_vaddr);
if (GC_roots_present(start)) goto irrelevant;
if ((word)start < (word)heap_end && (word)start >= (word)heap_start)
goto irrelevant;
limit = start + addr_map[i].pr_size;
/* The following seemed to be necessary for very old versions */
/* of Irix, but it has been reported to discard relevant */
/* segments under Irix 6.5. */
# ifndef IRIX6
if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
/* Discard text segments, i.e. 0-offset mappings against */
/* executable files which appear to have ELF headers. */
caddr_t arg;
int obj;
# define MAP_IRR_SZ 10
static ptr_t map_irr[MAP_IRR_SZ];
/* Known irrelevant map entries */
static int n_irr = 0;
struct stat buf;
int j;
for (j = 0; j < n_irr; j++) {
if (map_irr[j] == start) goto irrelevant;
}
arg = (caddr_t)start;
obj = ioctl(fd, PIOCOPENM, &arg);
if (obj >= 0) {
fstat(obj, &buf);
close(obj);
if ((buf.st_mode & 0111) != 0) {
if (n_irr < MAP_IRR_SZ) {
map_irr[n_irr++] = start;
}
goto irrelevant;
}
}
}
# endif /* !IRIX6 */
GC_add_roots_inner(start, limit, TRUE);
irrelevant: ;
}
/* Don't keep cached descriptor, for now. Some kernels don't like us */
/* to keep a /proc file descriptor around during kill -9. */
if (close(fd) < 0) ABORT("Couldn't close /proc file");
fd = -1;
}
# endif /* USE_PROC || IRIX5 */
# if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
# endif
# define NOSERVICE
# include <windows.h>
# include <stdlib.h>
/* We traverse the entire address space and register all segments */
/* that could possibly have been written to. */
STATIC void GC_cond_add_roots(char *base, char * limit)
{
# ifdef GC_WIN32_THREADS
char * curr_base = base;
char * next_stack_lo;
char * next_stack_hi;
if (base == limit) return;
for(;;) {
GC_get_next_stack(curr_base, limit, &next_stack_lo, &next_stack_hi);
if ((word)next_stack_lo >= (word)limit) break;
if ((word)next_stack_lo > (word)curr_base)
GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
curr_base = next_stack_hi;
}
if ((word)curr_base < (word)limit)
GC_add_roots_inner(curr_base, limit, TRUE);
# else
char * stack_top
= (char *)((word)GC_approx_sp() &
~(word)(GC_sysinfo.dwAllocationGranularity - 1));
if (base == limit) return;
if ((word)limit > (word)stack_top
&& (word)base < (word)GC_stackbottom) {
/* Part of the stack; ignore it. */
return;
}
GC_add_roots_inner(base, limit, TRUE);
# endif
}
#ifdef DYNAMIC_LOADING
/* GC_register_main_static_data is not needed unless DYNAMIC_LOADING. */
GC_INNER GC_bool GC_register_main_static_data(void)
{
# if defined(MSWINCE) || defined(CYGWIN32)
/* Do we need to separately register the main static data segment? */
return FALSE;
# else
return GC_no_win32_dlls;
# endif
}
# define HAVE_REGISTER_MAIN_STATIC_DATA
#endif /* DYNAMIC_LOADING */
# ifdef DEBUG_VIRTUALQUERY
void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf)
{
GC_printf("BaseAddress = 0x%lx, AllocationBase = 0x%lx,"
" RegionSize = 0x%lx(%lu)\n", buf -> BaseAddress,
buf -> AllocationBase, buf -> RegionSize, buf -> RegionSize);
GC_printf("\tAllocationProtect = 0x%lx, State = 0x%lx, Protect = 0x%lx, "
"Type = 0x%lx\n", buf -> AllocationProtect, buf -> State,
buf -> Protect, buf -> Type);
}
# endif /* DEBUG_VIRTUALQUERY */
# if defined(MSWINCE) || defined(CYGWIN32)
/* FIXME: Should we really need to scan MEM_PRIVATE sections? */
/* For now, we don't add MEM_PRIVATE sections to the data roots for */
/* WinCE because otherwise SEGV fault sometimes happens to occur in */
/* GC_mark_from() (and, even if we use WRAP_MARK_SOME, WinCE prints */
/* a "Data Abort" message to the debugging console). */
/* To workaround that, use -DGC_REGISTER_MEM_PRIVATE. */
# define GC_wnt TRUE