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