[corlib] Fix a few NSLogWriter issues. (#13799)
[mono-project.git] / libgc / dyn_load.c
blobc414c8788cc8ae5a9087a777c2a3bd2b75bcd434
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(__GLIBC__) || defined(__native_client__)) && !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(NACL)) && defined(__ELF__)) && \
58 !defined(RS6000) && !defined(SCO_ELF) && !defined(DGUX) && \
59 !(defined(FREEBSD) && defined(__ELF__)) && \
60 !(defined(OPENBSD) && (defined(__ELF__) || defined(M68K))) && \
61 !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD) && \
62 !defined(DARWIN) && !defined(HAIKU)
63 --> We only know how to find data segments of dynamic libraries for the
64 --> above. Additional SVR4 variants might not be too
65 --> hard to add.
66 #endif
68 #include <stdio.h>
69 #ifdef SUNOS5DL
70 /* Avoid #error "large files are not supported by libelf" errors */
71 #if defined(_ILP32) && (_FILE_OFFSET_BITS != 32)
72 #undef _FILE_OFFSET_BITS
73 #define _FILE_OFFSET_BITS 32
74 #endif
75 # include <sys/elf.h>
76 # include <dlfcn.h>
77 # include <link.h>
78 #endif
79 #ifdef SUNOS4
80 # include <dlfcn.h>
81 # include <link.h>
82 # include <a.out.h>
83 /* struct link_map field overrides */
84 # define l_next lm_next
85 # define l_addr lm_addr
86 # define l_name lm_name
87 #endif
89 #if defined(NETBSD)
90 # include <machine/elf_machdep.h>
91 # define ELFSIZE ARCH_ELFSIZE
92 #endif
94 #if (defined(LINUX) || defined(NACL)) && defined(__ELF__) || defined(SCO_ELF) || \
95 (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
96 (defined(OPENBSD) && defined(__ELF__)) || \
97 (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
98 # include <stddef.h>
99 # if !defined(OPENBSD)
100 # include <elf.h>
101 # endif
102 # include <link.h>
103 #endif
105 /* Newer versions of GNU/Linux define this macro. We
106 * define it similarly for any ELF systems that don't. */
107 # ifndef ElfW
108 # if defined(FREEBSD)
109 # if __ELF_WORD_SIZE == 32
110 # define ElfW(type) Elf32_##type
111 # else
112 # define ElfW(type) Elf64_##type
113 # endif
114 # else
115 # ifdef NETBSD
116 # if ELFSIZE == 32
117 # define ElfW(type) Elf32_##type
118 # else
119 # define ElfW(type) Elf64_##type
120 # endif
121 # else
122 # if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
123 # define ElfW(type) Elf32_##type
124 # else
125 # define ElfW(type) Elf64_##type
126 # endif
127 # endif
128 # endif
129 # endif
131 #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)
133 #ifdef LINT
134 Elf32_Dyn _DYNAMIC;
135 #endif
137 static struct link_map *
138 GC_FirstDLOpenedLinkMap()
140 extern ElfW(Dyn) _DYNAMIC;
141 ElfW(Dyn) *dp;
142 struct r_debug *r;
143 static struct link_map * cachedResult = 0;
144 static ElfW(Dyn) *dynStructureAddr = 0;
145 /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
147 # ifdef SUNOS53_SHARED_LIB
148 /* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
149 /* up properly in dynamically linked .so's. This means we have */
150 /* to use its value in the set of original object files loaded */
151 /* at program startup. */
152 if( dynStructureAddr == 0 ) {
153 void* startupSyms = dlopen(0, RTLD_LAZY);
154 dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
156 # else
157 dynStructureAddr = &_DYNAMIC;
158 # endif
160 if( dynStructureAddr == 0) {
161 return(0);
163 if( cachedResult == 0 ) {
164 int tag;
165 for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
166 if( tag == DT_DEBUG ) {
167 struct link_map *lm
168 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
169 if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
170 break;
174 return cachedResult;
177 #endif /* SUNOS5DL ... */
179 /* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
180 # if defined(GC_must_restore_redefined_dlopen)
181 # define dlopen GC_dlopen
182 # endif
184 #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)
186 #ifdef LINT
187 struct link_dynamic _DYNAMIC;
188 #endif
190 static struct link_map *
191 GC_FirstDLOpenedLinkMap()
193 extern struct link_dynamic _DYNAMIC;
195 if( &_DYNAMIC == 0) {
196 return(0);
198 return(_DYNAMIC.ld_un.ld_1->ld_loaded);
201 /* Return the address of the ld.so allocated common symbol */
202 /* with the least address, or 0 if none. */
203 static ptr_t GC_first_common()
205 ptr_t result = 0;
206 extern struct link_dynamic _DYNAMIC;
207 struct rtc_symb * curr_symbol;
209 if( &_DYNAMIC == 0) {
210 return(0);
212 curr_symbol = _DYNAMIC.ldd -> ldd_cp;
213 for (; curr_symbol != 0; curr_symbol = curr_symbol -> rtc_next) {
214 if (result == 0
215 || (ptr_t)(curr_symbol -> rtc_sp -> n_value) < result) {
216 result = (ptr_t)(curr_symbol -> rtc_sp -> n_value);
219 return(result);
222 #endif /* SUNOS4 ... */
224 # if defined(SUNOS4) || defined(SUNOS5DL)
225 /* Add dynamic library data sections to the root set. */
226 # if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS)
227 # ifndef SRC_M3
228 --> fix mutual exclusion with dlopen
229 # endif /* We assume M3 programs don't call dlopen for now */
230 # endif
232 # ifndef USE_PROC_FOR_LIBRARIES
233 void GC_register_dynamic_libraries()
235 struct link_map *lm = GC_FirstDLOpenedLinkMap();
238 for (lm = GC_FirstDLOpenedLinkMap();
239 lm != (struct link_map *) 0; lm = lm->l_next)
241 # ifdef SUNOS4
242 struct exec *e;
244 e = (struct exec *) lm->lm_addr;
245 GC_add_roots_inner(
246 ((char *) (N_DATOFF(*e) + lm->lm_addr)),
247 ((char *) (N_BSSADDR(*e) + e->a_bss + lm->lm_addr)),
248 TRUE);
249 # endif
250 # ifdef SUNOS5DL
251 ElfW(Ehdr) * e;
252 ElfW(Phdr) * p;
253 unsigned long offset;
254 char * start;
255 register int i;
257 e = (ElfW(Ehdr) *) lm->l_addr;
258 if (e == NULL)
259 continue;
261 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
262 offset = ((unsigned long)(lm->l_addr));
263 for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
264 switch( p->p_type ) {
265 case PT_LOAD:
267 if( !(p->p_flags & PF_W) ) break;
268 start = ((char *)(p->p_vaddr)) + offset;
269 GC_add_roots_inner(
270 start,
271 start + p->p_memsz,
272 TRUE
275 break;
276 default:
277 break;
280 # endif
282 # ifdef SUNOS4
284 static ptr_t common_start = 0;
285 ptr_t common_end;
286 extern ptr_t GC_find_limit();
288 if (common_start == 0) common_start = GC_first_common();
289 if (common_start != 0) {
290 common_end = GC_find_limit(common_start, TRUE);
291 GC_add_roots_inner((char *)common_start, (char *)common_end, TRUE);
294 # endif
297 # endif /* !USE_PROC ... */
298 # endif /* SUNOS */
300 #if (defined(LINUX) || defined(NACL)) && defined(__ELF__) || defined(SCO_ELF) || \
301 (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
302 (defined(OPENBSD) && defined(__ELF__)) || \
303 (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
306 #ifdef USE_PROC_FOR_LIBRARIES
308 #include <string.h>
310 #include <sys/stat.h>
311 #include <fcntl.h>
312 #include <unistd.h>
314 #define MAPS_BUF_SIZE (32*1024)
316 extern ssize_t GC_repeat_read(int fd, char *buf, size_t count);
317 /* Repeatedly read until buffer is filled, or EOF is encountered */
318 /* Defined in os_dep.c. */
320 char *GC_parse_map_entry(char *buf_ptr, word *start, word *end,
321 char *prot_buf, unsigned int *maj_dev);
322 word GC_apply_to_maps(word (*fn)(char *));
323 /* From os_dep.c */
325 word GC_register_map_entries(char *maps)
327 char prot_buf[5];
328 char *buf_ptr = maps;
329 int count;
330 word start, end;
331 unsigned int maj_dev;
332 word least_ha, greatest_ha;
333 unsigned i;
334 word datastart = (word)(DATASTART);
336 /* Compute heap bounds. FIXME: Should be done by add_to_heap? */
337 least_ha = (word)(-1);
338 greatest_ha = 0;
339 for (i = 0; i < GC_n_heap_sects; ++i) {
340 word sect_start = (word)GC_heap_sects[i].hs_start;
341 word sect_end = sect_start + GC_heap_sects[i].hs_bytes;
342 if (sect_start < least_ha) least_ha = sect_start;
343 if (sect_end > greatest_ha) greatest_ha = sect_end;
345 if (greatest_ha < (word)GC_scratch_last_end_ptr)
346 greatest_ha = (word)GC_scratch_last_end_ptr;
348 for (;;) {
349 buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
350 if (buf_ptr == NULL) return 1;
351 if (prot_buf[1] == 'w') {
352 /* This is a writable mapping. Add it to */
353 /* the root set unless it is already otherwise */
354 /* accounted for. */
355 if (start <= (word)GC_stackbottom && end >= (word)GC_stackbottom) {
356 /* Stack mapping; discard */
357 continue;
359 # ifdef THREADS
360 if (GC_segment_is_thread_stack(start, end)) continue;
361 # endif
362 /* We no longer exclude the main data segment. */
363 if (start < least_ha && end > least_ha) {
364 end = least_ha;
366 if (start < greatest_ha && end > greatest_ha) {
367 start = greatest_ha;
369 if (start >= least_ha && end <= greatest_ha) continue;
370 GC_add_roots_inner((char *)start, (char *)end, TRUE);
373 return 1;
376 void GC_register_dynamic_libraries()
378 if (!GC_apply_to_maps(GC_register_map_entries))
379 ABORT("Failed to read /proc for library registration.");
382 /* We now take care of the main data segment ourselves: */
383 GC_bool GC_register_main_static_data()
385 return FALSE;
388 # define HAVE_REGISTER_MAIN_STATIC_DATA
390 #endif /* USE_PROC_FOR_LIBRARIES */
392 #if !defined(USE_PROC_FOR_LIBRARIES)
393 /* The following is the preferred way to walk dynamic libraries */
394 /* For glibc 2.2.4+. Unfortunately, it doesn't work for older */
395 /* versions. Thanks to Jakub Jelinek for most of the code. */
397 # if (defined(LINUX) || defined (__GLIBC__) || defined(NACL)) /* Are others OK here, too? */ \
398 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
399 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))) \
400 || defined(OPENBSD)
402 /* We have the header files for a glibc that includes dl_iterate_phdr. */
403 /* It may still not be available in the library on the target system. */
404 /* Thus we also treat it as a weak symbol. */
405 #define HAVE_DL_ITERATE_PHDR
407 static int GC_register_dynlib_callback(info, size, ptr)
408 struct dl_phdr_info * info;
409 size_t size;
410 void * ptr;
412 const ElfW(Phdr) * p;
413 char * start;
414 register int i;
416 /* Make sure struct dl_phdr_info is at least as big as we need. */
417 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
418 + sizeof (info->dlpi_phnum))
419 return -1;
421 p = info->dlpi_phdr;
422 for( i = 0; i < (int)(info->dlpi_phnum); ((i++),(p++)) ) {
423 switch( p->p_type ) {
424 case PT_LOAD:
426 if( !(p->p_flags & PF_W) ) break;
427 start = ((char *)(p->p_vaddr)) + info->dlpi_addr;
428 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
430 break;
431 default:
432 break;
436 * (int *)ptr = 1; /* Signal that we were called */
437 return 0;
440 /* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
442 #pragma weak dl_iterate_phdr
444 GC_bool GC_register_dynamic_libraries_dl_iterate_phdr()
446 if (dl_iterate_phdr) {
447 int did_something = 0;
448 dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
449 if (!did_something) {
450 /* dl_iterate_phdr may forget the static data segment in */
451 /* statically linked executables. */
452 GC_add_roots_inner(DATASTART, (char *)(DATAEND), TRUE);
453 # if defined(DATASTART2)
454 GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);
455 # endif
458 return TRUE;
459 } else {
460 return FALSE;
464 /* Do we need to separately register the main static data segment? */
465 GC_bool GC_register_main_static_data()
467 return (dl_iterate_phdr == 0);
470 #define HAVE_REGISTER_MAIN_STATIC_DATA
472 # else /* !LINUX || version(glibc) < 2.2.4 */
474 /* Dynamic loading code for Linux running ELF. Somewhat tested on
475 * Linux/x86, untested but hopefully should work on Linux/Alpha.
476 * This code was derived from the Solaris/ELF support. Thanks to
477 * whatever kind soul wrote that. - Patrick Bridges */
479 /* This doesn't necessarily work in all cases, e.g. with preloaded
480 * dynamic libraries. */
482 #if defined(NETBSD) || defined(OPENBSD)
483 # if !defined(OPENBSD)
484 # include <sys/exec_elf.h>
485 # endif
486 /* for compatibility with 1.4.x */
487 # ifndef DT_DEBUG
488 # define DT_DEBUG 21
489 # endif
490 # ifndef PT_LOAD
491 # define PT_LOAD 1
492 # endif
493 # ifndef PF_W
494 # define PF_W 2
495 # endif
496 #else
497 # include <elf.h>
498 #endif
499 #include <link.h>
501 # endif
503 #ifdef __GNUC__
504 # pragma weak _DYNAMIC
505 #endif
506 extern ElfW(Dyn) _DYNAMIC[];
508 static struct link_map *
509 GC_FirstDLOpenedLinkMap()
511 ElfW(Dyn) *dp;
512 static struct link_map *cachedResult = 0;
514 if( _DYNAMIC == 0) {
515 return(0);
517 if( cachedResult == 0 ) {
518 int tag;
519 for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
520 /* FIXME: The DT_DEBUG header is not mandated by the */
521 /* ELF spec. This code appears to be dependent on */
522 /* idiosynchracies of older GNU tool chains. If this code */
523 /* fails for you, the real problem is probably that it is */
524 /* being used at all. You should be getting the */
525 /* dl_iterate_phdr version. */
526 if( tag == DT_DEBUG ) {
527 struct link_map *lm
528 = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
529 if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
530 break;
534 return cachedResult;
538 void GC_register_dynamic_libraries()
540 struct link_map *lm;
543 # ifdef HAVE_DL_ITERATE_PHDR
544 if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
545 return;
547 # endif
548 lm = GC_FirstDLOpenedLinkMap();
549 for (lm = GC_FirstDLOpenedLinkMap();
550 lm != (struct link_map *) 0; lm = lm->l_next)
552 ElfW(Ehdr) * e;
553 ElfW(Phdr) * p;
554 unsigned long offset;
555 char * start;
556 register int i;
558 e = (ElfW(Ehdr) *) lm->l_addr;
559 if (e == NULL)
560 continue;
562 p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
563 offset = ((unsigned long)(lm->l_addr));
564 for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
565 switch( p->p_type ) {
566 case PT_LOAD:
568 if( !(p->p_flags & PF_W) ) break;
569 start = ((char *)(p->p_vaddr)) + offset;
570 GC_add_roots_inner(start, start + p->p_memsz, TRUE);
572 break;
573 default:
574 break;
580 #endif /* !USE_PROC_FOR_LIBRARIES */
582 #endif /* LINUX */
584 #if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))
586 #include <sys/procfs.h>
587 #include <sys/stat.h>
588 #include <fcntl.h>
589 #include <elf.h>
590 #include <errno.h>
591 #include <signal.h> /* Only for the following test. */
592 #ifndef _sigargs
593 # define IRIX6
594 #endif
596 extern void * GC_roots_present();
597 /* The type is a lie, since the real type doesn't make sense here, */
598 /* and we only test for NULL. */
601 /* We use /proc to track down all parts of the address space that are */
602 /* mapped by the process, and throw out regions we know we shouldn't */
603 /* worry about. This may also work under other SVR4 variants. */
604 void GC_register_dynamic_libraries()
606 static int fd = -1;
607 char buf[30];
608 static prmap_t * addr_map = 0;
609 static int current_sz = 0; /* Number of records currently in addr_map */
610 static int needed_sz; /* Required size of addr_map */
611 register int i;
612 register long flags;
613 register ptr_t start;
614 register ptr_t limit;
615 ptr_t heap_start = (ptr_t)HEAP_START;
616 ptr_t heap_end = heap_start;
618 # ifdef SUNOS5DL
619 # define MA_PHYS 0
620 # endif /* SUNOS5DL */
622 if (fd < 0) {
623 sprintf(buf, "/proc/%d", getpid());
624 /* The above generates a lint complaint, since pid_t varies. */
625 /* It's unclear how to improve this. */
626 fd = open(buf, O_RDONLY);
627 if (fd < 0) {
628 ABORT("/proc open failed");
631 if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
632 GC_err_printf2("fd = %d, errno = %d\n", fd, errno);
633 ABORT("/proc PIOCNMAP ioctl failed");
635 if (needed_sz >= current_sz) {
636 current_sz = needed_sz * 2 + 1;
637 /* Expansion, plus room for 0 record */
638 addr_map = (prmap_t *)GC_scratch_alloc((word)
639 (current_sz * sizeof(prmap_t)));
641 if (ioctl(fd, PIOCMAP, addr_map) < 0) {
642 GC_err_printf4("fd = %d, errno = %d, needed_sz = %d, addr_map = 0x%X\n",
643 fd, errno, needed_sz, addr_map);
644 ABORT("/proc PIOCMAP ioctl failed");
646 if (GC_n_heap_sects > 0) {
647 heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
648 + GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
649 if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;
651 for (i = 0; i < needed_sz; i++) {
652 flags = addr_map[i].pr_mflags;
653 if ((flags & (MA_BREAK | MA_STACK | MA_PHYS
654 | MA_FETCHOP | MA_NOTCACHED)) != 0) goto irrelevant;
655 if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
656 goto irrelevant;
657 /* The latter test is empirically useless in very old Irix */
658 /* versions. Other than the */
659 /* main data and stack segments, everything appears to be */
660 /* mapped readable, writable, executable, and shared(!!). */
661 /* This makes no sense to me. - HB */
662 start = (ptr_t)(addr_map[i].pr_vaddr);
663 if (GC_roots_present(start)) goto irrelevant;
664 if (start < heap_end && start >= heap_start)
665 goto irrelevant;
666 # ifdef MMAP_STACKS
667 if (GC_is_thread_stack(start)) goto irrelevant;
668 # endif /* MMAP_STACKS */
670 limit = start + addr_map[i].pr_size;
671 /* The following seemed to be necessary for very old versions */
672 /* of Irix, but it has been reported to discard relevant */
673 /* segments under Irix 6.5. */
674 # ifndef IRIX6
675 if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
676 /* Discard text segments, i.e. 0-offset mappings against */
677 /* executable files which appear to have ELF headers. */
678 caddr_t arg;
679 int obj;
680 # define MAP_IRR_SZ 10
681 static ptr_t map_irr[MAP_IRR_SZ];
682 /* Known irrelevant map entries */
683 static int n_irr = 0;
684 struct stat buf;
685 register int i;
687 for (i = 0; i < n_irr; i++) {
688 if (map_irr[i] == start) goto irrelevant;
690 arg = (caddr_t)start;
691 obj = ioctl(fd, PIOCOPENM, &arg);
692 if (obj >= 0) {
693 fstat(obj, &buf);
694 close(obj);
695 if ((buf.st_mode & 0111) != 0) {
696 if (n_irr < MAP_IRR_SZ) {
697 map_irr[n_irr++] = start;
699 goto irrelevant;
703 # endif /* !IRIX6 */
704 GC_add_roots_inner(start, limit, TRUE);
705 irrelevant: ;
707 /* Dont keep cached descriptor, for now. Some kernels don't like us */
708 /* to keep a /proc file descriptor around during kill -9. */
709 if (close(fd) < 0) ABORT("Couldnt close /proc file");
710 fd = -1;
713 # endif /* USE_PROC || IRIX5 */
715 # if defined(MSWIN32) || defined(MSWINCE)
717 # define WIN32_LEAN_AND_MEAN
718 # define NOSERVICE
719 # include <windows.h>
720 # include <stdlib.h>
722 /* We traverse the entire address space and register all segments */
723 /* that could possibly have been written to. */
725 extern GC_bool GC_is_heap_base (ptr_t p);
727 # ifdef GC_WIN32_THREADS
728 extern void GC_get_next_stack(char *start, char **lo, char **hi);
729 void GC_cond_add_roots(char *base, char * limit)
731 char * curr_base = base;
732 char * next_stack_lo;
733 char * next_stack_hi;
735 if (base == limit) return;
736 for(;;) {
737 GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);
738 if (next_stack_lo >= limit) break;
739 GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
740 curr_base = next_stack_hi;
742 if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
744 # else
745 void GC_cond_add_roots(char *base, char * limit)
747 char dummy;
748 char * stack_top
749 = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));
750 if (base == limit) return;
751 if (limit > stack_top && base < GC_stackbottom) {
752 /* Part of the stack; ignore it. */
753 return;
755 GC_add_roots_inner(base, limit, TRUE);
757 # endif
759 # ifdef MSWINCE
760 /* Do we need to separately register the main static data segment? */
761 GC_bool GC_register_main_static_data()
763 return FALSE;
765 # else /* win32 */
766 extern GC_bool GC_no_win32_dlls;
768 GC_bool GC_register_main_static_data()
770 return GC_no_win32_dlls;
772 # endif /* win32 */
774 # define HAVE_REGISTER_MAIN_STATIC_DATA
776 /* The frame buffer testing code is dead in this version. */
777 /* We leave it here temporarily in case the switch to just */
778 /* testing for MEM_IMAGE sections causes un expected */
779 /* problems. */
780 GC_bool GC_warn_fb = TRUE; /* Warn about traced likely */
781 /* graphics memory. */
782 GC_bool GC_disallow_ignore_fb = FALSE;
783 int GC_ignore_fb_mb; /* Ignore mappings bigger than the */
784 /* specified number of MB. */
785 GC_bool GC_ignore_fb = FALSE; /* Enable frame buffer */
786 /* checking. */
788 /* Issue warning if tracing apparent framebuffer. */
789 /* This limits us to one warning, and it's a back door to */
790 /* disable that. */
792 /* Should [start, start+len) be treated as a frame buffer */
793 /* and ignored? */
794 /* Unfortunately, we currently are not quite sure how to tell */
795 /* this automatically, and rely largely on user input. */
796 /* We expect that any mapping with type MEM_MAPPED (which */
797 /* apparently excludes library data sections) can be safely */
798 /* ignored. But we're too chicken to do that in this */
799 /* version. */
800 /* Based on a very limited sample, it appears that: */
801 /* - Frame buffer mappings appear as mappings of large */
802 /* length, usually a bit less than a power of two. */
803 /* - The definition of "a bit less" in the above cannot */
804 /* be made more precise. */
805 /* - Have a starting address at best 64K aligned. */
806 /* - Have type == MEM_MAPPED. */
807 static GC_bool is_frame_buffer(ptr_t start, size_t len, DWORD tp)
809 static GC_bool initialized = FALSE;
810 # define MB (1024*1024)
811 # define DEFAULT_FB_MB 15
812 # define MIN_FB_MB 3
814 if (GC_disallow_ignore_fb || tp != MEM_MAPPED) return FALSE;
815 if (!initialized) {
816 char * ignore_fb_string = GETENV("GC_IGNORE_FB");
818 if (0 != ignore_fb_string) {
819 while (*ignore_fb_string == ' ' || *ignore_fb_string == '\t')
820 ++ignore_fb_string;
821 if (*ignore_fb_string == '\0') {
822 GC_ignore_fb_mb = DEFAULT_FB_MB;
823 } else {
824 GC_ignore_fb_mb = atoi(ignore_fb_string);
825 if (GC_ignore_fb_mb < MIN_FB_MB) {
826 WARN("Bad GC_IGNORE_FB value. Using %ld\n", DEFAULT_FB_MB);
827 GC_ignore_fb_mb = DEFAULT_FB_MB;
830 GC_ignore_fb = TRUE;
831 } else {
832 GC_ignore_fb_mb = DEFAULT_FB_MB; /* For warning */
834 initialized = TRUE;
836 if (len >= ((size_t)GC_ignore_fb_mb << 20)) {
837 if (GC_ignore_fb) {
838 return TRUE;
839 } else {
840 if (GC_warn_fb) {
841 WARN("Possible frame buffer mapping at 0x%lx: \n"
842 "\tConsider setting GC_IGNORE_FB to improve performance.\n",
843 start);
844 GC_warn_fb = FALSE;
846 return FALSE;
848 } else {
849 return FALSE;
853 # ifdef DEBUG_VIRTUALQUERY
854 void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf)
856 GC_printf4("BaseAddress = %lx, AllocationBase = %lx, RegionSize = %lx(%lu)\n",
857 buf -> BaseAddress, buf -> AllocationBase, buf -> RegionSize,
858 buf -> RegionSize);
859 GC_printf4("\tAllocationProtect = %lx, State = %lx, Protect = %lx, "
860 "Type = %lx\n",
861 buf -> AllocationProtect, buf -> State, buf -> Protect,
862 buf -> Type);
864 # endif /* DEBUG_VIRTUALQUERY */
866 void GC_register_dynamic_libraries()
868 MEMORY_BASIC_INFORMATION buf;
869 DWORD result;
870 DWORD protect;
871 LPVOID p;
872 char * base;
873 char * limit, * new_limit;
875 # ifdef MSWIN32
876 if (GC_no_win32_dlls) return;
877 # endif
878 base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
879 # if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
880 /* Only the first 32 MB of address space belongs to the current process */
881 while (p < (LPVOID)0x02000000) {
882 result = VirtualQuery(p, &buf, sizeof(buf));
883 if (result == 0) {
884 /* Page is free; advance to the next possible allocation base */
885 new_limit = (char *)
886 (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
887 & ~(GC_sysinfo.dwAllocationGranularity-1));
888 } else
889 # else
890 while (p < GC_sysinfo.lpMaximumApplicationAddress) {
891 result = VirtualQuery(p, &buf, sizeof(buf));
892 # endif
894 if (result != sizeof(buf)) {
895 ABORT("Weird VirtualQuery result");
897 new_limit = (char *)p + buf.RegionSize;
898 protect = buf.Protect;
899 if (buf.State == MEM_COMMIT
900 && (protect == PAGE_EXECUTE_READWRITE
901 || protect == PAGE_READWRITE)
902 && !GC_is_heap_base(buf.AllocationBase)
903 /* This used to check for
904 * !is_frame_buffer(p, buf.RegionSize, buf.Type)
905 * instead of just checking for MEM_IMAGE.
906 * If something breaks, change it back. */
907 && buf.Type == MEM_IMAGE) {
908 # ifdef DEBUG_VIRTUALQUERY
909 GC_dump_meminfo(&buf);
910 # endif
911 if ((char *)p != limit) {
912 GC_cond_add_roots(base, limit);
913 base = p;
915 limit = new_limit;
918 if (p > (LPVOID)new_limit /* overflow */) break;
919 p = (LPVOID)new_limit;
921 GC_cond_add_roots(base, limit);
924 #endif /* MSWIN32 || MSWINCE */
926 #if defined(ALPHA) && defined(OSF1)
928 #include <loader.h>
930 void GC_register_dynamic_libraries()
932 int status;
933 ldr_process_t mypid;
935 /* module */
936 ldr_module_t moduleid = LDR_NULL_MODULE;
937 ldr_module_info_t moduleinfo;
938 size_t moduleinfosize = sizeof(moduleinfo);
939 size_t modulereturnsize;
941 /* region */
942 ldr_region_t region;
943 ldr_region_info_t regioninfo;
944 size_t regioninfosize = sizeof(regioninfo);
945 size_t regionreturnsize;
947 /* Obtain id of this process */
948 mypid = ldr_my_process();
950 /* For each module */
951 while (TRUE) {
953 /* Get the next (first) module */
954 status = ldr_next_module(mypid, &moduleid);
956 /* Any more modules? */
957 if (moduleid == LDR_NULL_MODULE)
958 break; /* No more modules */
960 /* Check status AFTER checking moduleid because */
961 /* of a bug in the non-shared ldr_next_module stub */
962 if (status != 0 ) {
963 GC_printf1("dynamic_load: status = %ld\n", (long)status);
965 extern char *sys_errlist[];
966 extern int sys_nerr;
967 extern int errno;
968 if (errno <= sys_nerr) {
969 GC_printf1("dynamic_load: %s\n", (long)sys_errlist[errno]);
970 } else {
971 GC_printf1("dynamic_load: %d\n", (long)errno);
974 ABORT("ldr_next_module failed");
977 /* Get the module information */
978 status = ldr_inq_module(mypid, moduleid, &moduleinfo,
979 moduleinfosize, &modulereturnsize);
980 if (status != 0 )
981 ABORT("ldr_inq_module failed");
983 /* is module for the main program (i.e. nonshared portion)? */
984 if (moduleinfo.lmi_flags & LDR_MAIN)
985 continue; /* skip the main module */
987 # ifdef VERBOSE
988 GC_printf("---Module---\n");
989 GC_printf("Module ID = %16ld\n", moduleinfo.lmi_modid);
990 GC_printf("Count of regions = %16d\n", moduleinfo.lmi_nregion);
991 GC_printf("flags for module = %16lx\n", moduleinfo.lmi_flags);
992 GC_printf("pathname of module = \"%s\"\n", moduleinfo.lmi_name);
993 # endif
995 /* For each region in this module */
996 for (region = 0; region < moduleinfo.lmi_nregion; region++) {
998 /* Get the region information */
999 status = ldr_inq_region(mypid, moduleid, region, &regioninfo,
1000 regioninfosize, &regionreturnsize);
1001 if (status != 0 )
1002 ABORT("ldr_inq_region failed");
1004 /* only process writable (data) regions */
1005 if (! (regioninfo.lri_prot & LDR_W))
1006 continue;
1008 # ifdef VERBOSE
1009 GC_printf("--- Region ---\n");
1010 GC_printf("Region number = %16ld\n",
1011 regioninfo.lri_region_no);
1012 GC_printf("Protection flags = %016x\n", regioninfo.lri_prot);
1013 GC_printf("Virtual address = %16p\n", regioninfo.lri_vaddr);
1014 GC_printf("Mapped address = %16p\n", regioninfo.lri_mapaddr);
1015 GC_printf("Region size = %16ld\n", regioninfo.lri_size);
1016 GC_printf("Region name = \"%s\"\n", regioninfo.lri_name);
1017 # endif
1019 /* register region as a garbage collection root */
1020 GC_add_roots_inner (
1021 (char *)regioninfo.lri_mapaddr,
1022 (char *)regioninfo.lri_mapaddr + regioninfo.lri_size,
1023 TRUE);
1028 #endif
1030 #if defined(HPUX)
1032 #include <errno.h>
1033 #include <dl.h>
1035 extern int errno;
1036 extern char *sys_errlist[];
1037 extern int sys_nerr;
1039 void GC_register_dynamic_libraries()
1041 int status;
1042 int index = 1; /* Ordinal position in shared library search list */
1043 struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */
1045 /* For each dynamic library loaded */
1046 while (TRUE) {
1048 /* Get info about next shared library */
1049 status = shl_get(index, &shl_desc);
1051 /* Check if this is the end of the list or if some error occured */
1052 if (status != 0) {
1053 # ifdef GC_HPUX_THREADS
1054 /* I've seen errno values of 0. The man page is not clear */
1055 /* as to whether errno should get set on a -1 return. */
1056 break;
1057 # else
1058 if (errno == EINVAL) {
1059 break; /* Moved past end of shared library list --> finished */
1060 } else {
1061 if (errno <= sys_nerr) {
1062 GC_printf1("dynamic_load: %s\n", (long) sys_errlist[errno]);
1063 } else {
1064 GC_printf1("dynamic_load: %d\n", (long) errno);
1066 ABORT("shl_get failed");
1068 # endif
1071 # ifdef VERBOSE
1072 GC_printf0("---Shared library---\n");
1073 GC_printf1("\tfilename = \"%s\"\n", shl_desc->filename);
1074 GC_printf1("\tindex = %d\n", index);
1075 GC_printf1("\thandle = %08x\n",
1076 (unsigned long) shl_desc->handle);
1077 GC_printf1("\ttext seg. start = %08x\n", shl_desc->tstart);
1078 GC_printf1("\ttext seg. end = %08x\n", shl_desc->tend);
1079 GC_printf1("\tdata seg. start = %08x\n", shl_desc->dstart);
1080 GC_printf1("\tdata seg. end = %08x\n", shl_desc->dend);
1081 GC_printf1("\tref. count = %lu\n", shl_desc->ref_count);
1082 # endif
1084 /* register shared library's data segment as a garbage collection root */
1085 GC_add_roots_inner((char *) shl_desc->dstart,
1086 (char *) shl_desc->dend, TRUE);
1088 index++;
1091 #endif /* HPUX */
1093 #ifdef RS6000
1094 #pragma alloca
1095 #include <sys/ldr.h>
1096 #include <sys/errno.h>
1097 void GC_register_dynamic_libraries()
1099 int len;
1100 char *ldibuf;
1101 int ldibuflen;
1102 struct ld_info *ldi;
1104 ldibuf = alloca(ldibuflen = 8192);
1106 while ( (len = loadquery(L_GETINFO,ldibuf,ldibuflen)) < 0) {
1107 if (errno != ENOMEM) {
1108 ABORT("loadquery failed");
1110 ldibuf = alloca(ldibuflen *= 2);
1113 ldi = (struct ld_info *)ldibuf;
1114 while (ldi) {
1115 len = ldi->ldinfo_next;
1116 GC_add_roots_inner(
1117 ldi->ldinfo_dataorg,
1118 (ptr_t)(unsigned long)ldi->ldinfo_dataorg
1119 + ldi->ldinfo_datasize,
1120 TRUE);
1121 ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
1124 #endif /* RS6000 */
1126 #ifdef DARWIN
1128 /* __private_extern__ hack required for pre-3.4 gcc versions. */
1129 #ifndef __private_extern__
1130 # define __private_extern__ extern
1131 # include <mach-o/dyld.h>
1132 # undef __private_extern__
1133 #else
1134 # include <mach-o/dyld.h>
1135 #endif
1136 #include <mach-o/getsect.h>
1138 /*#define DARWIN_DEBUG*/
1140 const static struct {
1141 const char *seg;
1142 const char *sect;
1143 } GC_dyld_sections[] = {
1144 { SEG_DATA, SECT_DATA },
1145 { SEG_DATA, SECT_BSS },
1146 { SEG_DATA, SECT_COMMON }
1149 #ifdef DARWIN_DEBUG
1150 static const char *GC_dyld_name_for_hdr(struct mach_header *hdr) {
1151 unsigned long i,c;
1152 c = _dyld_image_count();
1153 for(i=0;i<c;i++) if(_dyld_get_image_header(i) == hdr)
1154 return _dyld_get_image_name(i);
1155 return NULL;
1157 #endif
1159 /* This should never be called by a thread holding the lock */
1160 static void GC_dyld_image_add(struct mach_header* hdr, unsigned long slide) {
1161 unsigned long start,end,i;
1162 const struct section *sec;
1163 if (GC_no_dls) return;
1164 for(i=0;i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]);i++) {
1165 sec = getsectbynamefromheader(
1166 hdr,GC_dyld_sections[i].seg,GC_dyld_sections[i].sect);
1167 if(sec == NULL || sec->size == 0) continue;
1168 start = slide + sec->addr;
1169 end = start + sec->size;
1170 # ifdef DARWIN_DEBUG
1171 GC_printf4("Adding section at %p-%p (%lu bytes) from image %s\n",
1172 start,end,sec->size,GC_dyld_name_for_hdr(hdr));
1173 # endif
1174 GC_add_roots((char*)start,(char*)end);
1176 # ifdef DARWIN_DEBUG
1177 GC_print_static_roots();
1178 # endif
1181 /* This should never be called by a thread holding the lock */
1182 static void GC_dyld_image_remove(struct mach_header* hdr, unsigned long slide) {
1183 unsigned long start,end,i;
1184 const struct section *sec;
1185 for(i=0;i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]);i++) {
1186 sec = getsectbynamefromheader(
1187 hdr,GC_dyld_sections[i].seg,GC_dyld_sections[i].sect);
1188 if(sec == NULL || sec->size == 0) continue;
1189 start = slide + sec->addr;
1190 end = start + sec->size;
1191 # ifdef DARWIN_DEBUG
1192 GC_printf4("Removing section at %p-%p (%lu bytes) from image %s\n",
1193 start,end,sec->size,GC_dyld_name_for_hdr(hdr));
1194 # endif
1195 GC_remove_roots((char*)start,(char*)end);
1197 # ifdef DARWIN_DEBUG
1198 GC_print_static_roots();
1199 # endif
1202 void GC_register_dynamic_libraries() {
1203 /* Currently does nothing. The callbacks are setup by GC_init_dyld()
1204 The dyld library takes it from there. */
1207 /* The _dyld_* functions have an internal lock so no _dyld functions
1208 can be called while the world is stopped without the risk of a deadlock.
1209 Because of this we MUST setup callbacks BEFORE we ever stop the world.
1210 This should be called BEFORE any thread in created and WITHOUT the
1211 allocation lock held. */
1213 void GC_init_dyld() {
1214 static GC_bool initialized = FALSE;
1215 char *bind_fully_env = NULL;
1217 if(initialized) return;
1219 # ifdef DARWIN_DEBUG
1220 GC_printf0("Registering dyld callbacks...\n");
1221 # endif
1223 /* Apple's Documentation:
1224 When you call _dyld_register_func_for_add_image, the dynamic linker runtime
1225 calls the specified callback (func) once for each of the images that is
1226 currently loaded into the program. When a new image is added to the program,
1227 your callback is called again with the mach_header for the new image, and the
1228 virtual memory slide amount of the new image.
1230 This WILL properly register already linked libraries and libraries
1231 linked in the future
1234 _dyld_register_func_for_add_image(GC_dyld_image_add);
1235 _dyld_register_func_for_remove_image(GC_dyld_image_remove);
1237 /* Set this early to avoid reentrancy issues. */
1238 initialized = TRUE;
1240 bind_fully_env = getenv("DYLD_BIND_AT_LAUNCH");
1242 if (bind_fully_env == NULL) {
1243 # ifdef DARWIN_DEBUG
1244 GC_printf0("Forcing full bind of GC code...\n");
1245 # endif
1247 if(!_dyld_bind_fully_image_containing_address((unsigned long*)GC_malloc))
1248 GC_abort("_dyld_bind_fully_image_containing_address failed");
1253 #define HAVE_REGISTER_MAIN_STATIC_DATA
1254 GC_bool GC_register_main_static_data()
1256 /* Already done through dyld callbacks */
1257 return FALSE;
1260 #endif /* DARWIN */
1262 #if defined(HAIKU)
1264 #include <kernel/image.h>
1266 void GC_register_dynamic_libraries()
1268 image_info info;
1269 int32 cookie = 0;
1270 while (get_next_image_info(0, &cookie, &info) == B_OK)
1272 void *data = info.data;
1273 GC_add_roots_inner(data, data + info.data_size, TRUE);
1277 #endif /* HAIKU */
1279 #else /* !DYNAMIC_LOADING */
1281 #ifdef PCR
1283 # include "il/PCR_IL.h"
1284 # include "th/PCR_ThCtl.h"
1285 # include "mm/PCR_MM.h"
1287 void GC_register_dynamic_libraries()
1289 /* Add new static data areas of dynamically loaded modules. */
1291 PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile();
1292 PCR_IL_LoadedSegment * q;
1294 /* Skip uncommited files */
1295 while (p != NIL && !(p -> lf_commitPoint)) {
1296 /* The loading of this file has not yet been committed */
1297 /* Hence its description could be inconsistent. */
1298 /* Furthermore, it hasn't yet been run. Hence its data */
1299 /* segments can't possibly reference heap allocated */
1300 /* objects. */
1301 p = p -> lf_prev;
1303 for (; p != NIL; p = p -> lf_prev) {
1304 for (q = p -> lf_ls; q != NIL; q = q -> ls_next) {
1305 if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK)
1306 == PCR_IL_SegFlags_Traced_on) {
1307 GC_add_roots_inner
1308 ((char *)(q -> ls_addr),
1309 (char *)(q -> ls_addr) + q -> ls_bytes,
1310 TRUE);
1318 #else /* !PCR */
1320 void GC_register_dynamic_libraries(){}
1322 int GC_no_dynamic_loading;
1324 #endif /* !PCR */
1326 #endif /* !DYNAMIC_LOADING */
1328 #ifndef HAVE_REGISTER_MAIN_STATIC_DATA
1330 /* Do we need to separately register the main static data segment? */
1331 GC_bool GC_register_main_static_data()
1333 return TRUE;
1335 #endif /* HAVE_REGISTER_MAIN_STATIC_DATA */