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 */
33 #if !defined(MACOS) && !defined(_WIN32_WCE)
34 # include <sys/types.h>
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(). */
47 # define GC_must_restore_redefined_dlopen
49 # undef GC_must_restore_redefined_dlopen
52 #if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE)) \
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) && \
59 !(defined(FREEBSD) && defined(__ELF__)) && \
60 !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD)
61 --> We only know how to find data segments of dynamic libraries for the
62 --> above. Additional SVR4 variants might not be too
76 /* struct link_map field overrides */
77 # define l_next lm_next
78 # define l_addr lm_addr
79 # define l_name lm_name
82 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
83 (defined(FREEBSD) && defined(__ELF__)) || \
84 (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
90 /* Newer versions of GNU/Linux define this macro. We
91 * define it similarly for any ELF systems that don't. */
93 # if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
94 # define ElfW(type) Elf32_##type
96 # define ElfW(type) Elf64_##type
100 #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)
106 static struct link_map *
107 GC_FirstDLOpenedLinkMap()
109 extern ElfW(Dyn) _DYNAMIC;
112 static struct link_map * cachedResult = 0;
113 static ElfW(Dyn) *dynStructureAddr = 0;
114 /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
116 # ifdef SUNOS53_SHARED_LIB
117 /* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
118 /* up properly in dynamically linked .so's. This means we have */
119 /* to use its value in the set of original object files loaded */
120 /* at program startup. */
121 if( dynStructureAddr == 0 ) {
122 void* startupSyms = dlopen(0, RTLD_LAZY);
123 dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
126 dynStructureAddr = &_DYNAMIC;
129 if( dynStructureAddr == 0) {
132 if( cachedResult == 0 ) {
134 for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
135 if( tag == DT_DEBUG ) {
137 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
138 if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
146 #endif /* SUNOS5DL ... */
148 /* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
149 # if defined(GC_must_restore_redefined_dlopen)
150 # define dlopen GC_dlopen
153 #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)
156 struct link_dynamic _DYNAMIC;
159 static struct link_map *
160 GC_FirstDLOpenedLinkMap()
162 extern struct link_dynamic _DYNAMIC;
164 if( &_DYNAMIC == 0) {
167 return(_DYNAMIC.ld_un.ld_1->ld_loaded);
170 /* Return the address of the ld.so allocated common symbol */
171 /* with the least address, or 0 if none. */
172 static ptr_t GC_first_common()
175 extern struct link_dynamic _DYNAMIC;
176 struct rtc_symb * curr_symbol;
178 if( &_DYNAMIC == 0) {
181 curr_symbol = _DYNAMIC.ldd -> ldd_cp;
182 for (; curr_symbol != 0; curr_symbol = curr_symbol -> rtc_next) {
184 || (ptr_t)(curr_symbol -> rtc_sp -> n_value) < result) {
185 result = (ptr_t)(curr_symbol -> rtc_sp -> n_value);
191 #endif /* SUNOS4 ... */
193 # if defined(SUNOS4) || defined(SUNOS5DL)
194 /* Add dynamic library data sections to the root set. */
195 # if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS)
197 --> fix mutual exclusion with dlopen
198 # endif /* We assume M3 programs don't call dlopen for now */
201 # ifndef USE_PROC_FOR_LIBRARIES
202 void GC_register_dynamic_libraries()
204 struct link_map *lm = GC_FirstDLOpenedLinkMap();
207 for (lm = GC_FirstDLOpenedLinkMap();
208 lm != (struct link_map *) 0; lm = lm->l_next)
213 e = (struct exec *) lm->lm_addr;
215 ((char *) (N_DATOFF(*e) + lm->lm_addr)),
216 ((char *) (N_BSSADDR(*e) + e->a_bss + lm->lm_addr)),
222 unsigned long offset;
226 e = (ElfW(Ehdr) *) lm->l_addr;
227 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
228 offset = ((unsigned long)(lm->l_addr));
229 for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
230 switch( p->p_type ) {
233 if( !(p->p_flags & PF_W) ) break;
234 start = ((char *)(p->p_vaddr)) + offset;
250 static ptr_t common_start = 0;
252 extern ptr_t GC_find_limit();
254 if (common_start == 0) common_start = GC_first_common();
255 if (common_start != 0) {
256 common_end = GC_find_limit(common_start, TRUE);
257 GC_add_roots_inner((char *)common_start, (char *)common_end, TRUE);
263 # endif /* !USE_PROC ... */
266 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
267 (defined(FREEBSD) && defined(__ELF__)) || \
268 (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
271 #ifdef USE_PROC_FOR_LIBRARIES
275 #include <sys/stat.h>
279 #define MAPS_BUF_SIZE (32*1024)
281 extern ssize_t GC_repeat_read(int fd, char *buf, size_t count);
282 /* Repeatedly read until buffer is filled, or EOF is encountered */
283 /* Defined in os_dep.c. */
285 static char *parse_map_entry(char *buf_ptr, word *start, word *end,
286 char *prot_buf, unsigned int *maj_dev);
288 void GC_register_dynamic_libraries()
294 char maps_temp[32768];
299 unsigned int maj_dev, min_dev;
300 word least_ha, greatest_ha;
302 word datastart = (word)(DATASTART);
304 /* Read /proc/self/maps */
305 /* Note that we may not allocate, and thus can't use stdio. */
306 f = open("/proc/self/maps", O_RDONLY);
307 if (-1 == f) ABORT("Couldn't open /proc/self/maps");
308 /* stat() doesn't work for /proc/self/maps, so we have to
309 read it to find out how large it is... */
312 result = GC_repeat_read(f, maps_temp, sizeof(maps_temp));
313 if (result <= 0) ABORT("Couldn't read /proc/self/maps");
315 } while (result == sizeof(maps_temp));
317 if (maps_size > sizeof(maps_temp)) {
318 /* If larger than our buffer, close and re-read it. */
320 f = open("/proc/self/maps", O_RDONLY);
321 if (-1 == f) ABORT("Couldn't open /proc/self/maps");
322 maps_buf = alloca(maps_size);
323 if (NULL == maps_buf) ABORT("/proc/self/maps alloca failed");
324 result = GC_repeat_read(f, maps_buf, maps_size);
325 if (result <= 0) ABORT("Couldn't read /proc/self/maps");
327 /* Otherwise use the fixed size buffer */
328 maps_buf = maps_temp;
332 maps_buf[result] = '\0';
334 /* Compute heap bounds. Should be done by add_to_heap? */
335 least_ha = (word)(-1);
337 for (i = 0; i < GC_n_heap_sects; ++i) {
338 word sect_start = (word)GC_heap_sects[i].hs_start;
339 word sect_end = sect_start + GC_heap_sects[i].hs_bytes;
340 if (sect_start < least_ha) least_ha = sect_start;
341 if (sect_end > greatest_ha) greatest_ha = sect_end;
343 if (greatest_ha < (word)GC_scratch_last_end_ptr)
344 greatest_ha = (word)GC_scratch_last_end_ptr;
347 buf_ptr = parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
348 if (buf_ptr == NULL) return;
350 if (prot_buf[1] == 'w') {
351 /* This is a writable mapping. Add it to */
352 /* the root set unless it is already otherwise */
354 if (start <= (word)GC_stackbottom && end >= (word)GC_stackbottom) {
355 /* Stack mapping; discard */
359 if (GC_segment_is_thread_stack(start, end)) continue;
361 /* The rest of this assumes that there is no mapping */
362 /* spanning the beginning of the data segment, or extending */
363 /* beyond the entire heap at both ends. */
364 /* Empirically these assumptions hold. */
366 if (start < (word)DATAEND && end > (word)DATAEND) {
367 /* Rld may use space at the end of the main data */
368 /* segment. Thus we add that in. */
369 start = (word)DATAEND;
371 if (start < least_ha && end > least_ha) {
374 if (start < greatest_ha && end > greatest_ha) {
377 if (start >= least_ha && end <= greatest_ha) continue;
378 GC_add_roots_inner((char *)start, (char *)end, TRUE);
383 /* We now take care of the main data segment ourselves: */
384 GC_bool GC_register_main_static_data()
389 # define HAVE_REGISTER_MAIN_STATIC_DATA
391 // parse_map_entry parses an entry from /proc/self/maps so we can
392 // locate all writable data segments that belong to shared libraries.
393 // The format of one of these entries and the fields we care about
395 // XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537 name of mapping...\n
396 // ^^^^^^^^ ^^^^^^^^ ^^^^ ^^
397 // start end prot maj_dev
400 // The parser is called with a pointer to the entry and the return value
401 // is either NULL or is advanced to the next entry(the byte after the
404 #define OFFSET_MAP_START 0
405 #define OFFSET_MAP_END 9
406 #define OFFSET_MAP_PROT 18
407 #define OFFSET_MAP_MAJDEV 32
409 static char *parse_map_entry(char *buf_ptr, word *start, word *end,
410 char *prot_buf, unsigned int *maj_dev)
416 if (buf_ptr == NULL || *buf_ptr == '\0') {
420 memcpy(prot_buf, buf_ptr+OFFSET_MAP_PROT, 4); // do the protections first
423 if (prot_buf[1] == 'w') { // we can skip all of this if it's not writable
426 buf_ptr[OFFSET_MAP_START+8] = '\0';
427 *start = strtoul(tok, NULL, 16);
429 tok = buf_ptr+OFFSET_MAP_END;
430 buf_ptr[OFFSET_MAP_END+8] = '\0';
431 *end = strtoul(tok, NULL, 16);
433 buf_ptr += OFFSET_MAP_MAJDEV;
435 while (*buf_ptr != ':') buf_ptr++;
437 *maj_dev = strtoul(tok, NULL, 16);
440 while (*buf_ptr && *buf_ptr++ != '\n');
445 #endif /* USE_PROC_FOR_LIBRARIES */
447 #if !defined(USE_PROC_FOR_LIBRARIES)
448 /* The following is the preferred way to walk dynamic libraries */
449 /* For glibc 2.2.4+. Unfortunately, it doesn't work for older */
450 /* versions. Thanks to Jakub Jelinek for most of the code. */
452 # if defined(LINUX) /* Are others OK here, too? */ \
453 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
454 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
456 /* We have the header files for a glibc that includes dl_iterate_phdr. */
457 /* It may still not be available in the library on the target system. */
458 /* Thus we also treat it as a weak symbol. */
459 #define HAVE_DL_ITERATE_PHDR
461 static int GC_register_dynlib_callback(info, size, ptr)
462 struct dl_phdr_info * info;
466 const ElfW(Phdr) * p;
470 /* Make sure struct dl_phdr_info is at least as big as we need. */
471 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
472 + sizeof (info->dlpi_phnum))
476 for( i = 0; i < (int)(info->dlpi_phnum); ((i++),(p++)) ) {
477 switch( p->p_type ) {
480 if( !(p->p_flags & PF_W) ) break;
481 start = ((char *)(p->p_vaddr)) + info->dlpi_addr;
482 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
490 * (int *)ptr = 1; /* Signal that we were called */
494 /* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
496 #pragma weak dl_iterate_phdr
498 GC_bool GC_register_dynamic_libraries_dl_iterate_phdr()
500 if (dl_iterate_phdr) {
501 int did_something = 0;
502 dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
503 if (!did_something) {
504 /* dl_iterate_phdr may forget the static data segment in */
505 /* statically linked executables. */
506 GC_add_roots_inner(DATASTART, (char *)(DATAEND), TRUE);
507 # if defined(DATASTART2)
508 GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);
517 /* Do we need to separately register the main static data segment? */
518 GC_bool GC_register_main_static_data()
520 return (dl_iterate_phdr == 0);
523 #define HAVE_REGISTER_MAIN_STATIC_DATA
525 # else /* !LINUX || version(glibc) < 2.2.4 */
527 /* Dynamic loading code for Linux running ELF. Somewhat tested on
528 * Linux/x86, untested but hopefully should work on Linux/Alpha.
529 * This code was derived from the Solaris/ELF support. Thanks to
530 * whatever kind soul wrote that. - Patrick Bridges */
532 /* This doesn't necessarily work in all cases, e.g. with preloaded
533 * dynamic libraries. */
536 # include <sys/exec_elf.h>
545 # pragma weak _DYNAMIC
547 extern ElfW(Dyn) _DYNAMIC[];
549 static struct link_map *
550 GC_FirstDLOpenedLinkMap()
554 static struct link_map *cachedResult = 0;
559 if( cachedResult == 0 ) {
561 for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
562 if( tag == DT_DEBUG ) {
564 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
565 if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
574 void GC_register_dynamic_libraries()
579 # ifdef HAVE_DL_ITERATE_PHDR
580 if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
584 lm = GC_FirstDLOpenedLinkMap();
585 for (lm = GC_FirstDLOpenedLinkMap();
586 lm != (struct link_map *) 0; lm = lm->l_next)
590 unsigned long offset;
594 e = (ElfW(Ehdr) *) lm->l_addr;
595 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
596 offset = ((unsigned long)(lm->l_addr));
597 for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
598 switch( p->p_type ) {
601 if( !(p->p_flags & PF_W) ) break;
602 start = ((char *)(p->p_vaddr)) + offset;
603 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
613 #endif /* !USE_PROC_FOR_LIBRARIES */
617 #if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
619 #include <sys/procfs.h>
620 #include <sys/stat.h>
624 #include <signal.h> /* Only for the following test. */
629 extern void * GC_roots_present();
630 /* The type is a lie, since the real type doesn't make sense here, */
631 /* and we only test for NULL. */
633 /* We use /proc to track down all parts of the address space that are */
634 /* mapped by the process, and throw out regions we know we shouldn't */
635 /* worry about. This may also work under other SVR4 variants. */
636 void GC_register_dynamic_libraries()
640 static prmap_t * addr_map = 0;
641 static int current_sz = 0; /* Number of records currently in addr_map */
642 static int needed_sz; /* Required size of addr_map */
645 register ptr_t start;
646 register ptr_t limit;
647 ptr_t heap_start = (ptr_t)HEAP_START;
648 ptr_t heap_end = heap_start;
652 # endif /* SUNOS5DL */
655 sprintf(buf, "/proc/%d", getpid());
656 /* The above generates a lint complaint, since pid_t varies. */
657 /* It's unclear how to improve this. */
658 fd = open(buf, O_RDONLY);
660 ABORT("/proc open failed");
663 if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
664 GC_err_printf2("fd = %d, errno = %d\n", fd, errno);
665 ABORT("/proc PIOCNMAP ioctl failed");
667 if (needed_sz >= current_sz) {
668 current_sz = needed_sz * 2 + 1;
669 /* Expansion, plus room for 0 record */
670 addr_map = (prmap_t *)GC_scratch_alloc((word)
671 (current_sz * sizeof(prmap_t)));
673 if (ioctl(fd, PIOCMAP, addr_map) < 0) {
674 GC_err_printf4("fd = %d, errno = %d, needed_sz = %d, addr_map = 0x%X\n",
675 fd, errno, needed_sz, addr_map);
676 ABORT("/proc PIOCMAP ioctl failed");
678 if (GC_n_heap_sects > 0) {
679 heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
680 + GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
681 if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;
683 for (i = 0; i < needed_sz; i++) {
684 flags = addr_map[i].pr_mflags;
685 if ((flags & (MA_BREAK | MA_STACK | MA_PHYS)) != 0) goto irrelevant;
686 if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
688 /* The latter test is empirically useless in very old Irix */
689 /* versions. Other than the */
690 /* main data and stack segments, everything appears to be */
691 /* mapped readable, writable, executable, and shared(!!). */
692 /* This makes no sense to me. - HB */
693 start = (ptr_t)(addr_map[i].pr_vaddr);
694 if (GC_roots_present(start)) goto irrelevant;
695 if (start < heap_end && start >= heap_start)
698 if (GC_is_thread_stack(start)) goto irrelevant;
699 # endif /* MMAP_STACKS */
701 limit = start + addr_map[i].pr_size;
702 /* The following seemed to be necessary for very old versions */
703 /* of Irix, but it has been reported to discard relevant */
704 /* segments under Irix 6.5. */
706 if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
707 /* Discard text segments, i.e. 0-offset mappings against */
708 /* executable files which appear to have ELF headers. */
711 # define MAP_IRR_SZ 10
712 static ptr_t map_irr[MAP_IRR_SZ];
713 /* Known irrelevant map entries */
714 static int n_irr = 0;
718 for (i = 0; i < n_irr; i++) {
719 if (map_irr[i] == start) goto irrelevant;
721 arg = (caddr_t)start;
722 obj = ioctl(fd, PIOCOPENM, &arg);
726 if ((buf.st_mode & 0111) != 0) {
727 if (n_irr < MAP_IRR_SZ) {
728 map_irr[n_irr++] = start;
735 GC_add_roots_inner(start, limit, TRUE);
738 /* Dont keep cached descriptor, for now. Some kernels don't like us */
739 /* to keep a /proc file descriptor around during kill -9. */
740 if (close(fd) < 0) ABORT("Couldnt close /proc file");
744 # endif /* USE_PROC || IRIX5 */
746 # if defined(MSWIN32) || defined(MSWINCE)
748 # define WIN32_LEAN_AND_MEAN
750 # include <windows.h>
753 /* We traverse the entire address space and register all segments */
754 /* that could possibly have been written to. */
756 extern GC_bool GC_is_heap_base (ptr_t p);
758 # ifdef GC_WIN32_THREADS
759 extern void GC_get_next_stack(char *start, char **lo, char **hi);
760 void GC_cond_add_roots(char *base, char * limit)
762 char * curr_base = base;
763 char * next_stack_lo;
764 char * next_stack_hi;
766 if (base == limit) return;
768 GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);
769 if (next_stack_lo >= limit) break;
770 GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
771 curr_base = next_stack_hi;
773 if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
776 void GC_cond_add_roots(char *base, char * limit)
780 = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));
781 if (base == limit) return;
782 if (limit > stack_top && base < GC_stackbottom) {
783 /* Part of the stack; ignore it. */
786 GC_add_roots_inner(base, limit, TRUE);
791 /* Do we need to separately register the main static data segment? */
792 GC_bool GC_register_main_static_data()
797 extern GC_bool GC_no_win32_dlls;
799 GC_bool GC_register_main_static_data()
801 return GC_no_win32_dlls;
805 # define HAVE_REGISTER_MAIN_STATIC_DATA
807 void GC_register_dynamic_libraries()
809 MEMORY_BASIC_INFORMATION buf;
814 char * limit, * new_limit;
817 if (GC_no_win32_dlls) return;
819 base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
820 # if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
821 /* Only the first 32 MB of address space belongs to the current process */
822 while (p < (LPVOID)0x02000000) {
823 result = VirtualQuery(p, &buf, sizeof(buf));
825 /* Page is free; advance to the next possible allocation base */
827 (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
828 & ~(GC_sysinfo.dwAllocationGranularity-1));
831 while (p < GC_sysinfo.lpMaximumApplicationAddress) {
832 result = VirtualQuery(p, &buf, sizeof(buf));
835 if (result != sizeof(buf)) {
836 ABORT("Weird VirtualQuery result");
838 new_limit = (char *)p + buf.RegionSize;
839 protect = buf.Protect;
840 if (buf.State == MEM_COMMIT
841 && (protect == PAGE_EXECUTE_READWRITE
842 || protect == PAGE_READWRITE)
843 && !GC_is_heap_base(buf.AllocationBase)) {
844 if ((char *)p != limit) {
845 GC_cond_add_roots(base, limit);
851 if (p > (LPVOID)new_limit /* overflow */) break;
852 p = (LPVOID)new_limit;
854 GC_cond_add_roots(base, limit);
857 #endif /* MSWIN32 || MSWINCE */
859 #if defined(ALPHA) && defined(OSF1)
863 void GC_register_dynamic_libraries()
869 ldr_module_t moduleid = LDR_NULL_MODULE;
870 ldr_module_info_t moduleinfo;
871 size_t moduleinfosize = sizeof(moduleinfo);
872 size_t modulereturnsize;
876 ldr_region_info_t regioninfo;
877 size_t regioninfosize = sizeof(regioninfo);
878 size_t regionreturnsize;
880 /* Obtain id of this process */
881 mypid = ldr_my_process();
883 /* For each module */
886 /* Get the next (first) module */
887 status = ldr_next_module(mypid, &moduleid);
889 /* Any more modules? */
890 if (moduleid == LDR_NULL_MODULE)
891 break; /* No more modules */
893 /* Check status AFTER checking moduleid because */
894 /* of a bug in the non-shared ldr_next_module stub */
896 GC_printf1("dynamic_load: status = %ld\n", (long)status);
898 extern char *sys_errlist[];
901 if (errno <= sys_nerr) {
902 GC_printf1("dynamic_load: %s\n", (long)sys_errlist[errno]);
904 GC_printf1("dynamic_load: %d\n", (long)errno);
907 ABORT("ldr_next_module failed");
910 /* Get the module information */
911 status = ldr_inq_module(mypid, moduleid, &moduleinfo,
912 moduleinfosize, &modulereturnsize);
914 ABORT("ldr_inq_module failed");
916 /* is module for the main program (i.e. nonshared portion)? */
917 if (moduleinfo.lmi_flags & LDR_MAIN)
918 continue; /* skip the main module */
921 GC_printf("---Module---\n");
922 GC_printf("Module ID = %16ld\n", moduleinfo.lmi_modid);
923 GC_printf("Count of regions = %16d\n", moduleinfo.lmi_nregion);
924 GC_printf("flags for module = %16lx\n", moduleinfo.lmi_flags);
925 GC_printf("pathname of module = \"%s\"\n", moduleinfo.lmi_name);
928 /* For each region in this module */
929 for (region = 0; region < moduleinfo.lmi_nregion; region++) {
931 /* Get the region information */
932 status = ldr_inq_region(mypid, moduleid, region, ®ioninfo,
933 regioninfosize, ®ionreturnsize);
935 ABORT("ldr_inq_region failed");
937 /* only process writable (data) regions */
938 if (! (regioninfo.lri_prot & LDR_W))
942 GC_printf("--- Region ---\n");
943 GC_printf("Region number = %16ld\n",
944 regioninfo.lri_region_no);
945 GC_printf("Protection flags = %016x\n", regioninfo.lri_prot);
946 GC_printf("Virtual address = %16p\n", regioninfo.lri_vaddr);
947 GC_printf("Mapped address = %16p\n", regioninfo.lri_mapaddr);
948 GC_printf("Region size = %16ld\n", regioninfo.lri_size);
949 GC_printf("Region name = \"%s\"\n", regioninfo.lri_name);
952 /* register region as a garbage collection root */
954 (char *)regioninfo.lri_mapaddr,
955 (char *)regioninfo.lri_mapaddr + regioninfo.lri_size,
969 extern char *sys_errlist[];
972 void GC_register_dynamic_libraries()
975 int index = 1; /* Ordinal position in shared library search list */
976 struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */
978 /* For each dynamic library loaded */
981 /* Get info about next shared library */
982 status = shl_get(index, &shl_desc);
984 /* Check if this is the end of the list or if some error occured */
986 # ifdef GC_HPUX_THREADS
987 /* I've seen errno values of 0. The man page is not clear */
988 /* as to whether errno should get set on a -1 return. */
991 if (errno == EINVAL) {
992 break; /* Moved past end of shared library list --> finished */
994 if (errno <= sys_nerr) {
995 GC_printf1("dynamic_load: %s\n", (long) sys_errlist[errno]);
997 GC_printf1("dynamic_load: %d\n", (long) errno);
999 ABORT("shl_get failed");
1005 GC_printf0("---Shared library---\n");
1006 GC_printf1("\tfilename = \"%s\"\n", shl_desc->filename);
1007 GC_printf1("\tindex = %d\n", index);
1008 GC_printf1("\thandle = %08x\n",
1009 (unsigned long) shl_desc->handle);
1010 GC_printf1("\ttext seg. start = %08x\n", shl_desc->tstart);
1011 GC_printf1("\ttext seg. end = %08x\n", shl_desc->tend);
1012 GC_printf1("\tdata seg. start = %08x\n", shl_desc->dstart);
1013 GC_printf1("\tdata seg. end = %08x\n", shl_desc->dend);
1014 GC_printf1("\tref. count = %lu\n", shl_desc->ref_count);
1017 /* register shared library's data segment as a garbage collection root */
1018 GC_add_roots_inner((char *) shl_desc->dstart,
1019 (char *) shl_desc->dend, TRUE);
1028 #include <sys/ldr.h>
1029 #include <sys/errno.h>
1030 void GC_register_dynamic_libraries()
1035 struct ld_info *ldi;
1037 ldibuf = alloca(ldibuflen = 8192);
1039 while ( (len = loadquery(L_GETINFO,ldibuf,ldibuflen)) < 0) {
1040 if (errno != ENOMEM) {
1041 ABORT("loadquery failed");
1043 ldibuf = alloca(ldibuflen *= 2);
1046 ldi = (struct ld_info *)ldibuf;
1048 len = ldi->ldinfo_next;
1050 ldi->ldinfo_dataorg,
1051 (unsigned long)ldi->ldinfo_dataorg
1052 + ldi->ldinfo_datasize,
1054 ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
1061 #else /* !DYNAMIC_LOADING */
1065 # include "il/PCR_IL.h"
1066 # include "th/PCR_ThCtl.h"
1067 # include "mm/PCR_MM.h"
1069 void GC_register_dynamic_libraries()
1071 /* Add new static data areas of dynamically loaded modules. */
1073 PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile();
1074 PCR_IL_LoadedSegment * q;
1076 /* Skip uncommited files */
1077 while (p != NIL && !(p -> lf_commitPoint)) {
1078 /* The loading of this file has not yet been committed */
1079 /* Hence its description could be inconsistent. */
1080 /* Furthermore, it hasn't yet been run. Hence its data */
1081 /* segments can't possibly reference heap allocated */
1085 for (; p != NIL; p = p -> lf_prev) {
1086 for (q = p -> lf_ls; q != NIL; q = q -> ls_next) {
1087 if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK)
1088 == PCR_IL_SegFlags_Traced_on) {
1090 ((char *)(q -> ls_addr),
1091 (char *)(q -> ls_addr) + q -> ls_bytes,
1102 void GC_register_dynamic_libraries(){}
1104 int GC_no_dynamic_loading;
1108 #endif /* !DYNAMIC_LOADING */
1110 #ifndef HAVE_REGISTER_MAIN_STATIC_DATA
1112 /* Do we need to separately register the main static data segment? */
1113 GC_bool GC_register_main_static_data()
1117 #endif /* HAVE_REGISTER_MAIN_STATIC_DATA */