OSDN Git Service

PR c/10175
[pf3gnuchains/gcc-fork.git] / boehm-gc / dyn_load.c
index 508fd24..71ad5fb 100644 (file)
  * None of this is safe with dlclose and incremental collection.
  * 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)
 #  include <sys/types.h>
 #endif
 #include "private/gc_priv.h"
 
-/* BTL: avoid circular redefinition of dlopen if SOLARIS_THREADS defined */
-# if (defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \
-      || defined(HPUX_THREADS) || defined(IRIX_THREADS)) && defined(dlopen) \
-     && !defined(GC_USE_LD_WRAP)
+/* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */
+# if (defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)) \
+      && defined(dlopen) && !defined(GC_USE_LD_WRAP)
     /* To support threads in Solaris, gc.h interposes on dlopen by       */
     /* defining "dlopen" to be "GC_dlopen", which is implemented below.  */
     /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the   */
@@ -53,6 +56,7 @@
     !(defined(ALPHA) && defined(OSF1)) && \
     !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
     !defined(RS6000) && !defined(SCO_ELF) && \
+    !(defined(FREEBSD) && defined(__ELF__)) && \
     !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD)
  --> We only know how to find data segments of dynamic libraries for the
  --> above.  Additional SVR4 variants might not be too
 #   define l_name      lm_name
 #endif
 
+#if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
+    (defined(FREEBSD) && defined(__ELF__)) || \
+    (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)
 
 static struct link_map *
 GC_FirstDLOpenedLinkMap()
 {
-    extern Elf32_Dyn _DYNAMIC;
-    Elf32_Dyn *dp;
+    extern ElfW(Dyn) _DYNAMIC;
+    ElfW(Dyn) *dp;
     struct r_debug *r;
     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 */
 
 #   ifdef SUNOS53_SHARED_LIB
@@ -99,7 +120,7 @@ GC_FirstDLOpenedLinkMap()
        /* at program startup.                                          */
        if( dynStructureAddr == 0 ) {
          void* startupSyms = dlopen(0, RTLD_LAZY);
-         dynStructureAddr = (Elf32_Dyn*)dlsym(startupSyms, "_DYNAMIC");
+         dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
                }
 #   else
        dynStructureAddr = &_DYNAMIC;
@@ -110,7 +131,7 @@ GC_FirstDLOpenedLinkMap()
     }
     if( cachedResult == 0 ) {
         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 ) {
                 struct link_map *lm
                         = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
@@ -124,7 +145,7 @@ GC_FirstDLOpenedLinkMap()
 
 #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)
 #   define dlopen GC_dlopen
 # endif
@@ -171,7 +192,7 @@ static ptr_t GC_first_common()
 
 # if defined(SUNOS4) || defined(SUNOS5DL)
 /* 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
        --> fix mutual exclusion with dlopen
 #   endif  /* We assume M3 programs don't call dlopen for now */
@@ -196,14 +217,14 @@ void GC_register_dynamic_libraries()
                    TRUE);
 #     endif
 #     ifdef SUNOS5DL
-       Elf32_Ehdr * e;
-        Elf32_Phdr * p;
+       ElfW(Ehdr) * e;
+        ElfW(Phdr) * p;
         unsigned long offset;
         char * start;
         register int i;
         
-       e = (Elf32_Ehdr *) lm->l_addr;
-        p = ((Elf32_Phdr *)(((char *)(e)) + e->e_phoff));
+       e = (ElfW(Ehdr) *) lm->l_addr;
+        p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
         offset = ((unsigned long)(lm->l_addr));
         for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
           switch( p->p_type ) {
@@ -243,6 +264,7 @@ void GC_register_dynamic_libraries()
 # endif /* SUNOS */
 
 #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
+    (defined(FREEBSD) && defined(__ELF__)) || \
     (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
 
 
@@ -333,10 +355,6 @@ void GC_register_dynamic_libraries()
                /* Stack mapping; discard       */
                continue;
            }
-           if (start <= datastart && end > datastart && maj_dev != 0) {
-               /* Main data segment; discard   */
-               continue;
-           }
 #          ifdef THREADS
              if (GC_segment_is_thread_stack(start, end)) continue;
 #          endif
@@ -362,6 +380,13 @@ void GC_register_dynamic_libraries()
      }
 }
 
+/* We now take care of the main data segment ourselves: */
+GC_bool GC_register_main_static_data()
+{
+  return FALSE;
+}
+  
+# define HAVE_REGISTER_MAIN_STATIC_DATA
 //
 //  parse_map_entry parses an entry from /proc/self/maps so we can
 //  locate all writable data segments that belong to shared libraries.
@@ -417,13 +442,96 @@ static char *parse_map_entry(char *buf_ptr, word *start, word *end,
     return buf_ptr;
 }
 
-#else /* !USE_PROC_FOR_LIBRARIES */
+#endif /* USE_PROC_FOR_LIBRARIES */
+
+#if !defined(USE_PROC_FOR_LIBRARIES)
+/* 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 defined(LINUX) /* Are others OK here, too? */ \
+     && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
+         || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))) 
+
+/* We have the header files for a glibc that includes dl_iterate_phdr. */
+/* It may still not be available in the library on the target system.   */
+/* Thus we also treat it as a weak symbol.                             */
+#define HAVE_DL_ITERATE_PHDR
+
+static int GC_register_dynlib_callback(info, size, ptr)
+     struct dl_phdr_info * info;
+     size_t size;
+     void * ptr;
+{
+  const ElfW(Phdr) * p;
+  char * start;
+  register int i;
+
+  /* Make sure struct dl_phdr_info is at least as big as we need.  */
+  if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
+      + sizeof (info->dlpi_phnum))
+    return -1;
+
+  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;
+    }
+  }
+
+  * (int *)ptr = 1;    /* Signal that we were called */
+  return 0;
+}     
+
+/* 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;
+  }
+}
+
+/* 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
  * Linux/x86, untested but hopefully should work on Linux/Alpha. 
  * This code was derived from the Solaris/ELF support. Thanks to
  * whatever kind soul wrote that.  - Patrick Bridges */
 
+/* This doesn't necessarily work in all cases, e.g. with preloaded
+ * dynamic libraries.                                          */
+
 #if defined(NETBSD)
 #  include <sys/exec_elf.h>
 #else
@@ -431,23 +539,16 @@ static char *parse_map_entry(char *buf_ptr, word *start, word *end,
 #endif
 #include <link.h>
 
-/* Newer versions of Linux/Alpha and Linux/x86 define this macro.  We
- * 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
+# endif
+
+#ifdef __GNUC__
+# pragma weak _DYNAMIC
+#endif
+extern ElfW(Dyn) _DYNAMIC[];
 
 static struct link_map *
 GC_FirstDLOpenedLinkMap()
 {
-#   ifdef __GNUC__
-#     pragma weak _DYNAMIC
-#   endif
-    extern ElfW(Dyn) _DYNAMIC[];
     ElfW(Dyn) *dp;
     struct r_debug *r;
     static struct link_map *cachedResult = 0;
@@ -472,9 +573,15 @@ GC_FirstDLOpenedLinkMap()
 
 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();
        lm != (struct link_map *) 0;  lm = lm->l_next)
     {
@@ -648,7 +755,7 @@ void GC_register_dynamic_libraries()
   
   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);
     void GC_cond_add_roots(char *base, char * limit)
     {
@@ -680,10 +787,23 @@ void GC_register_dynamic_libraries()
     }
 # endif
 
-# ifndef MSWINCE
-  extern GC_bool GC_win32s;
-# endif
+# ifdef MSWINCE
+  /* Do we need to separately register the main static data segment? */
+  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()
   {
     MEMORY_BASIC_INFORMATION buf;
@@ -694,7 +814,7 @@ void GC_register_dynamic_libraries()
     char * limit, * new_limit;
 
 #   ifdef MSWIN32
-      if (GC_win32s) return;
+      if (GC_no_win32_dlls) return;
 #   endif
     base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
 #   if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
@@ -863,7 +983,7 @@ void GC_register_dynamic_libraries()
 
       /* Check if this is the end of the list or if some error occured */
         if (status != 0) {
-#       ifdef HPUX_THREADS
+#       ifdef GC_HPUX_THREADS
           /* I've seen errno values of 0.  The man page is not clear   */
           /* as to whether errno should get set on a -1 return.        */
           break;
@@ -984,4 +1104,15 @@ void GC_register_dynamic_libraries(){}
 int GC_no_dynamic_loading;
 
 #endif /* !PCR */
+
 #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 */
+