Annotation of OpenXM_contrib2/asir2000/gc/dyn_load.c, Revision 1.4
1.1 noro 1: /*
2: * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3: * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
4: *
5: * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6: * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7: *
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.
13: *
14: * Original author: Bill Janssen
15: * Heavily modified by Hans Boehm and others
16: */
17:
18: /*
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 ...
25: *
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.
28: */
1.4 ! noro 29: #if !defined(MACOS) && !defined(_WIN32_WCE)
1.1 noro 30: # include <sys/types.h>
31: #endif
1.4 ! noro 32: #include "private/gc_priv.h"
1.1 noro 33:
34: /* BTL: avoid circular redefinition of dlopen if SOLARIS_THREADS defined */
1.3 noro 35: # if (defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \
36: || defined(HPUX_THREADS) || defined(IRIX_THREADS)) && defined(dlopen) \
1.4 ! noro 37: && !defined(GC_USE_LD_WRAP)
1.1 noro 38: /* To support threads in Solaris, gc.h interposes on dlopen by */
39: /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
40: /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
41: /* real system dlopen() in their implementation. We first remove */
42: /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
43: # undef dlopen
44: # define GC_must_restore_redefined_dlopen
45: # else
46: # undef GC_must_restore_redefined_dlopen
47: # endif
48:
1.4 ! noro 49: #if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE)) \
! 50: && !defined(PCR)
1.1 noro 51: #if !defined(SUNOS4) && !defined(SUNOS5DL) && !defined(IRIX5) && \
1.4 ! noro 52: !defined(MSWIN32) && !defined(MSWINCE) && \
! 53: !(defined(ALPHA) && defined(OSF1)) && \
1.2 noro 54: !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
1.4 ! noro 55: !defined(RS6000) && !defined(SCO_ELF) && \
! 56: !(defined(NETBSD) && defined(__ELF__))
1.1 noro 57: --> We only know how to find data segments of dynamic libraries for the
58: --> above. Additional SVR4 variants might not be too
59: --> hard to add.
60: #endif
61:
62: #include <stdio.h>
63: #ifdef SUNOS5DL
64: # include <sys/elf.h>
65: # include <dlfcn.h>
66: # include <link.h>
67: #endif
68: #ifdef SUNOS4
69: # include <dlfcn.h>
70: # include <link.h>
71: # include <a.out.h>
72: /* struct link_map field overrides */
73: # define l_next lm_next
74: # define l_addr lm_addr
75: # define l_name lm_name
76: #endif
77:
78:
79: #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)
80:
81: #ifdef LINT
82: Elf32_Dyn _DYNAMIC;
83: #endif
84:
85: static struct link_map *
86: GC_FirstDLOpenedLinkMap()
87: {
88: extern Elf32_Dyn _DYNAMIC;
89: Elf32_Dyn *dp;
90: struct r_debug *r;
91: static struct link_map * cachedResult = 0;
92: static Elf32_Dyn *dynStructureAddr = 0;
93: /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
94:
95: # ifdef SUNOS53_SHARED_LIB
96: /* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
97: /* up properly in dynamically linked .so's. This means we have */
98: /* to use its value in the set of original object files loaded */
99: /* at program startup. */
100: if( dynStructureAddr == 0 ) {
101: void* startupSyms = dlopen(0, RTLD_LAZY);
102: dynStructureAddr = (Elf32_Dyn*)dlsym(startupSyms, "_DYNAMIC");
103: }
104: # else
105: dynStructureAddr = &_DYNAMIC;
106: # endif
107:
108: if( dynStructureAddr == 0) {
109: return(0);
110: }
111: if( cachedResult == 0 ) {
112: int tag;
113: for( dp = ((Elf32_Dyn *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
114: if( tag == DT_DEBUG ) {
115: struct link_map *lm
116: = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
117: if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
118: break;
119: }
120: }
121: }
122: return cachedResult;
123: }
124:
125: #endif /* SUNOS5DL ... */
126:
1.4 ! noro 127: /* BTL: added to fix circular dlopen definition if SOLARIS_THREADS defined */
! 128: # if defined(GC_must_restore_redefined_dlopen)
! 129: # define dlopen GC_dlopen
! 130: # endif
! 131:
1.1 noro 132: #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)
133:
134: #ifdef LINT
135: struct link_dynamic _DYNAMIC;
136: #endif
137:
138: static struct link_map *
139: GC_FirstDLOpenedLinkMap()
140: {
141: extern struct link_dynamic _DYNAMIC;
142:
143: if( &_DYNAMIC == 0) {
144: return(0);
145: }
146: return(_DYNAMIC.ld_un.ld_1->ld_loaded);
147: }
148:
149: /* Return the address of the ld.so allocated common symbol */
150: /* with the least address, or 0 if none. */
151: static ptr_t GC_first_common()
152: {
153: ptr_t result = 0;
154: extern struct link_dynamic _DYNAMIC;
155: struct rtc_symb * curr_symbol;
156:
157: if( &_DYNAMIC == 0) {
158: return(0);
159: }
160: curr_symbol = _DYNAMIC.ldd -> ldd_cp;
161: for (; curr_symbol != 0; curr_symbol = curr_symbol -> rtc_next) {
162: if (result == 0
163: || (ptr_t)(curr_symbol -> rtc_sp -> n_value) < result) {
164: result = (ptr_t)(curr_symbol -> rtc_sp -> n_value);
165: }
166: }
167: return(result);
168: }
169:
170: #endif /* SUNOS4 ... */
171:
1.3 noro 172: # if defined(SUNOS4) || defined(SUNOS5DL)
173: /* Add dynamic library data sections to the root set. */
174: # if !defined(PCR) && !defined(SOLARIS_THREADS) && defined(THREADS)
175: # ifndef SRC_M3
176: --> fix mutual exclusion with dlopen
177: # endif /* We assume M3 programs don't call dlopen for now */
178: # endif
179:
1.1 noro 180: # ifndef USE_PROC_FOR_LIBRARIES
181: void GC_register_dynamic_libraries()
182: {
183: struct link_map *lm = GC_FirstDLOpenedLinkMap();
184:
185:
186: for (lm = GC_FirstDLOpenedLinkMap();
187: lm != (struct link_map *) 0; lm = lm->l_next)
188: {
189: # ifdef SUNOS4
190: struct exec *e;
191:
192: e = (struct exec *) lm->lm_addr;
193: GC_add_roots_inner(
194: ((char *) (N_DATOFF(*e) + lm->lm_addr)),
195: ((char *) (N_BSSADDR(*e) + e->a_bss + lm->lm_addr)),
196: TRUE);
197: # endif
198: # ifdef SUNOS5DL
199: Elf32_Ehdr * e;
200: Elf32_Phdr * p;
201: unsigned long offset;
202: char * start;
203: register int i;
204:
205: e = (Elf32_Ehdr *) lm->l_addr;
206: p = ((Elf32_Phdr *)(((char *)(e)) + e->e_phoff));
207: offset = ((unsigned long)(lm->l_addr));
208: for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
209: switch( p->p_type ) {
210: case PT_LOAD:
211: {
212: if( !(p->p_flags & PF_W) ) break;
213: start = ((char *)(p->p_vaddr)) + offset;
214: GC_add_roots_inner(
215: start,
216: start + p->p_memsz,
217: TRUE
218: );
219: }
220: break;
221: default:
222: break;
223: }
224: }
225: # endif
226: }
227: # ifdef SUNOS4
228: {
229: static ptr_t common_start = 0;
230: ptr_t common_end;
231: extern ptr_t GC_find_limit();
232:
233: if (common_start == 0) common_start = GC_first_common();
234: if (common_start != 0) {
235: common_end = GC_find_limit(common_start, TRUE);
236: GC_add_roots_inner((char *)common_start, (char *)common_end, TRUE);
237: }
238: }
239: # endif
240: }
241:
242: # endif /* !USE_PROC ... */
243: # endif /* SUNOS */
244:
1.4 ! noro 245: #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
! 246: (defined(NETBSD) && defined(__ELF__))
! 247:
! 248:
! 249: #ifdef USE_PROC_FOR_LIBRARIES
! 250:
! 251: #include <string.h>
! 252:
! 253: #include <sys/stat.h>
! 254: #include <fcntl.h>
! 255: #include <unistd.h>
! 256:
! 257: #define MAPS_BUF_SIZE (32*1024)
! 258:
! 259: extern ssize_t GC_repeat_read(int fd, char *buf, size_t count);
! 260: /* Repeatedly read until buffer is filled, or EOF is encountered */
! 261: /* Defined in os_dep.c. */
! 262:
! 263: static char *parse_map_entry(char *buf_ptr, word *start, word *end,
! 264: char *prot_buf, unsigned int *maj_dev);
! 265:
! 266: void GC_register_dynamic_libraries()
! 267: {
! 268: int f;
! 269: int result;
! 270: char prot_buf[5];
! 271: int maps_size;
! 272: char maps_temp[32768];
! 273: char *maps_buf;
! 274: char *buf_ptr;
! 275: int count;
! 276: word start, end;
! 277: unsigned int maj_dev, min_dev;
! 278: word least_ha, greatest_ha;
! 279: unsigned i;
! 280: word datastart = (word)(DATASTART);
! 281:
! 282: /* Read /proc/self/maps */
! 283: /* Note that we may not allocate, and thus can't use stdio. */
! 284: f = open("/proc/self/maps", O_RDONLY);
! 285: if (-1 == f) ABORT("Couldn't open /proc/self/maps");
! 286: /* stat() doesn't work for /proc/self/maps, so we have to
! 287: read it to find out how large it is... */
! 288: maps_size = 0;
! 289: do {
! 290: result = GC_repeat_read(f, maps_temp, sizeof(maps_temp));
! 291: if (result <= 0) ABORT("Couldn't read /proc/self/maps");
! 292: maps_size += result;
! 293: } while (result == sizeof(maps_temp));
! 294:
! 295: if (maps_size > sizeof(maps_temp)) {
! 296: /* If larger than our buffer, close and re-read it. */
! 297: close(f);
! 298: f = open("/proc/self/maps", O_RDONLY);
! 299: if (-1 == f) ABORT("Couldn't open /proc/self/maps");
! 300: maps_buf = alloca(maps_size);
! 301: if (NULL == maps_buf) ABORT("/proc/self/maps alloca failed");
! 302: result = GC_repeat_read(f, maps_buf, maps_size);
! 303: if (result <= 0) ABORT("Couldn't read /proc/self/maps");
! 304: } else {
! 305: /* Otherwise use the fixed size buffer */
! 306: maps_buf = maps_temp;
! 307: }
! 308:
! 309: close(f);
! 310: maps_buf[result] = '\0';
! 311: buf_ptr = maps_buf;
! 312: /* Compute heap bounds. Should be done by add_to_heap? */
! 313: least_ha = (word)(-1);
! 314: greatest_ha = 0;
! 315: for (i = 0; i < GC_n_heap_sects; ++i) {
! 316: word sect_start = (word)GC_heap_sects[i].hs_start;
! 317: word sect_end = sect_start + GC_heap_sects[i].hs_bytes;
! 318: if (sect_start < least_ha) least_ha = sect_start;
! 319: if (sect_end > greatest_ha) greatest_ha = sect_end;
! 320: }
! 321: if (greatest_ha < (word)GC_scratch_last_end_ptr)
! 322: greatest_ha = (word)GC_scratch_last_end_ptr;
! 323: for (;;) {
! 324:
! 325: buf_ptr = parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
! 326: if (buf_ptr == NULL) return;
! 327:
! 328: if (prot_buf[1] == 'w') {
! 329: /* This is a writable mapping. Add it to */
! 330: /* the root set unless it is already otherwise */
! 331: /* accounted for. */
! 332: if (start <= (word)GC_stackbottom && end >= (word)GC_stackbottom) {
! 333: /* Stack mapping; discard */
! 334: continue;
! 335: }
! 336: if (start <= datastart && end > datastart && maj_dev != 0) {
! 337: /* Main data segment; discard */
! 338: continue;
! 339: }
! 340: # ifdef THREADS
! 341: if (GC_segment_is_thread_stack(start, end)) continue;
! 342: # endif
! 343: /* The rest of this assumes that there is no mapping */
! 344: /* spanning the beginning of the data segment, or extending */
! 345: /* beyond the entire heap at both ends. */
! 346: /* Empirically these assumptions hold. */
! 347:
! 348: if (start < (word)DATAEND && end > (word)DATAEND) {
! 349: /* Rld may use space at the end of the main data */
! 350: /* segment. Thus we add that in. */
! 351: start = (word)DATAEND;
! 352: }
! 353: if (start < least_ha && end > least_ha) {
! 354: end = least_ha;
! 355: }
! 356: if (start < greatest_ha && end > greatest_ha) {
! 357: start = greatest_ha;
! 358: }
! 359: if (start >= least_ha && end <= greatest_ha) continue;
! 360: GC_add_roots_inner((char *)start, (char *)end, TRUE);
! 361: }
! 362: }
! 363: }
! 364:
! 365: //
! 366: // parse_map_entry parses an entry from /proc/self/maps so we can
! 367: // locate all writable data segments that belong to shared libraries.
! 368: // The format of one of these entries and the fields we care about
! 369: // is as follows:
! 370: // XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537 name of mapping...\n
! 371: // ^^^^^^^^ ^^^^^^^^ ^^^^ ^^
! 372: // start end prot maj_dev
! 373: // 0 9 18 32
! 374: //
! 375: // The parser is called with a pointer to the entry and the return value
! 376: // is either NULL or is advanced to the next entry(the byte after the
! 377: // trailing '\n'.)
! 378: //
! 379: #define OFFSET_MAP_START 0
! 380: #define OFFSET_MAP_END 9
! 381: #define OFFSET_MAP_PROT 18
! 382: #define OFFSET_MAP_MAJDEV 32
! 383:
! 384: static char *parse_map_entry(char *buf_ptr, word *start, word *end,
! 385: char *prot_buf, unsigned int *maj_dev)
! 386: {
! 387: int i;
! 388: unsigned int val;
! 389: char *tok;
! 390:
! 391: if (buf_ptr == NULL || *buf_ptr == '\0') {
! 392: return NULL;
! 393: }
! 394:
! 395: memcpy(prot_buf, buf_ptr+OFFSET_MAP_PROT, 4); // do the protections first
! 396: prot_buf[4] = '\0';
! 397:
! 398: if (prot_buf[1] == 'w') { // we can skip all of this if it's not writable
! 399:
! 400: tok = buf_ptr;
! 401: buf_ptr[OFFSET_MAP_START+8] = '\0';
! 402: *start = strtoul(tok, NULL, 16);
! 403:
! 404: tok = buf_ptr+OFFSET_MAP_END;
! 405: buf_ptr[OFFSET_MAP_END+8] = '\0';
! 406: *end = strtoul(tok, NULL, 16);
! 407:
! 408: buf_ptr += OFFSET_MAP_MAJDEV;
! 409: tok = buf_ptr;
! 410: while (*buf_ptr != ':') buf_ptr++;
! 411: *buf_ptr++ = '\0';
! 412: *maj_dev = strtoul(tok, NULL, 16);
! 413: }
! 414:
! 415: while (*buf_ptr && *buf_ptr++ != '\n');
! 416:
! 417: return buf_ptr;
! 418: }
! 419:
! 420: #else /* !USE_PROC_FOR_LIBRARIES */
1.1 noro 421:
422: /* Dynamic loading code for Linux running ELF. Somewhat tested on
423: * Linux/x86, untested but hopefully should work on Linux/Alpha.
424: * This code was derived from the Solaris/ELF support. Thanks to
425: * whatever kind soul wrote that. - Patrick Bridges */
426:
1.4 ! noro 427: #if defined(NETBSD)
! 428: # include <sys/exec_elf.h>
! 429: #else
! 430: # include <elf.h>
! 431: #endif
1.1 noro 432: #include <link.h>
433:
434: /* Newer versions of Linux/Alpha and Linux/x86 define this macro. We
435: * define it for those older versions that don't. */
436: # ifndef ElfW
437: # if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
438: # define ElfW(type) Elf32_##type
439: # else
440: # define ElfW(type) Elf64_##type
441: # endif
442: # endif
443:
444: static struct link_map *
445: GC_FirstDLOpenedLinkMap()
446: {
447: # ifdef __GNUC__
448: # pragma weak _DYNAMIC
449: # endif
450: extern ElfW(Dyn) _DYNAMIC[];
451: ElfW(Dyn) *dp;
452: struct r_debug *r;
453: static struct link_map *cachedResult = 0;
454:
455: if( _DYNAMIC == 0) {
456: return(0);
457: }
458: if( cachedResult == 0 ) {
459: int tag;
460: for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
461: if( tag == DT_DEBUG ) {
462: struct link_map *lm
463: = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
464: if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
465: break;
466: }
467: }
468: }
469: return cachedResult;
470: }
471:
472:
473: void GC_register_dynamic_libraries()
474: {
475: struct link_map *lm = GC_FirstDLOpenedLinkMap();
476:
477:
478: for (lm = GC_FirstDLOpenedLinkMap();
479: lm != (struct link_map *) 0; lm = lm->l_next)
480: {
481: ElfW(Ehdr) * e;
482: ElfW(Phdr) * p;
483: unsigned long offset;
484: char * start;
485: register int i;
486:
487: e = (ElfW(Ehdr) *) lm->l_addr;
488: p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
489: offset = ((unsigned long)(lm->l_addr));
490: for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
491: switch( p->p_type ) {
492: case PT_LOAD:
493: {
494: if( !(p->p_flags & PF_W) ) break;
495: start = ((char *)(p->p_vaddr)) + offset;
496: GC_add_roots_inner(start, start + p->p_memsz, TRUE);
497: }
498: break;
499: default:
500: break;
501: }
502: }
503: }
504: }
505:
1.4 ! noro 506: #endif /* !USE_PROC_FOR_LIBRARIES */
! 507:
! 508: #endif /* LINUX */
1.1 noro 509:
1.4 ! noro 510: #if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
1.1 noro 511:
512: #include <sys/procfs.h>
513: #include <sys/stat.h>
514: #include <fcntl.h>
515: #include <elf.h>
516: #include <errno.h>
517:
518: extern void * GC_roots_present();
519: /* The type is a lie, since the real type doesn't make sense here, */
520: /* and we only test for NULL. */
521:
522:
523: /* We use /proc to track down all parts of the address space that are */
524: /* mapped by the process, and throw out regions we know we shouldn't */
525: /* worry about. This may also work under other SVR4 variants. */
526: void GC_register_dynamic_libraries()
527: {
528: static int fd = -1;
529: char buf[30];
530: static prmap_t * addr_map = 0;
531: static int current_sz = 0; /* Number of records currently in addr_map */
532: static int needed_sz; /* Required size of addr_map */
533: register int i;
534: register long flags;
535: register ptr_t start;
536: register ptr_t limit;
537: ptr_t heap_start = (ptr_t)HEAP_START;
538: ptr_t heap_end = heap_start;
539:
540: # ifdef SUNOS5DL
541: # define MA_PHYS 0
542: # endif /* SUNOS5DL */
543:
544: if (fd < 0) {
545: sprintf(buf, "/proc/%d", getpid());
546: /* The above generates a lint complaint, since pid_t varies. */
547: /* It's unclear how to improve this. */
548: fd = open(buf, O_RDONLY);
549: if (fd < 0) {
550: ABORT("/proc open failed");
551: }
552: }
553: if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
554: GC_err_printf2("fd = %d, errno = %d\n", fd, errno);
555: ABORT("/proc PIOCNMAP ioctl failed");
556: }
557: if (needed_sz >= current_sz) {
558: current_sz = needed_sz * 2 + 1;
559: /* Expansion, plus room for 0 record */
560: addr_map = (prmap_t *)GC_scratch_alloc((word)
561: (current_sz * sizeof(prmap_t)));
562: }
563: if (ioctl(fd, PIOCMAP, addr_map) < 0) {
564: GC_err_printf4("fd = %d, errno = %d, needed_sz = %d, addr_map = 0x%X\n",
565: fd, errno, needed_sz, addr_map);
566: ABORT("/proc PIOCMAP ioctl failed");
567: };
568: if (GC_n_heap_sects > 0) {
569: heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
570: + GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
571: if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;
572: }
573: for (i = 0; i < needed_sz; i++) {
574: flags = addr_map[i].pr_mflags;
575: if ((flags & (MA_BREAK | MA_STACK | MA_PHYS)) != 0) goto irrelevant;
576: if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
577: goto irrelevant;
578: /* The latter test is empirically useless. Other than the */
579: /* main data and stack segments, everything appears to be */
580: /* mapped readable, writable, executable, and shared(!!). */
581: /* This makes no sense to me. - HB */
582: start = (ptr_t)(addr_map[i].pr_vaddr);
583: if (GC_roots_present(start)) goto irrelevant;
584: if (start < heap_end && start >= heap_start)
585: goto irrelevant;
586: # ifdef MMAP_STACKS
587: if (GC_is_thread_stack(start)) goto irrelevant;
588: # endif /* MMAP_STACKS */
589:
590: limit = start + addr_map[i].pr_size;
591: if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
592: /* Discard text segments, i.e. 0-offset mappings against */
593: /* executable files which appear to have ELF headers. */
594: caddr_t arg;
595: int obj;
596: # define MAP_IRR_SZ 10
597: static ptr_t map_irr[MAP_IRR_SZ];
598: /* Known irrelevant map entries */
599: static int n_irr = 0;
600: struct stat buf;
601: register int i;
602:
603: for (i = 0; i < n_irr; i++) {
604: if (map_irr[i] == start) goto irrelevant;
605: }
606: arg = (caddr_t)start;
607: obj = ioctl(fd, PIOCOPENM, &arg);
608: if (obj >= 0) {
609: fstat(obj, &buf);
610: close(obj);
611: if ((buf.st_mode & 0111) != 0) {
612: if (n_irr < MAP_IRR_SZ) {
613: map_irr[n_irr++] = start;
614: }
615: goto irrelevant;
616: }
617: }
618: }
619: GC_add_roots_inner(start, limit, TRUE);
620: irrelevant: ;
621: }
622: /* Dont keep cached descriptor, for now. Some kernels don't like us */
623: /* to keep a /proc file descriptor around during kill -9. */
624: if (close(fd) < 0) ABORT("Couldnt close /proc file");
625: fd = -1;
626: }
627:
628: # endif /* USE_PROC || IRIX5 */
629:
1.4 ! noro 630: # if defined(MSWIN32) || defined(MSWINCE)
1.1 noro 631:
632: # define WIN32_LEAN_AND_MEAN
633: # define NOSERVICE
634: # include <windows.h>
635: # include <stdlib.h>
636:
637: /* We traverse the entire address space and register all segments */
638: /* that could possibly have been written to. */
639:
640: extern GC_bool GC_is_heap_base (ptr_t p);
641:
642: # ifdef WIN32_THREADS
643: extern void GC_get_next_stack(char *start, char **lo, char **hi);
1.4 ! noro 644: void GC_cond_add_roots(char *base, char * limit)
! 645: {
! 646: char * curr_base = base;
! 647: char * next_stack_lo;
! 648: char * next_stack_hi;
! 649:
! 650: if (base == limit) return;
! 651: for(;;) {
! 652: GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);
! 653: if (next_stack_lo >= limit) break;
! 654: GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
! 655: curr_base = next_stack_hi;
! 656: }
! 657: if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
! 658: }
! 659: # else
! 660: void GC_cond_add_roots(char *base, char * limit)
1.1 noro 661: {
1.4 ! noro 662: char dummy;
! 663: char * stack_top
! 664: = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));
! 665: if (base == limit) return;
! 666: if (limit > stack_top && base < GC_stackbottom) {
! 667: /* Part of the stack; ignore it. */
! 668: return;
! 669: }
! 670: GC_add_roots_inner(base, limit, TRUE);
1.1 noro 671: }
1.4 ! noro 672: # endif
! 673:
! 674: # ifndef MSWINCE
1.1 noro 675: extern GC_bool GC_win32s;
1.4 ! noro 676: # endif
1.1 noro 677:
678: void GC_register_dynamic_libraries()
679: {
680: MEMORY_BASIC_INFORMATION buf;
681: DWORD result;
682: DWORD protect;
683: LPVOID p;
684: char * base;
685: char * limit, * new_limit;
1.4 ! noro 686:
! 687: # ifdef MSWIN32
! 688: if (GC_win32s) return;
! 689: # endif
! 690: base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
! 691: # if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
! 692: /* Only the first 32 MB of address space belongs to the current process */
! 693: while (p < (LPVOID)0x02000000) {
! 694: result = VirtualQuery(p, &buf, sizeof(buf));
! 695: if (result == 0) {
! 696: /* Page is free; advance to the next possible allocation base */
! 697: new_limit = (char *)
! 698: (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
! 699: & ~(GC_sysinfo.dwAllocationGranularity-1));
! 700: } else
! 701: # else
! 702: while (p < GC_sysinfo.lpMaximumApplicationAddress) {
1.1 noro 703: result = VirtualQuery(p, &buf, sizeof(buf));
1.4 ! noro 704: # endif
! 705: {
! 706: if (result != sizeof(buf)) {
! 707: ABORT("Weird VirtualQuery result");
! 708: }
! 709: new_limit = (char *)p + buf.RegionSize;
! 710: protect = buf.Protect;
! 711: if (buf.State == MEM_COMMIT
! 712: && (protect == PAGE_EXECUTE_READWRITE
! 713: || protect == PAGE_READWRITE)
! 714: && !GC_is_heap_base(buf.AllocationBase)) {
! 715: if ((char *)p != limit) {
! 716: GC_cond_add_roots(base, limit);
! 717: base = p;
! 718: }
! 719: limit = new_limit;
! 720: }
! 721: }
1.1 noro 722: if (p > (LPVOID)new_limit /* overflow */) break;
723: p = (LPVOID)new_limit;
724: }
725: GC_cond_add_roots(base, limit);
726: }
727:
1.4 ! noro 728: #endif /* MSWIN32 || MSWINCE */
! 729:
1.1 noro 730: #if defined(ALPHA) && defined(OSF1)
731:
732: #include <loader.h>
733:
734: void GC_register_dynamic_libraries()
735: {
736: int status;
737: ldr_process_t mypid;
738:
739: /* module */
740: ldr_module_t moduleid = LDR_NULL_MODULE;
741: ldr_module_info_t moduleinfo;
742: size_t moduleinfosize = sizeof(moduleinfo);
743: size_t modulereturnsize;
744:
745: /* region */
746: ldr_region_t region;
747: ldr_region_info_t regioninfo;
748: size_t regioninfosize = sizeof(regioninfo);
749: size_t regionreturnsize;
750:
751: /* Obtain id of this process */
752: mypid = ldr_my_process();
753:
754: /* For each module */
755: while (TRUE) {
756:
757: /* Get the next (first) module */
758: status = ldr_next_module(mypid, &moduleid);
759:
760: /* Any more modules? */
761: if (moduleid == LDR_NULL_MODULE)
762: break; /* No more modules */
763:
764: /* Check status AFTER checking moduleid because */
765: /* of a bug in the non-shared ldr_next_module stub */
766: if (status != 0 ) {
767: GC_printf1("dynamic_load: status = %ld\n", (long)status);
768: {
769: extern char *sys_errlist[];
770: extern int sys_nerr;
771: extern int errno;
772: if (errno <= sys_nerr) {
773: GC_printf1("dynamic_load: %s\n", (long)sys_errlist[errno]);
774: } else {
775: GC_printf1("dynamic_load: %d\n", (long)errno);
776: }
777: }
778: ABORT("ldr_next_module failed");
779: }
780:
781: /* Get the module information */
782: status = ldr_inq_module(mypid, moduleid, &moduleinfo,
783: moduleinfosize, &modulereturnsize);
784: if (status != 0 )
785: ABORT("ldr_inq_module failed");
786:
787: /* is module for the main program (i.e. nonshared portion)? */
788: if (moduleinfo.lmi_flags & LDR_MAIN)
789: continue; /* skip the main module */
790:
791: # ifdef VERBOSE
792: GC_printf("---Module---\n");
793: GC_printf("Module ID = %16ld\n", moduleinfo.lmi_modid);
794: GC_printf("Count of regions = %16d\n", moduleinfo.lmi_nregion);
795: GC_printf("flags for module = %16lx\n", moduleinfo.lmi_flags);
796: GC_printf("pathname of module = \"%s\"\n", moduleinfo.lmi_name);
797: # endif
798:
799: /* For each region in this module */
800: for (region = 0; region < moduleinfo.lmi_nregion; region++) {
801:
802: /* Get the region information */
803: status = ldr_inq_region(mypid, moduleid, region, ®ioninfo,
804: regioninfosize, ®ionreturnsize);
805: if (status != 0 )
806: ABORT("ldr_inq_region failed");
807:
808: /* only process writable (data) regions */
809: if (! (regioninfo.lri_prot & LDR_W))
810: continue;
811:
812: # ifdef VERBOSE
813: GC_printf("--- Region ---\n");
814: GC_printf("Region number = %16ld\n",
815: regioninfo.lri_region_no);
816: GC_printf("Protection flags = %016x\n", regioninfo.lri_prot);
817: GC_printf("Virtual address = %16p\n", regioninfo.lri_vaddr);
818: GC_printf("Mapped address = %16p\n", regioninfo.lri_mapaddr);
819: GC_printf("Region size = %16ld\n", regioninfo.lri_size);
820: GC_printf("Region name = \"%s\"\n", regioninfo.lri_name);
821: # endif
822:
823: /* register region as a garbage collection root */
824: GC_add_roots_inner (
825: (char *)regioninfo.lri_mapaddr,
826: (char *)regioninfo.lri_mapaddr + regioninfo.lri_size,
827: TRUE);
828:
829: }
830: }
831: }
832: #endif
833:
1.2 noro 834: #if defined(HPUX)
1.1 noro 835:
836: #include <errno.h>
837: #include <dl.h>
838:
839: extern int errno;
840: extern char *sys_errlist[];
841: extern int sys_nerr;
842:
843: void GC_register_dynamic_libraries()
844: {
845: int status;
846: int index = 1; /* Ordinal position in shared library search list */
847: struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */
848:
849: /* For each dynamic library loaded */
850: while (TRUE) {
851:
852: /* Get info about next shared library */
853: status = shl_get(index, &shl_desc);
854:
855: /* Check if this is the end of the list or if some error occured */
856: if (status != 0) {
1.2 noro 857: # ifdef HPUX_THREADS
858: /* I've seen errno values of 0. The man page is not clear */
859: /* as to whether errno should get set on a -1 return. */
860: break;
861: # else
1.1 noro 862: if (errno == EINVAL) {
863: break; /* Moved past end of shared library list --> finished */
864: } else {
865: if (errno <= sys_nerr) {
866: GC_printf1("dynamic_load: %s\n", (long) sys_errlist[errno]);
867: } else {
868: GC_printf1("dynamic_load: %d\n", (long) errno);
869: }
870: ABORT("shl_get failed");
871: }
1.2 noro 872: # endif
1.1 noro 873: }
874:
875: # ifdef VERBOSE
876: GC_printf0("---Shared library---\n");
877: GC_printf1("\tfilename = \"%s\"\n", shl_desc->filename);
878: GC_printf1("\tindex = %d\n", index);
879: GC_printf1("\thandle = %08x\n",
880: (unsigned long) shl_desc->handle);
881: GC_printf1("\ttext seg. start = %08x\n", shl_desc->tstart);
882: GC_printf1("\ttext seg. end = %08x\n", shl_desc->tend);
883: GC_printf1("\tdata seg. start = %08x\n", shl_desc->dstart);
884: GC_printf1("\tdata seg. end = %08x\n", shl_desc->dend);
885: GC_printf1("\tref. count = %lu\n", shl_desc->ref_count);
886: # endif
887:
888: /* register shared library's data segment as a garbage collection root */
889: GC_add_roots_inner((char *) shl_desc->dstart,
890: (char *) shl_desc->dend, TRUE);
891:
892: index++;
893: }
894: }
1.2 noro 895: #endif /* HPUX */
1.1 noro 896:
897: #ifdef RS6000
898: #pragma alloca
899: #include <sys/ldr.h>
900: #include <sys/errno.h>
901: void GC_register_dynamic_libraries()
902: {
903: int len;
904: char *ldibuf;
905: int ldibuflen;
906: struct ld_info *ldi;
907:
908: ldibuf = alloca(ldibuflen = 8192);
909:
910: while ( (len = loadquery(L_GETINFO,ldibuf,ldibuflen)) < 0) {
911: if (errno != ENOMEM) {
912: ABORT("loadquery failed");
913: }
914: ldibuf = alloca(ldibuflen *= 2);
915: }
916:
917: ldi = (struct ld_info *)ldibuf;
918: while (ldi) {
919: len = ldi->ldinfo_next;
920: GC_add_roots_inner(
921: ldi->ldinfo_dataorg,
922: (unsigned long)ldi->ldinfo_dataorg
923: + ldi->ldinfo_datasize,
924: TRUE);
925: ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
926: }
927: }
928: #endif /* RS6000 */
929:
930:
931:
932: #else /* !DYNAMIC_LOADING */
933:
934: #ifdef PCR
935:
936: # include "il/PCR_IL.h"
937: # include "th/PCR_ThCtl.h"
938: # include "mm/PCR_MM.h"
939:
940: void GC_register_dynamic_libraries()
941: {
942: /* Add new static data areas of dynamically loaded modules. */
943: {
944: PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile();
945: PCR_IL_LoadedSegment * q;
946:
947: /* Skip uncommited files */
948: while (p != NIL && !(p -> lf_commitPoint)) {
949: /* The loading of this file has not yet been committed */
950: /* Hence its description could be inconsistent. */
951: /* Furthermore, it hasn't yet been run. Hence its data */
952: /* segments can't possibly reference heap allocated */
953: /* objects. */
954: p = p -> lf_prev;
955: }
956: for (; p != NIL; p = p -> lf_prev) {
957: for (q = p -> lf_ls; q != NIL; q = q -> ls_next) {
958: if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK)
959: == PCR_IL_SegFlags_Traced_on) {
960: GC_add_roots_inner
961: ((char *)(q -> ls_addr),
962: (char *)(q -> ls_addr) + q -> ls_bytes,
963: TRUE);
964: }
965: }
966: }
967: }
968: }
969:
970:
971: #else /* !PCR */
972:
973: void GC_register_dynamic_libraries(){}
974:
975: int GC_no_dynamic_loading;
976:
977: #endif /* !PCR */
978: #endif /* !DYNAMIC_LOADING */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>