[BACK]Return to dyn_load.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc

Diff for /OpenXM_contrib2/asir2000/gc/dyn_load.c between version 1.4 and 1.7

version 1.4, 2001/04/20 07:39:18 version 1.7, 2003/06/24 05:11:32
Line 26 
Line 26 
  * None of this is safe with dlclose and incremental collection.   * None of this is safe with dlclose and incremental collection.
  * But then not much of anything is safe in the presence of dlclose.   * But then not much of anything is safe in the presence of dlclose.
  */   */
   #if defined(__linux__) && !defined(_GNU_SOURCE)
       /* Can't test LINUX, since this must be define before other includes */
   #   define _GNU_SOURCE
   #endif
 #if !defined(MACOS) && !defined(_WIN32_WCE)  #if !defined(MACOS) && !defined(_WIN32_WCE)
 #  include <sys/types.h>  #  include <sys/types.h>
 #endif  #endif
 #include "private/gc_priv.h"  #include "private/gc_priv.h"
   
 /* BTL: avoid circular redefinition of dlopen if SOLARIS_THREADS defined */  /* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */
 # if (defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \  # if (defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)) \
       || defined(HPUX_THREADS) || defined(IRIX_THREADS)) && defined(dlopen) \        && defined(dlopen) && !defined(GC_USE_LD_WRAP)
      && !defined(GC_USE_LD_WRAP)  
     /* To support threads in Solaris, gc.h interposes on dlopen by       */      /* To support threads in Solaris, gc.h interposes on dlopen by       */
     /* defining "dlopen" to be "GC_dlopen", which is implemented below.  */      /* defining "dlopen" to be "GC_dlopen", which is implemented below.  */
     /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the   */      /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the   */
Line 52 
Line 55 
     !defined(MSWIN32) && !defined(MSWINCE) && \      !defined(MSWIN32) && !defined(MSWINCE) && \
     !(defined(ALPHA) && defined(OSF1)) && \      !(defined(ALPHA) && defined(OSF1)) && \
     !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \      !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
     !defined(RS6000) && !defined(SCO_ELF) && \      !defined(RS6000) && !defined(SCO_ELF) && !defined(DGUX) && \
     !(defined(NETBSD) && defined(__ELF__))      !(defined(FREEBSD) && defined(__ELF__)) && \
       !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD) && \
       !defined(DARWIN)
  --> We only know how to find data segments of dynamic libraries for the   --> We only know how to find data segments of dynamic libraries for the
  --> above.  Additional SVR4 variants might not be too   --> above.  Additional SVR4 variants might not be too
  --> hard to add.   --> hard to add.
Line 75 
Line 80 
 #   define l_name       lm_name  #   define l_name       lm_name
 #endif  #endif
   
   #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
       (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
       (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
   #   include <stddef.h>
   #   include <elf.h>
   #   include <link.h>
   #endif
   
   /* Newer versions of GNU/Linux define this macro.  We
    * define it similarly for any ELF systems that don't.  */
   #  ifndef ElfW
   #    if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
   #      define ElfW(type) Elf32_##type
   #    else
   #      define ElfW(type) Elf64_##type
   #    endif
   #  endif
   
 #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)  #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)
   
 #ifdef LINT  #ifdef LINT
Line 85 
Line 107 
 static struct link_map *  static struct link_map *
 GC_FirstDLOpenedLinkMap()  GC_FirstDLOpenedLinkMap()
 {  {
     extern Elf32_Dyn _DYNAMIC;      extern ElfW(Dyn) _DYNAMIC;
     Elf32_Dyn *dp;      ElfW(Dyn) *dp;
     struct r_debug *r;      struct r_debug *r;
     static struct link_map * cachedResult = 0;      static struct link_map * cachedResult = 0;
     static Elf32_Dyn *dynStructureAddr = 0;      static ElfW(Dyn) *dynStructureAddr = 0;
                         /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */                          /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
   
 #   ifdef SUNOS53_SHARED_LIB  #   ifdef SUNOS53_SHARED_LIB
Line 99  GC_FirstDLOpenedLinkMap()
Line 121  GC_FirstDLOpenedLinkMap()
         /* at program startup.                                          */          /* at program startup.                                          */
         if( dynStructureAddr == 0 ) {          if( dynStructureAddr == 0 ) {
           void* startupSyms = dlopen(0, RTLD_LAZY);            void* startupSyms = dlopen(0, RTLD_LAZY);
           dynStructureAddr = (Elf32_Dyn*)dlsym(startupSyms, "_DYNAMIC");            dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
                 }                  }
 #   else  #   else
         dynStructureAddr = &_DYNAMIC;          dynStructureAddr = &_DYNAMIC;
Line 110  GC_FirstDLOpenedLinkMap()
Line 132  GC_FirstDLOpenedLinkMap()
     }      }
     if( cachedResult == 0 ) {      if( cachedResult == 0 ) {
         int tag;          int tag;
         for( dp = ((Elf32_Dyn *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {          for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
             if( tag == DT_DEBUG ) {              if( tag == DT_DEBUG ) {
                 struct link_map *lm                  struct link_map *lm
                         = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;                          = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
Line 124  GC_FirstDLOpenedLinkMap()
Line 146  GC_FirstDLOpenedLinkMap()
   
 #endif /* SUNOS5DL ... */  #endif /* SUNOS5DL ... */
   
 /* BTL: added to fix circular dlopen definition if SOLARIS_THREADS defined */  /* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
 # if defined(GC_must_restore_redefined_dlopen)  # if defined(GC_must_restore_redefined_dlopen)
 #   define dlopen GC_dlopen  #   define dlopen GC_dlopen
 # endif  # endif
Line 171  static ptr_t GC_first_common()
Line 193  static ptr_t GC_first_common()
   
 # if defined(SUNOS4) || defined(SUNOS5DL)  # if defined(SUNOS4) || defined(SUNOS5DL)
 /* Add dynamic library data sections to the root set.           */  /* Add dynamic library data sections to the root set.           */
 # if !defined(PCR) && !defined(SOLARIS_THREADS) && defined(THREADS)  # if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS)
 #   ifndef SRC_M3  #   ifndef SRC_M3
         --> fix mutual exclusion with dlopen          --> fix mutual exclusion with dlopen
 #   endif  /* We assume M3 programs don't call dlopen for now */  #   endif  /* We assume M3 programs don't call dlopen for now */
Line 196  void GC_register_dynamic_libraries()
Line 218  void GC_register_dynamic_libraries()
                     TRUE);                      TRUE);
 #     endif  #     endif
 #     ifdef SUNOS5DL  #     ifdef SUNOS5DL
         Elf32_Ehdr * e;          ElfW(Ehdr) * e;
         Elf32_Phdr * p;          ElfW(Phdr) * p;
         unsigned long offset;          unsigned long offset;
         char * start;          char * start;
         register int i;          register int i;
   
         e = (Elf32_Ehdr *) lm->l_addr;          e = (ElfW(Ehdr) *) lm->l_addr;
         p = ((Elf32_Phdr *)(((char *)(e)) + e->e_phoff));          p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
         offset = ((unsigned long)(lm->l_addr));          offset = ((unsigned long)(lm->l_addr));
         for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {          for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
           switch( p->p_type ) {            switch( p->p_type ) {
Line 243  void GC_register_dynamic_libraries()
Line 265  void GC_register_dynamic_libraries()
 # endif /* SUNOS */  # endif /* SUNOS */
   
 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \  #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
     (defined(NETBSD) && defined(__ELF__))      (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
       (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
   
   
 #ifdef USE_PROC_FOR_LIBRARIES  #ifdef USE_PROC_FOR_LIBRARIES
Line 260  extern ssize_t GC_repeat_read(int fd, char *buf, size_
Line 283  extern ssize_t GC_repeat_read(int fd, char *buf, size_
         /* Repeatedly read until buffer is filled, or EOF is encountered */          /* Repeatedly read until buffer is filled, or EOF is encountered */
         /* Defined in os_dep.c.                                          */          /* Defined in os_dep.c.                                          */
   
 static char *parse_map_entry(char *buf_ptr, word *start, word *end,  char *GC_parse_map_entry(char *buf_ptr, word *start, word *end,
                              char *prot_buf, unsigned int *maj_dev);                           char *prot_buf, unsigned int *maj_dev);
   word GC_apply_to_maps(word (*fn)(char *));
           /* From os_dep.c        */
   
 void GC_register_dynamic_libraries()  word GC_register_map_entries(char *maps)
 {  {
     int f;  
     int result;  
     char prot_buf[5];      char prot_buf[5];
     int maps_size;      char *buf_ptr = maps;
     char maps_temp[32768];  
     char *maps_buf;  
     char *buf_ptr;  
     int count;      int count;
     word start, end;      word start, end;
     unsigned int maj_dev, min_dev;      unsigned int maj_dev;
     word least_ha, greatest_ha;      word least_ha, greatest_ha;
     unsigned i;      unsigned i;
     word datastart = (word)(DATASTART);      word datastart = (word)(DATASTART);
   
     /* Read /proc/self/maps     */      /* Compute heap bounds. FIXME: Should be done by add_to_heap?       */
         /* Note that we may not allocate, and thus can't use stdio.     */  
         f = open("/proc/self/maps", O_RDONLY);  
         if (-1 == f) ABORT("Couldn't open /proc/self/maps");  
         /* stat() doesn't work for /proc/self/maps, so we have to  
            read it to find out how large it is... */  
         maps_size = 0;  
         do {  
             result = GC_repeat_read(f, maps_temp, sizeof(maps_temp));  
             if (result <= 0) ABORT("Couldn't read /proc/self/maps");  
             maps_size += result;  
         } while (result == sizeof(maps_temp));  
   
         if (maps_size > sizeof(maps_temp)) {  
             /* If larger than our buffer, close and re-read it. */  
             close(f);  
             f = open("/proc/self/maps", O_RDONLY);  
             if (-1 == f) ABORT("Couldn't open /proc/self/maps");  
             maps_buf = alloca(maps_size);  
             if (NULL == maps_buf) ABORT("/proc/self/maps alloca failed");  
             result = GC_repeat_read(f, maps_buf, maps_size);  
             if (result <= 0) ABORT("Couldn't read /proc/self/maps");  
         } else {  
             /* Otherwise use the fixed size buffer */  
             maps_buf = maps_temp;  
         }  
   
         close(f);  
         maps_buf[result] = '\0';  
         buf_ptr = maps_buf;  
     /* Compute heap bounds. Should be done by add_to_heap?      */  
         least_ha = (word)(-1);          least_ha = (word)(-1);
         greatest_ha = 0;          greatest_ha = 0;
         for (i = 0; i < GC_n_heap_sects; ++i) {          for (i = 0; i < GC_n_heap_sects; ++i) {
Line 320  void GC_register_dynamic_libraries()
Line 310  void GC_register_dynamic_libraries()
         }          }
         if (greatest_ha < (word)GC_scratch_last_end_ptr)          if (greatest_ha < (word)GC_scratch_last_end_ptr)
             greatest_ha = (word)GC_scratch_last_end_ptr;              greatest_ha = (word)GC_scratch_last_end_ptr;
     for (;;) {  
   
         buf_ptr = parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);      for (;;) {
         if (buf_ptr == NULL) return;          buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
           if (buf_ptr == NULL) return 1;
         if (prot_buf[1] == 'w') {          if (prot_buf[1] == 'w') {
             /* This is a writable mapping.  Add it to           */              /* This is a writable mapping.  Add it to           */
             /* the root set unless it is already otherwise      */              /* the root set unless it is already otherwise      */
Line 333  void GC_register_dynamic_libraries()
Line 322  void GC_register_dynamic_libraries()
                 /* Stack mapping; discard       */                  /* Stack mapping; discard       */
                 continue;                  continue;
             }              }
             if (start <= datastart && end > datastart && maj_dev != 0) {  
                 /* Main data segment; discard   */  
                 continue;  
             }  
 #           ifdef THREADS  #           ifdef THREADS
               if (GC_segment_is_thread_stack(start, end)) continue;                if (GC_segment_is_thread_stack(start, end)) continue;
 #           endif  #           endif
             /* The rest of this assumes that there is no mapping        */              /* We no longer exclude the main data segment.              */
             /* spanning the beginning of the data segment, or extending */  
             /* beyond the entire heap at both ends.                     */  
             /* Empirically these assumptions hold.                      */  
   
             if (start < (word)DATAEND && end > (word)DATAEND) {  
                 /* Rld may use space at the end of the main data        */  
                 /* segment.  Thus we add that in.                       */  
                 start = (word)DATAEND;  
             }  
             if (start < least_ha && end > least_ha) {              if (start < least_ha && end > least_ha) {
                 end = least_ha;                  end = least_ha;
             }              }
Line 359  void GC_register_dynamic_libraries()
Line 335  void GC_register_dynamic_libraries()
             if (start >= least_ha && end <= greatest_ha) continue;              if (start >= least_ha && end <= greatest_ha) continue;
             GC_add_roots_inner((char *)start, (char *)end, TRUE);              GC_add_roots_inner((char *)start, (char *)end, TRUE);
         }          }
      }      }
       return 1;
 }  }
   
 //  void GC_register_dynamic_libraries()
 //  parse_map_entry parses an entry from /proc/self/maps so we can  {
 //  locate all writable data segments that belong to shared libraries.     if (!GC_apply_to_maps(GC_register_map_entries))
 //  The format of one of these entries and the fields we care about         ABORT("Failed to read /proc for library registration.");
 //  is as follows:  }
 //  XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537     name of mapping...\n  
 //  ^^^^^^^^ ^^^^^^^^ ^^^^          ^^  
 //  start    end      prot          maj_dev  
 //  0        9        18            32  
 //  
 //  The parser is called with a pointer to the entry and the return value  
 //  is either NULL or is advanced to the next entry(the byte after the  
 //  trailing '\n'.)  
 //  
 #define OFFSET_MAP_START   0  
 #define OFFSET_MAP_END     9  
 #define OFFSET_MAP_PROT   18  
 #define OFFSET_MAP_MAJDEV 32  
   
 static char *parse_map_entry(char *buf_ptr, word *start, word *end,  /* We now take care of the main data segment ourselves: */
                              char *prot_buf, unsigned int *maj_dev)  GC_bool GC_register_main_static_data()
 {  {
     int i;    return FALSE;
     unsigned int val;  }
     char *tok;  
   # define HAVE_REGISTER_MAIN_STATIC_DATA
   
     if (buf_ptr == NULL || *buf_ptr == '\0') {  #endif /* USE_PROC_FOR_LIBRARIES */
         return NULL;  
     }  
   
     memcpy(prot_buf, buf_ptr+OFFSET_MAP_PROT, 4); // do the protections first  #if !defined(USE_PROC_FOR_LIBRARIES)
     prot_buf[4] = '\0';  /* The following is the preferred way to walk dynamic libraries */
   /* For glibc 2.2.4+.  Unfortunately, it doesn't work for older  */
   /* versions.  Thanks to Jakub Jelinek for most of the code.     */
   
     if (prot_buf[1] == 'w') { // we can skip all of this if it's not writable  # if defined(LINUX) /* Are others OK here, too? */ \
        && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
            || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
   
         tok = buf_ptr;  /* We have the header files for a glibc that includes dl_iterate_phdr.  */
         buf_ptr[OFFSET_MAP_START+8] = '\0';  /* It may still not be available in the library on the target system.   */
         *start = strtoul(tok, NULL, 16);  /* Thus we also treat it as a weak symbol.                              */
   #define HAVE_DL_ITERATE_PHDR
   
         tok = buf_ptr+OFFSET_MAP_END;  static int GC_register_dynlib_callback(info, size, ptr)
         buf_ptr[OFFSET_MAP_END+8] = '\0';       struct dl_phdr_info * info;
         *end = strtoul(tok, NULL, 16);       size_t size;
        void * ptr;
   {
     const ElfW(Phdr) * p;
     char * start;
     register int i;
   
         buf_ptr += OFFSET_MAP_MAJDEV;    /* Make sure struct dl_phdr_info is at least as big as we need.  */
         tok = buf_ptr;    if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
         while (*buf_ptr != ':') buf_ptr++;        + sizeof (info->dlpi_phnum))
         *buf_ptr++ = '\0';      return -1;
         *maj_dev = strtoul(tok, NULL, 16);  
     p = info->dlpi_phdr;
     for( i = 0; i < (int)(info->dlpi_phnum); ((i++),(p++)) ) {
       switch( p->p_type ) {
         case PT_LOAD:
           {
             if( !(p->p_flags & PF_W) ) break;
             start = ((char *)(p->p_vaddr)) + info->dlpi_addr;
             GC_add_roots_inner(start, start + p->p_memsz, TRUE);
           }
         break;
         default:
           break;
     }      }
     }
   
     while (*buf_ptr && *buf_ptr++ != '\n');    * (int *)ptr = 1;     /* Signal that we were called */
     return 0;
   }
   
     return buf_ptr;  /* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
   
   #pragma weak dl_iterate_phdr
   
   GC_bool GC_register_dynamic_libraries_dl_iterate_phdr()
   {
     if (dl_iterate_phdr) {
       int did_something = 0;
       dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
       if (!did_something) {
           /* dl_iterate_phdr may forget the static data segment in        */
           /* statically linked executables.                               */
           GC_add_roots_inner(DATASTART, (char *)(DATAEND), TRUE);
   #       if defined(DATASTART2)
             GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);
   #       endif
       }
   
       return TRUE;
     } else {
       return FALSE;
     }
 }  }
   
 #else /* !USE_PROC_FOR_LIBRARIES */  /* Do we need to separately register the main static data segment? */
   GC_bool GC_register_main_static_data()
   {
     return (dl_iterate_phdr == 0);
   }
   
   #define HAVE_REGISTER_MAIN_STATIC_DATA
   
   # else /* !LINUX || version(glibc) < 2.2.4 */
   
 /* Dynamic loading code for Linux running ELF. Somewhat tested on  /* Dynamic loading code for Linux running ELF. Somewhat tested on
  * Linux/x86, untested but hopefully should work on Linux/Alpha.   * Linux/x86, untested but hopefully should work on Linux/Alpha.
  * This code was derived from the Solaris/ELF support. Thanks to   * This code was derived from the Solaris/ELF support. Thanks to
  * whatever kind soul wrote that.  - Patrick Bridges */   * whatever kind soul wrote that.  - Patrick Bridges */
   
   /* This doesn't necessarily work in all cases, e.g. with preloaded
    * dynamic libraries.                                           */
   
 #if defined(NETBSD)  #if defined(NETBSD)
 #  include <sys/exec_elf.h>  #  include <sys/exec_elf.h>
 #else  #else
Line 431  static char *parse_map_entry(char *buf_ptr, word *star
Line 451  static char *parse_map_entry(char *buf_ptr, word *star
 #endif  #endif
 #include <link.h>  #include <link.h>
   
 /* Newer versions of Linux/Alpha and Linux/x86 define this macro.  We  # endif
  * define it for those older versions that don't.  */  
 #  ifndef ElfW  
 #    if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32  
 #      define ElfW(type) Elf32_##type  
 #    else  
 #      define ElfW(type) Elf64_##type  
 #    endif  
 #  endif  
   
   #ifdef __GNUC__
   # pragma weak _DYNAMIC
   #endif
   extern ElfW(Dyn) _DYNAMIC[];
   
 static struct link_map *  static struct link_map *
 GC_FirstDLOpenedLinkMap()  GC_FirstDLOpenedLinkMap()
 {  {
 #   ifdef __GNUC__  
 #     pragma weak _DYNAMIC  
 #   endif  
     extern ElfW(Dyn) _DYNAMIC[];  
     ElfW(Dyn) *dp;      ElfW(Dyn) *dp;
     struct r_debug *r;      struct r_debug *r;
     static struct link_map *cachedResult = 0;      static struct link_map *cachedResult = 0;
Line 472  GC_FirstDLOpenedLinkMap()
Line 485  GC_FirstDLOpenedLinkMap()
   
 void GC_register_dynamic_libraries()  void GC_register_dynamic_libraries()
 {  {
   struct link_map *lm = GC_FirstDLOpenedLinkMap();    struct link_map *lm;
   
   
   # ifdef HAVE_DL_ITERATE_PHDR
       if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
           return;
       }
   # endif
     lm = GC_FirstDLOpenedLinkMap();
   for (lm = GC_FirstDLOpenedLinkMap();    for (lm = GC_FirstDLOpenedLinkMap();
        lm != (struct link_map *) 0;  lm = lm->l_next)         lm != (struct link_map *) 0;  lm = lm->l_next)
     {      {
Line 514  void GC_register_dynamic_libraries()
Line 533  void GC_register_dynamic_libraries()
 #include <fcntl.h>  #include <fcntl.h>
 #include <elf.h>  #include <elf.h>
 #include <errno.h>  #include <errno.h>
   #include <signal.h>  /* Only for the following test. */
   #ifndef _sigargs
   # define IRIX6
   #endif
   
 extern void * GC_roots_present();  extern void * GC_roots_present();
         /* The type is a lie, since the real type doesn't make sense here, */          /* The type is a lie, since the real type doesn't make sense here, */
Line 575  void GC_register_dynamic_libraries()
Line 598  void GC_register_dynamic_libraries()
         if ((flags & (MA_BREAK | MA_STACK | MA_PHYS)) != 0) goto irrelevant;          if ((flags & (MA_BREAK | MA_STACK | MA_PHYS)) != 0) goto irrelevant;
         if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))          if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
             goto irrelevant;              goto irrelevant;
           /* The latter test is empirically useless.  Other than the    */            /* The latter test is empirically useless in very old Irix    */
             /* versions.  Other than the                                  */
           /* main data and stack segments, everything appears to be     */            /* main data and stack segments, everything appears to be     */
           /* mapped readable, writable, executable, and shared(!!).     */            /* mapped readable, writable, executable, and shared(!!).     */
           /* This makes no sense to me. - HB                            */            /* This makes no sense to me. - HB                            */
Line 588  void GC_register_dynamic_libraries()
Line 612  void GC_register_dynamic_libraries()
 #       endif /* MMAP_STACKS */  #       endif /* MMAP_STACKS */
   
         limit = start + addr_map[i].pr_size;          limit = start + addr_map[i].pr_size;
         if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {          /* The following seemed to be necessary for very old versions   */
           /* of Irix, but it has been reported to discard relevant        */
           /* segments under Irix 6.5.                                     */
   #       ifndef IRIX6
             if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
             /* Discard text segments, i.e. 0-offset mappings against    */              /* Discard text segments, i.e. 0-offset mappings against    */
             /* executable files which appear to have ELF headers.       */              /* executable files which appear to have ELF headers.       */
             caddr_t arg;              caddr_t arg;
Line 615  void GC_register_dynamic_libraries()
Line 643  void GC_register_dynamic_libraries()
                     goto irrelevant;                      goto irrelevant;
                 }                  }
             }              }
         }            }
   #       endif /* !IRIX6 */
         GC_add_roots_inner(start, limit, TRUE);          GC_add_roots_inner(start, limit, TRUE);
       irrelevant: ;        irrelevant: ;
     }      }
Line 639  void GC_register_dynamic_libraries()
Line 668  void GC_register_dynamic_libraries()
   
   extern GC_bool GC_is_heap_base (ptr_t p);    extern GC_bool GC_is_heap_base (ptr_t p);
   
 # ifdef WIN32_THREADS  # ifdef GC_WIN32_THREADS
     extern void GC_get_next_stack(char *start, char **lo, char **hi);      extern void GC_get_next_stack(char *start, char **lo, char **hi);
     void GC_cond_add_roots(char *base, char * limit)      void GC_cond_add_roots(char *base, char * limit)
     {      {
Line 671  void GC_register_dynamic_libraries()
Line 700  void GC_register_dynamic_libraries()
     }      }
 # endif  # endif
   
 # ifndef MSWINCE  # ifdef MSWINCE
   extern GC_bool GC_win32s;    /* Do we need to separately register the main static data segment? */
 # endif    GC_bool GC_register_main_static_data()
     {
       return FALSE;
     }
   # else /* win32 */
     extern GC_bool GC_no_win32_dlls;
   
     GC_bool GC_register_main_static_data()
     {
       return GC_no_win32_dlls;
     }
   # endif /* win32 */
   
   # define HAVE_REGISTER_MAIN_STATIC_DATA
   
   void GC_register_dynamic_libraries()    void GC_register_dynamic_libraries()
   {    {
     MEMORY_BASIC_INFORMATION buf;      MEMORY_BASIC_INFORMATION buf;
Line 685  void GC_register_dynamic_libraries()
Line 727  void GC_register_dynamic_libraries()
     char * limit, * new_limit;      char * limit, * new_limit;
   
 #   ifdef MSWIN32  #   ifdef MSWIN32
       if (GC_win32s) return;        if (GC_no_win32_dlls) return;
 #   endif  #   endif
     base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;      base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
 #   if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)  #   if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
Line 854  void GC_register_dynamic_libraries()
Line 896  void GC_register_dynamic_libraries()
   
       /* Check if this is the end of the list or if some error occured */        /* Check if this is the end of the list or if some error occured */
         if (status != 0) {          if (status != 0) {
 #        ifdef HPUX_THREADS  #        ifdef GC_HPUX_THREADS
            /* I've seen errno values of 0.  The man page is not clear   */             /* I've seen errno values of 0.  The man page is not clear   */
            /* as to whether errno should get set on a -1 return.        */             /* as to whether errno should get set on a -1 return.        */
            break;             break;
Line 919  void GC_register_dynamic_libraries()
Line 961  void GC_register_dynamic_libraries()
                 len = ldi->ldinfo_next;                  len = ldi->ldinfo_next;
                 GC_add_roots_inner(                  GC_add_roots_inner(
                                 ldi->ldinfo_dataorg,                                  ldi->ldinfo_dataorg,
                                 (unsigned long)ldi->ldinfo_dataorg                                  (ptr_t)(unsigned long)ldi->ldinfo_dataorg
                                 + ldi->ldinfo_datasize,                                  + ldi->ldinfo_datasize,
                                 TRUE);                                  TRUE);
                 ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;                  ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
Line 927  void GC_register_dynamic_libraries()
Line 969  void GC_register_dynamic_libraries()
 }  }
 #endif /* RS6000 */  #endif /* RS6000 */
   
   #ifdef DARWIN
   
   #include <mach-o/dyld.h>
   #include <mach-o/getsect.h>
   
   /*#define DARWIN_DEBUG*/
   
   const static struct {
           const char *seg;
           const char *sect;
   } GC_dyld_sections[] = {
           { SEG_DATA, SECT_DATA },
           { SEG_DATA, SECT_BSS },
           { SEG_DATA, SECT_COMMON }
   };
   
   #ifdef DARWIN_DEBUG
   static const char *GC_dyld_name_for_hdr(struct mach_header *hdr) {
       unsigned long i,c;
       c = _dyld_image_count();
       for(i=0;i<c;i++) if(_dyld_get_image_header(i) == hdr)
           return _dyld_get_image_name(i);
       return NULL;
   }
   #endif
   
   /* This should never be called by a thread holding the lock */
   static void GC_dyld_image_add(struct mach_header* hdr, unsigned long slide) {
       unsigned long start,end,i;
       const struct section *sec;
       for(i=0;i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]);i++) {
           sec = getsectbynamefromheader(
               hdr,GC_dyld_sections[i].seg,GC_dyld_sections[i].sect);
               if(sec == NULL || sec->size == 0) continue;
               start = slide + sec->addr;
               end = start + sec->size;
   #               ifdef DARWIN_DEBUG
                   GC_printf4("Adding section at %p-%p (%lu bytes) from image %s\n",
                   start,end,sec->size,GC_dyld_name_for_hdr(hdr));
   #                       endif
           GC_add_roots((char*)start,(char*)end);
           }
   #       ifdef DARWIN_DEBUG
       GC_print_static_roots();
   #       endif
   }
   
   /* This should never be called by a thread holding the lock */
   static void GC_dyld_image_remove(struct mach_header* hdr, unsigned long slide) {
       unsigned long start,end,i;
       const struct section *sec;
       for(i=0;i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]);i++) {
           sec = getsectbynamefromheader(
               hdr,GC_dyld_sections[i].seg,GC_dyld_sections[i].sect);
           if(sec == NULL || sec->size == 0) continue;
           start = slide + sec->addr;
           end = start + sec->size;
   #               ifdef DARWIN_DEBUG
               GC_printf4("Removing section at %p-%p (%lu bytes) from image %s\n",
                   start,end,sec->size,GC_dyld_name_for_hdr(hdr));
   #               endif
           GC_remove_roots((char*)start,(char*)end);
       }
   #       ifdef DARWIN_DEBUG
       GC_print_static_roots();
   #       endif
   }
   
   void GC_register_dynamic_libraries() {
       /* Currently does nothing. The callbacks are setup by GC_init_dyld()
       The dyld library takes it from there. */
   }
   
   /* The _dyld_* functions have an internal lock so no _dyld functions
      can be called while the world is stopped without the risk of a deadlock.
      Because of this we MUST setup callbacks BEFORE we ever stop the world.
      This should be called BEFORE any thread in created and WITHOUT the
      allocation lock held. */
   
   void GC_init_dyld() {
       static GC_bool initialized = FALSE;
   
       if(initialized) return;
   
   #   ifdef DARWIN_DEBUG
           GC_printf0("Forcing full bind of GC code...\n");
   #   endif
       if(!_dyld_bind_fully_image_containing_address((unsigned long*)GC_malloc))
           GC_abort("_dyld_bind_fully_image_containing_addres failed");
   
   #   ifdef DARWIN_DEBUG
           GC_printf0("Registering dyld callbacks...\n");
   #   endif
   
       /* Apple's Documentation:
       When you call _dyld_register_func_for_add_image, the dynamic linker runtime
       calls the specified callback (func) once for each of the images that is
       currently loaded into the program. When a new image is added to the program,
       your callback is called again with the mach_header for the new image, and the       virtual memory slide amount of the new image.
   
       This WILL properly register existing and all future libraries
       */
   
       _dyld_register_func_for_add_image(GC_dyld_image_add);
       _dyld_register_func_for_remove_image(GC_dyld_image_remove);
       initialized = TRUE;
   }
   
   #define HAVE_REGISTER_MAIN_STATIC_DATA
   GC_bool GC_register_main_static_data()
   {
     /* Already done through dyld callbacks */
     return FALSE;
   }
   
   #endif /* DARWIN */
   
 #else /* !DYNAMIC_LOADING */  #else /* !DYNAMIC_LOADING */
   
 #ifdef PCR  #ifdef PCR
Line 975  void GC_register_dynamic_libraries(){}
Line 1132  void GC_register_dynamic_libraries(){}
 int GC_no_dynamic_loading;  int GC_no_dynamic_loading;
   
 #endif /* !PCR */  #endif /* !PCR */
   
 #endif /* !DYNAMIC_LOADING */  #endif /* !DYNAMIC_LOADING */
   
   #ifndef HAVE_REGISTER_MAIN_STATIC_DATA
   
   /* Do we need to separately register the main static data segment? */
   GC_bool GC_register_main_static_data()
   {
     return TRUE;
   }
   #endif /* HAVE_REGISTER_MAIN_STATIC_DATA */
   

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.7

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>