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