Detect ld.so and libc.so version inconsistency during startup
[glibc.git] / scripts / libc_early_init_name.py
bloba56c2008f31a463820ffa2d488089a1fb575aae8
1 #!/usr/bin/python3
2 # Compute the hash-based name of the __libc_early_init function.
3 # Copyright (C) 2022 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <https://www.gnu.org/licenses/>.
20 """Compute the name of the __libc_early_init function, which is used
21 as a protocol version marker between ld.so and libc.so.
23 The name contains a hash suffix, and the hash changes if certain key
24 files in the source tree change. Distributions can also configure
25 with --with-extra-version-id, to make the computed hash dependent on
26 the package version.
28 """
30 import argparse
31 import hashlib
32 import os
33 import string
34 import sys
36 def gnu_hash(s):
37 """Computes the GNU hash of the string."""
38 h = 5381
39 for ch in s:
40 if type(ch) is not int:
41 ch = ord(ch)
42 h = (h * 33 + ch) & 0xffffffff
43 return h
45 # Parse the command line.
46 parser = argparse.ArgumentParser(description=__doc__)
47 parser.add_argument('--output', metavar='PATH',
48 help='path to header file this tool generates')
49 parser.add_argument('--extra-version-id', metavar='ID',
50 help='extra string to influence hash computation')
51 parser.add_argument('inputs', metavar='PATH', nargs='*',
52 help='files whose contents influences the generated hash')
53 opts = parser.parse_args()
55 # Obtain the blobs that affect the generated hash.
56 blobs = [(opts.extra_version_id or '').encode('UTF-8')]
57 for path in opts.inputs:
58 with open(path, 'rb') as inp:
59 blobs.append(inp.read())
61 # Hash the file boundaries.
62 md = hashlib.sha256()
63 md.update(repr([len(blob) for blob in blobs]).encode('UTF-8'))
65 # And then hash the file contents. Do not hash the paths, to avoid
66 # impacting reproducibility.
67 for blob in blobs:
68 md.update(blob)
70 # These are the bits used to compute the suffix.
71 derived_bits = int.from_bytes(md.digest(), byteorder='big', signed=False)
73 # These digits are used in the suffix (should result in base-62 encoding).
74 # They must be valid in C identifiers.
75 digits = string.digits + string.ascii_letters
77 # Generate eight digits as a suffix. They should provide enough
78 # uniqueness (47.6 bits).
79 name = '__libc_early_init_'
80 for n in range(8):
81 name += digits[derived_bits % len(digits)]
82 derived_bits //= len(digits)
84 # Write the output file.
85 with open(opts.output, 'w') if opts.output else sys.stdout as out:
86 out.write('#define LIBC_EARLY_INIT_NAME {}\n'.format(name))
87 out.write('#define LIBC_EARLY_INIT_NAME_STRING "{}"\n'.format(name))
88 out.write('#define LIBC_EARLY_INIT_GNU_HASH {}\n'.format(
89 gnu_hash(name)))