b25f0c2
2007-08-01  Andreas Jaeger  <aj@suse.de>
b25f0c2
	    Jakub Jelinek  <jakub@redhat.com>
b25f0c2
b25f0c2
	* elf/ldconfig.c (opt_ignore_aux_cache): Add new option.
b25f0c2
	(options): Add option.
b25f0c2
	(parse_opt): Handle option.
b25f0c2
	(manual_link): Adjust process_file caller.  Call implicit_soname.
b25f0c2
	(search_dir): Formatting.  Use and populate auxiliary cache.
b25f0c2
	(main): Load and save auxiliary cache.
b25f0c2
	* elf/readlib.c (process_file): Add stat_buf argument.  Pass struct
b25f0c2
	stat64 from fstat64 to caller.
b25f0c2
	(implicit_soname): New function.
b25f0c2
	* elf/readelflib.c (process_elf_file): If DT_SONAME is not present,
b25f0c2
	leave *soname as NULL.
b25f0c2
	* elf/cache.c: Include libgen.h.
b25f0c2
	(print_entry, print_cache, compare, save_cache, add_to_cache):
b25f0c2
	Formatting and cleanups.
b25f0c2
	(aux_cache_entry_id, aux_cache_entry, aux_cache_file_entry,
b25f0c2
	aux_cache_file): New structures.
b25f0c2
	(AUX_CACHEMAGIC): Define.
b25f0c2
	(primes): New array.
b25f0c2
	(aux_hash_size, aux_hash): New variables.
b25f0c2
	(aux_cache_entry_id_hash, nextprime, init_aux_cache,
b25f0c2
	search_aux_cache, insert_to_aux_cache, add_to_aux_cache,
b25f0c2
	load_aux_cache, save_aux_cache): New functions.
b25f0c2
	* sysdeps/generic/ldconfig.h (_PATH_LDCONFIG_AUX_CACHE): Define.
b25f0c2
	(init_aux_cache, search_aux_cache, add_to_aux_cache,
b25f0c2
	load_aux_cache, save_aux_cache, implicit_soname): New prototypes.
b25f0c2
	(process_file): Adjust prototype.
b25f0c2
b25f0c2
--- libc/elf/ldconfig.c.jj	2007-07-16 09:58:46.000000000 +0200
b25f0c2
+++ libc/elf/ldconfig.c	2007-08-01 18:54:26.000000000 +0200
b25f0c2
@@ -112,6 +112,9 @@ static char *opt_chroot;
b25f0c2
 /* Manually link given shared libraries.  */
b25f0c2
 static int opt_manual_link;
b25f0c2
 
b25f0c2
+/* Should we ignore an old auxiliary cache file?  */
b25f0c2
+static int opt_ignore_aux_cache;
b25f0c2
+
b25f0c2
 /* Cache file to use.  */
b25f0c2
 static char *cache_file;
b25f0c2
 
b25f0c2
@@ -142,6 +145,7 @@ static const struct argp_option options[
b25f0c2
   { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line.  Don't build cache."), 0},
b25f0c2
   { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0},
b25f0c2
   { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new, old or compat (default)"), 0},
b25f0c2
+  { "ignore-aux-cache", 'i', NULL, 0, N_("Ignore auxiliary cache file"), 0},
b25f0c2
   { NULL, 0, NULL, 0, NULL, 0 }
b25f0c2
 };
b25f0c2
 
b25f0c2
@@ -238,10 +242,15 @@ parse_opt (int key, char *arg, struct ar
b25f0c2
     {
b25f0c2
     case 'C':
b25f0c2
       cache_file = arg;
b25f0c2
+      /* Ignore auxiliary cache since we use non-standard cache.  */
b25f0c2
+      opt_ignore_aux_cache = 1;
b25f0c2
       break;
b25f0c2
     case 'f':
b25f0c2
       config_file = arg;
b25f0c2
       break;
b25f0c2
+    case 'i':
b25f0c2
+      opt_ignore_aux_cache = 1;
b25f0c2
+      break;
b25f0c2
     case 'l':
b25f0c2
       opt_manual_link = 1;
b25f0c2
       break;
b25f0c2
@@ -518,7 +527,7 @@ manual_link (char *library)
b25f0c2
   if (libname)
b25f0c2
     {
b25f0c2
       /* Successfully split names.  Check if path is just "/" to avoid
b25f0c2
-         an empty path.  */
b25f0c2
+	 an empty path.  */
b25f0c2
       if (libname == path)
b25f0c2
 	{
b25f0c2
 	  libname = library + 1;
b25f0c2
@@ -572,14 +581,17 @@ manual_link (char *library)
b25f0c2
       free (path);
b25f0c2
       return;
b25f0c2
     }
b25f0c2
+
b25f0c2
   if (process_file (real_library, library, libname, &flag, &osversion,
b25f0c2
-		    &soname, 0))
b25f0c2
+		    &soname, 0, &stat_buf))
b25f0c2
     {
b25f0c2
       error (0, 0, _("No link created since soname could not be found for %s"),
b25f0c2
 	     library);
b25f0c2
       free (path);
b25f0c2
       return;
b25f0c2
     }
b25f0c2
+  if (soname == NULL)
b25f0c2
+    soname = implicit_soname (libname, flag);
b25f0c2
   create_links (real_path, path, libname, soname);
b25f0c2
   free (soname);
b25f0c2
   free (path);
b25f0c2
@@ -625,23 +637,7 @@ struct dlib_entry
b25f0c2
 static void
b25f0c2
 search_dir (const struct dir_entry *entry)
b25f0c2
 {
b25f0c2
-  DIR *dir;
b25f0c2
-  struct dirent64 *direntry;
b25f0c2
-  char *file_name, *dir_name, *real_file_name, *real_name;
b25f0c2
-  int file_name_len, real_file_name_len, len;
b25f0c2
-  char *soname;
b25f0c2
-  struct dlib_entry *dlibs;
b25f0c2
-  struct dlib_entry *dlib_ptr;
b25f0c2
-  struct stat64 lstat_buf, stat_buf;
b25f0c2
-  int is_link, is_dir;
b25f0c2
   uint64_t hwcap = path_hwcap (entry->path);
b25f0c2
-  unsigned int osversion;
b25f0c2
-
b25f0c2
-  file_name_len = PATH_MAX;
b25f0c2
-  file_name = alloca (file_name_len);
b25f0c2
-
b25f0c2
-  dlibs = NULL;
b25f0c2
-
b25f0c2
   if (opt_verbose)
b25f0c2
     {
b25f0c2
       if (hwcap != 0)
b25f0c2
@@ -650,6 +646,11 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	printf ("%s:\n", entry->path);
b25f0c2
     }
b25f0c2
 
b25f0c2
+  char *dir_name;
b25f0c2
+  char *real_file_name;
b25f0c2
+  size_t real_file_name_len;
b25f0c2
+  size_t file_name_len = PATH_MAX;
b25f0c2
+  char *file_name = alloca (file_name_len);
b25f0c2
   if (opt_chroot)
b25f0c2
     {
b25f0c2
       dir_name = chroot_canon (opt_chroot, entry->path);
b25f0c2
@@ -663,6 +664,7 @@ search_dir (const struct dir_entry *entr
b25f0c2
       real_file_name = file_name;
b25f0c2
     }
b25f0c2
 
b25f0c2
+  DIR *dir;
b25f0c2
   if (dir_name == NULL || (dir = opendir (dir_name)) == NULL)
b25f0c2
     {
b25f0c2
       if (opt_verbose)
b25f0c2
@@ -672,6 +674,8 @@ search_dir (const struct dir_entry *entr
b25f0c2
       return;
b25f0c2
     }
b25f0c2
 
b25f0c2
+  struct dirent64 *direntry;
b25f0c2
+  struct dlib_entry *dlibs = NULL;
b25f0c2
   while ((direntry = readdir64 (dir)) != NULL)
b25f0c2
     {
b25f0c2
       int flag;
b25f0c2
@@ -695,7 +699,8 @@ search_dir (const struct dir_entry *entr
b25f0c2
 #endif
b25f0c2
 	      !is_hwcap_platform (direntry->d_name)))
b25f0c2
 	continue;
b25f0c2
-      len = strlen (direntry->d_name);
b25f0c2
+
b25f0c2
+      size_t len = strlen (direntry->d_name);
b25f0c2
       /* Skip temporary files created by the prelink program.  Files with
b25f0c2
 	 names like these are never really DSOs we want to look at.  */
b25f0c2
       if (len >= sizeof (".#prelink#") - 1)
b25f0c2
@@ -727,7 +732,10 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	    }
b25f0c2
 	  sprintf (real_file_name, "%s/%s", dir_name, direntry->d_name);
b25f0c2
 	}
b25f0c2
+
b25f0c2
+      struct stat64 lstat_buf;
b25f0c2
 #ifdef _DIRENT_HAVE_D_TYPE
b25f0c2
+      /* We optimize and try to do the lstat call only if needed.  */
b25f0c2
       if (direntry->d_type != DT_UNKNOWN)
b25f0c2
 	lstat_buf.st_mode = DTTOIF (direntry->d_type);
b25f0c2
       else
b25f0c2
@@ -738,9 +746,11 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	    continue;
b25f0c2
 	  }
b25f0c2
 
b25f0c2
-      is_link = S_ISLNK (lstat_buf.st_mode);
b25f0c2
+      struct stat64 stat_buf;
b25f0c2
+      int is_dir;
b25f0c2
+      int is_link = S_ISLNK (lstat_buf.st_mode);
b25f0c2
       if (is_link)
b25f0c2
-        {
b25f0c2
+	{
b25f0c2
 	  /* In case of symlink, we check if the symlink refers to
b25f0c2
 	     a directory. */
b25f0c2
 	  if (__builtin_expect (stat64 (real_file_name, &stat_buf), 0))
b25f0c2
@@ -754,6 +764,12 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	      continue;
b25f0c2
 	    }
b25f0c2
 	  is_dir = S_ISDIR (stat_buf.st_mode);
b25f0c2
+
b25f0c2
+	  /* lstat_buf is later stored, update contents.  */
b25f0c2
+	  lstat_buf.st_dev = stat_buf.st_dev;
b25f0c2
+	  lstat_buf.st_ino = stat_buf.st_ino;
b25f0c2
+	  lstat_buf.st_size = stat_buf.st_size;
b25f0c2
+	  lstat_buf.st_ctime = stat_buf.st_ctime;
b25f0c2
 	}
b25f0c2
       else
b25f0c2
 	is_dir = S_ISDIR (lstat_buf.st_mode);
b25f0c2
@@ -767,36 +783,28 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	  new_entry->path = xstrdup (file_name);
b25f0c2
 	  new_entry->flag = entry->flag;
b25f0c2
 	  new_entry->next = NULL;
b25f0c2
-	  if (is_link)
b25f0c2
+#ifdef _DIRENT_HAVE_D_TYPE
b25f0c2
+	  /* We have filled in lstat only #ifndef
b25f0c2
+	     _DIRENT_HAVE_D_TYPE.  Fill it in if needed.  */
b25f0c2
+	  if (!is_link
b25f0c2
+	      && direntry->d_type != DT_UNKNOWN
b25f0c2
+	      && __builtin_expect (lstat64 (real_file_name, &lstat_buf), 0))
b25f0c2
 	    {
b25f0c2
-	      new_entry->ino = stat_buf.st_ino;
b25f0c2
-	      new_entry->dev = stat_buf.st_dev;
b25f0c2
+	      error (0, errno, _("Cannot lstat %s"), file_name);
b25f0c2
+	      free (new_entry->path);
b25f0c2
+	      free (new_entry);
b25f0c2
+	      continue;
b25f0c2
 	    }
b25f0c2
-	  else
b25f0c2
-	    {
b25f0c2
-#ifdef _DIRENT_HAVE_D_TYPE
b25f0c2
-	      /* We have filled in lstat only #ifndef
b25f0c2
-		 _DIRENT_HAVE_D_TYPE.  Fill it in if needed.  */
b25f0c2
-	      if (direntry->d_type != DT_UNKNOWN
b25f0c2
-		  && __builtin_expect (lstat64 (real_file_name, &lstat_buf),
b25f0c2
-				       0))
b25f0c2
-		{
b25f0c2
-		  error (0, errno, _("Cannot lstat %s"), file_name);
b25f0c2
-		  free (new_entry->path);
b25f0c2
-		  free (new_entry);
b25f0c2
-		  continue;
b25f0c2
-		}
b25f0c2
 #endif
b25f0c2
-
b25f0c2
-	      new_entry->ino = lstat_buf.st_ino;
b25f0c2
-	      new_entry->dev = lstat_buf.st_dev;
b25f0c2
-	    }
b25f0c2
+	  new_entry->ino = lstat_buf.st_ino;
b25f0c2
+	  new_entry->dev = lstat_buf.st_dev;
b25f0c2
 	  add_single_dir (new_entry, 0);
b25f0c2
 	  continue;
b25f0c2
 	}
b25f0c2
       else if (!S_ISREG (lstat_buf.st_mode) && !is_link)
b25f0c2
 	continue;
b25f0c2
 
b25f0c2
+      char *real_name;
b25f0c2
       if (opt_chroot && is_link)
b25f0c2
 	{
b25f0c2
 	  real_name = chroot_canon (opt_chroot, file_name);
b25f0c2
@@ -810,14 +818,36 @@ search_dir (const struct dir_entry *entr
b25f0c2
       else
b25f0c2
 	real_name = real_file_name;
b25f0c2
 
b25f0c2
-      if (process_file (real_name, file_name, direntry->d_name, &flag,
b25f0c2
-			&osversion, &soname, is_link))
b25f0c2
+#ifdef _DIRENT_HAVE_D_TYPE
b25f0c2
+      /* Call lstat64 if not done yet.  */
b25f0c2
+      if (!is_link
b25f0c2
+	  && direntry->d_type != DT_UNKNOWN
b25f0c2
+	  && __builtin_expect (lstat64 (real_file_name, &lstat_buf), 0))
b25f0c2
 	{
b25f0c2
-	  if (real_name != real_file_name)
b25f0c2
-	    free (real_name);
b25f0c2
+	  error (0, errno, _("Cannot lstat %s"), file_name);
b25f0c2
 	  continue;
b25f0c2
 	}
b25f0c2
+#endif
b25f0c2
+
b25f0c2
+      /* First search whether the auxiliary cache contains this
b25f0c2
+	 library already and it's not changed.  */
b25f0c2
+      char *soname;
b25f0c2
+      unsigned int osversion;
b25f0c2
+      if (!search_aux_cache (&lstat_buf, &flag, &osversion, &soname))
b25f0c2
+	{
b25f0c2
+	  if (process_file (real_name, file_name, direntry->d_name, &flag,
b25f0c2
+			    &osversion, &soname, is_link, &lstat_buf))
b25f0c2
+	    {
b25f0c2
+	      if (real_name != real_file_name)
b25f0c2
+		free (real_name);
b25f0c2
+	      continue;
b25f0c2
+	    }
b25f0c2
+	  else if (opt_build_cache)
b25f0c2
+	    add_to_aux_cache (&lstat_buf, flag, osversion, soname);
b25f0c2
+	}
b25f0c2
 
b25f0c2
+      if (soname == NULL)
b25f0c2
+	soname = implicit_soname (direntry->d_name, flag);
b25f0c2
 
b25f0c2
       /* A link may just point to itself.  */
b25f0c2
       if (is_link)
b25f0c2
@@ -834,7 +864,7 @@ search_dir (const struct dir_entry *entr
b25f0c2
 		  || strncmp (real_base_name, soname, len) != 0)
b25f0c2
 		is_link = 0;
b25f0c2
 	    }
b25f0c2
-        }
b25f0c2
+	}
b25f0c2
 
b25f0c2
       if (real_name != real_file_name)
b25f0c2
 	free (real_name);
b25f0c2
@@ -849,6 +879,7 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	  && (entry->flag == FLAG_ELF_LIBC5
b25f0c2
 	      || entry->flag == FLAG_ELF_LIBC6))
b25f0c2
 	flag = entry->flag;
b25f0c2
+
b25f0c2
       /* Some sanity checks to print warnings.  */
b25f0c2
       if (opt_verbose)
b25f0c2
 	{
b25f0c2
@@ -864,6 +895,7 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	}
b25f0c2
 
b25f0c2
       /* Add library to list.  */
b25f0c2
+      struct dlib_entry *dlib_ptr;
b25f0c2
       for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
b25f0c2
 	{
b25f0c2
 	  /* Is soname already in list?  */
b25f0c2
@@ -888,12 +920,13 @@ search_dir (const struct dir_entry *entr
b25f0c2
 			dlib_ptr->flag = flag;
b25f0c2
 		      else
b25f0c2
 			error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
b25f0c2
-			       dlib_ptr->name, direntry->d_name, entry->path);
b25f0c2
+			       dlib_ptr->name, direntry->d_name,
b25f0c2
+			       entry->path);
b25f0c2
 		    }
b25f0c2
 		  free (dlib_ptr->name);
b25f0c2
-		  dlib_ptr->osversion = osversion;
b25f0c2
 		  dlib_ptr->name = xstrdup (direntry->d_name);
b25f0c2
 		  dlib_ptr->is_link = is_link;
b25f0c2
+		  dlib_ptr->osversion = osversion;
b25f0c2
 		}
b25f0c2
 	      /* Don't add this library, abort loop.  */
b25f0c2
 	      /* Also free soname, since it's dynamically allocated.  */
b25f0c2
@@ -906,10 +939,10 @@ search_dir (const struct dir_entry *entr
b25f0c2
 	{
b25f0c2
 	  dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry));
b25f0c2
 	  dlib_ptr->name = xstrdup (direntry->d_name);
b25f0c2
-	  dlib_ptr->flag = flag;
b25f0c2
-	  dlib_ptr->osversion = osversion;
b25f0c2
 	  dlib_ptr->soname = soname;
b25f0c2
+	  dlib_ptr->flag = flag;
b25f0c2
 	  dlib_ptr->is_link = is_link;
b25f0c2
+	  dlib_ptr->osversion = osversion;
b25f0c2
 	  /* Add at head of list.  */
b25f0c2
 	  dlib_ptr->next = dlibs;
b25f0c2
 	  dlibs = dlib_ptr;
b25f0c2
@@ -920,6 +953,7 @@ search_dir (const struct dir_entry *entr
b25f0c2
 
b25f0c2
   /* Now dlibs contains a list of all libs - add those to the cache
b25f0c2
      and created all symbolic links.  */
b25f0c2
+  struct dlib_entry *dlib_ptr;
b25f0c2
   for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
b25f0c2
     {
b25f0c2
       /* Don't create links to links.  */
b25f0c2
@@ -1246,7 +1280,7 @@ main (int argc, char **argv)
b25f0c2
   if (opt_chroot)
b25f0c2
     {
b25f0c2
       /* Canonicalize the directory name of cache_file, not cache_file,
b25f0c2
-         because we'll rename a temporary cache file to it.  */
b25f0c2
+	 because we'll rename a temporary cache file to it.  */
b25f0c2
       char *p = strrchr (cache_file, '/');
b25f0c2
       char *canon = chroot_canon (opt_chroot,
b25f0c2
 				  p ? (*p = '\0', cache_file) : "/");
b25f0c2
@@ -1293,10 +1327,18 @@ main (int argc, char **argv)
b25f0c2
 	add_system_dir (LIBDIR);
b25f0c2
     }
b25f0c2
 
b25f0c2
+  if (! opt_ignore_aux_cache)
b25f0c2
+    load_aux_cache (_PATH_LDCONFIG_AUX_CACHE);
b25f0c2
+  else
b25f0c2
+    init_aux_cache ();
b25f0c2
+
b25f0c2
   search_dirs ();
b25f0c2
 
b25f0c2
   if (opt_build_cache)
b25f0c2
-    save_cache (cache_file);
b25f0c2
+    {
b25f0c2
+      save_cache (cache_file);
b25f0c2
+      save_aux_cache (_PATH_LDCONFIG_AUX_CACHE);
b25f0c2
+    }
b25f0c2
 
b25f0c2
   return 0;
b25f0c2
 }
b25f0c2
--- libc/elf/readlib.c.jj	2007-07-16 09:58:46.000000000 +0200
b25f0c2
+++ libc/elf/readlib.c	2007-08-01 18:54:26.000000000 +0200
b25f0c2
@@ -1,4 +1,4 @@
b25f0c2
-/* Copyright (C) 1999-2003, 2005 Free Software Foundation, Inc.
b25f0c2
+/* Copyright (C) 1999-2003, 2005, 2007 Free Software Foundation, Inc.
b25f0c2
    This file is part of the GNU C Library.
b25f0c2
    Contributed by Andreas Jaeger <aj@suse.de>, 1999 and
b25f0c2
 		  Jakub Jelinek <jakub@redhat.com>, 1999.
b25f0c2
@@ -69,7 +69,7 @@ static struct known_names known_libs[] =
b25f0c2
 int
b25f0c2
 process_file (const char *real_file_name, const char *file_name,
b25f0c2
 	      const char *lib, int *flag, unsigned int *osversion,
b25f0c2
-	      char **soname, int is_link)
b25f0c2
+	      char **soname, int is_link, struct stat64 *stat_buf)
b25f0c2
 {
b25f0c2
   FILE *file;
b25f0c2
   struct stat64 statbuf;
b25f0c2
@@ -135,7 +135,7 @@ process_file (const char *real_file_name
b25f0c2
       )
b25f0c2
     {
b25f0c2
       /* Aout files don't have a soname, just return the name
b25f0c2
-         including the major number.  */
b25f0c2
+	 including the major number.  */
b25f0c2
       char *copy, *major, *dot;
b25f0c2
       copy = xstrdup (lib);
b25f0c2
       major = strstr (copy, ".so.");
b25f0c2
@@ -175,8 +175,31 @@ process_file (const char *real_file_name
b25f0c2
   munmap (file_contents, statbuf.st_size);
b25f0c2
   fclose (file);
b25f0c2
 
b25f0c2
+  *stat_buf = statbuf;
b25f0c2
   return ret;
b25f0c2
 }
b25f0c2
 
b25f0c2
+/* Returns made up soname if lib doesn't have explicit DT_SONAME.  */
b25f0c2
+
b25f0c2
+char *
b25f0c2
+implicit_soname (const char *lib, int flag)
b25f0c2
+{
b25f0c2
+  char *soname = xstrdup (lib);
b25f0c2
+
b25f0c2
+  if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4)
b25f0c2
+    return soname;
b25f0c2
+
b25f0c2
+  /* Aout files don't have a soname, just return the name
b25f0c2
+     including the major number.  */
b25f0c2
+  char *major = strstr (soname, ".so.");
b25f0c2
+  if (major)
b25f0c2
+    {
b25f0c2
+      char *dot = strstr (major + 4, ".");
b25f0c2
+      if (dot)
b25f0c2
+	*dot = '\0';
b25f0c2
+    }
b25f0c2
+  return soname;
b25f0c2
+}
b25f0c2
+
b25f0c2
 /* Get architecture specific version of process_elf_file.  */
b25f0c2
 #include <readelflib.c>
b25f0c2
--- libc/elf/readelflib.c.jj	2007-07-03 12:36:59.000000000 +0200
b25f0c2
+++ libc/elf/readelflib.c	2007-08-01 18:54:26.000000000 +0200
b25f0c2
@@ -231,11 +231,5 @@ process_elf_file (const char *file_name,
b25f0c2
 	}
b25f0c2
     }
b25f0c2
 
b25f0c2
-  /* We reach this point only if the file doesn't contain a DT_SONAME
b25f0c2
-     or if we can't classify the library.  If it doesn't have a
b25f0c2
-     soname, return the name of the library.  */
b25f0c2
-  if (*soname == NULL)
b25f0c2
-    *soname = xstrdup (lib);
b25f0c2
-
b25f0c2
   return 0;
b25f0c2
 }
b25f0c2
--- libc/elf/cache.c.jj	2007-07-16 09:58:46.000000000 +0200
b25f0c2
+++ libc/elf/cache.c	2007-08-01 19:00:30.000000000 +0200
b25f0c2
@@ -1,4 +1,4 @@
b25f0c2
-/* Copyright (C) 1999-2003,2005,2006 Free Software Foundation, Inc.
b25f0c2
+/* Copyright (C) 1999-2003,2005,2006,2007 Free Software Foundation, Inc.
b25f0c2
    This file is part of the GNU C Library.
b25f0c2
    Contributed by Andreas Jaeger <aj@suse.de>, 1999.
b25f0c2
 
b25f0c2
@@ -20,6 +20,7 @@
b25f0c2
 #include <error.h>
b25f0c2
 #include <dirent.h>
b25f0c2
 #include <inttypes.h>
b25f0c2
+#include <libgen.h>
b25f0c2
 #include <libintl.h>
b25f0c2
 #include <stdio.h>
b25f0c2
 #include <stdlib.h>
b25f0c2
@@ -80,16 +81,16 @@ print_entry (const char *lib, int flag, 
b25f0c2
       fputs (",x86-64", stdout);
b25f0c2
       break;
b25f0c2
     case FLAG_S390_LIB64:
b25f0c2
-      fputs(",64bit", stdout);
b25f0c2
+      fputs (",64bit", stdout);
b25f0c2
       break;
b25f0c2
     case FLAG_POWERPC_LIB64:
b25f0c2
-      fputs(",64bit", stdout);
b25f0c2
+      fputs (",64bit", stdout);
b25f0c2
       break;
b25f0c2
     case FLAG_MIPS64_LIBN32:
b25f0c2
-      fputs(",N32", stdout);
b25f0c2
+      fputs (",N32", stdout);
b25f0c2
       break;
b25f0c2
     case FLAG_MIPS64_LIBN64:
b25f0c2
-      fputs(",64bit", stdout);
b25f0c2
+      fputs (",64bit", stdout);
b25f0c2
     case 0:
b25f0c2
       break;
b25f0c2
     default:
b25f0c2
@@ -128,19 +129,11 @@ print_entry (const char *lib, int flag, 
b25f0c2
 void
b25f0c2
 print_cache (const char *cache_name)
b25f0c2
 {
b25f0c2
-  size_t cache_size;
b25f0c2
-  struct stat64 st;
b25f0c2
-  int fd;
b25f0c2
-  unsigned int i;
b25f0c2
-  struct cache_file *cache;
b25f0c2
-  struct cache_file_new *cache_new = NULL;
b25f0c2
-  const char *cache_data;
b25f0c2
-  int format = 0;
b25f0c2
-
b25f0c2
-  fd = open (cache_name, O_RDONLY);
b25f0c2
+  int fd = open (cache_name, O_RDONLY);
b25f0c2
   if (fd < 0)
b25f0c2
     error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"), cache_name);
b25f0c2
 
b25f0c2
+  struct stat64 st;
b25f0c2
   if (fstat64 (fd, &st) < 0
b25f0c2
       /* No need to map the file if it is empty.  */
b25f0c2
       || st.st_size == 0)
b25f0c2
@@ -149,14 +142,19 @@ print_cache (const char *cache_name)
b25f0c2
       return;
b25f0c2
     }
b25f0c2
 
b25f0c2
-  cache = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
b25f0c2
+  struct cache_file *cache
b25f0c2
+    = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
b25f0c2
   if (cache == MAP_FAILED)
b25f0c2
     error (EXIT_FAILURE, errno, _("mmap of cache file failed.\n"));
b25f0c2
-  cache_size = st.st_size;
b25f0c2
 
b25f0c2
+  size_t cache_size = st.st_size;
b25f0c2
   if (cache_size < sizeof (struct cache_file))
b25f0c2
     error (EXIT_FAILURE, 0, _("File is not a cache file.\n"));
b25f0c2
 
b25f0c2
+  struct cache_file_new *cache_new = NULL;
b25f0c2
+  const char *cache_data;
b25f0c2
+  int format = 0;
b25f0c2
+
b25f0c2
   if (memcmp (cache->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1))
b25f0c2
     {
b25f0c2
       /* This can only be the new format without the old one.  */
b25f0c2
@@ -201,7 +199,7 @@ print_cache (const char *cache_name)
b25f0c2
       printf (_("%d libs found in cache `%s'\n"), cache->nlibs, cache_name);
b25f0c2
 
b25f0c2
       /* Print everything.  */
b25f0c2
-      for (i = 0; i < cache->nlibs; i++)
b25f0c2
+      for (unsigned int i = 0; i < cache->nlibs; i++)
b25f0c2
 	print_entry (cache_data + cache->libs[i].key,
b25f0c2
 		     cache->libs[i].flags, 0, 0,
b25f0c2
 		     cache_data + cache->libs[i].value);
b25f0c2
@@ -212,7 +210,7 @@ print_cache (const char *cache_name)
b25f0c2
 	      cache_new->nlibs, cache_name);
b25f0c2
 
b25f0c2
       /* Print everything.  */
b25f0c2
-      for (i = 0; i < cache_new->nlibs; i++)
b25f0c2
+      for (unsigned int i = 0; i < cache_new->nlibs; i++)
b25f0c2
 	print_entry (cache_data + cache_new->libs[i].key,
b25f0c2
 		     cache_new->libs[i].flags,
b25f0c2
 		     cache_new->libs[i].osversion,
b25f0c2
@@ -231,15 +229,11 @@ init_cache (void)
b25f0c2
   entries = NULL;
b25f0c2
 }
b25f0c2
 
b25f0c2
-
b25f0c2
-
b25f0c2
-static
b25f0c2
-int compare (const struct cache_entry *e1, const struct cache_entry *e2)
b25f0c2
+static int
b25f0c2
+compare (const struct cache_entry *e1, const struct cache_entry *e2)
b25f0c2
 {
b25f0c2
-  int res;
b25f0c2
-
b25f0c2
   /* We need to swap entries here to get the correct sort order.  */
b25f0c2
-  res = _dl_cache_libcmp (e2->lib, e1->lib);
b25f0c2
+  int res = _dl_cache_libcmp (e2->lib, e1->lib);
b25f0c2
   if (res == 0)
b25f0c2
     {
b25f0c2
       if (e1->flags < e2->flags)
b25f0c2
@@ -267,29 +261,19 @@ int compare (const struct cache_entry *e
b25f0c2
 void
b25f0c2
 save_cache (const char *cache_name)
b25f0c2
 {
b25f0c2
-  struct cache_entry *entry;
b25f0c2
-  int fd, idx_old, idx_new;
b25f0c2
-  size_t total_strlen, len;
b25f0c2
-  char *strings, *str, *temp_name;
b25f0c2
-  struct cache_file *file_entries = NULL;
b25f0c2
-  struct cache_file_new *file_entries_new = NULL;
b25f0c2
-  size_t file_entries_size = 0;
b25f0c2
-  size_t file_entries_new_size = 0;
b25f0c2
-  unsigned int str_offset;
b25f0c2
-  /* Number of cache entries.  */
b25f0c2
-  int cache_entry_count = 0;
b25f0c2
-  /* Number of normal cache entries.  */
b25f0c2
-  int cache_entry_old_count = 0;
b25f0c2
-  /* Pad for alignment of cache_file_new.  */
b25f0c2
-  size_t pad;
b25f0c2
-
b25f0c2
   /* The cache entries are sorted already, save them in this order. */
b25f0c2
 
b25f0c2
   /* Count the length of all strings.  */
b25f0c2
   /* The old format doesn't contain hwcap entries and doesn't contain
b25f0c2
      libraries in subdirectories with hwcaps entries.  Count therefore
b25f0c2
      also all entries with hwcap == 0.  */
b25f0c2
-  total_strlen = 0;
b25f0c2
+  size_t total_strlen = 0;
b25f0c2
+  struct cache_entry *entry;
b25f0c2
+  /* Number of cache entries.  */
b25f0c2
+  int cache_entry_count = 0;
b25f0c2
+  /* Number of normal cache entries.  */
b25f0c2
+  int cache_entry_old_count = 0;
b25f0c2
+
b25f0c2
   for (entry = entries; entry != NULL; entry = entry->next)
b25f0c2
     {
b25f0c2
       /* Account the final NULs.  */
b25f0c2
@@ -300,8 +284,8 @@ save_cache (const char *cache_name)
b25f0c2
     }
b25f0c2
 
b25f0c2
   /* Create the on disk cache structure.  */
b25f0c2
-  /* First an array for all strings.  */
b25f0c2
-  strings = (char *)xmalloc (total_strlen);
b25f0c2
+  struct cache_file *file_entries = NULL;
b25f0c2
+  size_t file_entries_size = 0;
b25f0c2
 
b25f0c2
   if (opt_format != 2)
b25f0c2
     {
b25f0c2
@@ -315,25 +299,27 @@ save_cache (const char *cache_name)
b25f0c2
       /* And the list of all entries in the old format.  */
b25f0c2
       file_entries_size = sizeof (struct cache_file)
b25f0c2
 	+ cache_entry_old_count * sizeof (struct file_entry);
b25f0c2
-      file_entries = (struct cache_file *) xmalloc (file_entries_size);
b25f0c2
+      file_entries = xmalloc (file_entries_size);
b25f0c2
 
b25f0c2
       /* Fill in the header.  */
b25f0c2
-      memset (file_entries, 0, sizeof (struct cache_file));
b25f0c2
+      memset (file_entries, '\0', sizeof (struct cache_file));
b25f0c2
       memcpy (file_entries->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1);
b25f0c2
 
b25f0c2
       file_entries->nlibs = cache_entry_old_count;
b25f0c2
     }
b25f0c2
 
b25f0c2
+  struct cache_file_new *file_entries_new = NULL;
b25f0c2
+  size_t file_entries_new_size = 0;
b25f0c2
+
b25f0c2
   if (opt_format != 0)
b25f0c2
     {
b25f0c2
       /* And the list of all entries in the new format.  */
b25f0c2
       file_entries_new_size = sizeof (struct cache_file_new)
b25f0c2
 	+ cache_entry_count * sizeof (struct file_entry_new);
b25f0c2
-      file_entries_new =
b25f0c2
-	(struct cache_file_new *) xmalloc (file_entries_new_size);
b25f0c2
+      file_entries_new = xmalloc (file_entries_new_size);
b25f0c2
 
b25f0c2
       /* Fill in the header.  */
b25f0c2
-      memset (file_entries_new, 0, sizeof (struct cache_file_new));
b25f0c2
+      memset (file_entries_new, '\0', sizeof (struct cache_file_new));
b25f0c2
       memcpy (file_entries_new->magic, CACHEMAGIC_NEW,
b25f0c2
 	      sizeof CACHEMAGIC_NEW - 1);
b25f0c2
       memcpy (file_entries_new->version, CACHE_VERSION,
b25f0c2
@@ -343,17 +329,24 @@ save_cache (const char *cache_name)
b25f0c2
       file_entries_new->len_strings = total_strlen;
b25f0c2
     }
b25f0c2
 
b25f0c2
-  pad = ALIGN_CACHE (file_entries_size) - file_entries_size;
b25f0c2
+  /* Pad for alignment of cache_file_new.  */
b25f0c2
+  size_t pad = ALIGN_CACHE (file_entries_size) - file_entries_size;
b25f0c2
 
b25f0c2
   /* If we have both formats, we hide the new format in the strings
b25f0c2
      table, we have to adjust all string indices for this so that
b25f0c2
      old libc5/glibc 2 dynamic linkers just ignore them.  */
b25f0c2
+  unsigned int str_offset;
b25f0c2
   if (opt_format != 0)
b25f0c2
     str_offset = file_entries_new_size;
b25f0c2
   else
b25f0c2
     str_offset = 0;
b25f0c2
 
b25f0c2
-  str = strings;
b25f0c2
+  /* An array for all strings.  */
b25f0c2
+  char *strings = xmalloc (total_strlen);
b25f0c2
+  char *str = strings;
b25f0c2
+  int idx_old;
b25f0c2
+  int idx_new;
b25f0c2
+
b25f0c2
   for (idx_old = 0, idx_new = 0, entry = entries; entry != NULL;
b25f0c2
        entry = entry->next, ++idx_new)
b25f0c2
     {
b25f0c2
@@ -375,21 +368,18 @@ save_cache (const char *cache_name)
b25f0c2
 	  file_entries_new->libs[idx_new].hwcap = entry->hwcap;
b25f0c2
 	  file_entries_new->libs[idx_new].key = str_offset;
b25f0c2
 	}
b25f0c2
-      len = strlen (entry->lib);
b25f0c2
-      str = stpcpy (str, entry->lib);
b25f0c2
-      /* Account the final NUL.  */
b25f0c2
-      ++str;
b25f0c2
-      str_offset += len + 1;
b25f0c2
+
b25f0c2
+      size_t len = strlen (entry->lib) + 1;
b25f0c2
+      str = mempcpy (str, entry->lib, len);
b25f0c2
+      str_offset += len;
b25f0c2
       /* Then the path.  */
b25f0c2
       if (opt_format != 2 && entry->hwcap == 0)
b25f0c2
 	file_entries->libs[idx_old].value = str_offset + pad;
b25f0c2
       if (opt_format != 0)
b25f0c2
 	file_entries_new->libs[idx_new].value = str_offset;
b25f0c2
-      len = strlen (entry->path);
b25f0c2
-      str = stpcpy (str, entry->path);
b25f0c2
-      /* Account the final NUL.  */
b25f0c2
-      ++str;
b25f0c2
-      str_offset += len + 1;
b25f0c2
+      len = strlen (entry->path) + 1;
b25f0c2
+      str = mempcpy (str, entry->path, len);
b25f0c2
+      str_offset += len;
b25f0c2
       /* Ignore entries with hwcap for old format.  */
b25f0c2
       if (entry->hwcap == 0)
b25f0c2
 	++idx_old;
b25f0c2
@@ -403,16 +393,12 @@ save_cache (const char *cache_name)
b25f0c2
   /* Write out the cache.  */
b25f0c2
 
b25f0c2
   /* Write cache first to a temporary file and rename it later.  */
b25f0c2
-  temp_name = xmalloc (strlen (cache_name) + 2);
b25f0c2
+  char *temp_name = xmalloc (strlen (cache_name) + 2);
b25f0c2
   sprintf (temp_name, "%s~", cache_name);
b25f0c2
-  /* First remove an old copy if it exists.  */
b25f0c2
-  if (unlink (temp_name) && errno != ENOENT)
b25f0c2
-    error (EXIT_FAILURE, errno, _("Can't remove old temporary cache file %s"),
b25f0c2
-	   temp_name);
b25f0c2
 
b25f0c2
   /* Create file.  */
b25f0c2
-  fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW,
b25f0c2
-	     S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR);
b25f0c2
+  int fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW,
b25f0c2
+		 S_IRUSR|S_IWUSR);
b25f0c2
   if (fd < 0)
b25f0c2
     error (EXIT_FAILURE, errno, _("Can't create temporary cache file %s"),
b25f0c2
 	   temp_name);
b25f0c2
@@ -439,11 +425,10 @@ save_cache (const char *cache_name)
b25f0c2
 	error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
b25f0c2
     }
b25f0c2
 
b25f0c2
-  if (write (fd, strings, total_strlen) != (ssize_t) total_strlen)
b25f0c2
+  if (write (fd, strings, total_strlen) != (ssize_t) total_strlen
b25f0c2
+      || close (fd))
b25f0c2
     error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
b25f0c2
 
b25f0c2
-  close (fd);
b25f0c2
-
b25f0c2
   /* Make sure user can always read cache file */
b25f0c2
   if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR))
b25f0c2
     error (EXIT_FAILURE, errno,
b25f0c2
@@ -463,8 +448,6 @@ save_cache (const char *cache_name)
b25f0c2
   while (entries)
b25f0c2
     {
b25f0c2
       entry = entries;
b25f0c2
-      free (entry->path);
b25f0c2
-      free (entry->lib);
b25f0c2
       entries = entries->next;
b25f0c2
       free (entry);
b25f0c2
     }
b25f0c2
@@ -476,33 +459,29 @@ void
b25f0c2
 add_to_cache (const char *path, const char *lib, int flags,
b25f0c2
 	      unsigned int osversion, uint64_t hwcap)
b25f0c2
 {
b25f0c2
-  struct cache_entry *new_entry, *ptr, *prev;
b25f0c2
-  char *full_path;
b25f0c2
-  size_t len, i;
b25f0c2
-
b25f0c2
-  new_entry = (struct cache_entry *) xmalloc (sizeof (struct cache_entry));
b25f0c2
-
b25f0c2
-  len = strlen (lib) + strlen (path) + 2;
b25f0c2
-
b25f0c2
-  full_path = (char *) xmalloc (len);
b25f0c2
-  snprintf (full_path, len, "%s/%s", path, lib);
b25f0c2
-
b25f0c2
-  new_entry->lib = xstrdup (lib);
b25f0c2
-  new_entry->path = full_path;
b25f0c2
+  size_t liblen = strlen (lib) + 1;
b25f0c2
+  size_t len = liblen + strlen (path) + 1;
b25f0c2
+  struct cache_entry *new_entry
b25f0c2
+    = xmalloc (sizeof (struct cache_entry) + liblen + len);
b25f0c2
+
b25f0c2
+  new_entry->lib = memcpy ((char *) (new_entry + 1), lib, liblen);
b25f0c2
+  new_entry->path = new_entry->lib + liblen;
b25f0c2
+  snprintf (new_entry->path, len, "%s/%s", path, lib);
b25f0c2
   new_entry->flags = flags;
b25f0c2
   new_entry->osversion = osversion;
b25f0c2
   new_entry->hwcap = hwcap;
b25f0c2
   new_entry->bits_hwcap = 0;
b25f0c2
 
b25f0c2
   /* Count the number of bits set in the masked value.  */
b25f0c2
-  for (i = 0; (~((1ULL << i) - 1) & hwcap) != 0 && i < 8 * sizeof (hwcap); ++i)
b25f0c2
+  for (size_t i = 0;
b25f0c2
+       (~((1ULL << i) - 1) & hwcap) != 0 && i < 8 * sizeof (hwcap); ++i)
b25f0c2
     if ((hwcap & (1ULL << i)) != 0)
b25f0c2
       ++new_entry->bits_hwcap;
b25f0c2
 
b25f0c2
 
b25f0c2
   /* Keep the list sorted - search for right place to insert.  */
b25f0c2
-  ptr = entries;
b25f0c2
-  prev = entries;
b25f0c2
+  struct cache_entry *ptr = entries;
b25f0c2
+  struct cache_entry *prev = entries;
b25f0c2
   while (ptr != NULL)
b25f0c2
     {
b25f0c2
       if (compare (ptr, new_entry) > 0)
b25f0c2
@@ -522,3 +501,304 @@ add_to_cache (const char *path, const ch
b25f0c2
       prev->next = new_entry;
b25f0c2
     }
b25f0c2
 }
b25f0c2
+
b25f0c2
+
b25f0c2
+/* Auxiliary cache.  */
b25f0c2
+
b25f0c2
+struct aux_cache_entry_id
b25f0c2
+{
b25f0c2
+  uint64_t ino;
b25f0c2
+  uint64_t ctime;
b25f0c2
+  uint64_t size;
b25f0c2
+  uint64_t dev;
b25f0c2
+};
b25f0c2
+
b25f0c2
+struct aux_cache_entry
b25f0c2
+{
b25f0c2
+  struct aux_cache_entry_id id;
b25f0c2
+  int flags;
b25f0c2
+  unsigned int osversion;
b25f0c2
+  int used;
b25f0c2
+  char *soname;
b25f0c2
+  struct aux_cache_entry *next;
b25f0c2
+};
b25f0c2
+
b25f0c2
+#define AUX_CACHEMAGIC		"glibc-ld.so.auxcache-1.0"
b25f0c2
+
b25f0c2
+struct aux_cache_file_entry
b25f0c2
+{
b25f0c2
+  struct aux_cache_entry_id id;	/* Unique id of entry.  */
b25f0c2
+  int32_t flags;		/* This is 1 for an ELF library.  */
b25f0c2
+  uint32_t soname;		/* String table indice.  */
b25f0c2
+  uint32_t osversion;		/* Required OS version.	 */
b25f0c2
+  int32_t pad;
b25f0c2
+};
b25f0c2
+
b25f0c2
+/* ldconfig maintains an auxiliary cache file that allows
b25f0c2
+   only reading those libraries that have changed since the last iteration.
b25f0c2
+   For this for each library some information is cached in the auxiliary
b25f0c2
+   cache.  */
b25f0c2
+struct aux_cache_file
b25f0c2
+{
b25f0c2
+  char magic[sizeof AUX_CACHEMAGIC - 1];
b25f0c2
+  uint32_t nlibs;		/* Number of entries.  */
b25f0c2
+  uint32_t len_strings;		/* Size of string table. */
b25f0c2
+  struct aux_cache_file_entry libs[0]; /* Entries describing libraries.  */
b25f0c2
+  /* After this the string table of size len_strings is found.	*/
b25f0c2
+};
b25f0c2
+
b25f0c2
+static unsigned int primes[] =
b25f0c2
+{
b25f0c2
+  1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139,
b25f0c2
+  524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393,
b25f0c2
+  67108859, 134217689, 268435399, 536870909, 1073741789, 2147483647
b25f0c2
+};
b25f0c2
+
b25f0c2
+static size_t aux_hash_size;
b25f0c2
+static struct aux_cache_entry **aux_hash;
b25f0c2
+
b25f0c2
+/* Simplistic hash function for aux_cache_entry_id.  */
b25f0c2
+static unsigned int
b25f0c2
+aux_cache_entry_id_hash (struct aux_cache_entry_id *id)
b25f0c2
+{
b25f0c2
+  uint64_t ret = ((id->ino * 11 + id->ctime) * 11 + id->size) * 11 + id->dev;
b25f0c2
+  return ret ^ (ret >> 32);
b25f0c2
+}
b25f0c2
+
b25f0c2
+static size_t nextprime (size_t x)
b25f0c2
+{
b25f0c2
+  for (unsigned int i = 0; i < sizeof (primes) / sizeof (primes[0]); ++i)
b25f0c2
+    if (primes[i] >= x)
b25f0c2
+      return primes[i];
b25f0c2
+  return x;
b25f0c2
+}
b25f0c2
+
b25f0c2
+void
b25f0c2
+init_aux_cache (void)
b25f0c2
+{
b25f0c2
+  aux_hash_size = primes[3];
b25f0c2
+  aux_hash = xcalloc (aux_hash_size, sizeof (struct aux_cache_entry *));
b25f0c2
+}
b25f0c2
+
b25f0c2
+int
b25f0c2
+search_aux_cache (struct stat64 *stat_buf, int *flags,
b25f0c2
+		  unsigned int *osversion, char **soname)
b25f0c2
+{
b25f0c2
+  struct aux_cache_entry_id id;
b25f0c2
+  id.ino = (uint64_t) stat_buf->st_ino;
b25f0c2
+  id.ctime = (uint64_t) stat_buf->st_ctime;
b25f0c2
+  id.size = (uint64_t) stat_buf->st_size;
b25f0c2
+  id.dev = (uint64_t) stat_buf->st_dev;
b25f0c2
+
b25f0c2
+  unsigned int hash = aux_cache_entry_id_hash (&id;;
b25f0c2
+  struct aux_cache_entry *entry;
b25f0c2
+  for (entry = aux_hash[hash % aux_hash_size]; entry; entry = entry->next)
b25f0c2
+    if (id.ino == entry->id.ino
b25f0c2
+	&& id.ctime == entry->id.ctime
b25f0c2
+	&& id.size == entry->id.size
b25f0c2
+	&& id.dev == entry->id.dev)
b25f0c2
+      {
b25f0c2
+	*flags = entry->flags;
b25f0c2
+	*osversion = entry->osversion;
b25f0c2
+	if (entry->soname != NULL)
b25f0c2
+	  *soname = xstrdup (entry->soname);
b25f0c2
+	else
b25f0c2
+	  *soname = NULL;
b25f0c2
+	entry->used = 1;
b25f0c2
+	return 1;
b25f0c2
+      }
b25f0c2
+
b25f0c2
+  return 0;
b25f0c2
+}
b25f0c2
+
b25f0c2
+static void
b25f0c2
+insert_to_aux_cache (struct aux_cache_entry_id *id, int flags,
b25f0c2
+		     unsigned int osversion, const char *soname, int used)
b25f0c2
+{
b25f0c2
+  size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size;
b25f0c2
+  struct aux_cache_entry *entry;
b25f0c2
+  for (entry = aux_hash[hash]; entry; entry = entry->next)
b25f0c2
+    if (id->ino == entry->id.ino
b25f0c2
+	&& id->ctime == entry->id.ctime
b25f0c2
+	&& id->size == entry->id.size
b25f0c2
+	&& id->dev == entry->id.dev)
b25f0c2
+      abort ();
b25f0c2
+
b25f0c2
+  size_t len = soname ? strlen (soname) + 1 : 0;
b25f0c2
+  entry = xmalloc (sizeof (struct aux_cache_entry) + len);
b25f0c2
+  entry->id = *id;
b25f0c2
+  entry->flags = flags;
b25f0c2
+  entry->osversion = osversion;
b25f0c2
+  entry->used = used;
b25f0c2
+  if (soname != NULL)
b25f0c2
+    entry->soname = memcpy ((char *) (entry + 1), soname, len);
b25f0c2
+  else
b25f0c2
+    entry->soname = NULL;
b25f0c2
+  entry->next = aux_hash[hash];
b25f0c2
+  aux_hash[hash] = entry;
b25f0c2
+}
b25f0c2
+
b25f0c2
+void
b25f0c2
+add_to_aux_cache (struct stat64 *stat_buf, int flags,
b25f0c2
+		  unsigned int osversion, const char *soname)
b25f0c2
+{
b25f0c2
+  struct aux_cache_entry_id id;
b25f0c2
+  id.ino = (uint64_t) stat_buf->st_ino;
b25f0c2
+  id.ctime = (uint64_t) stat_buf->st_ctime;
b25f0c2
+  id.size = (uint64_t) stat_buf->st_size;
b25f0c2
+  id.dev = (uint64_t) stat_buf->st_dev;
b25f0c2
+  insert_to_aux_cache (&id, flags, osversion, soname, 1);
b25f0c2
+}
b25f0c2
+
b25f0c2
+/* Load auxiliary cache to search for unchanged entries.   */
b25f0c2
+void
b25f0c2
+load_aux_cache (const char *aux_cache_name)
b25f0c2
+{
b25f0c2
+  int fd = open (aux_cache_name, O_RDONLY);
b25f0c2
+  if (fd < 0)
b25f0c2
+    {
b25f0c2
+      init_aux_cache ();
b25f0c2
+      return;
b25f0c2
+    }
b25f0c2
+
b25f0c2
+  struct stat64 st;
b25f0c2
+  if (fstat64 (fd, &st) < 0 || st.st_size < sizeof (struct aux_cache_file))
b25f0c2
+    {
b25f0c2
+      close (fd);
b25f0c2
+      init_aux_cache ();
b25f0c2
+      return;
b25f0c2
+    }
b25f0c2
+
b25f0c2
+  size_t aux_cache_size = st.st_size;
b25f0c2
+  struct aux_cache_file *aux_cache
b25f0c2
+    = mmap (NULL, aux_cache_size, PROT_READ, MAP_PRIVATE, fd, 0);
b25f0c2
+  if (aux_cache == MAP_FAILED
b25f0c2
+      || aux_cache_size < sizeof (struct aux_cache_file)
b25f0c2
+      || memcmp (aux_cache->magic, AUX_CACHEMAGIC, sizeof AUX_CACHEMAGIC - 1)
b25f0c2
+      || aux_cache->nlibs < 0
b25f0c2
+      || aux_cache->nlibs >= aux_cache_size)
b25f0c2
+    {
b25f0c2
+      close (fd);
b25f0c2
+      init_aux_cache ();
b25f0c2
+      return;
b25f0c2
+    }
b25f0c2
+
b25f0c2
+  aux_hash_size = nextprime (aux_cache->nlibs);
b25f0c2
+  aux_hash = xcalloc (aux_hash_size, sizeof (struct aux_cache_entry *));
b25f0c2
+
b25f0c2
+  const char *aux_cache_data
b25f0c2
+    = (const char *) &aux_cache->libs[aux_cache->nlibs];
b25f0c2
+  for (unsigned int i = 0; i < aux_cache->nlibs; ++i)
b25f0c2
+    insert_to_aux_cache (&aux_cache->libs[i].id,
b25f0c2
+			 aux_cache->libs[i].flags,
b25f0c2
+			 aux_cache->libs[i].osversion,
b25f0c2
+			 aux_cache->libs[i].soname == 0
b25f0c2
+			 ? NULL : aux_cache_data + aux_cache->libs[i].soname,
b25f0c2
+			 0);
b25f0c2
+
b25f0c2
+  munmap (aux_cache, aux_cache_size);
b25f0c2
+  close (fd);
b25f0c2
+}
b25f0c2
+
b25f0c2
+/* Save the contents of the auxiliary cache.  */
b25f0c2
+void
b25f0c2
+save_aux_cache (const char *aux_cache_name)
b25f0c2
+{
b25f0c2
+  /* Count the length of all sonames.  We start with empty string.  */
b25f0c2
+  size_t total_strlen = 1;
b25f0c2
+  /* Number of cache entries.  */
b25f0c2
+  int cache_entry_count = 0;
b25f0c2
+
b25f0c2
+  for (size_t i = 0; i < aux_hash_size; ++i)
b25f0c2
+    for (struct aux_cache_entry *entry = aux_hash[i];
b25f0c2
+	 entry != NULL; entry = entry->next)
b25f0c2
+      if (entry->used)
b25f0c2
+	{
b25f0c2
+	  ++cache_entry_count;
b25f0c2
+	  if (entry->soname != NULL)
b25f0c2
+	    total_strlen += strlen (entry->soname) + 1;
b25f0c2
+	}
b25f0c2
+
b25f0c2
+  /* Auxiliary cache.  */
b25f0c2
+  size_t file_entries_size
b25f0c2
+    = sizeof (struct aux_cache_file)
b25f0c2
+      + cache_entry_count * sizeof (struct aux_cache_file_entry);
b25f0c2
+  struct aux_cache_file *file_entries
b25f0c2
+    = xmalloc (file_entries_size + total_strlen);
b25f0c2
+
b25f0c2
+  /* Fill in the header of the auxiliary cache.  */
b25f0c2
+  memset (file_entries, '\0', sizeof (struct aux_cache_file));
b25f0c2
+  memcpy (file_entries->magic, AUX_CACHEMAGIC, sizeof AUX_CACHEMAGIC - 1);
b25f0c2
+
b25f0c2
+  file_entries->nlibs = cache_entry_count;
b25f0c2
+  file_entries->len_strings = total_strlen;
b25f0c2
+
b25f0c2
+  /* Initial String offset for auxiliary cache is always after the
b25f0c2
+     special empty string.  */
b25f0c2
+  unsigned int str_offset = 1;
b25f0c2
+
b25f0c2
+  /* An array for all strings.  */
b25f0c2
+  char *str = (char *) file_entries + file_entries_size;
b25f0c2
+  *str++ = '\0';
b25f0c2
+
b25f0c2
+  size_t idx = 0;
b25f0c2
+  for (size_t i = 0; i < aux_hash_size; ++i)
b25f0c2
+    for (struct aux_cache_entry *entry = aux_hash[i];
b25f0c2
+	 entry != NULL; entry = entry->next)
b25f0c2
+      if (entry->used)
b25f0c2
+	{
b25f0c2
+	  file_entries->libs[idx].id = entry->id;
b25f0c2
+	  file_entries->libs[idx].flags = entry->flags;
b25f0c2
+	  if (entry->soname == NULL)
b25f0c2
+	    file_entries->libs[idx].soname = 0;
b25f0c2
+	  else
b25f0c2
+	    {
b25f0c2
+	      file_entries->libs[idx].soname = str_offset;
b25f0c2
+
b25f0c2
+	      size_t len = strlen (entry->soname) + 1;
b25f0c2
+	      str = mempcpy (str, entry->soname, len);
b25f0c2
+	      str_offset += len;
b25f0c2
+	    }
b25f0c2
+	  file_entries->libs[idx].osversion = entry->osversion;
b25f0c2
+	  file_entries->libs[idx++].pad = 0;
b25f0c2
+	}
b25f0c2
+
b25f0c2
+  /* Write out auxiliary cache file.  */
b25f0c2
+  /* Write auxiliary cache first to a temporary file and rename it later.  */
b25f0c2
+
b25f0c2
+  char *temp_name = xmalloc (strlen (aux_cache_name) + 2);
b25f0c2
+  sprintf (temp_name, "%s~", aux_cache_name);
b25f0c2
+
b25f0c2
+  /* Check that directory exists and create if needed.  */
b25f0c2
+  char *dir = strdupa (aux_cache_name);
b25f0c2
+  dir = dirname (dir);
b25f0c2
+
b25f0c2
+  struct stat64 st;
b25f0c2
+  if (stat64 (dir, &st) < 0)
b25f0c2
+    {
b25f0c2
+      if (mkdir (dir, 0700) < 0)
b25f0c2
+	goto out_fail;
b25f0c2
+    }
b25f0c2
+
b25f0c2
+  /* Create file.  */
b25f0c2
+  int fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW,
b25f0c2
+		 S_IRUSR|S_IWUSR);
b25f0c2
+  if (fd < 0)
b25f0c2
+    goto out_fail;
b25f0c2
+
b25f0c2
+  if (write (fd, file_entries, file_entries_size + total_strlen)
b25f0c2
+      != (ssize_t) (file_entries_size + total_strlen)
b25f0c2
+      || close (fd))
b25f0c2
+    {
b25f0c2
+      unlink (temp_name);
b25f0c2
+      goto out_fail;
b25f0c2
+    }
b25f0c2
+
b25f0c2
+  /* Move temporary to its final location.  */
b25f0c2
+  if (rename (temp_name, aux_cache_name))
b25f0c2
+    unlink (temp_name);
b25f0c2
+
b25f0c2
+out_fail:
b25f0c2
+  /* Free allocated memory.  */
b25f0c2
+  free (file_entries);
b25f0c2
+}
b25f0c2
--- libc/sysdeps/generic/ldconfig.h.jj	2003-03-14 06:32:49.000000000 +0100
b25f0c2
+++ libc/sysdeps/generic/ldconfig.h	2007-08-01 18:54:26.000000000 +0200
b25f0c2
@@ -1,4 +1,4 @@
b25f0c2
-/* Copyright (C) 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
b25f0c2
+/* Copyright (C) 1999, 2000, 2002, 2003, 2007 Free Software Foundation, Inc.
b25f0c2
    This file is part of the GNU C Library.
b25f0c2
    Contributed by Andreas Jaeger <aj@suse.de>, 1999.
b25f0c2
 
b25f0c2
@@ -35,6 +35,9 @@
b25f0c2
 #define FLAG_MIPS64_LIBN32	0x0600
b25f0c2
 #define FLAG_MIPS64_LIBN64	0x0700
b25f0c2
 
b25f0c2
+/* Name of auxiliary cache.  */
b25f0c2
+#define _PATH_LDCONFIG_AUX_CACHE "/var/cache/ldconfig/aux-cache"
b25f0c2
+
b25f0c2
 /* Declared in cache.c.  */
b25f0c2
 extern void print_cache (const char *cache_name);
b25f0c2
 
b25f0c2
@@ -45,10 +48,24 @@ extern void save_cache (const char *cach
b25f0c2
 extern void add_to_cache (const char *path, const char *lib, int flags,
b25f0c2
 			  unsigned int osversion, uint64_t hwcap);
b25f0c2
 
b25f0c2
+extern void init_aux_cache (void);
b25f0c2
+
b25f0c2
+extern void load_aux_cache (const char *aux_cache_name);
b25f0c2
+
b25f0c2
+extern int search_aux_cache (struct stat64 *stat_buf, int *flags,
b25f0c2
+			     unsigned int *osversion, char **soname);
b25f0c2
+
b25f0c2
+extern void add_to_aux_cache (struct stat64 *stat_buf, int flags,
b25f0c2
+			      unsigned int osversion, const char *soname);
b25f0c2
+
b25f0c2
+extern void save_aux_cache (const char *aux_cache_name);
b25f0c2
+
b25f0c2
 /* Declared in readlib.c.  */
b25f0c2
 extern int process_file (const char *real_file_name, const char *file_name,
b25f0c2
 			 const char *lib, int *flag, unsigned int *osversion,
b25f0c2
-			 char **soname, int is_link);
b25f0c2
+			 char **soname, int is_link, struct stat64 *stat_buf);
b25f0c2
+
b25f0c2
+extern char *implicit_soname (const char *lib, int flag);
b25f0c2
 
b25f0c2
 /* Declared in readelflib.c.  */
b25f0c2
 extern int process_elf_file (const char *file_name, const char *lib, int *flag,