Update syscall-names.list for Linux 5.18
[glibc.git] / scripts / glibcsymbols.py
blobc9369d7ef2f9be987a9b392ff94efbebf0c2c657
1 #!/usr/bin/python3
2 # Processing of symbols and abilist files.
3 # Copyright (C) 2020-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 """Symbol processing for glibc."""
22 import os
24 def replace_file(path, new_contents):
25 """Atomically replace PATH with lines from NEW_CONTENTS.
27 NEW_CONTENTS must be a sequence of strings.
29 """
30 temppath = path + 'T'
31 with open(temppath, 'w') as out:
32 for line in new_contents:
33 out.write(line)
34 os.rename(temppath, path)
36 class VersionedSymbol:
37 """A combination of a symbol and its version."""
39 def __init__(self, symbol, version):
40 """Construct a new versioned symbol."""
41 assert symbol
42 assert version
43 self.symbol = symbol
44 self.version = version
46 def __str__(self):
47 return self.symbol + '@' + self.version
49 def __eq__(self, other):
50 return self.symbol == other.symbol and self.version == other.version
52 def __hash__(self):
53 return hash(self.symbol) ^ hash(self.version)
55 def read_abilist(path):
56 """Read the abilist file at PATH.
58 Return a dictionary from VersionedSymbols to their flags (as
59 strings).
61 """
62 result = {}
63 with open(path) as inp:
64 for line in inp:
65 version, symbol, flags = line.strip().split(' ', 2)
66 versym = VersionedSymbol(symbol, version)
67 if versym in result:
68 raise IOError("{}: duplicate symbol {}".format(path, versym))
69 result[versym] = flags
70 return result
72 def abilist_lines(symbols):
73 """Build the abilist file contents (as a list of lines).
75 SYMBOLS is a dictionary from VersionedSymbols to their flags.
77 """
78 result = []
79 for versym, flags in symbols.items():
80 result.append('{} {} {}\n'.format(
81 versym.version, versym.symbol, flags))
82 result.sort()
83 return result