446cf2
commit 73b6e50a22dea9ae6144beaaa675d2ac62c281ca
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Fri Dec 4 09:13:43 2020 +0100
446cf2
446cf2
    elf: Implement tail merging of strings in ldconfig
446cf2
    
446cf2
    This simplifies the string table construction in elf/cache.c
446cf2
    because there is no more need to keep track of offsets explicitly;
446cf2
    the string table implementation does this internally.
446cf2
    
446cf2
    This change slightly reduces the size of the cache on disk.  The
446cf2
    file format does not change as a result.  The strings are
446cf2
    null-terminated, without explicit length, so tail merging is
446cf2
    transparent to readers.
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/elf/Makefile b/elf/Makefile
446cf2
index abb3e9d1179ef5cd..a3e802a9a99b759c 100644
446cf2
--- a/elf/Makefile
446cf2
+++ b/elf/Makefile
446cf2
@@ -112,7 +112,8 @@ others-static	+= ldconfig
446cf2
 others		+= ldconfig
446cf2
 install-rootsbin += ldconfig
446cf2
 
446cf2
-ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon static-stubs
446cf2
+ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon static-stubs \
446cf2
+  stringtable
446cf2
 extra-objs	+= $(ldconfig-modules:=.o)
446cf2
 others-extras   = $(ldconfig-modules)
446cf2
 endif
446cf2
diff --git a/elf/cache.c b/elf/cache.c
446cf2
index 5a8f1ad70cc3fead..f773cacacf26db1c 100644
446cf2
--- a/elf/cache.c
446cf2
+++ b/elf/cache.c
446cf2
@@ -35,11 +35,15 @@
446cf2
 #include <ldconfig.h>
446cf2
 #include <dl-cache.h>
446cf2
 #include <version.h>
446cf2
+#include <stringtable.h>
446cf2
+
446cf2
+/* Used to store library names, paths, and other strings.  */
446cf2
+static struct stringtable strings;
446cf2
 
446cf2
 struct cache_entry
446cf2
 {
446cf2
-  char *lib;			/* Library name.  */
446cf2
-  char *path;			/* Path to find library.  */
446cf2
+  struct stringtable_entry *lib; /* Library name.  */
446cf2
+  struct stringtable_entry *path; /* Path to find library.  */
446cf2
   int flags;			/* Flags to indicate kind of library.  */
446cf2
   unsigned int osversion;	/* Required OS version.  */
446cf2
   uint64_t hwcap;		/* Important hardware capabilities.  */
446cf2
@@ -300,7 +304,7 @@ static int
446cf2
 compare (const struct cache_entry *e1, const struct cache_entry *e2)
446cf2
 {
446cf2
   /* We need to swap entries here to get the correct sort order.  */
446cf2
-  int res = _dl_cache_libcmp (e2->lib, e1->lib);
446cf2
+  int res = _dl_cache_libcmp (e2->lib->string, e1->lib->string);
446cf2
   if (res == 0)
446cf2
     {
446cf2
       if (e1->flags < e2->flags)
446cf2
@@ -369,26 +373,24 @@ save_cache (const char *cache_name)
446cf2
 {
446cf2
   /* The cache entries are sorted already, save them in this order. */
446cf2
 
446cf2
-  /* Count the length of all strings.  */
446cf2
-  /* The old format doesn't contain hwcap entries and doesn't contain
446cf2
-     libraries in subdirectories with hwcaps entries.  Count therefore
446cf2
-     also all entries with hwcap == 0.  */
446cf2
-  size_t total_strlen = 0;
446cf2
   struct cache_entry *entry;
446cf2
   /* Number of cache entries.  */
446cf2
   int cache_entry_count = 0;
446cf2
-  /* Number of normal cache entries.  */
446cf2
+  /* The old format doesn't contain hwcap entries and doesn't contain
446cf2
+     libraries in subdirectories with hwcaps entries.  Count therefore
446cf2
+     also all entries with hwcap == 0.  */
446cf2
   int cache_entry_old_count = 0;
446cf2
 
446cf2
   for (entry = entries; entry != NULL; entry = entry->next)
446cf2
     {
446cf2
-      /* Account the final NULs.  */
446cf2
-      total_strlen += strlen (entry->lib) + strlen (entry->path) + 2;
446cf2
       ++cache_entry_count;
446cf2
       if (entry->hwcap == 0)
446cf2
 	++cache_entry_old_count;
446cf2
     }
446cf2
 
446cf2
+  struct stringtable_finalized strings_finalized;
446cf2
+  stringtable_finalize (&strings, &strings_finalized);
446cf2
+
446cf2
   /* Create the on disk cache structure.  */
446cf2
   struct cache_file *file_entries = NULL;
446cf2
   size_t file_entries_size = 0;
446cf2
@@ -432,7 +434,7 @@ save_cache (const char *cache_name)
446cf2
 	      sizeof CACHE_VERSION - 1);
446cf2
 
446cf2
       file_entries_new->nlibs = cache_entry_count;
446cf2
-      file_entries_new->len_strings = total_strlen;
446cf2
+      file_entries_new->len_strings = strings_finalized.size;
446cf2
       file_entries_new->flags = cache_file_new_flags_endian_current;
446cf2
     }
446cf2
 
446cf2
@@ -449,20 +451,20 @@ save_cache (const char *cache_name)
446cf2
     str_offset = 0;
446cf2
 
446cf2
   /* An array for all strings.  */
446cf2
-  char *strings = xmalloc (total_strlen);
446cf2
-  char *str = strings;
446cf2
   int idx_old;
446cf2
   int idx_new;
446cf2
 
446cf2
   for (idx_old = 0, idx_new = 0, entry = entries; entry != NULL;
446cf2
        entry = entry->next, ++idx_new)
446cf2
     {
446cf2
-      /* First the library.  */
446cf2
       if (opt_format != opt_format_new && entry->hwcap == 0)
446cf2
 	{
446cf2
 	  file_entries->libs[idx_old].flags = entry->flags;
446cf2
 	  /* XXX: Actually we can optimize here and remove duplicates.  */
446cf2
 	  file_entries->libs[idx_old].key = str_offset + pad;
446cf2
+	  file_entries->libs[idx_new].key = str_offset + entry->lib->offset;
446cf2
+	  file_entries->libs[idx_new].value
446cf2
+	    = str_offset + entry->path->offset;
446cf2
 	}
446cf2
       if (opt_format != opt_format_old)
446cf2
 	{
446cf2
@@ -473,20 +475,12 @@ save_cache (const char *cache_name)
446cf2
 	  file_entries_new->libs[idx_new].flags = entry->flags;
446cf2
 	  file_entries_new->libs[idx_new].osversion = entry->osversion;
446cf2
 	  file_entries_new->libs[idx_new].hwcap = entry->hwcap;
446cf2
-	  file_entries_new->libs[idx_new].key = str_offset;
446cf2
+	  file_entries_new->libs[idx_new].key
446cf2
+	    = str_offset + entry->lib->offset;
446cf2
+	  file_entries_new->libs[idx_new].value
446cf2
+	    = str_offset + entry->path->offset;
446cf2
 	}
446cf2
 
446cf2
-      size_t len = strlen (entry->lib) + 1;
446cf2
-      str = mempcpy (str, entry->lib, len);
446cf2
-      str_offset += len;
446cf2
-      /* Then the path.  */
446cf2
-      if (opt_format != opt_format_new && entry->hwcap == 0)
446cf2
-	file_entries->libs[idx_old].value = str_offset + pad;
446cf2
-      if (opt_format != opt_format_old)
446cf2
-	file_entries_new->libs[idx_new].value = str_offset;
446cf2
-      len = strlen (entry->path) + 1;
446cf2
-      str = mempcpy (str, entry->path, len);
446cf2
-      str_offset += len;
446cf2
       /* Ignore entries with hwcap for old format.  */
446cf2
       if (entry->hwcap == 0)
446cf2
 	++idx_old;
446cf2
@@ -511,7 +505,7 @@ save_cache (const char *cache_name)
446cf2
 	extension_offset += pad;
446cf2
       extension_offset += file_entries_new_size;
446cf2
     }
446cf2
-  extension_offset += total_strlen;
446cf2
+  extension_offset += strings_finalized.size;
446cf2
   extension_offset = roundup (extension_offset, 4); /* Provide alignment.  */
446cf2
   if (opt_format != opt_format_old)
446cf2
     file_entries_new->extension_offset = extension_offset;
446cf2
@@ -551,7 +545,8 @@ save_cache (const char *cache_name)
446cf2
 	error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
446cf2
     }
446cf2
 
446cf2
-  if (write (fd, strings, total_strlen) != (ssize_t) total_strlen)
446cf2
+  if (write (fd, strings_finalized.strings, strings_finalized.size)
446cf2
+      != (ssize_t) strings_finalized.size)
446cf2
     error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
446cf2
 
446cf2
   if (opt_format != opt_format_old)
446cf2
@@ -580,7 +575,7 @@ save_cache (const char *cache_name)
446cf2
   /* Free all allocated memory.  */
446cf2
   free (file_entries_new);
446cf2
   free (file_entries);
446cf2
-  free (strings);
446cf2
+  free (strings_finalized.strings);
446cf2
 
446cf2
   while (entries)
446cf2
     {
446cf2
@@ -596,14 +591,19 @@ void
446cf2
 add_to_cache (const char *path, const char *lib, int flags,
446cf2
 	      unsigned int osversion, uint64_t hwcap)
446cf2
 {
446cf2
-  size_t liblen = strlen (lib) + 1;
446cf2
-  size_t len = liblen + strlen (path) + 1;
446cf2
-  struct cache_entry *new_entry
446cf2
-    = xmalloc (sizeof (struct cache_entry) + liblen + len);
446cf2
-
446cf2
-  new_entry->lib = memcpy ((char *) (new_entry + 1), lib, liblen);
446cf2
-  new_entry->path = new_entry->lib + liblen;
446cf2
-  snprintf (new_entry->path, len, "%s/%s", path, lib);
446cf2
+  struct cache_entry *new_entry = xmalloc (sizeof (*new_entry));
446cf2
+
446cf2
+  struct stringtable_entry *path_interned;
446cf2
+  {
446cf2
+    char *p;
446cf2
+    if (asprintf (&p, "%s/%s", path, lib) < 0)
446cf2
+      error (EXIT_FAILURE, errno, _("Could not create library path"));
446cf2
+    path_interned = stringtable_add (&strings, p);
446cf2
+    free (p);
446cf2
+  }
446cf2
+
446cf2
+  new_entry->lib = stringtable_add (&strings, lib);
446cf2
+  new_entry->path = path_interned;
446cf2
   new_entry->flags = flags;
446cf2
   new_entry->osversion = osversion;
446cf2
   new_entry->hwcap = hwcap;