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