Maintain an explicit order of tunables, so that the tunable_list
array and the tunable_id_t constant can remain consistent over time.

Related to this upstream bug:

  Internal tunables ABI depends on awk array iteration order
  <https://sourceware.org/bugzilla/show_bug.cgi?id=30027>

The new dl-tunables.list files are already on the sysdeps search
path, which is why the existing Makeconfig rule picks them up.
The files for RHEL 8.7.z were created by applying the gen-tunables.awk
part of this patch to RHEL 8.7.0 (glibc-2.28-211.el8_7, to be precise).
The sysdeps/unix/sysv/linux/**/dl-tunables.list files were created
based on the generated error message during the RHEL 8.7.z build.
Afterwards, the glibc.rtld.dynamic_sort tunable was added at the
end of the files, for the RHEL 8.8.0 build.

Going forward, new tunables will have to be added manually to the end
of those files.  Existing tunables should not be deleted.  For
deletion, the script would have to be extended to be able to create
gaps in the tunable_list array.

diff --git a/scripts/gen-tunables.awk b/scripts/gen-tunables.awk
index 622199061a140ccd..8ebfb560976ead41 100644
--- a/scripts/gen-tunables.awk
+++ b/scripts/gen-tunables.awk
@@ -12,6 +12,7 @@ BEGIN {
   tunable=""
   ns=""
   top_ns=""
+  tunable_order_count = 0
 }
 
 # Skip over blank lines and comments.
@@ -78,6 +79,37 @@ $1 == "}" {
   next
 }
 
+$1 == "@order" {
+    if (top_ns != "") {
+	printf("%s:%d: error: invalid @order directive inside namespace %s\n",
+               FILENAME, FNR, top_ns) > "/dev/stderr"
+	exit 1
+    }
+    if (NF != 2) {
+	printf("%s:%d: error: invalid argument count in @order directive\n",
+               FILENAME, FNR) > "/dev/stderr"
+        exit 1
+    }
+    order_arg = $2
+    if (split(order_arg, indices, /\./) != 3) {
+	printf("%s:%d: error: invalid tunable syntax in @order directive\n",
+               FILENAME, FNR) > "/dev/stderr"
+        exit 1
+    }
+    t = indices[1]
+    n = indices[2]
+    m = indices[3]
+    if ((t, n, m) in tunable_order) {
+	printf("%s:%d: error: duplicate\"@order %s\"\n" \
+               FILENAME, FNR, order_arg) > "/dev/stderr"
+        exit 1
+    }
+    ++tunable_order_count
+    tunable_order[t,n,m] = tunable_order_count
+    tunable_order_list[tunable_order_count] = t SUBSEP n SUBSEP m
+    next
+}
+
 # Everything else, which could either be a tunable without any attributes or a
 # tunable attribute.
 {
@@ -137,6 +169,31 @@ END {
     exit 1
   }
 
+  missing_order = 0
+  for (tnm in types) {
+      if (!(tnm in tunable_order)) {
+	  if (!missing_order) {
+	      print "error: Missing @order directives:" > "/dev/stderr"
+	      missing_order = 1
+	  }
+	  split(tnm, indices, SUBSEP)
+	  printf("@order %s.%s.%s\n", indices[1], indices[2], indices[3]) \
+	      > "/dev/stderr"
+      }
+  }
+  for (i = 1; i <= tunable_order_count; ++i) {
+    tnm = tunable_order_list[i]
+    if (!(tnm in types)) {
+	split(tnm, indices, SUBSEP)
+	printf("error: tunable in \"@order %s.%s.%s\" not known\n", \
+	       indices[1], indices[2], indices[3]) > "/dev/stderr"
+	missing_order = 1
+    }
+  }
+  if (missing_order) {
+      exit 1
+  }
+
   print "/* AUTOGENERATED by gen-tunables.awk.  */"
   print "#ifndef _TUNABLES_H_"
   print "# error \"Do not include this file directly.\""
@@ -147,7 +204,8 @@ END {
   # Now, the enum names
   print "\ntypedef enum"
   print "{"
-  for (tnm in types) {
+  for (i = 1; i <= tunable_order_count; ++i) {
+    tnm = tunable_order_list[i]
     split (tnm, indices, SUBSEP);
     t = indices[1];
     n = indices[2];
@@ -159,7 +217,8 @@ END {
   # Finally, the tunable list.
   print "\n#ifdef TUNABLES_INTERNAL"
   print "static tunable_t tunable_list[] attribute_relro = {"
-  for (tnm in types) {
+  for (i = 1; i <= tunable_order_count; ++i) {
+    tnm = tunable_order_list[i]
     split (tnm, indices, SUBSEP);
     t = indices[1];
     n = indices[2];
diff --git a/sysdeps/unix/sysv/linux/aarch64/dl-tunables.list b/sysdeps/unix/sysv/linux/aarch64/dl-tunables.list
new file mode 100644
index 0000000000000000..5c3c5292025607a1
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/aarch64/dl-tunables.list
@@ -0,0 +1,26 @@
+# Order of tunables in RHEL 8.7.0.
+@order glibc.rtld.nns
+@order glibc.elision.skip_lock_after_retries
+@order glibc.malloc.trim_threshold
+@order glibc.malloc.perturb
+@order glibc.cpu.name
+@order glibc.elision.tries
+@order glibc.elision.enable
+@order glibc.malloc.mxfast
+@order glibc.elision.skip_lock_busy
+@order glibc.malloc.top_pad
+@order glibc.cpu.hwcap_mask
+@order glibc.malloc.mmap_max
+@order glibc.elision.skip_trylock_internal_abort
+@order glibc.malloc.tcache_unsorted_limit
+@order glibc.elision.skip_lock_internal_abort
+@order glibc.malloc.arena_max
+@order glibc.malloc.mmap_threshold
+@order glibc.malloc.tcache_count
+@order glibc.malloc.arena_test
+@order glibc.rtld.optional_static_tls
+@order glibc.malloc.tcache_max
+@order glibc.malloc.check
+
+# Tunables added in RHEL 8.8.0
+@order glibc.rtld.dynamic_sort
diff --git a/sysdeps/unix/sysv/linux/i386/dl-tunables.list b/sysdeps/unix/sysv/linux/i386/dl-tunables.list
new file mode 100644
index 0000000000000000..b9cad4af62d9f2e5
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/i386/dl-tunables.list
@@ -0,0 +1,33 @@
+# Order of tunables in RHEL 8.7.0.
+@order glibc.rtld.nns
+@order glibc.elision.skip_lock_after_retries
+@order glibc.malloc.trim_threshold
+@order glibc.malloc.perturb
+@order glibc.cpu.x86_shared_cache_size
+@order glibc.elision.tries
+@order glibc.elision.enable
+@order glibc.cpu.x86_rep_movsb_threshold
+@order glibc.malloc.mxfast
+@order glibc.elision.skip_lock_busy
+@order glibc.malloc.top_pad
+@order glibc.cpu.x86_rep_stosb_threshold
+@order glibc.cpu.x86_non_temporal_threshold
+@order glibc.cpu.x86_shstk
+@order glibc.cpu.hwcap_mask
+@order glibc.malloc.mmap_max
+@order glibc.elision.skip_trylock_internal_abort
+@order glibc.malloc.tcache_unsorted_limit
+@order glibc.cpu.x86_ibt
+@order glibc.cpu.hwcaps
+@order glibc.elision.skip_lock_internal_abort
+@order glibc.malloc.arena_max
+@order glibc.malloc.mmap_threshold
+@order glibc.cpu.x86_data_cache_size
+@order glibc.malloc.tcache_count
+@order glibc.malloc.arena_test
+@order glibc.rtld.optional_static_tls
+@order glibc.malloc.tcache_max
+@order glibc.malloc.check
+
+# Tunables added in RHEL 8.8.0
+@order glibc.rtld.dynamic_sort
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/dl-tunables.list b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/dl-tunables.list
new file mode 100644
index 0000000000000000..ee1e6fca95e1f2da
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/le/dl-tunables.list
@@ -0,0 +1,26 @@
+# Order of tunables in RHEL 8.7.0.
+@order glibc.rtld.nns
+@order glibc.elision.skip_lock_after_retries
+@order glibc.malloc.trim_threshold
+@order glibc.malloc.perturb
+@order glibc.elision.tries
+@order glibc.elision.enable
+@order glibc.malloc.mxfast
+@order glibc.elision.skip_lock_busy
+@order glibc.malloc.top_pad
+@order glibc.cpu.hwcap_mask
+@order glibc.malloc.mmap_max
+@order glibc.elision.skip_trylock_internal_abort
+@order glibc.malloc.tcache_unsorted_limit
+@order glibc.elision.skip_lock_internal_abort
+@order glibc.malloc.arena_max
+@order glibc.malloc.mmap_threshold
+@order glibc.cpu.cached_memopt
+@order glibc.malloc.tcache_count
+@order glibc.malloc.arena_test
+@order glibc.rtld.optional_static_tls
+@order glibc.malloc.tcache_max
+@order glibc.malloc.check
+
+# Tunables added in RHEL 8.8.0
+@order glibc.rtld.dynamic_sort
diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/dl-tunables.list b/sysdeps/unix/sysv/linux/s390/s390-64/dl-tunables.list
new file mode 100644
index 0000000000000000..099e28d8f8e67944
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/dl-tunables.list
@@ -0,0 +1,25 @@
+# Order of tunables in RHEL 8.7.0.
+@order glibc.rtld.nns
+@order glibc.elision.skip_lock_after_retries
+@order glibc.malloc.trim_threshold
+@order glibc.malloc.perturb
+@order glibc.elision.tries
+@order glibc.elision.enable
+@order glibc.malloc.mxfast
+@order glibc.elision.skip_lock_busy
+@order glibc.malloc.top_pad
+@order glibc.cpu.hwcap_mask
+@order glibc.malloc.mmap_max
+@order glibc.elision.skip_trylock_internal_abort
+@order glibc.malloc.tcache_unsorted_limit
+@order glibc.elision.skip_lock_internal_abort
+@order glibc.malloc.arena_max
+@order glibc.malloc.mmap_threshold
+@order glibc.malloc.tcache_count
+@order glibc.malloc.arena_test
+@order glibc.rtld.optional_static_tls
+@order glibc.malloc.tcache_max
+@order glibc.malloc.check
+
+# Tunables added in RHEL 8.8.0
+@order glibc.rtld.dynamic_sort
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list b/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list
new file mode 100644
index 0000000000000000..b9cad4af62d9f2e5
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list
@@ -0,0 +1,33 @@
+# Order of tunables in RHEL 8.7.0.
+@order glibc.rtld.nns
+@order glibc.elision.skip_lock_after_retries
+@order glibc.malloc.trim_threshold
+@order glibc.malloc.perturb
+@order glibc.cpu.x86_shared_cache_size
+@order glibc.elision.tries
+@order glibc.elision.enable
+@order glibc.cpu.x86_rep_movsb_threshold
+@order glibc.malloc.mxfast
+@order glibc.elision.skip_lock_busy
+@order glibc.malloc.top_pad
+@order glibc.cpu.x86_rep_stosb_threshold
+@order glibc.cpu.x86_non_temporal_threshold
+@order glibc.cpu.x86_shstk
+@order glibc.cpu.hwcap_mask
+@order glibc.malloc.mmap_max
+@order glibc.elision.skip_trylock_internal_abort
+@order glibc.malloc.tcache_unsorted_limit
+@order glibc.cpu.x86_ibt
+@order glibc.cpu.hwcaps
+@order glibc.elision.skip_lock_internal_abort
+@order glibc.malloc.arena_max
+@order glibc.malloc.mmap_threshold
+@order glibc.cpu.x86_data_cache_size
+@order glibc.malloc.tcache_count
+@order glibc.malloc.arena_test
+@order glibc.rtld.optional_static_tls
+@order glibc.malloc.tcache_max
+@order glibc.malloc.check
+
+# Tunables added in RHEL 8.8.0
+@order glibc.rtld.dynamic_sort
