4c1956
commit a64af8c9b6598f6d2685227f64f5ccb9b48c663c
4c1956
Author: Florian Weimer <fweimer@redhat.com>
4c1956
Date:   Mon May 10 10:31:41 2021 +0200
4c1956
4c1956
    scripts/versions.awk: Add strings and hashes to <first-versions.h>
4c1956
    
4c1956
    This generates new macros of this from:
4c1956
    
4c1956
    They are useful for symbol lookups using _dl_lookup_direct.
4c1956
    
4c1956
    Tested-by: Carlos O'Donell <carlos@redhat.com>
4c1956
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
4c1956
4c1956
diff --git a/scripts/versions.awk b/scripts/versions.awk
4c1956
index a3df316c703ea98b..0c900b83347ce8f9 100644
4c1956
--- a/scripts/versions.awk
4c1956
+++ b/scripts/versions.awk
4c1956
@@ -32,6 +32,29 @@ BEGIN {
4c1956
   sort = "sort -t. -k 1,1 -k 2n,2n -k 3 > " tmpfile;
4c1956
 }
4c1956
 
4c1956
+# GNU awk does not implement the ord and chr functions.
4c1956
+# <https://www.gnu.org/software/gawk/manual/html_node/Ordinal-Functions.html>
4c1956
+# says that they are "written very nicely", using code similar to what
4c1956
+# is included here.
4c1956
+function chr(c) {
4c1956
+    return sprintf("%c", c)
4c1956
+}
4c1956
+
4c1956
+BEGIN {
4c1956
+    for (c = 1; c < 127; c++) {
4c1956
+	ord_table[chr(c)] = c;
4c1956
+    }
4c1956
+}
4c1956
+
4c1956
+function ord(c) {
4c1956
+    if (ord_table[c]) {
4c1956
+	return ord_table[c];
4c1956
+    } else {
4c1956
+	printf("Invalid character reference: '%c'\n", c) > "/dev/stderr";
4c1956
+	++lossage;
4c1956
+    }
4c1956
+}
4c1956
+
4c1956
 # Remove comment lines.
4c1956
 /^ *#/ {
4c1956
   next;
4c1956
@@ -90,6 +113,17 @@ function close_and_move(name, real_name) {
4c1956
   system(move_if_change " " name " " real_name " >&2");
4c1956
 }
4c1956
 
4c1956
+# ELF hash, for use with symbol versions.
4c1956
+function elf_hash(s, i, acc) {
4c1956
+  acc = 0;
4c1956
+  for (i = 1; i <= length(s); ++i) {
4c1956
+      acc = and(lshift(acc, 4) + ord(substr(s, i, 1)), 0xffffffff);
4c1956
+      top = and(acc, 0xf0000000);
4c1956
+      acc = and(xor(acc, rshift(top, 24)), compl(top));
4c1956
+  }
4c1956
+  return acc;
4c1956
+}
4c1956
+
4c1956
 # Now print the accumulated information.
4c1956
 END {
4c1956
   close(sort);
4c1956
@@ -145,6 +179,8 @@ END {
4c1956
 	  && oldver ~ "^GLIBC_[0-9]" \
4c1956
 	  && sym ~ "^[A-Za-z0-9_]*$") {
4c1956
 	ver_val = oldver;
4c1956
+	printf("#define %s_STRING \"%s\"\n", first_ver_macro, ver_val) > first_ver_header;
4c1956
+	printf("#define %s_HASH 0x%x\n", first_ver_macro, elf_hash(ver_val)) > first_ver_header;
4c1956
 	gsub("\\.", "_", ver_val);
4c1956
 	printf("#define %s %s\n", first_ver_macro, ver_val) > first_ver_header;
4c1956
 	first_ver_seen[first_ver_macro] = 1;