446cf2
commit db07fae8250401adb2b97ab3e53d41da2a6bd767
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Thu Nov 26 16:59:44 2020 +0100
446cf2
446cf2
    elf: Introduce enum opt_format in the ldconfig implementation
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/elf/cache.c b/elf/cache.c
446cf2
index c4cd825c30e00e8e..edcdd4b7cc1a6a0b 100644
446cf2
--- a/elf/cache.c
446cf2
+++ b/elf/cache.c
446cf2
@@ -321,13 +321,13 @@ save_cache (const char *cache_name)
446cf2
   struct cache_file *file_entries = NULL;
446cf2
   size_t file_entries_size = 0;
446cf2
 
446cf2
-  if (opt_format != 2)
446cf2
+  if (opt_format != opt_format_new)
446cf2
     {
446cf2
       /* struct cache_file_new is 64-bit aligned on some arches while
446cf2
 	 only 32-bit aligned on other arches.  Duplicate last old
446cf2
 	 cache entry so that new cache in ld.so.cache can be used by
446cf2
 	 both.  */
446cf2
-      if (opt_format != 0)
446cf2
+      if (opt_format != opt_format_old)
446cf2
 	cache_entry_old_count = (cache_entry_old_count + 1) & ~1;
446cf2
 
446cf2
       /* And the list of all entries in the old format.  */
446cf2
@@ -345,7 +345,7 @@ save_cache (const char *cache_name)
446cf2
   struct cache_file_new *file_entries_new = NULL;
446cf2
   size_t file_entries_new_size = 0;
446cf2
 
446cf2
-  if (opt_format != 0)
446cf2
+  if (opt_format != opt_format_old)
446cf2
     {
446cf2
       /* And the list of all entries in the new format.  */
446cf2
       file_entries_new_size = sizeof (struct cache_file_new)
446cf2
@@ -370,7 +370,7 @@ save_cache (const char *cache_name)
446cf2
      table, we have to adjust all string indices for this so that
446cf2
      old libc5/glibc 2 dynamic linkers just ignore them.  */
446cf2
   unsigned int str_offset;
446cf2
-  if (opt_format != 0)
446cf2
+  if (opt_format != opt_format_old)
446cf2
     str_offset = file_entries_new_size;
446cf2
   else
446cf2
     str_offset = 0;
446cf2
@@ -385,13 +385,13 @@ save_cache (const char *cache_name)
446cf2
        entry = entry->next, ++idx_new)
446cf2
     {
446cf2
       /* First the library.  */
446cf2
-      if (opt_format != 2 && entry->hwcap == 0)
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
 	}
446cf2
-      if (opt_format != 0)
446cf2
+      if (opt_format != opt_format_old)
446cf2
 	{
446cf2
 	  /* We could subtract file_entries_new_size from str_offset -
446cf2
 	     not doing so makes the code easier, the string table
446cf2
@@ -407,9 +407,9 @@ save_cache (const char *cache_name)
446cf2
       str = mempcpy (str, entry->lib, len);
446cf2
       str_offset += len;
446cf2
       /* Then the path.  */
446cf2
-      if (opt_format != 2 && entry->hwcap == 0)
446cf2
+      if (opt_format != opt_format_new && entry->hwcap == 0)
446cf2
 	file_entries->libs[idx_old].value = str_offset + pad;
446cf2
-      if (opt_format != 0)
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
@@ -420,7 +420,7 @@ save_cache (const char *cache_name)
446cf2
     }
446cf2
 
446cf2
   /* Duplicate last old cache entry if needed.  */
446cf2
-  if (opt_format != 2
446cf2
+  if (opt_format != opt_format_new
446cf2
       && idx_old < cache_entry_old_count)
446cf2
     file_entries->libs[idx_old] = file_entries->libs[idx_old - 1];
446cf2
 
446cf2
@@ -438,16 +438,16 @@ save_cache (const char *cache_name)
446cf2
 	   temp_name);
446cf2
 
446cf2
   /* Write contents.  */
446cf2
-  if (opt_format != 2)
446cf2
+  if (opt_format != opt_format_new)
446cf2
     {
446cf2
       if (write (fd, file_entries, file_entries_size)
446cf2
 	  != (ssize_t) file_entries_size)
446cf2
 	error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
446cf2
     }
446cf2
-  if (opt_format != 0)
446cf2
+  if (opt_format != opt_format_old)
446cf2
     {
446cf2
       /* Align cache.  */
446cf2
-      if (opt_format != 2)
446cf2
+      if (opt_format != opt_format_new)
446cf2
 	{
446cf2
 	  char zero[pad];
446cf2
 	  memset (zero, '\0', pad);
446cf2
diff --git a/elf/ldconfig.c b/elf/ldconfig.c
446cf2
index be730ceb075f6c1f..0fa5aef83f9cd86c 100644
446cf2
--- a/elf/ldconfig.c
446cf2
+++ b/elf/ldconfig.c
446cf2
@@ -100,8 +100,7 @@ static int opt_print_cache;
446cf2
 int opt_verbose;
446cf2
 
446cf2
 /* Format to support.  */
446cf2
-/* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2.  */
446cf2
-int opt_format = 2;
446cf2
+enum opt_format opt_format = opt_format_new;
446cf2
 
446cf2
 /* Build cache.  */
446cf2
 static int opt_build_cache = 1;
446cf2
@@ -281,11 +280,11 @@ parse_opt (int key, char *arg, struct argp_state *state)
446cf2
       break;
446cf2
     case 'c':
446cf2
       if (strcmp (arg, "old") == 0)
446cf2
-	opt_format = 0;
446cf2
+	opt_format = opt_format_old;
446cf2
       else if (strcmp (arg, "compat") == 0)
446cf2
-	opt_format = 1;
446cf2
+	opt_format = opt_format_compat;
446cf2
       else if (strcmp (arg, "new") == 0)
446cf2
-	opt_format = 2;
446cf2
+	opt_format = opt_format_new;
446cf2
       break;
446cf2
     default:
446cf2
       return ARGP_ERR_UNKNOWN;
446cf2
diff --git a/sysdeps/generic/ldconfig.h b/sysdeps/generic/ldconfig.h
446cf2
index 6774212110d23eae..b15b142511829436 100644
446cf2
--- a/sysdeps/generic/ldconfig.h
446cf2
+++ b/sysdeps/generic/ldconfig.h
446cf2
@@ -90,7 +90,14 @@ extern char *chroot_canon (const char *chroot, const char *name);
446cf2
 /* Declared in ldconfig.c.  */
446cf2
 extern int opt_verbose;
446cf2
 
446cf2
-extern int opt_format;
446cf2
+enum opt_format
446cf2
+  {
446cf2
+    opt_format_old = 0,	/* Use struct cache_file.  */
446cf2
+    opt_format_compat = 1, /* Use both, old format followed by new.  */
446cf2
+    opt_format_new = 2,	/* Use struct cache_file_new.  */
446cf2
+  };
446cf2
+
446cf2
+extern enum opt_format opt_format;
446cf2
 
446cf2
 /* Prototypes for a few program-wide used functions.  */
446cf2
 #include <programs/xmalloc.h>