Fix recursive dlopen.
[platform/upstream/glibc.git] / elf / dl-open.c
index ea222d0..47b4cb5 100644 (file)
@@ -1,5 +1,5 @@
 /* Load a shared object at runtime, relocate it, and run its initializer.
-   Copyright (C) 1996-2014 Free Software Foundation, Inc.
+   Copyright (C) 1996-2015 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -147,7 +147,7 @@ add_to_global (struct link_map *new)
          ns->_ns_main_searchlist->r_list[new_nlist++] = map;
 
          /* We modify the global scope.  Report this.  */
-         if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
+         if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
            _dl_debug_printf ("\nadd %s [%lu] to global scope\n",
                              map->l_name, map->l_ns);
        }
@@ -217,7 +217,9 @@ dl_open_worker (void *a)
        args->nsid = call_map->l_ns;
     }
 
-  assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
+  /* One might be tempted to assert that we are RT_CONSISTENT at this point, but that
+     may not be true if this is a recursive call to dlopen.  */
+  _dl_debug_initialize (0, args->nsid);
 
   /* Load the named object.  */
   struct link_map *new;
@@ -232,7 +234,7 @@ dl_open_worker (void *a)
       return;
     }
 
-  if (__builtin_expect (mode & __RTLD_SPROF, 0))
+  if (__glibc_unlikely (mode & __RTLD_SPROF))
     /* This happens only if we load a DSO for 'sprof'.  */
     return;
 
@@ -240,10 +242,10 @@ dl_open_worker (void *a)
   ++new->l_direct_opencount;
 
   /* It was already open.  */
-  if (__builtin_expect (new->l_searchlist.r_list != NULL, 0))
+  if (__glibc_unlikely (new->l_searchlist.r_list != NULL))
     {
       /* Let the user know about the opencount.  */
-      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
        _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
                          new->l_name, new->l_ns, new->l_direct_opencount);
 
@@ -269,7 +271,7 @@ dl_open_worker (void *a)
 
 #ifdef SHARED
   /* Auditing checkpoint: we have added all objects.  */
-  if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
+  if (__glibc_unlikely (GLRO(dl_naudit) > 0))
     {
       struct link_map *head = GL(dl_ns)[new->l_ns]._ns_loaded;
       /* Do not call the functions for any auditing object.  */
@@ -294,7 +296,7 @@ dl_open_worker (void *a)
   LIBC_PROBE (map_complete, 3, args->nsid, r, new);
 
   /* Print scope information.  */
-  if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
+  if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
     _dl_show_scope (new, 0);
 
   /* Only do lazy relocation if `LD_BIND_NOW' is not set.  */
@@ -344,7 +346,7 @@ dl_open_worker (void *a)
              if (runp != NULL)
                /* Look through the dependencies of the object.  */
                while (*runp != NULL)
-                 if (__builtin_expect (*runp++ == thisp, 0))
+                 if (__glibc_unlikely (*runp++ == thisp))
                    {
                      /* Move the current object to the back past the last
                         object with it as the dependency.  */
@@ -391,7 +393,7 @@ dl_open_worker (void *a)
        }
 
 #ifdef SHARED
-      if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
+      if (__glibc_unlikely (GLRO(dl_profile) != NULL))
        {
          /* If this here is the shared object which we want to profile
             make sure the profile is started.  We can find out whether
@@ -444,7 +446,7 @@ dl_open_worker (void *a)
            /* Avoid duplicates.  */
            continue;
 
-         if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
+         if (__glibc_unlikely (cnt + 1 >= imap->l_scope_max))
            {
              /* The 'r_scope' array is too small.  Allocate a new one
                 dynamically.  */
@@ -511,7 +513,7 @@ dl_open_worker (void *a)
        }
 
       /* Print scope information.  */
-      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
        _dl_show_scope (imap, from_scope);
     }
 
@@ -548,10 +550,7 @@ cannot load any more object with static TLS"));
             generation of the DSO we are allocating data for.  */
          _dl_update_slotinfo (imap->l_tls_modid);
 #endif
-         /* We do this iteration under a signal mask in dl-reloc; why not
-            here?  Because these symbols are new and dlopen hasn't
-            returned yet.  So we can't possibly be racing with a TLS
-            access to them from another thread.  */
+
          GL(dl_init_static_tls) (imap);
          assert (imap->l_need_tls_init == 0);
        }
@@ -577,7 +576,7 @@ cannot load any more object with static TLS"));
 
   /* Mark the object as not deletable if the RTLD_NODELETE flags was
      passed.  */
-  if (__builtin_expect (mode & RTLD_NODELETE, 0))
+  if (__glibc_unlikely (mode & RTLD_NODELETE))
     new->l_flags_1 |= DF_1_NODELETE;
 
 #ifndef SHARED
@@ -587,7 +586,7 @@ cannot load any more object with static TLS"));
 #endif
 
   /* Let the user know about the opencount.  */
-  if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
+  if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
     _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
                      new->l_name, new->l_ns, new->l_direct_opencount);
 }
@@ -604,14 +603,14 @@ _dl_open (const char *file, int mode, const void *caller_dlopen, Lmid_t nsid,
   /* Make sure we are alone.  */
   __rtld_lock_lock_recursive (GL(dl_load_lock));
 
-  if (__builtin_expect (nsid == LM_ID_NEWLM, 0))
+  if (__glibc_unlikely (nsid == LM_ID_NEWLM))
     {
       /* Find a new namespace.  */
       for (nsid = 1; DL_NNS > 1 && nsid < GL(dl_nns); ++nsid)
        if (GL(dl_ns)[nsid]._ns_loaded == NULL)
          break;
 
-      if (__builtin_expect (nsid == DL_NNS, 0))
+      if (__glibc_unlikely (nsid == DL_NNS))
        {
          /* No more namespace available.  */
          __rtld_lock_unlock_recursive (GL(dl_load_lock));
@@ -659,7 +658,7 @@ no more namespaces available for dlmopen()"));
 #endif
 
   /* See if an error occurred during loading.  */
-  if (__builtin_expect (errstring != NULL, 0))
+  if (__glibc_unlikely (errstring != NULL))
     {
       /* Remove the object from memory.  It may be in an inconsistent
         state if relocation failed, for example.  */
@@ -740,7 +739,7 @@ _dl_show_scope (struct link_map *l, int from)
   _dl_debug_printf ("\n");
 }
 
-#ifdef IS_IN_rtld
+#if IS_IN (rtld)
 /* Return non-zero if ADDR lies within one of L's segments.  */
 int
 internal_function