OSDN Git Service

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