2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 * Original author: Bill Janssen
15 * Heavily modified by Hans Boehm and others
18 #include "private/gc_priv.h"
21 * This is incredibly OS specific code for tracking down data sections in
22 * dynamic libraries. There appears to be no way of doing this quickly
23 * without groveling through undocumented data structures. We would argue
24 * that this is a bug in the design of the dlopen interface. THIS CODE
25 * MAY BREAK IN FUTURE OS RELEASES. If this matters to you, don't hesitate
26 * to let your vendor know ...
28 * None of this is safe with dlclose and incremental collection.
29 * But then not much of anything is safe in the presence of dlclose.
32 #if !defined(MACOS) && !defined(_WIN32_WCE) && !defined(__CC_ARM)
33 # include <sys/types.h>
36 /* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */
37 #undef GC_MUST_RESTORE_REDEFINED_DLOPEN
38 #if defined(GC_PTHREADS) && !defined(GC_NO_DLOPEN) \
39 && !defined(GC_NO_THREAD_REDIRECTS) && !defined(GC_USE_LD_WRAP)
40 /* To support threads in Solaris, gc.h interposes on dlopen by */
41 /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
42 /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
43 /* real system dlopen() in their implementation. We first remove */
44 /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
46 # define GC_MUST_RESTORE_REDEFINED_DLOPEN
47 #endif /* !GC_NO_DLOPEN */
49 /* A user-supplied routine (custom filter) that might be called to */
50 /* determine whether a DSO really needs to be scanned by the GC. */
51 /* 0 means no filter installed. May be unused on some platforms. */
52 /* FIXME: Add filter support for more platforms. */
53 STATIC GC_has_static_roots_func GC_has_static_roots = 0;
55 #if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
56 || defined(CYGWIN32)) && !defined(PCR)
58 #if !defined(SOLARISDL) && !defined(IRIX5) && \
59 !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32) && \
60 !(defined(ALPHA) && defined(OSF1)) && \
61 !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
62 !defined(AIX) && !defined(SCO_ELF) && !defined(DGUX) && \
63 !(defined(FREEBSD) && defined(__ELF__)) && \
64 !(defined(OPENBSD) && (defined(__ELF__) || defined(M68K))) && \
65 !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD) && \
66 !defined(DARWIN) && !defined(CYGWIN32)
67 --> We only know how to find data segments of dynamic libraries for the
68 --> above. Additional SVR4 variants might not be too
80 # include <sys/param.h>
82 # include <machine/elf_machdep.h>
83 # define ELFSIZE ARCH_ELFSIZE
86 #if defined(SCO_ELF) || defined(DGUX) || defined(HURD) \
87 || (defined(__ELF__) && (defined(LINUX) || defined(FREEBSD) \
88 || defined(NETBSD) || defined(OPENBSD)))
90 # if !defined(OPENBSD) && !defined(PLATFORM_ANDROID)
91 /* FIXME: Why we exclude it for OpenBSD? */
92 /* Exclude Android because linker.h below includes its own version. */
95 # ifdef PLATFORM_ANDROID
96 /* The header file is in "bionic/linker" folder of Android sources. */
97 /* If you don't need the "dynamic loading" feature, you may build */
98 /* the collector with -D IGNORE_DYNAMIC_LOADING. */
105 /* Newer versions of GNU/Linux define this macro. We
106 * define it similarly for any ELF systems that don't. */
108 # if defined(FREEBSD)
109 # if __ELF_WORD_SIZE == 32
110 # define ElfW(type) Elf32_##type
112 # define ElfW(type) Elf64_##type
114 # elif defined(NETBSD) || defined(OPENBSD)
116 # define ElfW(type) Elf32_##type
118 # define ElfW(type) Elf64_##type
121 # if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
122 # define ElfW(type) Elf32_##type
124 # define ElfW(type) Elf64_##type
129 #if defined(SOLARISDL) && !defined(USE_PROC_FOR_LIBRARIES)
135 STATIC struct link_map *
136 GC_FirstDLOpenedLinkMap(void)
138 extern ElfW(Dyn) _DYNAMIC;
140 static struct link_map * cachedResult = 0;
141 static ElfW(Dyn) *dynStructureAddr = 0;
142 /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
144 # ifdef SUNOS53_SHARED_LIB
145 /* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
146 /* up properly in dynamically linked .so's. This means we have */
147 /* to use its value in the set of original object files loaded */
148 /* at program startup. */
149 if( dynStructureAddr == 0 ) {
150 void* startupSyms = dlopen(0, RTLD_LAZY);
151 dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
154 dynStructureAddr = &_DYNAMIC;
157 if( dynStructureAddr == 0) {
160 if( cachedResult == 0 ) {
162 for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
163 if( tag == DT_DEBUG ) {
165 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
166 if( lm != 0 ) cachedResult = lm->l_next; /* might be NULL */
174 #endif /* SOLARISDL ... */
176 /* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
177 # ifdef GC_MUST_RESTORE_REDEFINED_DLOPEN
178 # define dlopen GC_dlopen
181 # if defined(SOLARISDL)
182 /* Add dynamic library data sections to the root set. */
183 # if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS)
184 --> fix mutual exclusion with dlopen
187 # ifndef USE_PROC_FOR_LIBRARIES
188 GC_INNER void GC_register_dynamic_libraries(void)
192 for (lm = GC_FirstDLOpenedLinkMap(); lm != 0; lm = lm->l_next) {
195 unsigned long offset;
199 e = (ElfW(Ehdr) *) lm->l_addr;
200 # ifdef PLATFORM_ANDROID
204 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
205 offset = ((unsigned long)(lm->l_addr));
206 for( i = 0; i < (int)e->e_phnum; i++, p++ ) {
207 switch( p->p_type ) {
210 if( !(p->p_flags & PF_W) ) break;
211 start = ((char *)(p->p_vaddr)) + offset;
226 # endif /* !USE_PROC ... */
227 # endif /* SOLARISDL */
229 #if defined(SCO_ELF) || defined(DGUX) || defined(HURD) \
230 || (defined(__ELF__) && (defined(LINUX) || defined(FREEBSD) \
231 || defined(NETBSD) || defined(OPENBSD)))
233 #ifdef USE_PROC_FOR_LIBRARIES
237 #include <sys/stat.h>
241 #define MAPS_BUF_SIZE (32*1024)
243 /* Sort an array of HeapSects by start address. */
244 /* Unfortunately at least some versions of */
245 /* Linux qsort end up calling malloc by way of sysconf, and hence can't */
246 /* be used in the collector. Hence we roll our own. Should be */
247 /* reasonably fast if the array is already mostly sorted, as we expect */
249 static void sort_heap_sects(struct HeapSect *base, size_t number_of_elements)
251 signed_word n = (signed_word)number_of_elements;
252 signed_word nsorted = 1;
255 while (nsorted < n) {
256 while (nsorted < n &&
257 base[nsorted-1].hs_start < base[nsorted].hs_start)
259 if (nsorted == n) break;
260 GC_ASSERT(base[nsorted-1].hs_start > base[nsorted].hs_start);
262 while (i >= 0 && base[i].hs_start > base[i+1].hs_start) {
263 struct HeapSect tmp = base[i];
268 GC_ASSERT(base[nsorted-1].hs_start < base[nsorted].hs_start);
273 STATIC word GC_register_map_entries(char *maps)
276 char *buf_ptr = maps;
278 unsigned int maj_dev;
279 ptr_t least_ha, greatest_ha;
283 # ifdef DATASTART_IS_FUNC
284 static ptr_t datastart_cached = (ptr_t)(word)-1;
286 /* Evaluate DATASTART only once. */
287 if (datastart_cached == (ptr_t)(word)-1) {
288 datastart_cached = (ptr_t)(DATASTART);
290 datastart = datastart_cached;
292 datastart = (ptr_t)(DATASTART);
295 GC_ASSERT(I_HOLD_LOCK());
296 sort_heap_sects(GC_our_memory, GC_n_memory);
297 least_ha = GC_our_memory[0].hs_start;
298 greatest_ha = GC_our_memory[GC_n_memory-1].hs_start
299 + GC_our_memory[GC_n_memory-1].hs_bytes;
302 buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, &prot,
304 if (buf_ptr == NULL) return 1;
305 if (prot[1] == 'w') {
306 /* This is a writable mapping. Add it to */
307 /* the root set unless it is already otherwise */
309 if (start <= GC_stackbottom && end >= GC_stackbottom) {
310 /* Stack mapping; discard */
314 /* This may fail, since a thread may already be */
315 /* unregistered, but its thread stack may still be there. */
316 /* That can fail because the stack may disappear while */
317 /* we're marking. Thus the marker is, and has to be */
318 /* prepared to recover from segmentation faults. */
320 if (GC_segment_is_thread_stack(start, end)) continue;
322 /* FIXME: NPTL squirrels */
323 /* away pointers in pieces of the stack segment that we */
324 /* don't scan. We work around this */
325 /* by treating anything allocated by libpthread as */
326 /* uncollectable, as we do in some other cases. */
327 /* A specifically identified problem is that */
328 /* thread stacks contain pointers to dynamic thread */
329 /* vectors, which may be reused due to thread caching. */
330 /* They may not be marked if the thread is still live. */
331 /* This specific instance should be addressed by */
332 /* INCLUDE_LINUX_THREAD_DESCR, but that doesn't quite */
333 /* seem to suffice. */
334 /* We currently trace entire thread stacks, if they are */
335 /* are currently cached but unused. This is */
336 /* very suboptimal for performance reasons. */
338 /* We no longer exclude the main data segment. */
339 if (end <= least_ha || start >= greatest_ha) {
340 /* The easy case; just trace entire segment */
341 GC_add_roots_inner((char *)start, (char *)end, TRUE);
344 /* Add sections that don't belong to us. */
346 while (GC_our_memory[i].hs_start + GC_our_memory[i].hs_bytes
349 GC_ASSERT(i < GC_n_memory);
350 if (GC_our_memory[i].hs_start <= start) {
351 start = GC_our_memory[i].hs_start
352 + GC_our_memory[i].hs_bytes;
355 while (i < GC_n_memory && GC_our_memory[i].hs_start < end
357 if ((char *)start < GC_our_memory[i].hs_start)
358 GC_add_roots_inner((char *)start,
359 GC_our_memory[i].hs_start, TRUE);
360 start = GC_our_memory[i].hs_start
361 + GC_our_memory[i].hs_bytes;
365 GC_add_roots_inner((char *)start, (char *)end, TRUE);
371 GC_INNER void GC_register_dynamic_libraries(void)
373 if (!GC_register_map_entries(GC_get_maps()))
374 ABORT("Failed to read /proc for library registration");
377 /* We now take care of the main data segment ourselves: */
378 GC_INNER GC_bool GC_register_main_static_data(void)
383 # define HAVE_REGISTER_MAIN_STATIC_DATA
385 #else /* !USE_PROC_FOR_LIBRARIES */
387 /* The following is the preferred way to walk dynamic libraries */
388 /* For glibc 2.2.4+. Unfortunately, it doesn't work for older */
389 /* versions. Thanks to Jakub Jelinek for most of the code. */
391 #if (defined(LINUX) || defined (__GLIBC__)) /* Are others OK here, too? */ \
392 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
393 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
394 /* We have the header files for a glibc that includes dl_iterate_phdr. */
395 /* It may still not be available in the library on the target system. */
396 /* Thus we also treat it as a weak symbol. */
397 # define HAVE_DL_ITERATE_PHDR
398 # pragma weak dl_iterate_phdr
401 #if (defined(FREEBSD) && __FreeBSD__ >= 7)
402 /* On the FreeBSD system, any target system at major version 7 shall */
403 /* have dl_iterate_phdr; therefore, we need not make it weak as above. */
404 # define HAVE_DL_ITERATE_PHDR
405 # define DL_ITERATE_PHDR_STRONG
408 #if defined(HAVE_DL_ITERATE_PHDR)
411 /* Instead of registering PT_LOAD sections directly, we keep them */
412 /* in a temporary list, and filter them by excluding PT_GNU_RELRO */
413 /* segments. Processing PT_GNU_RELRO sections with */
414 /* GC_exclude_static_roots instead would be superficially cleaner. But */
415 /* it runs into trouble if a client registers an overlapping segment, */
416 /* which unfortunately seems quite possible. */
418 # define MAX_LOAD_SEGS MAX_ROOT_SETS
420 static struct load_segment {
423 /* Room for a second segment if we remove a RELRO segment */
424 /* from the middle. */
427 } load_segs[MAX_LOAD_SEGS];
429 static int n_load_segs;
430 # endif /* PT_GNU_RELRO */
432 STATIC int GC_register_dynlib_callback(struct dl_phdr_info * info,
433 size_t size, void * ptr)
435 const ElfW(Phdr) * p;
439 /* Make sure struct dl_phdr_info is at least as big as we need. */
440 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
441 + sizeof (info->dlpi_phnum))
445 for( i = 0; i < (int)info->dlpi_phnum; i++, p++ ) {
446 switch( p->p_type ) {
449 /* This entry is known to be constant and will eventually be remapped
450 read-only. However, the address range covered by this entry is
451 typically a subset of a previously encountered `LOAD' segment, so
452 we need to exclude it. */
456 start = ((ptr_t)(p->p_vaddr)) + info->dlpi_addr;
457 end = start + p->p_memsz;
458 for (j = n_load_segs; --j >= 0; ) {
459 if (start >= load_segs[j].start && start < load_segs[j].end) {
460 if (load_segs[j].start2 != 0) {
461 WARN("More than one GNU_RELRO segment per load seg\n",0);
463 GC_ASSERT(end <= load_segs[j].end);
464 /* Remove from the existing load segment */
465 load_segs[j].end2 = load_segs[j].end;
466 load_segs[j].end = start;
467 load_segs[j].start2 = end;
471 if (j == 0) WARN("Failed to find PT_GNU_RELRO segment"
472 " inside PT_LOAD region", 0);
481 GC_has_static_roots_func callback = GC_has_static_roots;
482 if( !(p->p_flags & PF_W) ) break;
483 start = ((char *)(p->p_vaddr)) + info->dlpi_addr;
484 end = start + p->p_memsz;
486 if (callback != 0 && !callback(info->dlpi_name, start, p->p_memsz))
489 if (n_load_segs >= MAX_LOAD_SEGS) ABORT("Too many PT_LOAD segs");
490 # if CPP_WORDSZ == 64
491 /* FIXME: GC_push_all eventually does the correct */
492 /* rounding to the next multiple of ALIGNMENT, so, most */
493 /* probably, we should remove the corresponding assertion */
494 /* check in GC_add_roots_inner along with this code line. */
495 /* start pointer value may require aligning */
496 start = (ptr_t)((word)start & ~(sizeof(word) - 1));
498 load_segs[n_load_segs].start = start;
499 load_segs[n_load_segs].end = end;
500 load_segs[n_load_segs].start2 = 0;
501 load_segs[n_load_segs].end2 = 0;
504 GC_add_roots_inner(start, end, TRUE);
505 # endif /* PT_GNU_RELRO */
513 *(int *)ptr = 1; /* Signal that we were called */
517 /* Do we need to separately register the main static data segment? */
518 GC_INNER GC_bool GC_register_main_static_data(void)
520 # ifdef DL_ITERATE_PHDR_STRONG
521 /* If dl_iterate_phdr is not a weak symbol then don't test against */
522 /* zero (otherwise a compiler might issue a warning). */
525 return (dl_iterate_phdr == 0); /* implicit conversion to function ptr */
529 /* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
530 STATIC GC_bool GC_register_dynamic_libraries_dl_iterate_phdr(void)
533 if (GC_register_main_static_data())
538 static GC_bool excluded_segs = FALSE;
540 if (!excluded_segs) {
541 GC_exclude_static_roots_inner((ptr_t)load_segs,
542 (ptr_t)load_segs + sizeof(load_segs));
543 excluded_segs = TRUE;
549 dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
554 for (i = 0; i < n_load_segs; ++i) {
555 if (load_segs[i].end > load_segs[i].start) {
556 GC_add_roots_inner(load_segs[i].start, load_segs[i].end, TRUE);
558 if (load_segs[i].end2 > load_segs[i].start2) {
559 GC_add_roots_inner(load_segs[i].start2, load_segs[i].end2, TRUE);
566 # ifdef DATASTART_IS_FUNC
567 static ptr_t datastart_cached = (ptr_t)(word)-1;
569 /* Evaluate DATASTART only once. */
570 if (datastart_cached == (ptr_t)(word)-1) {
571 datastart_cached = (ptr_t)(DATASTART);
573 datastart = (char *)datastart_cached;
575 datastart = DATASTART;
577 # ifdef DATAEND_IS_FUNC
579 static ptr_t dataend_cached = 0;
580 /* Evaluate DATAEND only once. */
581 if (dataend_cached == 0) {
582 dataend_cached = (ptr_t)(DATAEND);
584 dataend = (char *)dataend_cached;
590 /* dl_iterate_phdr may forget the static data segment in */
591 /* statically linked executables. */
592 GC_add_roots_inner(datastart, dataend, TRUE);
593 # if defined(DATASTART2)
594 GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);
600 # define HAVE_REGISTER_MAIN_STATIC_DATA
602 #else /* !HAVE_DL_ITERATE_PHDR */
604 /* Dynamic loading code for Linux running ELF. Somewhat tested on
605 * Linux/x86, untested but hopefully should work on Linux/Alpha.
606 * This code was derived from the Solaris/ELF support. Thanks to
607 * whatever kind soul wrote that. - Patrick Bridges */
609 /* This doesn't necessarily work in all cases, e.g. with preloaded
610 * dynamic libraries. */
612 # if defined(NETBSD) || defined(OPENBSD)
613 # include <sys/exec_elf.h>
614 /* for compatibility with 1.4.x */
624 # elif !defined(PLATFORM_ANDROID)
628 # ifndef PLATFORM_ANDROID
632 #endif /* !HAVE_DL_ITERATE_PHDR */
635 # pragma weak _DYNAMIC
637 extern ElfW(Dyn) _DYNAMIC[];
639 STATIC struct link_map *
640 GC_FirstDLOpenedLinkMap(void)
643 static struct link_map *cachedResult = 0;
648 if( cachedResult == 0 ) {
649 # if defined(NETBSD) && defined(RTLD_DI_LINKMAP)
650 struct link_map *lm = NULL;
651 if (!dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lm))
655 for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
656 if( tag == DT_DEBUG ) {
658 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
659 if( lm != 0 ) cachedResult = lm->l_next; /* might be NULL */
663 # endif /* !NETBSD || !RTLD_DI_LINKMAP */
668 GC_INNER void GC_register_dynamic_libraries(void)
672 # ifdef HAVE_DL_ITERATE_PHDR
673 if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
677 for (lm = GC_FirstDLOpenedLinkMap(); lm != 0; lm = lm->l_next)
681 unsigned long offset;
685 e = (ElfW(Ehdr) *) lm->l_addr;
686 # ifdef PLATFORM_ANDROID
690 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
691 offset = ((unsigned long)(lm->l_addr));
692 for( i = 0; i < (int)e->e_phnum; i++, p++ ) {
693 switch( p->p_type ) {
696 if( !(p->p_flags & PF_W) ) break;
697 start = ((char *)(p->p_vaddr)) + offset;
698 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
708 #endif /* !USE_PROC_FOR_LIBRARIES */
712 #if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
714 #include <sys/procfs.h>
715 #include <sys/stat.h>
719 #include <signal.h> /* Only for the following test. */
724 /* We use /proc to track down all parts of the address space that are */
725 /* mapped by the process, and throw out regions we know we shouldn't */
726 /* worry about. This may also work under other SVR4 variants. */
727 GC_INNER void GC_register_dynamic_libraries(void)
731 static prmap_t * addr_map = 0;
732 static int current_sz = 0; /* Number of records currently in addr_map */
733 static int needed_sz; /* Required size of addr_map */
738 ptr_t heap_start = HEAP_START;
739 ptr_t heap_end = heap_start;
743 # endif /* SOLARISDL */
746 sprintf(buf, "/proc/%ld", (long)getpid());
747 /* The above generates a lint complaint, since pid_t varies. */
748 /* It's unclear how to improve this. */
749 fd = open(buf, O_RDONLY);
751 ABORT("/proc open failed");
754 if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
755 GC_err_printf("fd = %d, errno = %d\n", fd, errno);
756 ABORT("/proc PIOCNMAP ioctl failed");
758 if (needed_sz >= current_sz) {
759 current_sz = needed_sz * 2 + 1;
760 /* Expansion, plus room for 0 record */
761 addr_map = (prmap_t *)GC_scratch_alloc(
762 (word)current_sz * sizeof(prmap_t));
763 if (addr_map == NULL)
764 ABORT("Insufficient memory for address map");
766 if (ioctl(fd, PIOCMAP, addr_map) < 0) {
767 GC_err_printf("fd = %d, errno = %d, needed_sz = %d, addr_map = %p\n",
768 fd, errno, needed_sz, addr_map);
769 ABORT("/proc PIOCMAP ioctl failed");
771 if (GC_n_heap_sects > 0) {
772 heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
773 + GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
774 if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;
776 for (i = 0; i < needed_sz; i++) {
777 flags = addr_map[i].pr_mflags;
778 if ((flags & (MA_BREAK | MA_STACK | MA_PHYS
779 | MA_FETCHOP | MA_NOTCACHED)) != 0) goto irrelevant;
780 if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
782 /* The latter test is empirically useless in very old Irix */
783 /* versions. Other than the */
784 /* main data and stack segments, everything appears to be */
785 /* mapped readable, writable, executable, and shared(!!). */
786 /* This makes no sense to me. - HB */
787 start = (ptr_t)(addr_map[i].pr_vaddr);
788 if (GC_roots_present(start)) goto irrelevant;
789 if (start < heap_end && start >= heap_start)
792 if (GC_is_thread_stack(start)) goto irrelevant;
793 # endif /* MMAP_STACKS */
795 limit = start + addr_map[i].pr_size;
796 /* The following seemed to be necessary for very old versions */
797 /* of Irix, but it has been reported to discard relevant */
798 /* segments under Irix 6.5. */
800 if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
801 /* Discard text segments, i.e. 0-offset mappings against */
802 /* executable files which appear to have ELF headers. */
805 # define MAP_IRR_SZ 10
806 static ptr_t map_irr[MAP_IRR_SZ];
807 /* Known irrelevant map entries */
808 static int n_irr = 0;
812 for (j = 0; j < n_irr; j++) {
813 if (map_irr[j] == start) goto irrelevant;
815 arg = (caddr_t)start;
816 obj = ioctl(fd, PIOCOPENM, &arg);
820 if ((buf.st_mode & 0111) != 0) {
821 if (n_irr < MAP_IRR_SZ) {
822 map_irr[n_irr++] = start;
829 GC_add_roots_inner(start, limit, TRUE);
832 /* Don't keep cached descriptor, for now. Some kernels don't like us */
833 /* to keep a /proc file descriptor around during kill -9. */
834 if (close(fd) < 0) ABORT("Couldn't close /proc file");
838 # endif /* USE_PROC || IRIX5 */
840 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
842 # ifndef WIN32_LEAN_AND_MEAN
843 # define WIN32_LEAN_AND_MEAN 1
846 # include <windows.h>
849 /* We traverse the entire address space and register all segments */
850 /* that could possibly have been written to. */
851 STATIC void GC_cond_add_roots(char *base, char * limit)
853 # ifdef GC_WIN32_THREADS
854 char * curr_base = base;
855 char * next_stack_lo;
856 char * next_stack_hi;
858 if (base == limit) return;
860 GC_get_next_stack(curr_base, limit, &next_stack_lo, &next_stack_hi);
861 if (next_stack_lo >= limit) break;
862 if (next_stack_lo > curr_base)
863 GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
864 curr_base = next_stack_hi;
866 if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
869 = (char *)((word)GC_approx_sp() &
870 ~(GC_sysinfo.dwAllocationGranularity - 1));
871 if (base == limit) return;
872 if (limit > stack_top && base < GC_stackbottom) {
873 /* Part of the stack; ignore it. */
876 GC_add_roots_inner(base, limit, TRUE);
880 #ifdef DYNAMIC_LOADING
881 /* GC_register_main_static_data is not needed unless DYNAMIC_LOADING. */
882 GC_INNER GC_bool GC_register_main_static_data(void)
884 # if defined(MSWINCE) || defined(CYGWIN32)
885 /* Do we need to separately register the main static data segment? */
888 return GC_no_win32_dlls;
891 # define HAVE_REGISTER_MAIN_STATIC_DATA
892 #endif /* DYNAMIC_LOADING */
894 # ifdef DEBUG_VIRTUALQUERY
895 void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf)
897 GC_printf("BaseAddress = 0x%lx, AllocationBase = 0x%lx,"
898 " RegionSize = 0x%lx(%lu)\n", buf -> BaseAddress,
899 buf -> AllocationBase, buf -> RegionSize, buf -> RegionSize);
900 GC_printf("\tAllocationProtect = 0x%lx, State = 0x%lx, Protect = 0x%lx, "
901 "Type = 0x%lx\n", buf -> AllocationProtect, buf -> State,
902 buf -> Protect, buf -> Type);
904 # endif /* DEBUG_VIRTUALQUERY */
906 # if defined(MSWINCE) || defined(CYGWIN32)
907 /* FIXME: Should we really need to scan MEM_PRIVATE sections? */
908 /* For now, we don't add MEM_PRIVATE sections to the data roots for */
909 /* WinCE because otherwise SEGV fault sometimes happens to occur in */
910 /* GC_mark_from() (and, even if we use WRAP_MARK_SOME, WinCE prints */
911 /* a "Data Abort" message to the debugging console). */
912 /* To workaround that, use -DGC_REGISTER_MEM_PRIVATE. */
916 GC_INNER void GC_register_dynamic_libraries(void)
918 MEMORY_BASIC_INFORMATION buf;
923 char * limit, * new_limit;
926 if (GC_no_win32_dlls) return;
928 base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
929 while (p < GC_sysinfo.lpMaximumApplicationAddress) {
930 result = VirtualQuery(p, &buf, sizeof(buf));
933 /* Page is free; advance to the next possible allocation base */
935 (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
936 & ~(GC_sysinfo.dwAllocationGranularity-1));
940 if (result != sizeof(buf)) {
941 ABORT("Weird VirtualQuery result");
943 new_limit = (char *)p + buf.RegionSize;
944 protect = buf.Protect;
945 if (buf.State == MEM_COMMIT
946 && (protect == PAGE_EXECUTE_READWRITE
947 || protect == PAGE_READWRITE)
948 && (buf.Type == MEM_IMAGE
949 # ifdef GC_REGISTER_MEM_PRIVATE
950 || (protect == PAGE_READWRITE && buf.Type == MEM_PRIVATE)
952 /* There is some evidence that we cannot always */
953 /* ignore MEM_PRIVATE sections under Windows ME */
954 /* and predecessors. Hence we now also check for */
956 || (!GC_wnt && buf.Type == MEM_PRIVATE)
959 && !GC_is_heap_base(buf.AllocationBase)) {
960 # ifdef DEBUG_VIRTUALQUERY
961 GC_dump_meminfo(&buf);
963 if ((char *)p != limit) {
964 GC_cond_add_roots(base, limit);
970 if (p > (LPVOID)new_limit /* overflow */) break;
971 p = (LPVOID)new_limit;
973 GC_cond_add_roots(base, limit);
976 #endif /* MSWIN32 || MSWINCE || CYGWIN32 */
978 #if defined(ALPHA) && defined(OSF1)
982 extern char *sys_errlist[];
986 GC_INNER void GC_register_dynamic_libraries(void)
992 ldr_module_t moduleid = LDR_NULL_MODULE;
993 ldr_module_info_t moduleinfo;
994 size_t moduleinfosize = sizeof(moduleinfo);
995 size_t modulereturnsize;
999 ldr_region_info_t regioninfo;
1000 size_t regioninfosize = sizeof(regioninfo);
1001 size_t regionreturnsize;
1003 /* Obtain id of this process */
1004 mypid = ldr_my_process();
1006 /* For each module */
1009 /* Get the next (first) module */
1010 status = ldr_next_module(mypid, &moduleid);
1012 /* Any more modules? */
1013 if (moduleid == LDR_NULL_MODULE)
1014 break; /* No more modules */
1016 /* Check status AFTER checking moduleid because */
1017 /* of a bug in the non-shared ldr_next_module stub */
1019 if (GC_print_stats) {
1020 GC_log_printf("dynamic_load: status = %d\n", status);
1021 if (errno < sys_nerr) {
1022 GC_log_printf("dynamic_load: %s\n", sys_errlist[errno]);
1024 GC_log_printf("dynamic_load: err_code = %d\n", errno);
1027 ABORT("ldr_next_module failed");
1030 /* Get the module information */
1031 status = ldr_inq_module(mypid, moduleid, &moduleinfo,
1032 moduleinfosize, &modulereturnsize);
1034 ABORT("ldr_inq_module failed");
1036 /* is module for the main program (i.e. nonshared portion)? */
1037 if (moduleinfo.lmi_flags & LDR_MAIN)
1038 continue; /* skip the main module */
1041 GC_log_printf("---Module---\n");
1042 GC_log_printf("Module ID\t = %16ld\n", moduleinfo.lmi_modid);
1043 GC_log_printf("Count of regions = %16d\n", moduleinfo.lmi_nregion);
1044 GC_log_printf("flags for module = %16lx\n", moduleinfo.lmi_flags);
1045 GC_log_printf("module pathname\t = \"%s\"\n", moduleinfo.lmi_name);
1048 /* For each region in this module */
1049 for (region = 0; region < moduleinfo.lmi_nregion; region++) {
1050 /* Get the region information */
1051 status = ldr_inq_region(mypid, moduleid, region, ®ioninfo,
1052 regioninfosize, ®ionreturnsize);
1054 ABORT("ldr_inq_region failed");
1056 /* only process writable (data) regions */
1057 if (! (regioninfo.lri_prot & LDR_W))
1061 GC_log_printf("--- Region ---\n");
1062 GC_log_printf("Region number\t = %16ld\n",
1063 regioninfo.lri_region_no);
1064 GC_log_printf("Protection flags = %016x\n", regioninfo.lri_prot);
1065 GC_log_printf("Virtual address\t = %16p\n", regioninfo.lri_vaddr);
1066 GC_log_printf("Mapped address\t = %16p\n",
1067 regioninfo.lri_mapaddr);
1068 GC_log_printf("Region size\t = %16ld\n", regioninfo.lri_size);
1069 GC_log_printf("Region name\t = \"%s\"\n", regioninfo.lri_name);
1072 /* register region as a garbage collection root */
1073 GC_add_roots_inner((char *)regioninfo.lri_mapaddr,
1074 (char *)regioninfo.lri_mapaddr + regioninfo.lri_size,
1087 extern char *sys_errlist[];
1088 extern int sys_nerr;
1090 GC_INNER void GC_register_dynamic_libraries(void)
1093 int index = 1; /* Ordinal position in shared library search list */
1094 struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */
1096 /* For each dynamic library loaded */
1099 /* Get info about next shared library */
1100 status = shl_get(index, &shl_desc);
1102 /* Check if this is the end of the list or if some error occured */
1104 # ifdef GC_HPUX_THREADS
1105 /* I've seen errno values of 0. The man page is not clear */
1106 /* as to whether errno should get set on a -1 return. */
1109 if (errno == EINVAL) {
1110 break; /* Moved past end of shared library list --> finished */
1112 if (GC_print_stats) {
1113 if (errno < sys_nerr) {
1114 GC_log_printf("dynamic_load: %s\n", sys_errlist[errno]);
1116 GC_log_printf("dynamic_load: err_code = %d\n", errno);
1119 ABORT("shl_get failed");
1125 GC_log_printf("---Shared library---\n");
1126 GC_log_printf("\tfilename\t= \"%s\"\n", shl_desc->filename);
1127 GC_log_printf("\tindex\t\t= %d\n", index);
1128 GC_log_printf("\thandle\t\t= %08x\n",
1129 (unsigned long) shl_desc->handle);
1130 GC_log_printf("\ttext seg.start\t= %08x\n", shl_desc->tstart);
1131 GC_log_printf("\ttext seg.end\t= %08x\n", shl_desc->tend);
1132 GC_log_printf("\tdata seg.start\t= %08x\n", shl_desc->dstart);
1133 GC_log_printf("\tdata seg.end\t= %08x\n", shl_desc->dend);
1134 GC_log_printf("\tref.count\t= %lu\n", shl_desc->ref_count);
1137 /* register shared library's data segment as a garbage collection root */
1138 GC_add_roots_inner((char *) shl_desc->dstart,
1139 (char *) shl_desc->dend, TRUE);
1148 # include <sys/ldr.h>
1149 # include <sys/errno.h>
1150 GC_INNER void GC_register_dynamic_libraries(void)
1155 struct ld_info *ldi;
1157 ldibuf = alloca(ldibuflen = 8192);
1159 while ( (len = loadquery(L_GETINFO,ldibuf,ldibuflen)) < 0) {
1160 if (errno != ENOMEM) {
1161 ABORT("loadquery failed");
1163 ldibuf = alloca(ldibuflen *= 2);
1166 ldi = (struct ld_info *)ldibuf;
1168 len = ldi->ldinfo_next;
1170 ldi->ldinfo_dataorg,
1171 (ptr_t)(unsigned long)ldi->ldinfo_dataorg
1172 + ldi->ldinfo_datasize,
1174 ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
1181 /* __private_extern__ hack required for pre-3.4 gcc versions. */
1182 #ifndef __private_extern__
1183 # define __private_extern__ extern
1184 # include <mach-o/dyld.h>
1185 # undef __private_extern__
1187 # include <mach-o/dyld.h>
1189 #include <mach-o/getsect.h>
1191 /*#define DARWIN_DEBUG*/
1193 /* Writable sections generally available on Darwin. */
1194 STATIC const struct {
1197 } GC_dyld_sections[] = {
1198 { SEG_DATA, SECT_DATA },
1199 /* Used by FSF GCC, but not by OS X system tools, so far. */
1200 { SEG_DATA, "__static_data" },
1201 { SEG_DATA, SECT_BSS },
1202 { SEG_DATA, SECT_COMMON },
1203 /* FSF GCC - zero-sized object sections for targets */
1204 /*supporting section anchors. */
1205 { SEG_DATA, "__zobj_data" },
1206 { SEG_DATA, "__zobj_bss" }
1209 /* Additional writable sections: */
1210 /* GCC on Darwin constructs aligned sections "on demand", where */
1211 /* the alignment size is embedded in the section name. */
1212 /* Furthermore, there are distinctions between sections */
1213 /* containing private vs. public symbols. It also constructs */
1214 /* sections specifically for zero-sized objects, when the */
1215 /* target supports section anchors. */
1216 STATIC const char * GC_dyld_add_sect_fmts[] =
1225 /* Currently, mach-o will allow up to the max of 2^15 alignment */
1226 /* in an object file. */
1227 #ifndef L2_MAX_OFILE_ALIGNMENT
1228 # define L2_MAX_OFILE_ALIGNMENT 15
1231 STATIC const char *GC_dyld_name_for_hdr(const struct GC_MACH_HEADER *hdr)
1234 c = _dyld_image_count();
1235 for (i = 0; i < c; i++)
1236 if ((const struct GC_MACH_HEADER *)_dyld_get_image_header(i) == hdr)
1237 return _dyld_get_image_name(i);
1241 /* This should never be called by a thread holding the lock. */
1242 STATIC void GC_dyld_image_add(const struct GC_MACH_HEADER *hdr,
1245 unsigned long start, end;
1247 const struct GC_MACH_SECTION *sec;
1249 GC_has_static_roots_func callback = GC_has_static_roots;
1254 if (GC_no_dls) return;
1255 # ifdef DARWIN_DEBUG
1256 name = GC_dyld_name_for_hdr(hdr);
1258 name = callback != 0 ? GC_dyld_name_for_hdr(hdr) : NULL;
1260 for (i = 0; i < sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]); i++) {
1261 sec = GC_GETSECTBYNAME(hdr, GC_dyld_sections[i].seg,
1262 GC_dyld_sections[i].sect);
1263 if (sec == NULL || sec->size < sizeof(word))
1265 start = slide + sec->addr;
1266 end = start + sec->size;
1268 /* The user callback is called holding the lock. */
1269 if (callback == 0 || callback(name, (void*)start, (size_t)sec->size)) {
1270 # ifdef DARWIN_DEBUG
1272 "Adding section __DATA,%s at %p-%p (%lu bytes) from image %s\n",
1273 GC_dyld_sections[i].sect, (void*)start, (void*)end,
1274 (unsigned long)sec->size, name);
1276 GC_add_roots_inner((ptr_t)start, (ptr_t)end, FALSE);
1281 /* Sections constructed on demand. */
1282 for (j = 0; (fmt = GC_dyld_add_sect_fmts[j]) != NULL; j++) {
1283 /* Add our manufactured aligned BSS sections. */
1284 for (i = 0; i <= L2_MAX_OFILE_ALIGNMENT; i++) {
1285 snprintf(secnam, sizeof(secnam), fmt, (unsigned)i);
1286 sec = GC_GETSECTBYNAME(hdr, SEG_DATA, secnam);
1287 if (sec == NULL || sec->size == 0)
1289 start = slide + sec->addr;
1290 end = start + sec->size;
1291 # ifdef DARWIN_DEBUG
1292 GC_log_printf("Adding on-demand section __DATA,%s at"
1293 " %p-%p (%lu bytes) from image %s\n",
1294 secnam, (void*)start, (void*)end,
1295 (unsigned long)sec->size, name);
1297 GC_add_roots((char*)start, (char*)end);
1301 # ifdef DARWIN_DEBUG
1302 GC_print_static_roots();
1306 /* This should never be called by a thread holding the lock. */
1307 STATIC void GC_dyld_image_remove(const struct GC_MACH_HEADER *hdr,
1310 unsigned long start, end;
1312 const struct GC_MACH_SECTION *sec;
1316 for (i = 0; i < sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]); i++) {
1317 sec = GC_GETSECTBYNAME(hdr, GC_dyld_sections[i].seg,
1318 GC_dyld_sections[i].sect);
1319 if (sec == NULL || sec->size == 0)
1321 start = slide + sec->addr;
1322 end = start + sec->size;
1323 # ifdef DARWIN_DEBUG
1325 "Removing section __DATA,%s at %p-%p (%lu bytes) from image %s\n",
1326 GC_dyld_sections[i].sect, (void*)start, (void*)end,
1327 (unsigned long)sec->size, GC_dyld_name_for_hdr(hdr));
1329 GC_remove_roots((char*)start, (char*)end);
1332 /* Remove our on-demand sections. */
1333 for (j = 0; (fmt = GC_dyld_add_sect_fmts[j]) != NULL; j++) {
1334 for (i = 0; i <= L2_MAX_OFILE_ALIGNMENT; i++) {
1335 snprintf(secnam, sizeof(secnam), fmt, (unsigned)i);
1336 sec = GC_GETSECTBYNAME(hdr, SEG_DATA, secnam);
1337 if (sec == NULL || sec->size == 0)
1339 start = slide + sec->addr;
1340 end = start + sec->size;
1341 # ifdef DARWIN_DEBUG
1342 GC_log_printf("Removing on-demand section __DATA,%s at"
1343 " %p-%p (%lu bytes) from image %s\n", secnam,
1344 (void*)start, (void*)end, (unsigned long)sec->size,
1345 GC_dyld_name_for_hdr(hdr));
1347 GC_remove_roots((char*)start, (char*)end);
1351 # ifdef DARWIN_DEBUG
1352 GC_print_static_roots();
1356 GC_INNER void GC_register_dynamic_libraries(void)
1358 /* Currently does nothing. The callbacks are setup by GC_init_dyld()
1359 The dyld library takes it from there. */
1362 /* The _dyld_* functions have an internal lock so no _dyld functions
1363 can be called while the world is stopped without the risk of a deadlock.
1364 Because of this we MUST setup callbacks BEFORE we ever stop the world.
1365 This should be called BEFORE any thread in created and WITHOUT the
1366 allocation lock held. */
1368 GC_INNER void GC_init_dyld(void)
1370 static GC_bool initialized = FALSE;
1372 if (initialized) return;
1374 # ifdef DARWIN_DEBUG
1375 GC_log_printf("Registering dyld callbacks...\n");
1378 /* Apple's Documentation:
1379 When you call _dyld_register_func_for_add_image, the dynamic linker
1380 runtime calls the specified callback (func) once for each of the images
1381 that is currently loaded into the program. When a new image is added to
1382 the program, your callback is called again with the mach_header for the
1383 new image, and the virtual memory slide amount of the new image.
1385 This WILL properly register already linked libraries and libraries
1386 linked in the future.
1389 _dyld_register_func_for_add_image(GC_dyld_image_add);
1390 _dyld_register_func_for_remove_image(GC_dyld_image_remove);
1391 /* Ignore 2 compiler warnings here: passing argument 1 of */
1392 /* '_dyld_register_func_for_add/remove_image' from incompatible */
1395 /* Set this early to avoid reentrancy issues. */
1398 # ifdef NO_DYLD_BIND_FULLY_IMAGE
1399 /* FIXME: What should we do in this case? */
1401 if (GC_no_dls) return; /* skip main data segment registration */
1403 /* When the environment variable is set, the dynamic linker binds */
1404 /* all undefined symbols the application needs at launch time. */
1405 /* This includes function symbols that are normally bound lazily at */
1406 /* the time of their first invocation. */
1407 if (GETENV("DYLD_BIND_AT_LAUNCH") == 0) {
1408 /* The environment variable is unset, so we should bind manually. */
1409 # ifdef DARWIN_DEBUG
1410 GC_log_printf("Forcing full bind of GC code...\n");
1412 /* FIXME: '_dyld_bind_fully_image_containing_address' is deprecated. */
1413 if (!_dyld_bind_fully_image_containing_address(
1414 (unsigned long *)GC_malloc))
1415 ABORT("_dyld_bind_fully_image_containing_address failed");
1420 #define HAVE_REGISTER_MAIN_STATIC_DATA
1421 GC_INNER GC_bool GC_register_main_static_data(void)
1423 /* Already done through dyld callbacks */
1431 # include "il/PCR_IL.h"
1432 # include "th/PCR_ThCtl.h"
1433 # include "mm/PCR_MM.h"
1435 GC_INNER void GC_register_dynamic_libraries(void)
1437 /* Add new static data areas of dynamically loaded modules. */
1438 PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile();
1439 PCR_IL_LoadedSegment * q;
1441 /* Skip uncommitted files */
1442 while (p != NIL && !(p -> lf_commitPoint)) {
1443 /* The loading of this file has not yet been committed */
1444 /* Hence its description could be inconsistent. */
1445 /* Furthermore, it hasn't yet been run. Hence its data */
1446 /* segments can't possibly reference heap allocated */
1450 for (; p != NIL; p = p -> lf_prev) {
1451 for (q = p -> lf_ls; q != NIL; q = q -> ls_next) {
1452 if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK)
1453 == PCR_IL_SegFlags_Traced_on) {
1454 GC_add_roots_inner((char *)(q -> ls_addr),
1455 (char *)(q -> ls_addr) + q -> ls_bytes, TRUE);
1460 #endif /* PCR && !DYNAMIC_LOADING && !MSWIN32 */
1462 #if !defined(HAVE_REGISTER_MAIN_STATIC_DATA) && defined(DYNAMIC_LOADING)
1463 /* Do we need to separately register the main static data segment? */
1464 GC_INNER GC_bool GC_register_main_static_data(void)
1468 #endif /* HAVE_REGISTER_MAIN_STATIC_DATA */
1470 /* Register a routine to filter dynamic library registration. */
1471 GC_API void GC_CALL GC_register_has_static_roots_callback(
1472 GC_has_static_roots_func callback)
1474 GC_has_static_roots = callback;