* config.gcc: Remove --enable-altivec support.
[official-gcc.git] / boehm-gc / dyn_load.c
blob1f4a63646b11ff91b25b5ca3b8351e04fc8ee0aa
1 /*
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
19 * This is incredibly OS specific code for tracking down data sections in
20 * dynamic libraries. There appears to be no way of doing this quickly
21 * without groveling through undocumented data structures. We would argue
22 * that this is a bug in the design of the dlopen interface. THIS CODE
23 * MAY BREAK IN FUTURE OS RELEASES. If this matters to you, don't hesitate
24 * to let your vendor know ...
26 * None of this is safe with dlclose and incremental collection.
27 * But then not much of anything is safe in the presence of dlclose.
29 #if defined(__linux__) && !defined(_GNU_SOURCE)
30 /* Can't test LINUX, since this must be define before other includes */
31 # define _GNU_SOURCE
32 #endif
33 #if !defined(MACOS) && !defined(_WIN32_WCE)
34 # include <sys/types.h>
35 #endif
36 #include "private/gc_priv.h"
38 /* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */
39 # if (defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)) \
40 && defined(dlopen) && !defined(GC_USE_LD_WRAP)
41 /* To support threads in Solaris, gc.h interposes on dlopen by */
42 /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
43 /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
44 /* real system dlopen() in their implementation. We first remove */
45 /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
46 # undef dlopen
47 # define GC_must_restore_redefined_dlopen
48 # else
49 # undef GC_must_restore_redefined_dlopen
50 # endif
52 #if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE)) \
53 && !defined(PCR)
54 #if !defined(SUNOS4) && !defined(SUNOS5DL) && !defined(IRIX5) && \
55 !defined(MSWIN32) && !defined(MSWINCE) && \
56 !(defined(ALPHA) && defined(OSF1)) && \
57 !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
58 !defined(RS6000) && !defined(SCO_ELF) && !defined(DGUX) && \
59 !(defined(FREEBSD) && defined(__ELF__)) && \
60 !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD) && \
61 !defined(DARWIN)
62 --> We only know how to find data segments of dynamic libraries for the
63 --> above. Additional SVR4 variants might not be too
64 --> hard to add.
65 #endif
67 #include <stdio.h>
68 #ifdef SUNOS5DL
69 # include <sys/elf.h>
70 # include <dlfcn.h>
71 # include <link.h>
72 #endif
73 #ifdef SUNOS4
74 # include <dlfcn.h>
75 # include <link.h>
76 # include <a.out.h>
77 /* struct link_map field overrides */
78 # define l_next lm_next
79 # define l_addr lm_addr
80 # define l_name lm_name
81 #endif
83 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
84 (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
85 (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
86 # include <stddef.h>
87 # include <elf.h>
88 # include <link.h>
89 #endif
91 /* Newer versions of GNU/Linux define this macro. We
92 * define it similarly for any ELF systems that don't. */
93 # ifndef ElfW
94 # if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
95 # define ElfW(type) Elf32_##type
96 # else
97 # define ElfW(type) Elf64_##type
98 # endif
99 # endif
101 #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)
103 #ifdef LINT
104 Elf32_Dyn _DYNAMIC;
105 #endif
107 static struct link_map *
108 GC_FirstDLOpenedLinkMap()
110 extern ElfW(Dyn) _DYNAMIC;
111 ElfW(Dyn) *dp;
112 struct r_debug *r;
113 static struct link_map * cachedResult = 0;
114 static ElfW(Dyn) *dynStructureAddr = 0;
115 /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
117 # ifdef SUNOS53_SHARED_LIB
118 /* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
119 /* up properly in dynamically linked .so's. This means we have */
120 /* to use its value in the set of original object files loaded */
121 /* at program startup. */
122 if( dynStructureAddr == 0 ) {
123 void* startupSyms = dlopen(0, RTLD_LAZY);
124 dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
126 # else
127 dynStructureAddr = &_DYNAMIC;
128 # endif
130 if( dynStructureAddr == 0) {
131 return(0);
133 if( cachedResult == 0 ) {
134 int tag;
135 for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
136 if( tag == DT_DEBUG ) {
137 struct link_map *lm
138 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
139 if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
140 break;
144 return cachedResult;
147 #endif /* SUNOS5DL ... */
149 /* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
150 # if defined(GC_must_restore_redefined_dlopen)
151 # define dlopen GC_dlopen
152 # endif
154 #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)
156 #ifdef LINT
157 struct link_dynamic _DYNAMIC;
158 #endif
160 static struct link_map *
161 GC_FirstDLOpenedLinkMap()
163 extern struct link_dynamic _DYNAMIC;
165 if( &_DYNAMIC == 0) {
166 return(0);
168 return(_DYNAMIC.ld_un.ld_1->ld_loaded);
171 /* Return the address of the ld.so allocated common symbol */
172 /* with the least address, or 0 if none. */
173 static ptr_t GC_first_common()
175 ptr_t result = 0;
176 extern struct link_dynamic _DYNAMIC;
177 struct rtc_symb * curr_symbol;
179 if( &_DYNAMIC == 0) {
180 return(0);
182 curr_symbol = _DYNAMIC.ldd -> ldd_cp;
183 for (; curr_symbol != 0; curr_symbol = curr_symbol -> rtc_next) {
184 if (result == 0
185 || (ptr_t)(curr_symbol -> rtc_sp -> n_value) < result) {
186 result = (ptr_t)(curr_symbol -> rtc_sp -> n_value);
189 return(result);
192 #endif /* SUNOS4 ... */
194 # if defined(SUNOS4) || defined(SUNOS5DL)
195 /* Add dynamic library data sections to the root set. */
196 # if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS)
197 # ifndef SRC_M3
198 --> fix mutual exclusion with dlopen
199 # endif /* We assume M3 programs don't call dlopen for now */
200 # endif
202 # ifndef USE_PROC_FOR_LIBRARIES
203 void GC_register_dynamic_libraries()
205 struct link_map *lm = GC_FirstDLOpenedLinkMap();
208 for (lm = GC_FirstDLOpenedLinkMap();
209 lm != (struct link_map *) 0; lm = lm->l_next)
211 # ifdef SUNOS4
212 struct exec *e;
214 e = (struct exec *) lm->lm_addr;
215 GC_add_roots_inner(
216 ((char *) (N_DATOFF(*e) + lm->lm_addr)),
217 ((char *) (N_BSSADDR(*e) + e->a_bss + lm->lm_addr)),
218 TRUE);
219 # endif
220 # ifdef SUNOS5DL
221 ElfW(Ehdr) * e;
222 ElfW(Phdr) * p;
223 unsigned long offset;
224 char * start;
225 register int i;
227 e = (ElfW(Ehdr) *) lm->l_addr;
228 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
229 offset = ((unsigned long)(lm->l_addr));
230 for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
231 switch( p->p_type ) {
232 case PT_LOAD:
234 if( !(p->p_flags & PF_W) ) break;
235 start = ((char *)(p->p_vaddr)) + offset;
236 GC_add_roots_inner(
237 start,
238 start + p->p_memsz,
239 TRUE
242 break;
243 default:
244 break;
247 # endif
249 # ifdef SUNOS4
251 static ptr_t common_start = 0;
252 ptr_t common_end;
253 extern ptr_t GC_find_limit();
255 if (common_start == 0) common_start = GC_first_common();
256 if (common_start != 0) {
257 common_end = GC_find_limit(common_start, TRUE);
258 GC_add_roots_inner((char *)common_start, (char *)common_end, TRUE);
261 # endif
264 # endif /* !USE_PROC ... */
265 # endif /* SUNOS */
267 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
268 (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
269 (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
272 #ifdef USE_PROC_FOR_LIBRARIES
274 #include <string.h>
276 #include <sys/stat.h>
277 #include <fcntl.h>
278 #include <unistd.h>
280 #define MAPS_BUF_SIZE (32*1024)
282 extern ssize_t GC_repeat_read(int fd, char *buf, size_t count);
283 /* Repeatedly read until buffer is filled, or EOF is encountered */
284 /* Defined in os_dep.c. */
286 char *GC_parse_map_entry(char *buf_ptr, word *start, word *end,
287 char *prot_buf, unsigned int *maj_dev);
288 word GC_apply_to_maps(word (*fn)(char *));
289 /* From os_dep.c */
291 word GC_register_map_entries(char *maps)
293 char prot_buf[5];
294 char *buf_ptr = maps;
295 int count;
296 word start, end;
297 unsigned int maj_dev;
298 word least_ha, greatest_ha;
299 unsigned i;
300 word datastart = (word)(DATASTART);
302 /* Compute heap bounds. FIXME: Should be done by add_to_heap? */
303 least_ha = (word)(-1);
304 greatest_ha = 0;
305 for (i = 0; i < GC_n_heap_sects; ++i) {
306 word sect_start = (word)GC_heap_sects[i].hs_start;
307 word sect_end = sect_start + GC_heap_sects[i].hs_bytes;
308 if (sect_start < least_ha) least_ha = sect_start;
309 if (sect_end > greatest_ha) greatest_ha = sect_end;
311 if (greatest_ha < (word)GC_scratch_last_end_ptr)
312 greatest_ha = (word)GC_scratch_last_end_ptr;
314 for (;;) {
315 buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
316 if (buf_ptr == NULL) return 1;
317 if (prot_buf[1] == 'w') {
318 /* This is a writable mapping. Add it to */
319 /* the root set unless it is already otherwise */
320 /* accounted for. */
321 if (start <= (word)GC_stackbottom && end >= (word)GC_stackbottom) {
322 /* Stack mapping; discard */
323 continue;
325 # ifdef THREADS
326 if (GC_segment_is_thread_stack(start, end)) continue;
327 # endif
328 /* We no longer exclude the main data segment. */
329 if (start < least_ha && end > least_ha) {
330 end = least_ha;
332 if (start < greatest_ha && end > greatest_ha) {
333 start = greatest_ha;
335 if (start >= least_ha && end <= greatest_ha) continue;
336 GC_add_roots_inner((char *)start, (char *)end, TRUE);
339 return 1;
342 void GC_register_dynamic_libraries()
344 if (!GC_apply_to_maps(GC_register_map_entries))
345 ABORT("Failed to read /proc for library registration.");
348 /* We now take care of the main data segment ourselves: */
349 GC_bool GC_register_main_static_data()
351 return FALSE;
354 # define HAVE_REGISTER_MAIN_STATIC_DATA
356 #endif /* USE_PROC_FOR_LIBRARIES */
358 #if !defined(USE_PROC_FOR_LIBRARIES)
359 /* The following is the preferred way to walk dynamic libraries */
360 /* For glibc 2.2.4+. Unfortunately, it doesn't work for older */
361 /* versions. Thanks to Jakub Jelinek for most of the code. */
363 # if defined(LINUX) /* Are others OK here, too? */ \
364 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
365 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
367 /* We have the header files for a glibc that includes dl_iterate_phdr. */
368 /* It may still not be available in the library on the target system. */
369 /* Thus we also treat it as a weak symbol. */
370 #define HAVE_DL_ITERATE_PHDR
372 static int GC_register_dynlib_callback(info, size, ptr)
373 struct dl_phdr_info * info;
374 size_t size;
375 void * ptr;
377 const ElfW(Phdr) * p;
378 char * start;
379 register int i;
381 /* Make sure struct dl_phdr_info is at least as big as we need. */
382 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
383 + sizeof (info->dlpi_phnum))
384 return -1;
386 p = info->dlpi_phdr;
387 for( i = 0; i < (int)(info->dlpi_phnum); ((i++),(p++)) ) {
388 switch( p->p_type ) {
389 case PT_LOAD:
391 if( !(p->p_flags & PF_W) ) break;
392 start = ((char *)(p->p_vaddr)) + info->dlpi_addr;
393 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
395 break;
396 default:
397 break;
401 * (int *)ptr = 1; /* Signal that we were called */
402 return 0;
405 /* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
407 #pragma weak dl_iterate_phdr
409 GC_bool GC_register_dynamic_libraries_dl_iterate_phdr()
411 if (dl_iterate_phdr) {
412 int did_something = 0;
413 dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
414 if (!did_something) {
415 /* dl_iterate_phdr may forget the static data segment in */
416 /* statically linked executables. */
417 GC_add_roots_inner(DATASTART, (char *)(DATAEND), TRUE);
418 # if defined(DATASTART2)
419 GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);
420 # endif
423 return TRUE;
424 } else {
425 return FALSE;
429 /* Do we need to separately register the main static data segment? */
430 GC_bool GC_register_main_static_data()
432 return (dl_iterate_phdr == 0);
435 #define HAVE_REGISTER_MAIN_STATIC_DATA
437 # else /* !LINUX || version(glibc) < 2.2.4 */
439 /* Dynamic loading code for Linux running ELF. Somewhat tested on
440 * Linux/x86, untested but hopefully should work on Linux/Alpha.
441 * This code was derived from the Solaris/ELF support. Thanks to
442 * whatever kind soul wrote that. - Patrick Bridges */
444 /* This doesn't necessarily work in all cases, e.g. with preloaded
445 * dynamic libraries. */
447 #if defined(NETBSD)
448 # include <sys/exec_elf.h>
449 /* for compatibility with 1.4.x */
450 # ifndef DT_DEBUG
451 # define DT_DEBUG 21
452 # endif
453 # ifndef PT_LOAD
454 # define PT_LOAD 1
455 # endif
456 # ifndef PF_W
457 # define PF_W 2
458 # endif
459 #else
460 # include <elf.h>
461 #endif
462 #include <link.h>
464 # endif
466 #ifdef __GNUC__
467 # pragma weak _DYNAMIC
468 #endif
469 extern ElfW(Dyn) _DYNAMIC[];
471 static struct link_map *
472 GC_FirstDLOpenedLinkMap()
474 ElfW(Dyn) *dp;
475 struct r_debug *r;
476 static struct link_map *cachedResult = 0;
478 if( _DYNAMIC == 0) {
479 return(0);
481 if( cachedResult == 0 ) {
482 int tag;
483 for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
484 if( tag == DT_DEBUG ) {
485 struct link_map *lm
486 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
487 if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
488 break;
492 return cachedResult;
496 void GC_register_dynamic_libraries()
498 struct link_map *lm;
501 # ifdef HAVE_DL_ITERATE_PHDR
502 if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
503 return;
505 # endif
506 lm = GC_FirstDLOpenedLinkMap();
507 for (lm = GC_FirstDLOpenedLinkMap();
508 lm != (struct link_map *) 0; lm = lm->l_next)
510 ElfW(Ehdr) * e;
511 ElfW(Phdr) * p;
512 unsigned long offset;
513 char * start;
514 register int i;
516 e = (ElfW(Ehdr) *) lm->l_addr;
517 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
518 offset = ((unsigned long)(lm->l_addr));
519 for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
520 switch( p->p_type ) {
521 case PT_LOAD:
523 if( !(p->p_flags & PF_W) ) break;
524 start = ((char *)(p->p_vaddr)) + offset;
525 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
527 break;
528 default:
529 break;
535 #endif /* !USE_PROC_FOR_LIBRARIES */
537 #endif /* LINUX */
539 #if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
541 #include <sys/procfs.h>
542 #include <sys/stat.h>
543 #include <fcntl.h>
544 #include <elf.h>
545 #include <errno.h>
546 #include <signal.h> /* Only for the following test. */
547 #ifndef _sigargs
548 # define IRIX6
549 #endif
551 extern void * GC_roots_present();
552 /* The type is a lie, since the real type doesn't make sense here, */
553 /* and we only test for NULL. */
555 /* We use /proc to track down all parts of the address space that are */
556 /* mapped by the process, and throw out regions we know we shouldn't */
557 /* worry about. This may also work under other SVR4 variants. */
558 void GC_register_dynamic_libraries()
560 static int fd = -1;
561 char buf[30];
562 static prmap_t * addr_map = 0;
563 static int current_sz = 0; /* Number of records currently in addr_map */
564 static int needed_sz; /* Required size of addr_map */
565 register int i;
566 register long flags;
567 register ptr_t start;
568 register ptr_t limit;
569 ptr_t heap_start = (ptr_t)HEAP_START;
570 ptr_t heap_end = heap_start;
572 # ifdef SUNOS5DL
573 # define MA_PHYS 0
574 # endif /* SUNOS5DL */
576 if (fd < 0) {
577 sprintf(buf, "/proc/%d", getpid());
578 /* The above generates a lint complaint, since pid_t varies. */
579 /* It's unclear how to improve this. */
580 fd = open(buf, O_RDONLY);
581 if (fd < 0) {
582 ABORT("/proc open failed");
585 if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
586 GC_err_printf2("fd = %d, errno = %d\n", fd, errno);
587 ABORT("/proc PIOCNMAP ioctl failed");
589 if (needed_sz >= current_sz) {
590 current_sz = needed_sz * 2 + 1;
591 /* Expansion, plus room for 0 record */
592 addr_map = (prmap_t *)GC_scratch_alloc((word)
593 (current_sz * sizeof(prmap_t)));
595 if (ioctl(fd, PIOCMAP, addr_map) < 0) {
596 GC_err_printf4("fd = %d, errno = %d, needed_sz = %d, addr_map = 0x%X\n",
597 fd, errno, needed_sz, addr_map);
598 ABORT("/proc PIOCMAP ioctl failed");
600 if (GC_n_heap_sects > 0) {
601 heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
602 + GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
603 if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;
605 for (i = 0; i < needed_sz; i++) {
606 flags = addr_map[i].pr_mflags;
607 if ((flags & (MA_BREAK | MA_STACK | MA_PHYS)) != 0) goto irrelevant;
608 if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
609 goto irrelevant;
610 /* The latter test is empirically useless in very old Irix */
611 /* versions. Other than the */
612 /* main data and stack segments, everything appears to be */
613 /* mapped readable, writable, executable, and shared(!!). */
614 /* This makes no sense to me. - HB */
615 start = (ptr_t)(addr_map[i].pr_vaddr);
616 if (GC_roots_present(start)) goto irrelevant;
617 if (start < heap_end && start >= heap_start)
618 goto irrelevant;
619 # ifdef MMAP_STACKS
620 if (GC_is_thread_stack(start)) goto irrelevant;
621 # endif /* MMAP_STACKS */
623 limit = start + addr_map[i].pr_size;
624 /* The following seemed to be necessary for very old versions */
625 /* of Irix, but it has been reported to discard relevant */
626 /* segments under Irix 6.5. */
627 # ifndef IRIX6
628 if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
629 /* Discard text segments, i.e. 0-offset mappings against */
630 /* executable files which appear to have ELF headers. */
631 caddr_t arg;
632 int obj;
633 # define MAP_IRR_SZ 10
634 static ptr_t map_irr[MAP_IRR_SZ];
635 /* Known irrelevant map entries */
636 static int n_irr = 0;
637 struct stat buf;
638 register int i;
640 for (i = 0; i < n_irr; i++) {
641 if (map_irr[i] == start) goto irrelevant;
643 arg = (caddr_t)start;
644 obj = ioctl(fd, PIOCOPENM, &arg);
645 if (obj >= 0) {
646 fstat(obj, &buf);
647 close(obj);
648 if ((buf.st_mode & 0111) != 0) {
649 if (n_irr < MAP_IRR_SZ) {
650 map_irr[n_irr++] = start;
652 goto irrelevant;
656 # endif /* !IRIX6 */
657 GC_add_roots_inner(start, limit, TRUE);
658 irrelevant: ;
660 /* Dont keep cached descriptor, for now. Some kernels don't like us */
661 /* to keep a /proc file descriptor around during kill -9. */
662 if (close(fd) < 0) ABORT("Couldnt close /proc file");
663 fd = -1;
666 # endif /* USE_PROC || IRIX5 */
668 # if defined(MSWIN32) || defined(MSWINCE)
670 # define WIN32_LEAN_AND_MEAN
671 # define NOSERVICE
672 # include <windows.h>
673 # include <stdlib.h>
675 /* We traverse the entire address space and register all segments */
676 /* that could possibly have been written to. */
678 extern GC_bool GC_is_heap_base (ptr_t p);
680 # ifdef GC_WIN32_THREADS
681 extern void GC_get_next_stack(char *start, char **lo, char **hi);
682 void GC_cond_add_roots(char *base, char * limit)
684 char * curr_base = base;
685 char * next_stack_lo;
686 char * next_stack_hi;
688 if (base == limit) return;
689 for(;;) {
690 GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);
691 if (next_stack_lo >= limit) break;
692 GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
693 curr_base = next_stack_hi;
695 if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
697 # else
698 void GC_cond_add_roots(char *base, char * limit)
700 char dummy;
701 char * stack_top
702 = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));
703 if (base == limit) return;
704 if (limit > stack_top && base < GC_stackbottom) {
705 /* Part of the stack; ignore it. */
706 return;
708 GC_add_roots_inner(base, limit, TRUE);
710 # endif
712 # ifdef MSWINCE
713 /* Do we need to separately register the main static data segment? */
714 GC_bool GC_register_main_static_data()
716 return FALSE;
718 # else /* win32 */
719 extern GC_bool GC_no_win32_dlls;
721 GC_bool GC_register_main_static_data()
723 return GC_no_win32_dlls;
725 # endif /* win32 */
727 # define HAVE_REGISTER_MAIN_STATIC_DATA
729 void GC_register_dynamic_libraries()
731 MEMORY_BASIC_INFORMATION buf;
732 DWORD result;
733 DWORD protect;
734 LPVOID p;
735 char * base;
736 char * limit, * new_limit;
738 # ifdef MSWIN32
739 if (GC_no_win32_dlls) return;
740 # endif
741 base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
742 # if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
743 /* Only the first 32 MB of address space belongs to the current process */
744 while (p < (LPVOID)0x02000000) {
745 result = VirtualQuery(p, &buf, sizeof(buf));
746 if (result == 0) {
747 /* Page is free; advance to the next possible allocation base */
748 new_limit = (char *)
749 (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
750 & ~(GC_sysinfo.dwAllocationGranularity-1));
751 } else
752 # else
753 while (p < GC_sysinfo.lpMaximumApplicationAddress) {
754 result = VirtualQuery(p, &buf, sizeof(buf));
755 # endif
757 if (result != sizeof(buf)) {
758 ABORT("Weird VirtualQuery result");
760 new_limit = (char *)p + buf.RegionSize;
761 protect = buf.Protect;
762 if (buf.State == MEM_COMMIT
763 && (protect == PAGE_EXECUTE_READWRITE
764 || protect == PAGE_READWRITE)
765 && !GC_is_heap_base(buf.AllocationBase)) {
766 if ((char *)p != limit) {
767 GC_cond_add_roots(base, limit);
768 base = p;
770 limit = new_limit;
773 if (p > (LPVOID)new_limit /* overflow */) break;
774 p = (LPVOID)new_limit;
776 GC_cond_add_roots(base, limit);
779 #endif /* MSWIN32 || MSWINCE */
781 #if defined(ALPHA) && defined(OSF1)
783 #include <loader.h>
785 void GC_register_dynamic_libraries()
787 int status;
788 ldr_process_t mypid;
790 /* module */
791 ldr_module_t moduleid = LDR_NULL_MODULE;
792 ldr_module_info_t moduleinfo;
793 size_t moduleinfosize = sizeof(moduleinfo);
794 size_t modulereturnsize;
796 /* region */
797 ldr_region_t region;
798 ldr_region_info_t regioninfo;
799 size_t regioninfosize = sizeof(regioninfo);
800 size_t regionreturnsize;
802 /* Obtain id of this process */
803 mypid = ldr_my_process();
805 /* For each module */
806 while (TRUE) {
808 /* Get the next (first) module */
809 status = ldr_next_module(mypid, &moduleid);
811 /* Any more modules? */
812 if (moduleid == LDR_NULL_MODULE)
813 break; /* No more modules */
815 /* Check status AFTER checking moduleid because */
816 /* of a bug in the non-shared ldr_next_module stub */
817 if (status != 0 ) {
818 GC_printf1("dynamic_load: status = %ld\n", (long)status);
820 extern char *sys_errlist[];
821 extern int sys_nerr;
822 extern int errno;
823 if (errno <= sys_nerr) {
824 GC_printf1("dynamic_load: %s\n", (long)sys_errlist[errno]);
825 } else {
826 GC_printf1("dynamic_load: %d\n", (long)errno);
829 ABORT("ldr_next_module failed");
832 /* Get the module information */
833 status = ldr_inq_module(mypid, moduleid, &moduleinfo,
834 moduleinfosize, &modulereturnsize);
835 if (status != 0 )
836 ABORT("ldr_inq_module failed");
838 /* is module for the main program (i.e. nonshared portion)? */
839 if (moduleinfo.lmi_flags & LDR_MAIN)
840 continue; /* skip the main module */
842 # ifdef VERBOSE
843 GC_printf("---Module---\n");
844 GC_printf("Module ID = %16ld\n", moduleinfo.lmi_modid);
845 GC_printf("Count of regions = %16d\n", moduleinfo.lmi_nregion);
846 GC_printf("flags for module = %16lx\n", moduleinfo.lmi_flags);
847 GC_printf("pathname of module = \"%s\"\n", moduleinfo.lmi_name);
848 # endif
850 /* For each region in this module */
851 for (region = 0; region < moduleinfo.lmi_nregion; region++) {
853 /* Get the region information */
854 status = ldr_inq_region(mypid, moduleid, region, &regioninfo,
855 regioninfosize, &regionreturnsize);
856 if (status != 0 )
857 ABORT("ldr_inq_region failed");
859 /* only process writable (data) regions */
860 if (! (regioninfo.lri_prot & LDR_W))
861 continue;
863 # ifdef VERBOSE
864 GC_printf("--- Region ---\n");
865 GC_printf("Region number = %16ld\n",
866 regioninfo.lri_region_no);
867 GC_printf("Protection flags = %016x\n", regioninfo.lri_prot);
868 GC_printf("Virtual address = %16p\n", regioninfo.lri_vaddr);
869 GC_printf("Mapped address = %16p\n", regioninfo.lri_mapaddr);
870 GC_printf("Region size = %16ld\n", regioninfo.lri_size);
871 GC_printf("Region name = \"%s\"\n", regioninfo.lri_name);
872 # endif
874 /* register region as a garbage collection root */
875 GC_add_roots_inner (
876 (char *)regioninfo.lri_mapaddr,
877 (char *)regioninfo.lri_mapaddr + regioninfo.lri_size,
878 TRUE);
883 #endif
885 #if defined(HPUX)
887 #include <errno.h>
888 #include <dl.h>
890 extern int errno;
891 extern char *sys_errlist[];
892 extern int sys_nerr;
894 void GC_register_dynamic_libraries()
896 int status;
897 int index = 1; /* Ordinal position in shared library search list */
898 struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */
900 /* For each dynamic library loaded */
901 while (TRUE) {
903 /* Get info about next shared library */
904 status = shl_get(index, &shl_desc);
906 /* Check if this is the end of the list or if some error occured */
907 if (status != 0) {
908 # ifdef GC_HPUX_THREADS
909 /* I've seen errno values of 0. The man page is not clear */
910 /* as to whether errno should get set on a -1 return. */
911 break;
912 # else
913 if (errno == EINVAL) {
914 break; /* Moved past end of shared library list --> finished */
915 } else {
916 if (errno <= sys_nerr) {
917 GC_printf1("dynamic_load: %s\n", (long) sys_errlist[errno]);
918 } else {
919 GC_printf1("dynamic_load: %d\n", (long) errno);
921 ABORT("shl_get failed");
923 # endif
926 # ifdef VERBOSE
927 GC_printf0("---Shared library---\n");
928 GC_printf1("\tfilename = \"%s\"\n", shl_desc->filename);
929 GC_printf1("\tindex = %d\n", index);
930 GC_printf1("\thandle = %08x\n",
931 (unsigned long) shl_desc->handle);
932 GC_printf1("\ttext seg. start = %08x\n", shl_desc->tstart);
933 GC_printf1("\ttext seg. end = %08x\n", shl_desc->tend);
934 GC_printf1("\tdata seg. start = %08x\n", shl_desc->dstart);
935 GC_printf1("\tdata seg. end = %08x\n", shl_desc->dend);
936 GC_printf1("\tref. count = %lu\n", shl_desc->ref_count);
937 # endif
939 /* register shared library's data segment as a garbage collection root */
940 GC_add_roots_inner((char *) shl_desc->dstart,
941 (char *) shl_desc->dend, TRUE);
943 index++;
946 #endif /* HPUX */
948 #ifdef RS6000
949 #pragma alloca
950 #include <sys/ldr.h>
951 #include <sys/errno.h>
952 void GC_register_dynamic_libraries()
954 int len;
955 char *ldibuf;
956 int ldibuflen;
957 struct ld_info *ldi;
959 ldibuf = alloca(ldibuflen = 8192);
961 while ( (len = loadquery(L_GETINFO,ldibuf,ldibuflen)) < 0) {
962 if (errno != ENOMEM) {
963 ABORT("loadquery failed");
965 ldibuf = alloca(ldibuflen *= 2);
968 ldi = (struct ld_info *)ldibuf;
969 while (ldi) {
970 len = ldi->ldinfo_next;
971 GC_add_roots_inner(
972 ldi->ldinfo_dataorg,
973 (ptr_t)(unsigned long)ldi->ldinfo_dataorg
974 + ldi->ldinfo_datasize,
975 TRUE);
976 ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
979 #endif /* RS6000 */
981 #ifdef DARWIN
983 #ifndef __private_extern__
984 #define __private_extern__ extern
985 #include <mach-o/dyld.h>
986 #undef __private_extern__
987 #else
988 #include <mach-o/dyld.h>
989 #endif
990 #include <mach-o/getsect.h>
992 /*#define DARWIN_DEBUG*/
994 const static struct {
995 const char *seg;
996 const char *sect;
997 } GC_dyld_sections[] = {
998 { SEG_DATA, SECT_DATA },
999 { SEG_DATA, SECT_BSS },
1000 { SEG_DATA, SECT_COMMON }
1003 #ifdef DARWIN_DEBUG
1004 static const char *GC_dyld_name_for_hdr(struct mach_header *hdr) {
1005 unsigned long i,c;
1006 c = _dyld_image_count();
1007 for(i=0;i<c;i++) if(_dyld_get_image_header(i) == hdr)
1008 return _dyld_get_image_name(i);
1009 return NULL;
1011 #endif
1013 /* This should never be called by a thread holding the lock */
1014 static void GC_dyld_image_add(struct mach_header* hdr, unsigned long slide) {
1015 unsigned long start,end,i;
1016 const struct section *sec;
1017 for(i=0;i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]);i++) {
1018 sec = getsectbynamefromheader(
1019 hdr,GC_dyld_sections[i].seg,GC_dyld_sections[i].sect);
1020 if(sec == NULL || sec->size == 0) continue;
1021 start = slide + sec->addr;
1022 end = start + sec->size;
1023 # ifdef DARWIN_DEBUG
1024 GC_printf4("Adding section at %p-%p (%lu bytes) from image %s\n",
1025 start,end,sec->size,GC_dyld_name_for_hdr(hdr));
1026 # endif
1027 GC_add_roots((char*)start,(char*)end);
1029 # ifdef DARWIN_DEBUG
1030 GC_print_static_roots();
1031 # endif
1034 /* This should never be called by a thread holding the lock */
1035 static void GC_dyld_image_remove(struct mach_header* hdr, unsigned long slide) {
1036 unsigned long start,end,i;
1037 const struct section *sec;
1038 for(i=0;i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]);i++) {
1039 sec = getsectbynamefromheader(
1040 hdr,GC_dyld_sections[i].seg,GC_dyld_sections[i].sect);
1041 if(sec == NULL || sec->size == 0) continue;
1042 start = slide + sec->addr;
1043 end = start + sec->size;
1044 # ifdef DARWIN_DEBUG
1045 GC_printf4("Removing section at %p-%p (%lu bytes) from image %s\n",
1046 start,end,sec->size,GC_dyld_name_for_hdr(hdr));
1047 # endif
1048 GC_remove_roots((char*)start,(char*)end);
1050 # ifdef DARWIN_DEBUG
1051 GC_print_static_roots();
1052 # endif
1055 void GC_register_dynamic_libraries() {
1056 /* Currently does nothing. The callbacks are setup by GC_init_dyld()
1057 The dyld library takes it from there. */
1060 /* The _dyld_* functions have an internal lock so no _dyld functions
1061 can be called while the world is stopped without the risk of a deadlock.
1062 Because of this we MUST setup callbacks BEFORE we ever stop the world.
1063 This should be called BEFORE any thread in created and WITHOUT the
1064 allocation lock held. */
1066 void GC_init_dyld() {
1067 static GC_bool initialized = FALSE;
1068 char *bind_fully_env = NULL;
1070 if(initialized) return;
1072 # ifdef DARWIN_DEBUG
1073 GC_printf0("Registering dyld callbacks...\n");
1074 # endif
1076 /* Apple's Documentation:
1077 When you call _dyld_register_func_for_add_image, the dynamic linker runtime
1078 calls the specified callback (func) once for each of the images that is
1079 currently loaded into the program. When a new image is added to the program,
1080 your callback is called again with the mach_header for the new image, and the
1081 virtual memory slide amount of the new image.
1083 This WILL properly register already linked libraries and libraries
1084 linked in the future
1087 _dyld_register_func_for_add_image(GC_dyld_image_add);
1088 _dyld_register_func_for_remove_image(GC_dyld_image_remove);
1090 /* Set this early to avoid reentrancy issues. */
1091 initialized = TRUE;
1093 bind_fully_env = getenv("DYLD_BIND_AT_LAUNCH");
1095 if (bind_fully_env == NULL) {
1096 # ifdef DARWIN_DEBUG
1097 GC_printf0("Forcing full bind of GC code...\n");
1098 # endif
1100 if(!_dyld_bind_fully_image_containing_address((unsigned long*)GC_malloc))
1101 GC_abort("_dyld_bind_fully_image_containing_address failed");
1106 #define HAVE_REGISTER_MAIN_STATIC_DATA
1107 GC_bool GC_register_main_static_data()
1109 /* Already done through dyld callbacks */
1110 return FALSE;
1113 #endif /* DARWIN */
1115 #else /* !DYNAMIC_LOADING */
1117 #ifdef PCR
1119 # include "il/PCR_IL.h"
1120 # include "th/PCR_ThCtl.h"
1121 # include "mm/PCR_MM.h"
1123 void GC_register_dynamic_libraries()
1125 /* Add new static data areas of dynamically loaded modules. */
1127 PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile();
1128 PCR_IL_LoadedSegment * q;
1130 /* Skip uncommited files */
1131 while (p != NIL && !(p -> lf_commitPoint)) {
1132 /* The loading of this file has not yet been committed */
1133 /* Hence its description could be inconsistent. */
1134 /* Furthermore, it hasn't yet been run. Hence its data */
1135 /* segments can't possibly reference heap allocated */
1136 /* objects. */
1137 p = p -> lf_prev;
1139 for (; p != NIL; p = p -> lf_prev) {
1140 for (q = p -> lf_ls; q != NIL; q = q -> ls_next) {
1141 if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK)
1142 == PCR_IL_SegFlags_Traced_on) {
1143 GC_add_roots_inner
1144 ((char *)(q -> ls_addr),
1145 (char *)(q -> ls_addr) + q -> ls_bytes,
1146 TRUE);
1154 #else /* !PCR */
1156 void GC_register_dynamic_libraries(){}
1158 int GC_no_dynamic_loading;
1160 #endif /* !PCR */
1162 #endif /* !DYNAMIC_LOADING */
1164 #ifndef HAVE_REGISTER_MAIN_STATIC_DATA
1166 /* Do we need to separately register the main static data segment? */
1167 GC_bool GC_register_main_static_data()
1169 return TRUE;
1171 #endif /* HAVE_REGISTER_MAIN_STATIC_DATA */