about summary refs log tree commit diff
path: root/elf
diff options
context:
space:
mode:
Diffstat (limited to 'elf')
-rw-r--r--elf/Makefile14
-rw-r--r--elf/cache.c2
-rw-r--r--elf/dl-addr.c236
-rw-r--r--elf/dl-cache.c6
-rw-r--r--elf/dl-close.c158
-rw-r--r--elf/dl-deps.c2
-rw-r--r--elf/dl-dst.h9
-rw-r--r--elf/dl-fptr.c4
-rw-r--r--elf/dl-iteratephdr.c10
-rw-r--r--elf/dl-libc.c2
-rw-r--r--elf/dl-load.c18
-rw-r--r--elf/dl-lookup.c22
-rw-r--r--elf/dl-minimal.c24
-rw-r--r--elf/dl-object.c9
-rw-r--r--elf/dl-open.c139
-rw-r--r--elf/dl-profile.c14
-rw-r--r--elf/dl-reloc.c24
-rw-r--r--elf/dl-runtime.c42
-rw-r--r--elf/dl-support.c9
-rw-r--r--elf/dl-sym.c23
-rw-r--r--elf/dl-sysdep.c50
-rw-r--r--elf/dl-tls.c79
-rw-r--r--elf/do-lookup.h9
-rw-r--r--elf/elf.h5
-rw-r--r--elf/ldconfig.c22
-rw-r--r--elf/ldd.bash.in4
-rw-r--r--elf/rtld.c71
-rw-r--r--elf/sprof.c4
-rw-r--r--elf/tst-thrlock.c8
-rw-r--r--elf/tst-tls-dlinfo.c4
-rw-r--r--elf/tst-tls1.c8
-rw-r--r--elf/tst-tls10.h3
-rw-r--r--elf/tst-tls14.c8
-rw-r--r--elf/tst-tls2.c8
-rw-r--r--elf/tst-tls3.c8
-rw-r--r--elf/tst-tls4.c4
-rw-r--r--elf/tst-tls5.c4
-rw-r--r--elf/tst-tls6.c4
-rw-r--r--elf/tst-tls7.c4
-rw-r--r--elf/tst-tls8.c4
-rw-r--r--elf/tst-tls9.c4
-rw-r--r--elf/tst-tlsmod1.c4
-rw-r--r--elf/tst-tlsmod13.c3
-rw-r--r--elf/tst-tlsmod13a.c3
-rw-r--r--elf/tst-tlsmod14a.c10
-rw-r--r--elf/tst-tlsmod2.c2
-rw-r--r--elf/tst-tlsmod3.c4
-rw-r--r--elf/tst-tlsmod4.c4
-rw-r--r--elf/tst-tlsmod5.c2
-rw-r--r--elf/tst-tlsmod6.c2
50 files changed, 707 insertions, 409 deletions
diff --git a/elf/Makefile b/elf/Makefile
index afebaec1ec..06e376d803 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -159,8 +159,8 @@ endif
 ifeq (yes,$(build-shared))
 tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
 	 constload1 order $(tests-vis-$(have-protected)) noload filter unload \
-	 reldep reldep2 reldep3 reldep4 nodelete nodelete2 \
-	 nodlopen nodlopen2 neededtest neededtest2 \
+	 reldep reldep2 reldep3 reldep4 $(tests-nodelete-$(have-z-nodelete)) \
+	 $(tests-nodlopen-$(have-z-nodlopen)) neededtest neededtest2 \
 	 neededtest3 neededtest4 unload2 lateglobal initfirst global \
 	 restest2 next dblload dblunload reldep5 reldep6 reldep7 reldep8 \
 	 circleload1 tst-tls3 tst-tls4 tst-tls5 tst-tls6 tst-tls7 tst-tls8 \
@@ -175,6 +175,8 @@ tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
 #	 reldep9
 test-srcs = tst-pathopt
 tests-vis-yes = vismain
+tests-nodelete-yes = nodelete nodelete2
+tests-nodlopen-yes = nodlopen nodlopen2
 tests-execstack-yes = tst-execstack tst-execstack-needed tst-execstack-prog
 endif
 ifeq (yesyes,$(have-fpie)$(build-shared))
@@ -184,9 +186,8 @@ tests: $(objpfx)tst-leaks1-mem
 modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
 		testobj1_1 failobj constload2 constload3 unloadmod \
 		dep1 dep2 dep3 dep4 $(modules-vis-$(have-protected)) \
-		nodelmod1 nodelmod2 nodelmod3 nodelmod4 \
-		nodel2mod1 nodel2mod2 nodel2mod3 \
-		nodlopenmod nodlopenmod2 filtmod1 filtmod2 \
+		$(modules-nodelete-$(have-z-nodelete)) \
+		$(modules-nodlopen-$(have-z-nodlopen)) filtmod1 filtmod2 \
 		reldepmod1 reldepmod2 reldepmod3 reldepmod4 nextmod1 nextmod2 \
 		reldep4mod1 reldep4mod2 reldep4mod3 reldep4mod4 \
 		neededobj1 neededobj2 neededobj3 neededobj4 \
@@ -220,6 +221,9 @@ ifeq (yesyes,$(have-fpie)$(build-shared))
 modules-names += tst-piemod1
 endif
 modules-vis-yes = vismod1 vismod2 vismod3
+modules-nodelete-yes = nodelmod1 nodelmod2 nodelmod3 nodelmod4 \
+		       nodel2mod1 nodel2mod2 nodel2mod3
+modules-nodlopen-yes = nodlopenmod nodlopenmod2
 modules-execstack-yes = tst-execstack-mod
 extra-test-objs += $(addsuffix .os,$(strip $(modules-names)))
 # We need this variable to be sure the test modules get the right CPPFLAGS.
diff --git a/elf/cache.c b/elf/cache.c
index e18446644e..6730fb36eb 100644
--- a/elf/cache.c
+++ b/elf/cache.c
@@ -439,7 +439,7 @@ save_cache (const char *cache_name)
     }
 
   if (write (fd, strings, total_strlen) != (ssize_t) total_strlen)
-    error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
+    error (EXIT_FAILURE, errno, _("Writing of cache data failed."));
 
   close (fd);
 
diff --git a/elf/dl-addr.c b/elf/dl-addr.c
index e55dc4b46f..7dbf716cfa 100644
--- a/elf/dl-addr.c
+++ b/elf/dl-addr.c
@@ -1,5 +1,5 @@
 /* Locate the shared object symbol nearest a given address.
-   Copyright (C) 1996-2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1996-2004, 2005, 2006, 2007 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
@@ -22,139 +22,149 @@
 #include <ldsodefs.h>
 
 
-int
-internal_function
-_dl_addr (const void *address, Dl_info *info,
-	  struct link_map **mapp, const ElfW(Sym) **symbolp)
+static void
+__attribute ((always_inline))
+determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
+		struct link_map **mapp, const ElfW(Sym) **symbolp)
 {
-  const ElfW(Addr) addr = DL_LOOKUP_ADDRESS (address);
+  /* Now we know what object the address lies in.  */
+  info->dli_fname = match->l_name;
+  info->dli_fbase = (void *) match->l_map_start;
 
-  /* Protect against concurrent loads and unloads.  */
-  __rtld_lock_lock_recursive (GL(dl_load_lock));
+  /* If this is the main program the information is incomplete.  */
+  if (__builtin_expect (match->l_name[0], 'a') == '\0'
+      && match->l_type == lt_executable)
+    info->dli_fname = _dl_argv[0];
 
-  /* Find the highest-addressed object that ADDRESS is not below.  */
-  struct link_map *match = NULL;
-  for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
-    for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l; l = l->l_next)
-      if (addr >= l->l_map_start && addr < l->l_map_end)
-	{
-	  /* We know ADDRESS lies within L if in any shared object.
-	     Make sure it isn't past the end of L's segments.  */
-	  size_t n = l->l_phnum;
-	  if (n > 0)
-	    {
-	      do
-		--n;
-	      while (l->l_phdr[n].p_type != PT_LOAD);
-	      if (addr >= (l->l_addr +
-			   l->l_phdr[n].p_vaddr + l->l_phdr[n].p_memsz))
-		/* Off the end of the highest-addressed shared object.  */
-		continue;
-	    }
+  const ElfW(Sym) *symtab
+    = (const ElfW(Sym) *) D_PTR (match, l_info[DT_SYMTAB]);
+  const char *strtab = (const char *) D_PTR (match, l_info[DT_STRTAB]);
 
-	  match = l;
-	  break;
-	}
+  ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
 
-  int result = 0;
-  if (match != NULL)
+  const ElfW(Sym) *matchsym = NULL;
+  if (match->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM
+		    + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] != NULL)
     {
-      /* Now we know what object the address lies in.  */
-      info->dli_fname = match->l_name;
-      info->dli_fbase = (void *) match->l_map_start;
-
-      /* If this is the main program the information is incomplete.  */
-      if (__builtin_expect (match->l_name[0], 'a') == '\0'
-	  && match->l_type == lt_executable)
-	info->dli_fname = _dl_argv[0];
-
-      const ElfW(Sym) *symtab
-	= (const ElfW(Sym) *) D_PTR (match, l_info[DT_SYMTAB]);
-      const char *strtab = (const char *) D_PTR (match, l_info[DT_STRTAB]);
-
-      ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
-
-      const ElfW(Sym) *matchsym = NULL;
-      if (match->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM
-			+ DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] != NULL)
+      /* We look at all symbol table entries referenced by the hash
+	 table.  */
+      for (Elf_Symndx bucket = 0; bucket < match->l_nbuckets; ++bucket)
 	{
-	  /* We look at all symbol table entries referenced by the
-	     hash table.  */
-	  for (Elf_Symndx bucket = 0; bucket < match->l_nbuckets; ++bucket)
+	  Elf32_Word symndx = match->l_gnu_buckets[bucket];
+	  if (symndx != 0)
 	    {
-	      Elf32_Word symndx = match->l_gnu_buckets[bucket];
-	      if (symndx != 0)
+	      const Elf32_Word *hasharr = &match->l_gnu_chain_zero[symndx];
+
+	      do
 		{
-		  const Elf32_Word *hasharr = &match->l_gnu_chain_zero[symndx];
-
-		  do
-		    {
-		      /* The hash table never references local symbols
-			 so we can omit that test here.  */
-		      if ((symtab[symndx].st_shndx != SHN_UNDEF
-			   || symtab[symndx].st_value != 0)
-			  && ELFW(ST_TYPE) (symtab[symndx].st_info) != STT_TLS
-			  && DL_ADDR_SYM_MATCH (match, &symtab[symndx],
-						matchsym, addr)
-			  && symtab[symndx].st_name < strtabsize)
-			matchsym = (ElfW(Sym) *) &symtab[symndx];
-
-		      ++symndx;
-		    }
-		  while ((*hasharr++ & 1u) == 0);
+		  /* The hash table never references local symbols so
+		     we can omit that test here.  */
+		  if ((symtab[symndx].st_shndx != SHN_UNDEF
+		       || symtab[symndx].st_value != 0)
+#ifdef USE_TLS
+		      && ELFW(ST_TYPE) (symtab[symndx].st_info) != STT_TLS
+#endif
+		      && DL_ADDR_SYM_MATCH (match, &symtab[symndx],
+					    matchsym, addr)
+		      && symtab[symndx].st_name < strtabsize)
+		    matchsym = (ElfW(Sym) *) &symtab[symndx];
+
+		  ++symndx;
 		}
+	      while ((*hasharr++ & 1u) == 0);
 	    }
 	}
+    }
+  else
+    {
+      const ElfW(Sym) *symtabend;
+      if (match->l_info[DT_HASH] != NULL)
+	symtabend = (symtab
+		     + ((Elf_Symndx *) D_PTR (match, l_info[DT_HASH]))[1]);
       else
-	{
-	  const ElfW(Sym) *symtabend;
-	  if (match->l_info[DT_HASH] != NULL)
-	    symtabend = (symtab
-			 + ((Elf_Symndx *) D_PTR (match, l_info[DT_HASH]))[1]);
-	  else
-	    /* There is no direct way to determine the number of symbols in the
-	       dynamic symbol table and no hash table is present.  The ELF
-	       binary is ill-formed but what shall we do?  Use the beginning of
-	       the string table which generally follows the symbol table.  */
-	    symtabend = (const ElfW(Sym) *) strtab;
-
-	  for (; (void *) symtab < (void *) symtabend; ++symtab)
-	    if ((ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL
-		 || ELFW(ST_BIND) (symtab->st_info) == STB_WEAK)
-		&& ELFW(ST_TYPE) (symtab->st_info) != STT_TLS
-		&& (symtab->st_shndx != SHN_UNDEF
-		    || symtab->st_value != 0)
-		&& DL_ADDR_SYM_MATCH (match, symtab, matchsym, addr)
-		&& symtab->st_name < strtabsize)
-	      matchsym = (ElfW(Sym) *) symtab;
-	}
+	/* There is no direct way to determine the number of symbols in the
+	   dynamic symbol table and no hash table is present.  The ELF
+	   binary is ill-formed but what shall we do?  Use the beginning of
+	   the string table which generally follows the symbol table.  */
+	symtabend = (const ElfW(Sym) *) strtab;
+
+      for (; (void *) symtab < (void *) symtabend; ++symtab)
+	if ((ELFW(ST_BIND) (symtab->st_info) == STB_GLOBAL
+	     || ELFW(ST_BIND) (symtab->st_info) == STB_WEAK)
+#ifdef USE_TLS
+	    && ELFW(ST_TYPE) (symtab->st_info) != STT_TLS
+#endif
+	    && (symtab->st_shndx != SHN_UNDEF
+		|| symtab->st_value != 0)
+	    && DL_ADDR_SYM_MATCH (match, symtab, matchsym, addr)
+	    && symtab->st_name < strtabsize)
+	  matchsym = (ElfW(Sym) *) symtab;
+    }
 
-      if (mapp)
-	*mapp = match;
-      if (symbolp)
-	*symbolp = matchsym;
+  if (mapp)
+    *mapp = match;
+  if (symbolp)
+    *symbolp = matchsym;
 
-      if (matchsym)
-	{
-	  /* We found a symbol close by.  Fill in its name and exact
-	     address.  */
-	  lookup_t matchl = LOOKUP_VALUE (match);
+  if (matchsym)
+    {
+      /* We found a symbol close by.  Fill in its name and exact
+	 address.  */
+      lookup_t matchl = LOOKUP_VALUE (match);
 
-	  info->dli_sname = strtab + matchsym->st_name;
-	  info->dli_saddr = DL_SYMBOL_ADDRESS (matchl, matchsym);
-	}
-      else
+      info->dli_sname = strtab + matchsym->st_name;
+      info->dli_saddr = DL_SYMBOL_ADDRESS (matchl, matchsym);
+    }
+  else
+    {
+      /* No symbol matches.  We return only the containing object.  */
+      info->dli_sname = NULL;
+      info->dli_saddr = NULL;
+    }
+}
+
+
+int
+internal_function
+_dl_addr (const void *address, Dl_info *info,
+	  struct link_map **mapp, const ElfW(Sym) **symbolp)
+{
+  const ElfW(Addr) addr = DL_LOOKUP_ADDRESS (address);
+  int result = 0;
+
+  /* Protect against concurrent loads and unloads.  */
+  __rtld_lock_lock_recursive (GL(dl_load_lock));
+
+  /* Find the highest-addressed object that ADDRESS is not below.  */
+  for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
+    for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l; l = l->l_next)
+      if (addr >= l->l_map_start && addr < l->l_map_end
+	  && (l->l_contiguous || _dl_addr_inside_object (l, addr)))
 	{
-	  /* No symbol matches.  We return only the containing object.  */
-	  info->dli_sname = NULL;
-	  info->dli_saddr = NULL;
+	  determine_info (addr, l, info, mapp, symbolp);
+	  result = 1;
+	  goto out;
 	}
 
-      result = 1;
-    }
-
+ out:
   __rtld_lock_unlock_recursive (GL(dl_load_lock));
 
   return result;
 }
 libc_hidden_def (_dl_addr)
+
+/* Return non-zero if ADDR lies within one of L's segments.  */
+int
+internal_function
+_dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
+{
+  int n = l->l_phnum;
+  const ElfW(Addr) reladdr = addr - l->l_addr;
+
+  while (--n >= 0)
+    if (l->l_phdr[n].p_type == PT_LOAD
+	&& reladdr - l->l_phdr[n].p_vaddr >= 0
+	&& reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
+      return 1;
+  return 0;
+}
diff --git a/elf/dl-cache.c b/elf/dl-cache.c
index bbeba77e4a..fc7d9916c6 100644
--- a/elf/dl-cache.c
+++ b/elf/dl-cache.c
@@ -256,7 +256,11 @@ _dl_load_cache_lookup (const char *name)
 	platform = 1ULL << platform;
 
       /* Only accept hwcap if it's for the right platform.  */
-#define _DL_HWCAP_TLS_MASK (1LL << 63)
+#ifdef USE_TLS
+# define _DL_HWCAP_TLS_MASK (1LL << 63)
+#else
+# define _DL_HWCAP_TLS_MASK 0
+#endif
 #define HWCAP_CHECK \
       if (GLRO(dl_osversion) && lib->osversion > GLRO(dl_osversion))	      \
 	continue;							      \
diff --git a/elf/dl-close.c b/elf/dl-close.c
index df968fe649..5cd8b9be2d 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -1,5 +1,5 @@
 /* Close a shared object opened by `_dl_open'.
-   Copyright (C) 1996-2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1996-2005, 2006, 2007 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
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <sysdep-cancel.h>
+#include <tls.h>
 
 
 /* Type of the constructor functions.  */
@@ -41,6 +42,7 @@ typedef void (*fini_t) (void);
 #define IDX_STILL_USED -1
 
 
+#ifdef USE_TLS
 /* Returns true we an non-empty was found.  */
 static bool
 remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
@@ -102,13 +104,12 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
   /* No non-entry in this list element.  */
   return false;
 }
+#endif
 
 
 void
 _dl_close_worker (struct link_map *map)
 {
-  Lmid_t ns = map->l_ns;
-
   /* One less direct use.  */
   --map->l_direct_opencount;
 
@@ -131,11 +132,16 @@ _dl_close_worker (struct link_map *map)
       return;
     }
 
+  Lmid_t nsid = map->l_ns;
+  struct link_namespaces *ns = &GL(dl_ns)[nsid];
+
  retry:
   dl_close_state = pending;
 
+#ifdef USE_TLS
   bool any_tls = false;
-  const unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded;
+#endif
+  const unsigned int nloaded = ns->_ns_nloaded;
   char used[nloaded];
   char done[nloaded];
   struct link_map *maps[nloaded];
@@ -143,7 +149,7 @@ _dl_close_worker (struct link_map *map)
   /* Run over the list and assign indexes to the link maps and enter
      them into the MAPS array.  */
   int idx = 0;
-  for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
+  for (struct link_map *l = ns->_ns_loaded; l != NULL; l = l->l_next)
     {
       l->l_idx = idx;
       maps[idx] = l;
@@ -220,20 +226,22 @@ _dl_close_worker (struct link_map *map)
     }
 
   /* Sort the entries.  */
-  _dl_sort_fini (GL(dl_ns)[ns]._ns_loaded, maps, nloaded, used, ns);
+  _dl_sort_fini (ns->_ns_loaded, maps, nloaded, used, nsid);
 
   /* Call all termination functions at once.  */
 #ifdef SHARED
-  bool do_audit = GLRO(dl_naudit) > 0 && !GL(dl_ns)[ns]._ns_loaded->l_auditing;
+  bool do_audit = GLRO(dl_naudit) > 0 && !ns->_ns_loaded->l_auditing;
 #endif
   bool unload_any = false;
+  bool scope_mem_left = false;
+  unsigned int unload_global = 0;
   unsigned int first_loaded = ~0;
   for (unsigned int i = 0; i < nloaded; ++i)
     {
       struct link_map *imap = maps[i];
 
       /* All elements must be in the same namespace.  */
-      assert (imap->l_ns == ns);
+      assert (imap->l_ns == nsid);
 
       if (!used[i])
 	{
@@ -248,7 +256,7 @@ _dl_close_worker (struct link_map *map)
 	      if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS,
 				    0))
 		_dl_debug_printf ("\ncalling fini: %s [%lu]\n\n",
-				  imap->l_name, ns);
+				  imap->l_name, nsid);
 
 	      if (imap->l_info[DT_FINI_ARRAY] != NULL)
 		{
@@ -291,6 +299,9 @@ _dl_close_worker (struct link_map *map)
 	  /* We indeed have an object to remove.  */
 	  unload_any = true;
 
+	  if (imap->l_global)
+	    ++unload_global;
+
 	  /* Remember where the first dynamically loaded object is.  */
 	  if (i < first_loaded)
 	    first_loaded = i;
@@ -334,7 +345,7 @@ _dl_close_worker (struct link_map *map)
 		struct link_map *tmap = (struct link_map *)
 		  ((char *) imap->l_scope[cnt]
 		   - offsetof (struct link_map, l_searchlist));
-		assert (tmap->l_ns == ns);
+		assert (tmap->l_ns == nsid);
 		if (tmap->l_idx == IDX_STILL_USED)
 		  ++remain;
 		else
@@ -399,18 +410,18 @@ _dl_close_worker (struct link_map *map)
 
 	      struct r_scope_elem **old = imap->l_scope;
 
-	      if (RTLD_SINGLE_THREAD_P)
-		imap->l_scope = newp;
-	      else
-		{
-		  __rtld_mrlock_change (imap->l_scope_lock);
-		  imap->l_scope = newp;
-		  __rtld_mrlock_done (imap->l_scope_lock);
-		}
+	      imap->l_scope = newp;
 
 	      /* No user anymore, we can free it now.  */
 	      if (old != imap->l_scope_mem)
-		free (old);
+		{
+		  if (_dl_scope_free (old))
+		    /* If _dl_scope_free used THREAD_GSCOPE_WAIT (),
+		       no need to repeat it.  */
+		    scope_mem_left = false;
+		}
+	      else
+		scope_mem_left = true;
 
 	      imap->l_scope_max = new_size;
 	    }
@@ -435,7 +446,7 @@ _dl_close_worker (struct link_map *map)
   /* Auditing checkpoint: we will start deleting objects.  */
   if (__builtin_expect (do_audit, 0))
     {
-      struct link_map *head = GL(dl_ns)[ns]._ns_loaded;
+      struct link_map *head = ns->_ns_loaded;
       struct audit_ifaces *afct = GLRO(dl_audit);
       /* Do not call the functions for any auditing object.  */
       if (head->l_auditing == 0)
@@ -452,13 +463,55 @@ _dl_close_worker (struct link_map *map)
 #endif
 
   /* Notify the debugger we are about to remove some loaded objects.  */
-  struct r_debug *r = _dl_debug_initialize (0, ns);
+  struct r_debug *r = _dl_debug_initialize (0, nsid);
   r->r_state = RT_DELETE;
   _dl_debug_state ();
 
+  if (unload_global)
+    {
+      /* Some objects are in the global scope list.  Remove them.  */
+      struct r_scope_elem *ns_msl = ns->_ns_main_searchlist;
+      unsigned int i;
+      unsigned int j = 0;
+      unsigned int cnt = ns_msl->r_nlist;
+
+      while (cnt > 0 && ns_msl->r_list[cnt - 1]->l_removed)
+	--cnt;
+
+      if (cnt + unload_global == ns_msl->r_nlist)
+	/* Speed up removing most recently added objects.  */
+	j = cnt;
+      else
+	for (i = 0; i < cnt; i++)
+	  if (ns_msl->r_list[i]->l_removed == 0)
+	    {
+	      if (i != j)
+		ns_msl->r_list[j] = ns_msl->r_list[i];
+	      j++;
+	    }
+      ns_msl->r_nlist = j;
+    }
+
+  if (!RTLD_SINGLE_THREAD_P
+      && (unload_global
+	  || scope_mem_left
+	  || (GL(dl_scope_free_list) != NULL
+	      && GL(dl_scope_free_list)->count)))
+    {
+      struct dl_scope_free_list *fsl;
+
+      THREAD_GSCOPE_WAIT ();
+      /* Now we can free any queued old scopes.  */
+      if ((fsl = GL(dl_scope_free_list)) != NULL)
+	while (fsl->count > 0)
+	  free (fsl->list[--fsl->count]);
+    }
+
+#ifdef USE_TLS
   size_t tls_free_start;
   size_t tls_free_end;
   tls_free_start = tls_free_end = NO_TLS_OFFSET;
+#endif
 
   /* Check each element of the search list to see if all references to
      it are gone.  */
@@ -471,24 +524,8 @@ _dl_close_worker (struct link_map *map)
 
 	  /* That was the last reference, and this was a dlopen-loaded
 	     object.  We can unmap it.  */
-	  if (__builtin_expect (imap->l_global, 0))
-	    {
-	      /* This object is in the global scope list.  Remove it.  */
-	      unsigned int cnt = GL(dl_ns)[ns]._ns_main_searchlist->r_nlist;
-
-	      do
-		--cnt;
-	      while (GL(dl_ns)[ns]._ns_main_searchlist->r_list[cnt] != imap);
-
-	      /* The object was already correctly registered.  */
-	      while (++cnt
-		     < GL(dl_ns)[ns]._ns_main_searchlist->r_nlist)
-		GL(dl_ns)[ns]._ns_main_searchlist->r_list[cnt - 1]
-		  = GL(dl_ns)[ns]._ns_main_searchlist->r_list[cnt];
-
-	      --GL(dl_ns)[ns]._ns_main_searchlist->r_nlist;
-	    }
 
+#ifdef USE_TLS
 	  /* Remove the object from the dtv slotinfo array if it uses TLS.  */
 	  if (__builtin_expect (imap->l_tls_blocksize > 0, 0))
 	    {
@@ -507,7 +544,7 @@ _dl_close_worker (struct link_map *map)
 		     this search list, going in either direction.  When the
 		     whole chunk is at the end of the used area then we can
 		     reclaim it.  */
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
 		  if (tls_free_start == NO_TLS_OFFSET
 		      || (size_t) imap->l_tls_offset == tls_free_start)
 		    {
@@ -547,7 +584,7 @@ _dl_close_worker (struct link_map *map)
 			    = tls_free_end - imap->l_tls_blocksize;
 			}
 		    }
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
 		  if ((size_t) imap->l_tls_offset == tls_free_end)
 		    /* Extend the contiguous chunk being reclaimed.  */
 		    tls_free_end -= imap->l_tls_blocksize;
@@ -564,11 +601,12 @@ _dl_close_worker (struct link_map *map)
 		      tls_free_start = imap->l_tls_offset;
 		      tls_free_end = tls_free_start + imap->l_tls_blocksize;
 		    }
-#else
-# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
-#endif
+# else
+#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+# endif
 		}
 	    }
+#endif
 
 	  /* We can unmap all the maps at once.  We determined the
 	     start address and length when we loaded the object and
@@ -581,12 +619,12 @@ _dl_close_worker (struct link_map *map)
 	  else
 	    {
 #ifdef SHARED
-	      assert (ns != LM_ID_BASE);
+	      assert (nsid != LM_ID_BASE);
 #endif
-	      GL(dl_ns)[ns]._ns_loaded = imap->l_next;
+	      ns->_ns_loaded = imap->l_next;
 	    }
 
-	  --GL(dl_ns)[ns]._ns_nloaded;
+	  --ns->_ns_nloaded;
 	  if (imap->l_next != NULL)
 	    imap->l_next->l_prev = imap->l_prev;
 
@@ -634,6 +672,7 @@ _dl_close_worker (struct link_map *map)
 	}
     }
 
+#ifdef USE_TLS
   /* If we removed any object which uses TLS bump the generation counter.  */
   if (any_tls)
     {
@@ -643,12 +682,13 @@ _dl_close_worker (struct link_map *map)
       if (tls_free_end == GL(dl_tls_static_used))
 	GL(dl_tls_static_used) = tls_free_start;
     }
+#endif
 
 #ifdef SHARED
   /* Auditing checkpoint: we have deleted all objects.  */
   if (__builtin_expect (do_audit, 0))
     {
-      struct link_map *head = GL(dl_ns)[ns]._ns_loaded;
+      struct link_map *head = ns->_ns_loaded;
       /* Do not call the functions for any auditing object.  */
       if (head->l_auditing == 0)
 	{
@@ -702,6 +742,7 @@ _dl_close (void *_map)
 }
 
 
+#ifdef USE_TLS
 static bool __libc_freeres_fn_section
 free_slotinfo (struct dtv_slotinfo_list **elemp)
 {
@@ -728,45 +769,52 @@ free_slotinfo (struct dtv_slotinfo_list **elemp)
 
   return true;
 }
+#endif
 
 
 libc_freeres_fn (free_mem)
 {
-  for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
-    if (__builtin_expect (GL(dl_ns)[ns]._ns_global_scope_alloc, 0) != 0
-	&& (GL(dl_ns)[ns]._ns_main_searchlist->r_nlist
+  for (Lmid_t nsid = 0; nsid < DL_NNS; ++nsid)
+    if (__builtin_expect (GL(dl_ns)[nsid]._ns_global_scope_alloc, 0) != 0
+	&& (GL(dl_ns)[nsid]._ns_main_searchlist->r_nlist
 	    // XXX Check whether we need NS-specific initial_searchlist
 	    == GLRO(dl_initial_searchlist).r_nlist))
       {
 	/* All object dynamically loaded by the program are unloaded.  Free
 	   the memory allocated for the global scope variable.  */
-	struct link_map **old = GL(dl_ns)[ns]._ns_main_searchlist->r_list;
+	struct link_map **old = GL(dl_ns)[nsid]._ns_main_searchlist->r_list;
 
 	/* Put the old map in.  */
-	GL(dl_ns)[ns]._ns_main_searchlist->r_list
+	GL(dl_ns)[nsid]._ns_main_searchlist->r_list
 	  // XXX Check whether we need NS-specific initial_searchlist
 	  = GLRO(dl_initial_searchlist).r_list;
 	/* Signal that the original map is used.  */
-	GL(dl_ns)[ns]._ns_global_scope_alloc = 0;
+	GL(dl_ns)[nsid]._ns_global_scope_alloc = 0;
 
 	/* Now free the old map.  */
 	free (old);
       }
 
+#ifdef USE_TLS
   if (USE___THREAD || GL(dl_tls_dtv_slotinfo_list) != NULL)
     {
       /* Free the memory allocated for the dtv slotinfo array.  We can do
 	 this only if all modules which used this memory are unloaded.  */
-#ifdef SHARED
+# ifdef SHARED
       if (GL(dl_initial_dtv) == NULL)
 	/* There was no initial TLS setup, it was set up later when
 	   it used the normal malloc.  */
 	free_slotinfo (&GL(dl_tls_dtv_slotinfo_list));
       else
-#endif
+# endif
 	/* The first element of the list does not have to be deallocated.
 	   It was allocated in the dynamic linker (i.e., with a different
 	   malloc), and in the static library it's in .bss space.  */
 	free_slotinfo (&GL(dl_tls_dtv_slotinfo_list)->next);
     }
+#endif
+
+  void *scope_free_list = GL(dl_scope_free_list);
+  GL(dl_scope_free_list) = NULL;
+  free (scope_free_list);
 }
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
index 4ec984e15b..c35cc977fa 100644
--- a/elf/dl-deps.c
+++ b/elf/dl-deps.c
@@ -123,7 +123,7 @@ DST not allowed in SUID/SGID programs"));				      \
 	       processed.  */						      \
 	    if (fatal)							      \
 	      _dl_signal_error (0, __str, NULL, N_("\
-empty dynamic string token substitution"));				      \
+empty dynamics string token substitution"));				      \
 	    else							      \
 	      {								      \
 		/* This is for DT_AUXILIARY.  */			      \
diff --git a/elf/dl-dst.h b/elf/dl-dst.h
index 83d16bdb0e..175b7cd195 100644
--- a/elf/dl-dst.h
+++ b/elf/dl-dst.h
@@ -1,5 +1,6 @@
 /* Handling of dynamic sring tokens.
-   Copyright (C) 1999,2001,2002,2003,2004,2006 Free Software Foundation, Inc.
+   Copyright (C) 1999,2001,2002,2003,2004,2006,2007
+   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
@@ -50,7 +51,7 @@
 									      \
 	   First get the origin string if it is not available yet.	      \
 	   This can only happen for the map of the executable.  */	      \
-	DL_DST_REQ_STATIC						      \
+	DL_DST_REQ_STATIC (l)						      \
 	if ((l)->l_origin == NULL)					      \
 	  {								      \
 	    assert ((l)->l_name[0] == '\0');				      \
@@ -68,9 +69,9 @@
     __len; })
 
 #ifdef SHARED
-# define DL_DST_REQ_STATIC /* nothing */
+# define DL_DST_REQ_STATIC(l) /* nothing */
 #else
-# define DL_DST_REQ_STATIC \
+# define DL_DST_REQ_STATIC(l) \
   if ((l) == NULL)							      \
     {									      \
       const char *origin = _dl_get_origin ();				      \
diff --git a/elf/dl-fptr.c b/elf/dl-fptr.c
index e068124d6f..78beecfdcb 100644
--- a/elf/dl-fptr.c
+++ b/elf/dl-fptr.c
@@ -1,5 +1,5 @@
 /* Manage function descriptors.  Generic version.
-   Copyright (C) 1999-2004, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1999,2000,2001,2002,2003,2004 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
@@ -40,7 +40,7 @@
 
 #ifndef COMPARE_AND_SWAP
 # define COMPARE_AND_SWAP(ptr, old, new) \
-  (catomic_compare_and_exchange_bool_acq (ptr, new, old) == 0)
+  (atomic_compare_and_exchange_bool_acq (ptr, new, old) == 0)
 #endif
 
 ElfW(Addr) _dl_boot_fptr_table [ELF_MACHINE_BOOT_FPTR_TABLE_LEN];
diff --git a/elf/dl-iteratephdr.c b/elf/dl-iteratephdr.c
index d03d8b6daf..b29534d5c9 100644
--- a/elf/dl-iteratephdr.c
+++ b/elf/dl-iteratephdr.c
@@ -1,5 +1,5 @@
 /* Get loaded objects program headers.
-   Copyright (C) 2001,2002,2003,2004,2006 Free Software Foundation, Inc.
+   Copyright (C) 2001,2002,2003,2004,2006,2007 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Jakub Jelinek <jakub@redhat.com>, 2001.
 
@@ -54,9 +54,9 @@ __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
 	nloaded += GL(dl_ns)[cnt]._ns_nloaded;
 
 	if (caller >= (const void *) l->l_map_start
-	    && caller < (const void *) l->l_map_end)
-	  /* There must be exactly one DSO for the range of the virtual
-	     memory.  Otherwise something is really broken.  */
+	    && caller < (const void *) l->l_map_end
+	    && (l->l_contiguous
+		|| _dl_addr_inside_object (l, (ElfW(Addr)) caller)))
 	  ns = cnt;
       }
 
@@ -70,9 +70,11 @@ __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
       info.dlpi_subs = GL(dl_load_adds) - nloaded;
       info.dlpi_tls_modid = 0;
       info.dlpi_tls_data = NULL;
+#ifdef USE_TLS
       info.dlpi_tls_modid = l->l_tls_modid;
       if (info.dlpi_tls_modid != 0)
 	info.dlpi_tls_data = _dl_tls_get_addr_soft (l);
+#endif
       ret = callback (&info, sizeof (struct dl_phdr_info), data);
       if (ret)
 	break;
diff --git a/elf/dl-libc.c b/elf/dl-libc.c
index a6d0d1fcef..1b995eda92 100644
--- a/elf/dl-libc.c
+++ b/elf/dl-libc.c
@@ -1,5 +1,5 @@
 /* Handle loading and unloading shared objects for internal libc purposes.
-   Copyright (C) 1999-2002,2004,2005,2006 Free Software Foundation, Inc.
+   Copyright (C) 1999,2000,2001,2002,2004,2005 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Zack Weinberg <zack@rabi.columbia.edu>, 1999.
 
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 1650ef953a..9625030e88 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1,5 +1,5 @@
 /* Map in a shared object's segments from the file.
-   Copyright (C) 1995-2005, 2006  Free Software Foundation, Inc.
+   Copyright (C) 1995-2005, 2006, 2007  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
@@ -1078,6 +1078,7 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
 	  break;
 
 	case PT_TLS:
+#ifdef USE_TLS
 	  if (ph->p_memsz == 0)
 	    /* Nothing to do for an empty segment.  */
 	    break;
@@ -1105,7 +1106,7 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
 	      break;
 	    }
 
-#ifdef SHARED
+# ifdef SHARED
 	  if (l->l_prev == NULL || (mode & __RTLD_AUDIT) != 0)
 	    /* We are loading the executable itself when the dynamic linker
 	       was executed directly.  The setup will happen later.  */
@@ -1114,7 +1115,7 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
 	  /* In a static binary there is no way to tell if we dynamically
 	     loaded libpthread.  */
 	  if (GL(dl_error_catch_tsd) == &_dl_initial_error_catch_tsd)
-#endif
+# endif
 	    {
 	      /* We have not yet loaded libpthread.
 		 We can do the TLS setup right now!  */
@@ -1147,6 +1148,7 @@ cannot allocate TLS data structures for initial thread");
 	      _dl_deallocate_tls (tcb, 1);
 	      goto call_lose;
 	    }
+#endif
 
 	  /* Uh-oh, the binary expects TLS support but we cannot
 	     provide it.  */
@@ -1223,6 +1225,8 @@ cannot allocate TLS data structures for initial thread");
 		      loadcmds[nloadcmds - 1].mapstart - c->mapend,
 		      PROT_NONE);
 
+	l->l_contiguous = 1;
+
 	goto postmap;
       }
 
@@ -1242,6 +1246,7 @@ cannot allocate TLS data structures for initial thread");
     /* Remember which part of the address space this object uses.  */
     l->l_map_start = c->mapstart + l->l_addr;
     l->l_map_end = l->l_map_start + maplength;
+    l->l_contiguous = !has_holes;
 
     while (c < &loadcmds[nloadcmds])
       {
@@ -1385,7 +1390,7 @@ cannot allocate TLS data structures for initial thread");
 	 requires that it be executable.  We must change the
 	 protection of the variable which contains the flags used in
 	 the mprotect calls.  */
-#ifdef SHARED
+#if defined HAVE_Z_RELRO && defined SHARED
       if ((mode & (__RTLD_DLOPEN | __RTLD_AUDIT)) == __RTLD_DLOPEN)
 	{
 	  const uintptr_t p = (uintptr_t) &__stack_prot & -GLRO(dl_pagesize);
@@ -1422,9 +1427,11 @@ cannot enable executable stack as shared object requires");
 	}
     }
 
+#ifdef USE_TLS
   /* Adjust the address of the TLS initialization image.  */
   if (l->l_tls_initimage != NULL)
     l->l_tls_initimage = (char *) l->l_tls_initimage + l->l_addr;
+#endif
 
   /* We are done mapping in the file.  We no longer need the descriptor.  */
   if (__builtin_expect (__close (fd) != 0, 0))
@@ -1928,10 +1935,11 @@ open_path (const char *name, size_t namelen, int preloaded,
 	 must not be freed using the general free() in libc.  */
       if (sps->malloced)
 	free (sps->dirs);
-
+#ifdef HAVE_Z_RELRO
       /* rtld_search_dirs is attribute_relro, therefore avoid writing
 	 into it.  */
       if (sps != &rtld_search_dirs)
+#endif
 	sps->dirs = (void *) -1;
     }
 
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index 019278c9b0..e971929d0e 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -1,5 +1,5 @@
 /* Look up a symbol in the loaded objects.
-   Copyright (C) 1995-2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-2005, 2006, 2007 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
@@ -25,6 +25,7 @@
 #include <ldsodefs.h>
 #include <dl-hash.h>
 #include <dl-machine.h>
+#include <sysdep-cancel.h>
 #include <bits/libc-lock.h>
 #include <tls.h>
 
@@ -228,17 +229,13 @@ _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
 
   /* No other flag than DL_LOOKUP_ADD_DEPENDENCY is allowed if we look
      up a versioned symbol.  */
-  assert (version == NULL || flags == 0 || flags == DL_LOOKUP_ADD_DEPENDENCY);
+  assert (version == NULL || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY)) == 0);
 
   size_t i = 0;
   if (__builtin_expect (skip_map != NULL, 0))
-    {
-      /* Search the relevant loaded objects for a definition.  */
-      while ((*scope)->r_list[i] != skip_map)
-	++i;
-
-      assert (i < (*scope)->r_nlist);
-    }
+    /* Search the relevant loaded objects for a definition.  */
+    while ((*scope)->r_list[i] != skip_map)
+      ++i;
 
   /* Search the relevant loaded objects for a definition.  */
   for (size_t start = i; *scope != NULL; start = 0, ++scope)
@@ -341,9 +338,8 @@ _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
       && add_dependency (undef_map, current_value.m) < 0)
       /* Something went wrong.  Perhaps the object we tried to reference
 	 was just removed.  Try finding another definition.  */
-      return _dl_lookup_symbol_x (undef_name, undef_map, ref,
-				  symbol_scope, version, type_class,
-				  flags, skip_map);
+      return _dl_lookup_symbol_x (undef_name, undef_map, ref, symbol_scope,
+				  version, type_class, flags, skip_map);
 
   /* The object is used.  */
   current_value.m->l_used = 1;
@@ -449,10 +445,12 @@ _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
 	    conflict = 1;
 	}
 
+# ifdef USE_TLS
       if (value->s
 	  && (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
 				== STT_TLS, 0)))
 	type_class = 4;
+# endif
 
       if (conflict
 	  || GLRO(dl_trace_prelink_map) == undef_map
diff --git a/elf/dl-minimal.c b/elf/dl-minimal.c
index 8e78709b5a..eeea790e40 100644
--- a/elf/dl-minimal.c
+++ b/elf/dl-minimal.c
@@ -1,5 +1,5 @@
 /* Minimal replacements for basic facilities used in the dynamic linker.
-   Copyright (C) 1995-1998,2000-2002,2004,2005,2006
+   Copyright (C) 1995-1998,2000-2002,2004-2006,2007
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -75,14 +75,21 @@ __libc_memalign (size_t align, size_t n)
   alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + align - 1)
 			    & ~(align - 1));
 
-  if (alloc_ptr + n >= alloc_end)
+  if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
     {
       /* Insufficient space left; allocate another page.  */
       caddr_t page;
       size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
+      if (__builtin_expect (nup == 0, 0))
+	{
+	  if (n)
+	    return NULL;
+	  nup = GLRO(dl_pagesize);
+	}
       page = __mmap (0, nup, PROT_READ|PROT_WRITE,
 		     MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
-      assert (page != MAP_FAILED);
+      if (page == MAP_FAILED)
+	return NULL;
       if (page != alloc_end)
 	alloc_ptr = page;
       alloc_end = page + nup;
@@ -108,7 +115,14 @@ calloc (size_t nmemb, size_t size)
   /* New memory from the trivial malloc above is always already cleared.
      (We make sure that's true in the rare occasion it might not be,
      by clearing memory in free, below.)  */
-  return malloc (nmemb * size);
+  size_t bytes = nmemb * size;
+
+#define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
+  if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
+      && size != 0 && bytes / size != nmemb)
+    return NULL;
+
+  return malloc (bytes);
 }
 
 /* This will rarely be called.  */
@@ -264,7 +278,7 @@ __strtoul_internal (const char *nptr, char **endptr, int base, int group)
   while (*nptr >= '0' && *nptr <= '9')
     {
       unsigned long int digval = *nptr - '0';
-      if (result > LONG_MAX / 10
+      if (result > ULONG_MAX / 10
 	  || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
 	{
 	  errno = ERANGE;
diff --git a/elf/dl-object.c b/elf/dl-object.c
index 33ee860e59..86f7a8e4d9 100644
--- a/elf/dl-object.c
+++ b/elf/dl-object.c
@@ -1,5 +1,5 @@
 /* Storage management for the chain of loaded shared objects.
-   Copyright (C) 1995-2002, 2004, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-2002, 2004 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
@@ -64,7 +64,7 @@ _dl_new_object (char *realname, const char *libname, int type,
   new->l_name = realname;
   new->l_type = type;
   new->l_loader = loader;
-#if NO_TLS_OFFSET != 0
+#if defined USE_TLS && NO_TLS_OFFSET != 0
   new->l_tls_offset = NO_TLS_OFFSET;
 #endif
   new->l_ns = nsid;
@@ -85,11 +85,6 @@ _dl_new_object (char *realname, const char *libname, int type,
   new->l_scope = new->l_scope_mem;
   new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
 
-  /* No need to initialize the scope lock if the initializer is zero.  */
-#if _RTLD_MRLOCK_INITIALIZER != 0
-  __rtld_mrlock_initialize (new->l_scope_lock);
-#endif
-
   /* Counter for the scopes we have to handle.  */
   idx = 0;
 
diff --git a/elf/dl-open.c b/elf/dl-open.c
index c997521126..32e7caac7f 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -1,5 +1,5 @@
 /* Load a shared object at runtime, relocate it, and run its initializer.
-   Copyright (C) 1996-2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1996-2004, 2005, 2006, 2007 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
@@ -32,6 +32,7 @@
 #include <bp-sym.h>
 #include <caller.h>
 #include <sysdep-cancel.h>
+#include <tls.h>
 
 #include <dl-dst.h>
 
@@ -97,17 +98,17 @@ add_to_global (struct link_map *new)
      in an realloc() call.  Therefore we allocate a completely new
      array the first time we have to add something to the locale scope.  */
 
-  if (GL(dl_ns)[new->l_ns]._ns_global_scope_alloc == 0)
+  struct link_namespaces *ns = &GL(dl_ns)[new->l_ns];
+  if (ns->_ns_global_scope_alloc == 0)
     {
       /* This is the first dynamic object given global scope.  */
-      GL(dl_ns)[new->l_ns]._ns_global_scope_alloc
-	= GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist + to_add + 8;
+      ns->_ns_global_scope_alloc
+	= ns->_ns_main_searchlist->r_nlist + to_add + 8;
       new_global = (struct link_map **)
-	malloc (GL(dl_ns)[new->l_ns]._ns_global_scope_alloc
-		* sizeof (struct link_map *));
+	malloc (ns->_ns_global_scope_alloc * sizeof (struct link_map *));
       if (new_global == NULL)
 	{
-	  GL(dl_ns)[new->l_ns]._ns_global_scope_alloc = 0;
+	  ns->_ns_global_scope_alloc = 0;
 	nomem:
 	  _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
 			    N_("cannot extend global scope"));
@@ -115,29 +116,39 @@ add_to_global (struct link_map *new)
 	}
 
       /* Copy over the old entries.  */
-      GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list
-	= memcpy (new_global,
-		  GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list,
-		  (GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist
+      ns->_ns_main_searchlist->r_list
+	= memcpy (new_global, ns->_ns_main_searchlist->r_list,
+		  (ns->_ns_main_searchlist->r_nlist
 		   * sizeof (struct link_map *)));
     }
-  else if (GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist + to_add
-	   > GL(dl_ns)[new->l_ns]._ns_global_scope_alloc)
+  else if (ns->_ns_main_searchlist->r_nlist + to_add
+	   > ns->_ns_global_scope_alloc)
     {
       /* We have to extend the existing array of link maps in the
 	 main map.  */
+      struct link_map **old_global
+	= GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list;
+      size_t new_nalloc = ((ns->_ns_global_scope_alloc + to_add) * 2);
+
       new_global = (struct link_map **)
-	realloc (GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list,
-		 ((GL(dl_ns)[new->l_ns]._ns_global_scope_alloc + to_add + 8)
-		  * sizeof (struct link_map *)));
+	malloc (new_nalloc * sizeof (struct link_map *));
       if (new_global == NULL)
 	goto nomem;
 
-      GL(dl_ns)[new->l_ns]._ns_global_scope_alloc += to_add + 8;
-      GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list = new_global;
+      memcpy (new_global, old_global,
+	      ns->_ns_global_scope_alloc * sizeof (struct link_map *));
+
+      ns->_ns_global_scope_alloc = new_nalloc;
+      ns->_ns_main_searchlist->r_list = new_global;
+
+      if (!RTLD_SINGLE_THREAD_P)
+	THREAD_GSCOPE_WAIT ();
+
+      free (old_global);
     }
 
   /* Now add the new entries.  */
+  unsigned int new_nlist = ns->_ns_main_searchlist->r_nlist;
   for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
     {
       struct link_map *map = new->l_searchlist.r_list[cnt];
@@ -145,15 +156,49 @@ add_to_global (struct link_map *new)
       if (map->l_global == 0)
 	{
 	  map->l_global = 1;
-	  GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list[GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist]
-	    = map;
-	  ++GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist;
+	  ns->_ns_main_searchlist->r_list[new_nlist++] = map;
 	}
     }
+  atomic_write_barrier ();
+  ns->_ns_main_searchlist->r_nlist = new_nlist;
 
   return 0;
 }
 
+int
+_dl_scope_free (struct r_scope_elem **old)
+{
+  struct dl_scope_free_list *fsl;
+#define DL_SCOPE_FREE_LIST_SIZE (sizeof (fsl->list) / sizeof (fsl->list[0]))
+
+  if (RTLD_SINGLE_THREAD_P)
+    free (old);
+  else if ((fsl = GL(dl_scope_free_list)) == NULL)
+    {
+      GL(dl_scope_free_list) = fsl = malloc (sizeof (*fsl));
+      if (fsl == NULL)
+	{
+	  THREAD_GSCOPE_WAIT ();
+	  free (old);
+	  return 1;
+	}
+      else
+	{
+	  fsl->list[0] = old;
+	  fsl->count = 1;
+	}
+    }
+  else if (fsl->count < DL_SCOPE_FREE_LIST_SIZE)
+    fsl->list[fsl->count++] = old;
+  else
+    {
+      THREAD_GSCOPE_WAIT ();
+      while (fsl->count > 0)
+	free (fsl->list[--fsl->count]);
+      return 1;
+    }
+  return 0;
+}
 
 static void
 dl_open_worker (void *a)
@@ -161,10 +206,12 @@ dl_open_worker (void *a)
   struct dl_open_args *args = a;
   const char *file = args->file;
   int mode = args->mode;
-  struct link_map *new, *l;
+  struct link_map *new;
   int lazy;
   unsigned int i;
+#ifdef USE_TLS
   bool any_tls = false;
+#endif
   struct link_map *call_map = NULL;
 
   /* Check whether _dl_open() has been called from a valid DSO.  */
@@ -186,13 +233,14 @@ dl_open_worker (void *a)
 	 By default we assume this is the main application.  */
       call_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
 
+      struct link_map *l;
       for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
 	for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
 	  if (caller_dlopen >= (const void *) l->l_map_start
-	      && caller_dlopen < (const void *) l->l_map_end)
+	      && caller_dlopen < (const void *) l->l_map_end
+	      && (l->l_contiguous
+		  || _dl_addr_inside_object (l, (ElfW(Addr)) caller_dlopen)))
 	    {
-	      /* There must be exactly one DSO for the range of the virtual
-		 memory.  Otherwise something is really broken.  */
 	      assert (ns == l->l_ns);
 	      call_map = l;
 	      goto found_caller;
@@ -325,7 +373,7 @@ dl_open_worker (void *a)
   /* Relocate the objects loaded.  We do this in reverse order so that copy
      relocs of earlier objects overwrite the data written by later objects.  */
 
-  l = new;
+  struct link_map *l = new;
   while (l->l_next)
     l = l->l_next;
   while (1)
@@ -417,17 +465,10 @@ dl_open_worker (void *a)
 	      memcpy (newp, imap->l_scope, cnt * sizeof (imap->l_scope[0]));
 	      struct r_scope_elem **old = imap->l_scope;
 
-	      if (RTLD_SINGLE_THREAD_P)
-		imap->l_scope = newp;
-	      else
-		{
-		  __rtld_mrlock_change (imap->l_scope_lock);
-		  imap->l_scope = newp;
-		  __rtld_mrlock_done (imap->l_scope_lock);
-		}
+	      imap->l_scope = newp;
 
 	      if (old != imap->l_scope_mem)
-		free (old);
+		_dl_scope_free (old);
 
 	      imap->l_scope_max = new_size;
 	    }
@@ -439,6 +480,7 @@ dl_open_worker (void *a)
 	  atomic_write_barrier ();
 	  imap->l_scope[cnt] = &new->l_searchlist;
 	}
+#if USE_TLS
       /* Only add TLS memory if this object is loaded now and
 	 therefore is not yet initialized.  */
       else if (! imap->l_init_called
@@ -453,11 +495,11 @@ dl_open_worker (void *a)
 	  if (imap->l_need_tls_init)
 	    {
 	      imap->l_need_tls_init = 0;
-#ifdef SHARED
+# ifdef SHARED
 	      /* Update the slot information data for at least the
 		 generation of the DSO we are allocating data for.  */
 	      _dl_update_slotinfo (imap->l_tls_modid);
-#endif
+# endif
 
 	      GL(dl_init_static_tls) (imap);
 	      assert (imap->l_need_tls_init == 0);
@@ -466,12 +508,15 @@ dl_open_worker (void *a)
 	  /* We have to bump the generation counter.  */
 	  any_tls = true;
 	}
+#endif
     }
 
+#if USE_TLS
   /* Bump the generation number if necessary.  */
   if (any_tls && __builtin_expect (++GL(dl_tls_generation) == 0, 0))
     _dl_fatal_printf (N_("\
 TLS generation counter wrapped!  Please report this."));
+#endif
 
   /* Run the initializer functions of new objects.  */
   _dl_init (new, args->argc, args->argv, args->env);
@@ -568,6 +613,7 @@ no more namespaces available for dlmopen()"));
 	 state if relocation failed, for example.  */
       if (args.map)
 	{
+#ifdef USE_TLS
 	  /* Maybe some of the modules which were loaded use TLS.
 	     Since it will be removed in the following _dl_close call
 	     we have to mark the dtv array as having gaps to fill the
@@ -577,6 +623,7 @@ no more namespaces available for dlmopen()"));
 	     up.  */
 	  if ((mode & __RTLD_AUDIT) == 0)
 	    GL(dl_tls_dtv_gaps) = true;
+#endif
 
 	  _dl_close_worker (args.map);
 	}
@@ -650,3 +697,21 @@ show_scope (struct link_map *new)
     }
 }
 #endif
+
+#ifdef IS_IN_rtld
+/* Return non-zero if ADDR lies within one of L's segments.  */
+int
+internal_function
+_dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
+{
+  int n = l->l_phnum;
+  const ElfW(Addr) reladdr = addr - l->l_addr;
+
+  while (--n >= 0)
+    if (l->l_phdr[n].p_type == PT_LOAD
+	&& reladdr - l->l_phdr[n].p_vaddr >= 0
+	&& reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
+      return 1;
+  return 0;
+}
+#endif
diff --git a/elf/dl-profile.c b/elf/dl-profile.c
index 47033f32ef..41214c1b08 100644
--- a/elf/dl-profile.c
+++ b/elf/dl-profile.c
@@ -1,5 +1,5 @@
 /* Profiling of shared libraries.
-   Copyright (C) 1997-2002, 2003, 2004, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1997-2002, 2003, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
    Based on the BSD mcount implementation.
@@ -509,24 +509,24 @@ _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc)
 	      size_t newfromidx;
 	      to_index = (data[narcs].self_pc
 			  / (HASHFRACTION * sizeof (*tos)));
-	      newfromidx = catomic_exchange_and_add (&fromidx, 1) + 1;
+	      newfromidx = atomic_exchange_and_add (&fromidx, 1) + 1;
 	      froms[newfromidx].here = &data[narcs];
 	      froms[newfromidx].link = tos[to_index];
 	      tos[to_index] = newfromidx;
-	      catomic_increment (&narcs);
+	      atomic_increment (&narcs);
 	    }
 
 	  /* If we still have no entry stop searching and insert.  */
 	  if (*topcindex == 0)
 	    {
-	      uint_fast32_t newarc = catomic_exchange_and_add (narcsp, 1);
+	      uint_fast32_t newarc = atomic_exchange_and_add (narcsp, 1);
 
 	      /* In rare cases it could happen that all entries in FROMS are
 		 occupied.  So we cannot count this anymore.  */
 	      if (newarc >= fromlimit)
 		goto done;
 
-	      *topcindex = catomic_exchange_and_add (&fromidx, 1) + 1;
+	      *topcindex = atomic_exchange_and_add (&fromidx, 1) + 1;
 	      fromp = &froms[*topcindex];
 
 	      fromp->here = &data[newarc];
@@ -534,7 +534,7 @@ _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc)
 	      data[newarc].self_pc = selfpc;
 	      data[newarc].count = 0;
 	      fromp->link = 0;
-	      catomic_increment (&narcs);
+	      atomic_increment (&narcs);
 
 	      break;
 	    }
@@ -547,7 +547,7 @@ _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc)
     }
 
   /* Increment the counter.  */
-  catomic_increment (&fromp->here->count);
+  atomic_increment (&fromp->here->count);
 
  done:
   ;
diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c
index c315b5d972..117410e924 100644
--- a/elf/dl-reloc.c
+++ b/elf/dl-reloc.c
@@ -1,5 +1,5 @@
 /* Relocate a shared object and resolve its references to other loaded objects.
-   Copyright (C) 1995-2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-2004, 2005 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
@@ -35,6 +35,7 @@
 #endif
 
 
+#ifdef USE_TLS
 /* We are trying to perform a static TLS relocation in MAP, but it was
    dynamically loaded.  This can only work if there is enough surplus in
    the static TLS area already allocated for each running thread.  If this
@@ -55,7 +56,7 @@ _dl_allocate_static_tls (struct link_map *map)
 cannot allocate memory in static TLS block"));
     }
 
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
   size_t freebytes;
   size_t n;
   size_t blsize;
@@ -72,7 +73,7 @@ cannot allocate memory in static TLS block"));
 					    - map->l_tls_firstbyte_offset);
 
   map->l_tls_offset = GL(dl_tls_static_used) = offset;
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
   size_t used;
   size_t check;
 
@@ -86,9 +87,9 @@ cannot allocate memory in static TLS block"));
 
   map->l_tls_offset = offset;
   GL(dl_tls_static_used) = used;
-#else
-# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
-#endif
+# else
+#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+# endif
 
   /* If the object is not yet relocated we cannot initialize the
      static TLS region.  Delay it.  */
@@ -114,13 +115,13 @@ cannot allocate memory in static TLS block"));
 void
 _dl_nothread_init_static_tls (struct link_map *map)
 {
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
   void *dest = (char *) THREAD_SELF - map->l_tls_offset;
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
   void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE;
-#else
-# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
-#endif
+# else
+#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+# endif
 
   /* Fill in the DTV slot so that a later LD/GD access will find it.  */
   dtv_t *dtv = THREAD_DTV ();
@@ -132,6 +133,7 @@ _dl_nothread_init_static_tls (struct link_map *map)
   memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
 	  '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
 }
+#endif
 
 
 void
diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c
index afc99f6150..ee2b8b5f6c 100644
--- a/elf/dl-runtime.c
+++ b/elf/dl-runtime.c
@@ -1,5 +1,5 @@
 /* On-demand PLT fixup for shared objects.
-   Copyright (C) 1995-2002,2003,2004,2005,2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-2006, 2007 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
@@ -26,6 +26,8 @@
 #include <ldsodefs.h>
 #include <sysdep-cancel.h>
 #include "dynamic-link.h"
+#include <tls.h>
+
 
 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
     || ELF_MACHINE_NO_REL
@@ -93,15 +95,19 @@ _dl_fixup (
 	    version = NULL;
 	}
 
-      if (l->l_type == lt_loaded && !RTLD_SINGLE_THREAD_P)
-	__rtld_mrlock_lock (l->l_scope_lock);
+      /* We need to keep the scope around so do some locking.  This is
+	 not necessary for objects which cannot be unloaded or when
+	 we are not using any threads (yet).  */
+      int flags = DL_LOOKUP_ADD_DEPENDENCY;
+      if (!RTLD_SINGLE_THREAD_P)
+	THREAD_GSCOPE_SET_FLAG ();
 
-      result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
-				    l->l_scope, version, ELF_RTYPE_CLASS_PLT,
-				    DL_LOOKUP_ADD_DEPENDENCY, NULL);
+      result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym, l->l_scope,
+				    version, ELF_RTYPE_CLASS_PLT, flags, NULL);
 
-      if (l->l_type == lt_loaded && !RTLD_SINGLE_THREAD_P)
-	__rtld_mrlock_unlock (l->l_scope_lock);
+      /* We are done with the global scope.  */
+      if (!RTLD_SINGLE_THREAD_P)
+	THREAD_GSCOPE_RESET_FLAG ();
 
       /* Currently result contains the base load address (or link map)
 	 of the object that defines sym.  Now add in the symbol
@@ -181,16 +187,20 @@ _dl_profile_fixup (
 		version = NULL;
 	    }
 
-	  if (l->l_type == lt_loaded && !RTLD_SINGLE_THREAD_P)
-	    __rtld_mrlock_lock (l->l_scope_lock);
+	  /* We need to keep the scope around so do some locking.  This is
+	     not necessary for objects which cannot be unloaded or when
+	     we are not using any threads (yet).  */
+	  int flags = DL_LOOKUP_ADD_DEPENDENCY;
+	  if (!RTLD_SINGLE_THREAD_P)
+	    THREAD_GSCOPE_SET_FLAG ();
 
-	  result = _dl_lookup_symbol_x (strtab + refsym->st_name, l, &defsym,
-					l->l_scope, version,
-					ELF_RTYPE_CLASS_PLT,
-					DL_LOOKUP_ADD_DEPENDENCY, NULL);
+	  result = _dl_lookup_symbol_x (strtab + refsym->st_name, l,
+					&defsym, l->l_scope, version,
+					ELF_RTYPE_CLASS_PLT, flags, NULL);
 
-	  if (l->l_type == lt_loaded && !RTLD_SINGLE_THREAD_P)
-	    __rtld_mrlock_unlock (l->l_scope_lock);
+	  /* We are done with the global scope.  */
+	  if (!RTLD_SINGLE_THREAD_P)
+	    THREAD_GSCOPE_RESET_FLAG ();
 
 	  /* Currently result contains the base load address (or link map)
 	     of the object that defines sym.  Now add in the symbol
diff --git a/elf/dl-support.c b/elf/dl-support.c
index c1cfc0df41..4b7be6bc27 100644
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -1,5 +1,5 @@
 /* Support for dynamic linking code in static libc.
-   Copyright (C) 1996-2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1996-2005, 2006, 2007 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
@@ -98,7 +98,9 @@ int _dl_starting_up = 1;
 hp_timing_t _dl_cpuclock_offset;
 #endif
 
+#ifdef USE_TLS
 void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
+#endif
 
 size_t _dl_pagesize;
 
@@ -132,6 +134,11 @@ int (*_dl_make_stack_executable_hook) (void **) internal_function
   = _dl_make_stack_executable;
 
 
+/* Function in libpthread to wait for termination of lookups.  */
+void (*_dl_wait_lookup_done) (void);
+
+struct dl_scope_free_list *_dl_scope_free_list;
+
 #ifdef NEED_DL_SYSINFO
 /* Needed for improved syscall handling on at least x86/Linux.  */
 uintptr_t _dl_sysinfo = DL_SYSINFO_DEFAULT;
diff --git a/elf/dl-sym.c b/elf/dl-sym.c
index 88a5adb0d5..fa0e3a67fc 100644
--- a/elf/dl-sym.c
+++ b/elf/dl-sym.c
@@ -1,5 +1,5 @@
 /* Look up a symbol in a shared object loaded by `dlopen'.
-   Copyright (C) 1999,2000,2001,2002,2004,2006 Free Software Foundation, Inc.
+   Copyright (C) 1999-2002,2004,2006,2007 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
@@ -26,10 +26,12 @@
 #include <ldsodefs.h>
 #include <dl-hash.h>
 #include <sysdep-cancel.h>
-#include <dl-tls.h>
+#ifdef USE_TLS
+# include <dl-tls.h>
+#endif
 
 
-#ifdef SHARED
+#if defined USE_TLS && defined SHARED
 /* Systems which do not have tls_index also probably have to define
    DONT_USE_TLS_INDEX.  */
 
@@ -98,10 +100,9 @@ do_sym (void *handle, const char *name, void *who,
   for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
     for (struct link_map *l = GL(dl_ns)[ns]._ns_loaded; l != NULL;
 	 l = l->l_next)
-      if (caller >= l->l_map_start && caller < l->l_map_end)
+      if (caller >= l->l_map_start && caller < l->l_map_end
+	  && (l->l_contiguous || _dl_addr_inside_object (l, caller)))
 	{
-	  /* There must be exactly one DSO for the range of the virtual
-	     memory.  Otherwise something is really broken.  */
 	  match = l;
 	  break;
 	}
@@ -113,15 +114,13 @@ do_sym (void *handle, const char *name, void *who,
 	 the initial binary.  And then the more complex part
 	 where the object is dynamically loaded and the scope
 	 array can change.  */
-      if (match->l_type != lt_loaded || RTLD_SINGLE_THREAD_P)
+      if (RTLD_SINGLE_THREAD_P)
 	result = GLRO(dl_lookup_symbol_x) (name, match, &ref,
 					   match->l_scope, vers, 0,
 					   flags | DL_LOOKUP_ADD_DEPENDENCY,
 					   NULL);
       else
 	{
-	  __rtld_mrlock_lock (match->l_scope_lock);
-
 	  struct call_dl_lookup_args args;
 	  args.name = name;
 	  args.map = match;
@@ -129,13 +128,15 @@ do_sym (void *handle, const char *name, void *who,
 	  args.flags = flags | DL_LOOKUP_ADD_DEPENDENCY;
 	  args.refp = &ref;
 
+	  THREAD_GSCOPE_SET_FLAG ();
+
 	  const char *objname;
 	  const char *errstring = NULL;
 	  bool malloced;
 	  int err = GLRO(dl_catch_error) (&objname, &errstring, &malloced,
 					  call_dl_lookup, &args);
 
-	  __rtld_mrlock_unlock (match->l_scope_lock);
+	  THREAD_GSCOPE_RESET_FLAG ();
 
 	  if (__builtin_expect (errstring != NULL, 0))
 	    {
@@ -182,7 +183,7 @@ RTLD_NEXT used in code not dynamically loaded"));
     {
       void *value;
 
-#ifdef SHARED
+#if defined USE_TLS && defined SHARED
       if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
 	/* The found symbol is a thread-local storage variable.
 	   Return the address for to the current thread.  */
diff --git a/elf/dl-sysdep.c b/elf/dl-sysdep.c
index d06ce1754e..68e08f480a 100644
--- a/elf/dl-sysdep.c
+++ b/elf/dl-sysdep.c
@@ -1,5 +1,5 @@
 /* Operating system support for run-time dynamic linker.  Generic Unix version.
-   Copyright (C) 1995-1998, 2000-2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-1998, 2000-2005, 2007 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
@@ -405,8 +405,25 @@ _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
     }
 #endif
 
+#ifdef USE_TLS
   /* For TLS enabled builds always add 'tls'.  */
   ++cnt;
+#else
+  if (cnt == 0)
+    {
+      /* If we no have platform name and no important capability we only
+	 have the base directory to search.  */
+      result = (struct r_strlenpair *) malloc (sizeof (*result));
+      if (result == NULL)
+	goto no_memory;
+
+      result[0].str = (char *) result;	/* Does not really matter.  */
+      result[0].len = 0;
+
+      *sz = 1;
+      return result;
+    }
+#endif
 
   /* Create temporary data structure to generate result table.  */
   temp = (struct r_strlenpair *) alloca (cnt * sizeof (*temp));
@@ -448,11 +465,11 @@ _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
       temp[m].len = platform_len;
       ++m;
     }
-
+#ifdef USE_TLS
   temp[m].str = "tls";
   temp[m].len = 3;
   ++m;
-
+#endif
   assert (m == cnt);
 
   /* Determine the total size of all strings together.  */
@@ -460,9 +477,21 @@ _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
     total = temp[0].len + 1;
   else
     {
-      total = (1UL << (cnt - 2)) * (temp[0].len + temp[cnt - 1].len + 2);
-      for (n = 1; n + 1 < cnt; ++n)
-	total += (1UL << (cnt - 3)) * (temp[n].len + 1);
+      total = temp[0].len + temp[cnt - 1].len + 2;
+      if (cnt > 2)
+	{
+	  total <<= 1;
+	  for (n = 1; n + 1 < cnt; ++n)
+	    total += temp[n].len + 1;
+	  if (cnt > 3
+	      && (cnt >= sizeof (size_t) * 8
+		  || total + (sizeof (*result) << 3)
+		     >= (1UL << (sizeof (size_t) * 8 - cnt + 3))))
+	    _dl_signal_error (ENOMEM, NULL, NULL,
+			      N_("cannot create capability list"));
+
+	  total <<= cnt - 3;
+	}
     }
 
   /* The result structure: we use a very compressed way to store the
@@ -470,8 +499,13 @@ _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
   *sz = 1 << cnt;
   result = (struct r_strlenpair *) malloc (*sz * sizeof (*result) + total);
   if (result == NULL)
-    _dl_signal_error (ENOMEM, NULL, NULL,
-		      N_("cannot create capability list"));
+    {
+#ifndef USE_TLS
+    no_memory:
+#endif
+      _dl_signal_error (ENOMEM, NULL, NULL,
+			N_("cannot create capability list"));
+    }
 
   if (cnt == 1)
     {
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
index d5865ab409..a0f4f77ffa 100644
--- a/elf/dl-tls.c
+++ b/elf/dl-tls.c
@@ -26,26 +26,30 @@
 #include <sys/param.h>
 
 #include <tls.h>
-#include <dl-tls.h>
-#include <ldsodefs.h>
+
+/* We don't need any of this if TLS is not supported.  */
+#ifdef USE_TLS
+
+# include <dl-tls.h>
+# include <ldsodefs.h>
 
 /* Amount of excess space to allocate in the static TLS area
    to allow dynamic loading of modules defining IE-model TLS data.  */
-#define TLS_STATIC_SURPLUS	64 + DL_NNS * 100
+# define TLS_STATIC_SURPLUS	64 + DL_NNS * 100
 
 /* Value used for dtv entries for which the allocation is delayed.  */
-#define TLS_DTV_UNALLOCATED	((void *) -1l)
+# define TLS_DTV_UNALLOCATED	((void *) -1l)
 
 
 /* Out-of-memory handler.  */
-#ifdef SHARED
+# ifdef SHARED
 static void
 __attribute__ ((__noreturn__))
 oom (void)
 {
   _dl_fatal_printf ("cannot allocate memory for thread-local data: ABORT\n");
 }
-#endif
+# endif
 
 
 size_t
@@ -109,7 +113,7 @@ _dl_next_tls_modid (void)
 }
 
 
-#ifdef SHARED
+# ifdef SHARED
 void
 internal_function
 _dl_determine_tlsoffset (void)
@@ -154,7 +158,7 @@ _dl_determine_tlsoffset (void)
      memory requirement for the next TLS block is smaller than the
      gap.  */
 
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
   /* We simply start with zero.  */
   size_t offset = 0;
 
@@ -201,7 +205,7 @@ _dl_determine_tlsoffset (void)
   GL(dl_tls_static_used) = offset;
   GL(dl_tls_static_size) = (roundup (offset + TLS_STATIC_SURPLUS, max_align)
 			    + TLS_TCB_SIZE);
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
   /* The TLS blocks start right after the TCB.  */
   size_t offset = TLS_TCB_SIZE;
 
@@ -245,9 +249,9 @@ _dl_determine_tlsoffset (void)
   GL(dl_tls_static_used) = offset;
   GL(dl_tls_static_size) = roundup (offset + TLS_STATIC_SURPLUS,
 				    TLS_TCB_ALIGN);
-#else
-# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
-#endif
+# else
+#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+# endif
 
   /* The alignment requirement for the static TLS block.  */
   GL(dl_tls_static_align) = max_align;
@@ -284,7 +288,7 @@ _dl_tls_setup (void)
   return 0;
 }
 rtld_hidden_def (_dl_tls_setup)
-#endif
+# endif
 
 static void *
 internal_function
@@ -333,13 +337,13 @@ _dl_allocate_tls_storage (void)
   void *result;
   size_t size = GL(dl_tls_static_size);
 
-#if TLS_DTV_AT_TP
+# if TLS_DTV_AT_TP
   /* Memory layout is:
      [ TLS_PRE_TCB_SIZE ] [ TLS_TCB_SIZE ] [ TLS blocks ]
 			  ^ This should be returned.  */
   size += (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
 	  & ~(GL(dl_tls_static_align) - 1);
-#endif
+# endif
 
   /* Allocate a correctly aligned chunk of memory.  */
   result = __libc_memalign (GL(dl_tls_static_align), size);
@@ -348,14 +352,14 @@ _dl_allocate_tls_storage (void)
       /* Allocate the DTV.  */
       void *allocated = result;
 
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
       /* The TCB follows the TLS blocks.  */
       result = (char *) result + size - TLS_TCB_SIZE;
 
       /* Clear the TCB data structure.  We can't ask the caller (i.e.
 	 libpthread) to do it, because we will initialize the DTV et al.  */
       memset (result, '\0', TLS_TCB_SIZE);
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
       result = (char *) result + size - GL(dl_tls_static_size);
 
       /* Clear the TCB data structure and TLS_PRE_TCB_SIZE bytes before it.
@@ -363,7 +367,7 @@ _dl_allocate_tls_storage (void)
 	 initialize the DTV et al.  */
       memset ((char *) result - TLS_PRE_TCB_SIZE, '\0',
 	      TLS_PRE_TCB_SIZE + TLS_TCB_SIZE);
-#endif
+# endif
 
       result = allocate_dtv (result);
       if (result == NULL)
@@ -424,14 +428,14 @@ _dl_allocate_tls_init (void *result)
 
 	  assert (map->l_tls_modid == cnt);
 	  assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
 	  assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
 	  dest = (char *) result - map->l_tls_offset;
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
 	  dest = (char *) result + map->l_tls_offset;
-#else
-# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
-#endif
+# else
+#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+# endif
 
 	  /* Copy the initialization image and clear the BSS part.  */
 	  dtv[map->l_tls_modid].pointer.val = dest;
@@ -487,21 +491,21 @@ _dl_deallocate_tls (void *tcb, bool dealloc_tcb)
 
   if (dealloc_tcb)
     {
-#if TLS_TCB_AT_TP
+# if TLS_TCB_AT_TP
       /* The TCB follows the TLS blocks.  Back up to free the whole block.  */
       tcb -= GL(dl_tls_static_size) - TLS_TCB_SIZE;
-#elif TLS_DTV_AT_TP
+# elif TLS_DTV_AT_TP
       /* Back up the TLS_PRE_TCB_SIZE bytes.  */
       tcb -= (TLS_PRE_TCB_SIZE + GL(dl_tls_static_align) - 1)
 	     & ~(GL(dl_tls_static_align) - 1);
-#endif
+# endif
       free (tcb);
     }
 }
 rtld_hidden_def (_dl_deallocate_tls)
 
 
-#ifdef SHARED
+# ifdef SHARED
 /* The __tls_get_addr function has two basic forms which differ in the
    arguments.  The IA-64 form takes two parameters, the module ID and
    offset.  The form used, among others, on IA-32 takes a reference to
@@ -509,15 +513,15 @@ rtld_hidden_def (_dl_deallocate_tls)
    form seems to be more often used (in the moment) so we default to
    it.  Users of the IA-64 form have to provide adequate definitions
    of the following macros.  */
-# ifndef GET_ADDR_ARGS
-#  define GET_ADDR_ARGS tls_index *ti
-# endif
-# ifndef GET_ADDR_MODULE
-#  define GET_ADDR_MODULE ti->ti_module
-# endif
-# ifndef GET_ADDR_OFFSET
-#  define GET_ADDR_OFFSET ti->ti_offset
-# endif
+#  ifndef GET_ADDR_ARGS
+#   define GET_ADDR_ARGS tls_index *ti
+#  endif
+#  ifndef GET_ADDR_MODULE
+#   define GET_ADDR_MODULE ti->ti_module
+#  endif
+#  ifndef GET_ADDR_OFFSET
+#   define GET_ADDR_OFFSET ti->ti_offset
+#  endif
 
 
 static void *
@@ -728,7 +732,7 @@ __tls_get_addr (GET_ADDR_ARGS)
 
   return (char *) p + GET_ADDR_OFFSET;
 }
-#endif
+# endif
 
 
 /* Look up the module's TLS block as for __tls_get_addr,
@@ -836,3 +840,4 @@ cannot create TLS data structures"));
   listp->slotinfo[idx].map = l;
   listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
 }
+#endif	/* use TLS */
diff --git a/elf/do-lookup.h b/elf/do-lookup.h
index 2585d83005..ab9a510ba1 100644
--- a/elf/do-lookup.h
+++ b/elf/do-lookup.h
@@ -1,5 +1,5 @@
 /* Look up a symbol in the loaded objects.
-   Copyright (C) 1995-2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-2004, 2005, 2006, 2007 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
@@ -29,8 +29,13 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
 	     const struct r_found_version *const version, int flags,
 	     struct link_map *skip, int type_class)
 {
-  struct link_map **list = scope->r_list;
   size_t n = scope->r_nlist;
+  /* Make sure we read the value before proceeding.  Otherwise we
+     might use r_list pointing to the initial scope and r_nlist being
+     the value after a resize.  That is the only path in dl-open.c not
+     protected by GSCOPE.  A read barrier here might be to expensive.  */
+  __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
+  struct link_map **list = scope->r_list;
 
   do
     {
diff --git a/elf/elf.h b/elf/elf.h
index d27e5f085d..dae359778b 100644
--- a/elf/elf.h
+++ b/elf/elf.h
@@ -1,5 +1,5 @@
 /* This file defines standard ELF types, structures, and macros.
-   Copyright (C) 1995-2003,2004,2005,2006,2007 Free Software Foundation, Inc.
+   Copyright (C) 1995-2003,2004,2005,2006 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
@@ -1511,9 +1511,8 @@ typedef struct
 #define R_MIPS_TLS_TPREL64	48	/* TP-relative offset, 64 bit */
 #define R_MIPS_TLS_TPREL_HI16	49	/* TP-relative offset, high 16 bits */
 #define R_MIPS_TLS_TPREL_LO16	50	/* TP-relative offset, low 16 bits */
-#define R_MIPS_GLOB_DAT		51
 /* Keep this the last entry.  */
-#define R_MIPS_NUM		52
+#define R_MIPS_NUM		51
 
 /* Legal values for p_type field of Elf32_Phdr.  */
 
diff --git a/elf/ldconfig.c b/elf/ldconfig.c
index ec81cbb817..4a088af1ed 100644
--- a/elf/ldconfig.c
+++ b/elf/ldconfig.c
@@ -24,7 +24,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <libintl.h>
-#include <locale.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdio_ext.h>
@@ -287,7 +286,7 @@ print_version (FILE *stream, struct argp_state *state)
 Copyright (C) %s Free Software Foundation, Inc.\n\
 This is free software; see the source for copying conditions.  There is NO\n\
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
-"), "2007");
+"), "2006");
   fprintf (stream, gettext ("Written by %s.\n"),
 	   "Andreas Jaeger");
 }
@@ -559,7 +558,7 @@ manual_link (char *library)
   /* Do some sanity checks first.  */
   if (lstat64 (real_library, &stat_buf))
     {
-      error (0, errno, _("Cannot lstat %s"), library);
+      error (0, errno, _("Can't lstat %s"), library);
       free (path);
       return;
     }
@@ -707,10 +706,10 @@ search_dir (const struct dir_entry *entry)
 			 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
 	    continue;
 	}
-      len += strlen (entry->path);
+      len += strlen (entry->path) + 2;
       if (len > file_name_len)
 	{
-	  file_name_len = len + 1;
+	  file_name_len = len;
 	  file_name = alloca (file_name_len);
 	  if (!opt_chroot)
 	    real_file_name = file_name;
@@ -718,10 +717,10 @@ search_dir (const struct dir_entry *entry)
       sprintf (file_name, "%s/%s", entry->path, direntry->d_name);
       if (opt_chroot)
 	{
-	  len = strlen (dir_name) + strlen (direntry->d_name);
+	  len = strlen (dir_name) + strlen (direntry->d_name) + 2;
 	  if (len > real_file_name_len)
 	    {
-	      real_file_name_len = len + 1;
+	      real_file_name_len = len;
 	      real_file_name = alloca (real_file_name_len);
 	    }
 	  sprintf (real_file_name, "%s/%s", dir_name, direntry->d_name);
@@ -1167,14 +1166,9 @@ set_hwcap (void)
 int
 main (int argc, char **argv)
 {
-  /* Set locale via LC_ALL.  */
-  setlocale (LC_ALL, "");
-
-  /* Set the text message domain.  */
-  textdomain (_libc_intl_domainname);
+  int remaining;
 
   /* Parse and process arguments.  */
-  int remaining;
   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
 
   /* Remaining arguments are additional directories if opt_manual_link
@@ -1191,7 +1185,9 @@ main (int argc, char **argv)
 	  add_dir (argv[i]);
     }
 
+#ifdef USE_TLS
   hwcap_extra[63 - _DL_FIRST_EXTRA] = "tls";
+#endif
 
   set_hwcap ();
 
diff --git a/elf/ldd.bash.in b/elf/ldd.bash.in
index 05282fe99b..d1591a5785 100644
--- a/elf/ldd.bash.in
+++ b/elf/ldd.bash.in
@@ -1,5 +1,5 @@
 #! @BASH@
-# Copyright (C) 1996-2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+# Copyright (C) 1996-2004, 2005, 2006 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
@@ -39,7 +39,7 @@ while test $# -gt 0; do
     printf $"Copyright (C) %s Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-" "2007"
+" "2006"
     printf $"Written by %s and %s.
 " "Roland McGrath" "Ulrich Drepper"
     exit 0
diff --git a/elf/rtld.c b/elf/rtld.c
index 5e6ee51603..a357a46987 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -1,5 +1,5 @@
 /* Run time dynamic linker.
-   Copyright (C) 1995-2002,2003,2004,2005,2006 Free Software Foundation, Inc.
+   Copyright (C) 1995-2006, 2007 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
@@ -207,7 +207,8 @@ DL_SYSINFO_IMPLEMENTATION
    is fine, too.  The latter is important here.  We can avoid setting
    up a temporary link map for ld.so if we can mark _rtld_global as
    hidden.  */
-#ifdef PI_STATIC_AND_HIDDEN
+#if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \
+    && defined HAVE_VISIBILITY_ATTRIBUTE
 # define DONT_USE_BOOTSTRAP_MAP	1
 #endif
 
@@ -291,7 +292,7 @@ _dl_start_final (void *arg, struct dl_start_final_info *info)
   GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
   GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
   /* Copy the TLS related data if necessary.  */
-#ifndef DONT_USE_BOOTSTRAP_MAP
+#if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
 # if USE___THREAD
   assert (info->l.l_tls_modid != 0);
   GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize;
@@ -399,7 +400,7 @@ _dl_start (void *arg)
   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
   elf_get_dynamic_info (&bootstrap_map, NULL);
 
-#if NO_TLS_OFFSET != 0
+#if defined USE_TLS && NO_TLS_OFFSET != 0
   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
 #endif
 
@@ -700,6 +701,7 @@ match_version (const char *string, struct link_map *map)
   return 0;
 }
 
+#ifdef USE_TLS
 static bool tls_init_tp_called;
 
 static void *
@@ -771,6 +773,7 @@ cannot allocate TLS data structures for initial thread");
 
   return tcbp;
 }
+#endif
 
 #ifdef _LIBC_REENTRANT
 /* _dl_error_catch_tsd points to this for the single-threaded case.
@@ -858,14 +861,18 @@ dl_main (const ElfW(Phdr) *phdr,
   hp_timing_t stop;
   hp_timing_t diff;
 #endif
+#ifdef USE_TLS
   void *tcbp = NULL;
+#endif
 
 #ifdef _LIBC_REENTRANT
   /* Explicit initialization since the reloc would just be more work.  */
   GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
 #endif
 
+#ifdef USE_TLS
   GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
+#endif
 
 #if defined SHARED && defined _LIBC_REENTRANT \
     && defined __rtld_lock_default_lock_recursive
@@ -1150,6 +1157,7 @@ of this helper program; chances are you did not intend to run this program.\n\
 	break;
 
       case PT_TLS:
+#ifdef USE_TLS
 	if (ph->p_memsz > 0)
 	  {
 	    /* Note that in the case the dynamic linker we duplicate work
@@ -1169,6 +1177,10 @@ of this helper program; chances are you did not intend to run this program.\n\
 	    /* This image gets the ID one.  */
 	    GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
 	  }
+#else
+	_dl_fatal_printf ("\
+ld.so does not support TLS, but program uses it!\n");
+#endif
 	break;
 
       case PT_GNU_STACK:
@@ -1180,12 +1192,13 @@ of this helper program; chances are you did not intend to run this program.\n\
 	main_map->l_relro_size = ph->p_memsz;
 	break;
       }
-
-  /* Adjust the address of the TLS initialization image in case
-     the executable is actually an ET_DYN object.  */
-  if (main_map->l_tls_initimage != NULL)
-    main_map->l_tls_initimage
-      = (char *) main_map->l_tls_initimage + main_map->l_addr;
+#ifdef USE_TLS
+    /* Adjust the address of the TLS initialization image in case
+       the executable is actually an ET_DYN object.  */
+    if (main_map->l_tls_initimage != NULL)
+      main_map->l_tls_initimage
+	= (char *) main_map->l_tls_initimage + main_map->l_addr;
+#endif
   if (! main_map->l_map_end)
     main_map->l_map_end = ~0;
   if (! main_map->l_text_end)
@@ -1388,10 +1401,12 @@ of this helper program; chances are you did not intend to run this program.\n\
 	break;
       }
 
+#ifdef USE_TLS
   /* Add the dynamic linker to the TLS list if it also uses TLS.  */
   if (GL(dl_rtld_map).l_tls_blocksize != 0)
     /* Assign a module ID.  Do this before loading any audit modules.  */
     GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
+#endif
 
   /* If we have auditing DSOs to load, do it now.  */
   if (__builtin_expect (audit_list != NULL, 0))
@@ -1399,8 +1414,15 @@ of this helper program; chances are you did not intend to run this program.\n\
       /* Iterate over all entries in the list.  The order is important.  */
       struct audit_ifaces *last_audit = NULL;
       struct audit_list *al = audit_list->next;
+
+#ifdef USE_TLS
+      /* Since we start using the auditing DSOs right away we need to
+	 initialize the data structures now.  */
+      tcbp = init_tls ();
+#endif
       do
 	{
+#ifdef USE_TLS
 	  int tls_idx = GL(dl_tls_max_dtv_idx);
 
 	  /* Now it is time to determine the layout of the static TLS
@@ -1408,11 +1430,7 @@ of this helper program; chances are you did not intend to run this program.\n\
 	     always allocate the static block, we never defer it even if
 	     no DF_STATIC_TLS bit is set.  The reason is that we know
 	     glibc will use the static model.  */
-
-	  /* Since we start using the auditing DSOs right away we need to
-	     initialize the data structures now.  */
-	  tcbp = init_tls ();
-
+#endif
 	  struct dlmopen_args dlmargs;
 	  dlmargs.fname = al->name;
 	  dlmargs.map = NULL;
@@ -1527,7 +1545,9 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 		  assert (GL(dl_ns)[ns]._ns_loaded == NULL);
 		  assert (GL(dl_ns)[ns]._ns_nloaded == 0);
 
+#ifdef USE_TLS
 		  GL(dl_tls_max_dtv_idx) = tls_idx;
+#endif
 		  goto not_loaded;
 		}
 	    }
@@ -1803,6 +1823,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
     _dl_receive_error (print_missing_version, version_check_doit, &args);
   }
 
+#ifdef USE_TLS
   /* We do not initialize any of the TLS functionality unless any of the
      initial modules uses TLS.  This makes dynamic loading of modules with
      TLS impossible, but to support it requires either eagerly doing setup
@@ -1813,6 +1834,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
   bool was_tls_init_tp_called = tls_init_tp_called;
   if (tcbp == NULL)
     tcbp = init_tls ();
+#endif
 
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard ();
@@ -1869,12 +1891,13 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 			  (size_t) l->l_map_start,
 			  (int) sizeof l->l_addr * 2,
 			  (size_t) l->l_addr);
-
+#ifdef USE_TLS
 	      if (l->l_tls_modid)
 		_dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
 			    (int) sizeof l->l_tls_offset * 2,
 			    (size_t) l->l_tls_offset);
 	      else
+#endif
 		_dl_printf ("\n");
 	    }
 	}
@@ -1942,8 +1965,8 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 	    lookup_t result;
 
 	    result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map,
-					  &ref, main_map->l_scope,
-					  NULL, ELF_RTYPE_CLASS_PLT,
+					  &ref, main_map->l_scope, NULL,
+					  ELF_RTYPE_CLASS_PLT,
 					  DL_LOOKUP_ADD_DEPENDENCY, NULL);
 
 	    loadbase = LOOKUP_VALUE_ADDRESS (result);
@@ -1985,8 +2008,8 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 		{
 		  /* Mark the link map as not yet relocated again.  */
 		  GL(dl_rtld_map).l_relocated = 0;
-		  _dl_relocate_object (&GL(dl_rtld_map),
-				       main_map->l_scope, 0, 0);
+		  _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope,
+				       0, 0);
 		}
             }
 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
@@ -2157,9 +2180,11 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 	  if (l->l_relro_size)
 	    _dl_protect_relro (l);
 
+#ifdef USE_TLS
 	  /* Add object to slot information data if necessasy.  */
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
 	    _dl_add_to_slotinfo (l);
+#endif
 	}
 
       _dl_sysdep_start_cleanup ();
@@ -2206,9 +2231,11 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 	    _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy),
 				 consider_profiling);
 
+#ifdef USE_TLS
 	  /* Add object to slot information data if necessasy.  */
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
 	    _dl_add_to_slotinfo (l);
+#endif
 
 	  l = l->l_prev;
 	}
@@ -2237,6 +2264,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 # define NONTLS_INIT_TP do { } while (0)
 #endif
 
+#ifdef USE_TLS
   if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
     ++GL(dl_tls_generation);
 
@@ -2254,6 +2282,9 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
 	_dl_fatal_printf ("cannot set up thread-local storage: %s\n",
 			  lossage);
     }
+#else
+  NONTLS_INIT_TP;
+#endif
 
   if (! prelinked && rtld_multiple_ref)
     {
diff --git a/elf/sprof.c b/elf/sprof.c
index 01d27e1eff..e53a7ba7a3 100644
--- a/elf/sprof.c
+++ b/elf/sprof.c
@@ -1,5 +1,5 @@
 /* Read and display shared object profiling data.
-   Copyright (C) 1997-2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   Copyright (C) 1997-2004, 2005, 2006 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
 
@@ -357,7 +357,7 @@ Copyright (C) %s Free Software Foundation, Inc.\n\
 This is free software; see the source for copying conditions.  There is NO\n\
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
 "),
-	   "2007");
+	   "2006");
   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
 }
 
diff --git a/elf/tst-thrlock.c b/elf/tst-thrlock.c
index fe72eba141..71f1fbb35d 100644
--- a/elf/tst-thrlock.c
+++ b/elf/tst-thrlock.c
@@ -27,9 +27,8 @@ tf (void *arg)
   return NULL;
 }
 
-
-static int
-do_test (void)
+int
+main (void)
 {
 #define N 10
   pthread_t th[N];
@@ -54,6 +53,3 @@ do_test (void)
     }
   return 0;
 }
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/elf/tst-tls-dlinfo.c b/elf/tst-tls-dlinfo.c
index 26c2811178..e97b5081fd 100644
--- a/elf/tst-tls-dlinfo.c
+++ b/elf/tst-tls-dlinfo.c
@@ -9,6 +9,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname[] = "tst-tlsmod2.so";
   int result = 0;
   int *foop;
@@ -82,6 +83,9 @@ do_test (void)
   dlclose (h);
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls1.c b/elf/tst-tls1.c
index 3b9b10f9aa..478f5bbdcd 100644
--- a/elf/tst-tls1.c
+++ b/elf/tst-tls1.c
@@ -3,18 +3,21 @@
 
 #include <tls.h>
 
-#include "tls-macros.h"
+#ifdef USE_TLS
+# include "tls-macros.h"
 
 
 /* Two common 'int' variables in TLS.  */
 COMMON_INT_DEF(foo);
 COMMON_INT_DEF(bar);
+#endif
 
 
 #define TEST_FUNCTION do_test ()
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   int result = 0;
   int *ap, *bp;
 
@@ -79,6 +82,9 @@ do_test (void)
     }
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls10.h b/elf/tst-tls10.h
index bffc332a0a..1be6adc293 100644
--- a/elf/tst-tls10.h
+++ b/elf/tst-tls10.h
@@ -1,7 +1,8 @@
 #include <tls.h>
 #include <stdlib.h>
 
-#if defined HAVE___THREAD && defined HAVE_TLS_MODEL_ATTRIBUTE
+#if defined USE_TLS && defined HAVE___THREAD \
+    && defined HAVE_TLS_MODEL_ATTRIBUTE
 # define USE_TLS__THREAD
 
 struct A
diff --git a/elf/tst-tls14.c b/elf/tst-tls14.c
index c1e6ba7e2c..428fd5293e 100644
--- a/elf/tst-tls14.c
+++ b/elf/tst-tls14.c
@@ -6,9 +6,9 @@
 
 #include <tls.h>
 
-#if HAVE___THREAD
+#if USE_TLS && HAVE___THREAD
 
-# define AL 4096
+#define AL 4096
 struct foo
 {
   int i;
@@ -55,11 +55,11 @@ do_test (void)
   return result;
 }
 
-# define TEST_FUNCTION do_test ()
+#define TEST_FUNCTION do_test ()
 
 #else
 
-# define TEST_FUNCTION 0
+#define TEST_FUNCTION 0
 
 #endif
 
diff --git a/elf/tst-tls2.c b/elf/tst-tls2.c
index 3d13272c08..417489968e 100644
--- a/elf/tst-tls2.c
+++ b/elf/tst-tls2.c
@@ -3,18 +3,21 @@
 
 #include <tls.h>
 
-#include "tls-macros.h"
+#ifdef USE_TLS
+# include "tls-macros.h"
 
 
 /* Two 'int' variables in TLS.  */
 VAR_INT_DEF(foo);
 VAR_INT_DEF(bar);
+#endif
 
 
 #define TEST_FUNCTION do_test ()
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   int result = 0;
   int *ap, *bp;
 
@@ -79,6 +82,9 @@ do_test (void)
     }
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls3.c b/elf/tst-tls3.c
index c5e501eb4e..84be43575b 100644
--- a/elf/tst-tls3.c
+++ b/elf/tst-tls3.c
@@ -3,13 +3,15 @@
 
 #include <tls.h>
 
-#include "tls-macros.h"
+#ifdef USE_TLS
+# include "tls-macros.h"
 
 
 /* One define int variable, two externs.  */
 COMMON_INT_DECL(foo);
 VAR_INT_DECL(bar);
 VAR_INT_DEF(baz);
+#endif
 
 
 extern int in_dso (void);
@@ -19,6 +21,7 @@ extern int in_dso (void);
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   int result = 0;
   int *ap, *bp, *cp;
 
@@ -64,6 +67,9 @@ do_test (void)
   result |= in_dso ();
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls4.c b/elf/tst-tls4.c
index 4ae33db24d..f92ee53ce5 100644
--- a/elf/tst-tls4.c
+++ b/elf/tst-tls4.c
@@ -9,6 +9,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname[] = "tst-tlsmod2.so";
   int result = 0;
   int *foop;
@@ -46,6 +47,9 @@ do_test (void)
   dlclose (h);
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls5.c b/elf/tst-tls5.c
index 27b18294fb..a571d2cd3f 100644
--- a/elf/tst-tls5.c
+++ b/elf/tst-tls5.c
@@ -9,6 +9,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname[] = "tst-tlsmod2.so";
   int result = 0;
   int *foop;
@@ -62,6 +63,9 @@ do_test (void)
   dlclose (h);
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls6.c b/elf/tst-tls6.c
index 021622d9c7..68d706538f 100644
--- a/elf/tst-tls6.c
+++ b/elf/tst-tls6.c
@@ -10,6 +10,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname[] = "tst-tlsmod2.so";
   int result = 0;
   int *foop;
@@ -80,6 +81,9 @@ do_test (void)
     }
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls7.c b/elf/tst-tls7.c
index 1edc2b6356..37f1a63e16 100644
--- a/elf/tst-tls7.c
+++ b/elf/tst-tls7.c
@@ -10,6 +10,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname[] = "tst-tlsmod3.so";
   int result = 0;
   int (*fp) (void);
@@ -51,6 +52,9 @@ do_test (void)
     }
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls8.c b/elf/tst-tls8.c
index 36b1baca63..ccc4e9f6fa 100644
--- a/elf/tst-tls8.c
+++ b/elf/tst-tls8.c
@@ -10,6 +10,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname1[] = "$ORIGIN/tst-tlsmod3.so";
   static const char modname2[] = "$ORIGIN/tst-tlsmod4.so";
   int result = 0;
@@ -164,6 +165,9 @@ do_test (void)
     }
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tls9.c b/elf/tst-tls9.c
index 12078518fa..e317696dfc 100644
--- a/elf/tst-tls9.c
+++ b/elf/tst-tls9.c
@@ -9,6 +9,7 @@
 static int
 do_test (void)
 {
+#ifdef USE_TLS
   static const char modname1[] = "tst-tlsmod5.so";
   static const char modname2[] = "tst-tlsmod6.so";
   int result = 0;
@@ -32,6 +33,9 @@ do_test (void)
     dlclose (h2);
 
   return result;
+#else
+  return 0;
+#endif
 }
 
 
diff --git a/elf/tst-tlsmod1.c b/elf/tst-tlsmod1.c
index 4d966c9472..c74a617b77 100644
--- a/elf/tst-tlsmod1.c
+++ b/elf/tst-tlsmod1.c
@@ -2,6 +2,7 @@
 
 #include <tls.h>
 
+#ifdef USE_TLS
 #include "tls-macros.h"
 
 
@@ -9,6 +10,7 @@
 COMMON_INT_DEF(foo);
 VAR_INT_DEF(bar);
 VAR_INT_DECL(baz);
+#endif
 
 extern int in_dso (void);
 
@@ -16,6 +18,7 @@ int
 in_dso (void)
 {
   int result = 0;
+#ifdef USE_TLS
   int *ap, *bp, *cp;
 
   /* Get variables using initial exec model.  */
@@ -59,6 +62,7 @@ in_dso (void)
       printf ("baz = %d\n", *cp);
       result = 1;
     }
+#endif
 
   return result;
 }
diff --git a/elf/tst-tlsmod13.c b/elf/tst-tlsmod13.c
index e4e23af2d8..beca89f6fe 100644
--- a/elf/tst-tlsmod13.c
+++ b/elf/tst-tlsmod13.c
@@ -1,6 +1,7 @@
 #include <tls.h>
 
-#if defined HAVE___THREAD && defined HAVE_TLS_MODEL_ATTRIBUTE
+#if defined USE_TLS && defined HAVE___THREAD \
+    && defined HAVE_TLS_MODEL_ATTRIBUTE
 __thread int a[2] __attribute__ ((tls_model ("initial-exec")));
 #else
 int a[2];
diff --git a/elf/tst-tlsmod13a.c b/elf/tst-tlsmod13a.c
index 70a02131ee..14b12b032b 100644
--- a/elf/tst-tlsmod13a.c
+++ b/elf/tst-tlsmod13a.c
@@ -1,6 +1,7 @@
 #include <tls.h>
 
-#if defined HAVE___THREAD && defined HAVE_TLS_MODEL_ATTRIBUTE
+#if defined USE_TLS && defined HAVE___THREAD \
+    && defined HAVE_TLS_MODEL_ATTRIBUTE
 __thread int b[2] __attribute__ ((tls_model ("initial-exec")));
 #else
 int b[2];
diff --git a/elf/tst-tlsmod14a.c b/elf/tst-tlsmod14a.c
index ad1d179aac..0bb393d9c5 100644
--- a/elf/tst-tlsmod14a.c
+++ b/elf/tst-tlsmod14a.c
@@ -3,9 +3,9 @@
 
 #include <tls.h>
 
-#if HAVE___THREAD
+#if USE_TLS && HAVE___THREAD
 
-# define AL 4096
+#define AL 4096
 struct foo
 {
   int i;
@@ -15,9 +15,9 @@ static __thread struct foo f;
 static struct foo g;
 
 
-# ifndef FCT
-#  define FCT in_dso1
-# endif
+#ifndef FCT
+# define FCT in_dso1
+#endif
 
 
 int
diff --git a/elf/tst-tlsmod2.c b/elf/tst-tlsmod2.c
index 981923313c..98d9d3e518 100644
--- a/elf/tst-tlsmod2.c
+++ b/elf/tst-tlsmod2.c
@@ -2,6 +2,7 @@
 
 #include <tls.h>
 
+#ifdef USE_TLS
 #include "tls-macros.h"
 
 
@@ -34,3 +35,4 @@ in_dso (int n, int *caller_foop)
 
   return result;
 }
+#endif
diff --git a/elf/tst-tlsmod3.c b/elf/tst-tlsmod3.c
index 5c456ee2da..4a8aad659f 100644
--- a/elf/tst-tlsmod3.c
+++ b/elf/tst-tlsmod3.c
@@ -2,7 +2,8 @@
 
 #include <tls.h>
 
-#include "tls-macros.h"
+#ifdef USE_TLS
+# include "tls-macros.h"
 
 extern int in_dso (int n, int *caller_foop);
 
@@ -37,3 +38,4 @@ in_dso2 (void)
 
   return result;
 }
+#endif
diff --git a/elf/tst-tlsmod4.c b/elf/tst-tlsmod4.c
index dd95486618..5285e821b0 100644
--- a/elf/tst-tlsmod4.c
+++ b/elf/tst-tlsmod4.c
@@ -2,7 +2,8 @@
 
 #include <tls.h>
 
-#include "tls-macros.h"
+#ifdef USE_TLS
+# include "tls-macros.h"
 
 
 COMMON_INT_DEF(baz);
@@ -34,3 +35,4 @@ in_dso (int n, int *caller_bazp)
 
   return result;
 }
+#endif
diff --git a/elf/tst-tlsmod5.c b/elf/tst-tlsmod5.c
index 00d3a9d920..2ec69e13b3 100644
--- a/elf/tst-tlsmod5.c
+++ b/elf/tst-tlsmod5.c
@@ -1,5 +1,7 @@
 #include <tls.h>
 
+#ifdef USE_TLS
 #include "tls-macros.h"
 
 COMMON_INT_DEF(foo);
+#endif
diff --git a/elf/tst-tlsmod6.c b/elf/tst-tlsmod6.c
index 244d9ae485..0fda51b226 100644
--- a/elf/tst-tlsmod6.c
+++ b/elf/tst-tlsmod6.c
@@ -1,5 +1,7 @@
 #include <tls.h>
 
+#ifdef USE_TLS
 #include "tls-macros.h"
 
 COMMON_INT_DEF(bar);
+#endif