glibcextract.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/python3
  2. # Extract information from C headers.
  3. # Copyright (C) 2018-2026 Free Software Foundation, Inc.
  4. # This file is part of the GNU C Library.
  5. #
  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.
  10. #
  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.
  15. #
  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/>.
  19. import collections
  20. import os.path
  21. import re
  22. import subprocess
  23. import tempfile
  24. def compute_c_consts(sym_data, cc):
  25. """Compute the values of some C constants.
  26. The first argument is a list whose elements are either strings
  27. (preprocessor directives, or the special string 'START' to
  28. indicate this function should insert its initial boilerplate text
  29. in the output there) or pairs of strings (a name and a C
  30. expression for the corresponding value). Preprocessor directives
  31. in the middle of the list may be used to select which constants
  32. end up being evaluated using which expressions.
  33. """
  34. out_lines = []
  35. for arg in sym_data:
  36. if isinstance(arg, str):
  37. if arg == 'START':
  38. out_lines.append('void\ndummy (void)\n{')
  39. else:
  40. out_lines.append(arg)
  41. continue
  42. name = arg[0]
  43. value = arg[1]
  44. out_lines.append('asm ("/* @@@name@@@%s@@@value@@@%%0@@@end@@@ */" '
  45. ': : \"i\" ((long int) (%s)));'
  46. % (name, value))
  47. out_lines.append('}')
  48. out_lines.append('')
  49. out_text = '\n'.join(out_lines)
  50. with tempfile.TemporaryDirectory() as temp_dir:
  51. c_file_name = os.path.join(temp_dir, 'test.c')
  52. s_file_name = os.path.join(temp_dir, 'test.s')
  53. with open(c_file_name, 'w') as c_file:
  54. c_file.write(out_text)
  55. # Compilation has to be from stdin to avoid the temporary file
  56. # name being written into the generated dependencies.
  57. cmd = ('%s -S -o %s -x c - < %s' % (cc, s_file_name, c_file_name))
  58. subprocess.check_call(cmd, shell=True)
  59. consts = {}
  60. with open(s_file_name, 'r') as s_file:
  61. for line in s_file:
  62. match = re.search('@@@name@@@([^@]*)'
  63. '@@@value@@@[^0-9Xxa-fA-F-]*'
  64. '([0-9Xxa-fA-F-]+).*@@@end@@@', line)
  65. if match:
  66. if (match.group(1) in consts
  67. and match.group(2) != consts[match.group(1)]):
  68. raise ValueError('duplicate constant %s'
  69. % match.group(1))
  70. consts[match.group(1)] = match.group(2)
  71. return consts
  72. def list_macros(source_text, cc):
  73. """List the preprocessor macros defined by the given source code.
  74. The return value is a pair of dicts, the first one mapping macro
  75. names to their expansions and the second one mapping macro names
  76. to lists of their arguments, or to None for object-like macros.
  77. """
  78. with tempfile.TemporaryDirectory() as temp_dir:
  79. c_file_name = os.path.join(temp_dir, 'test.c')
  80. i_file_name = os.path.join(temp_dir, 'test.i')
  81. with open(c_file_name, 'w') as c_file:
  82. c_file.write(source_text)
  83. cmd = ('%s -E -dM -o %s %s' % (cc, i_file_name, c_file_name))
  84. subprocess.check_call(cmd, shell=True)
  85. macros_exp = {}
  86. macros_args = {}
  87. with open(i_file_name, 'r') as i_file:
  88. for line in i_file:
  89. match = re.fullmatch('#define ([0-9A-Za-z_]+)(.*)\n', line)
  90. if not match:
  91. raise ValueError('bad -dM output line: %s' % line)
  92. name = match.group(1)
  93. value = match.group(2)
  94. if value.startswith(' '):
  95. value = value[1:]
  96. args = None
  97. elif value.startswith('('):
  98. match = re.fullmatch(r'\((.*?)\) (.*)', value)
  99. if not match:
  100. raise ValueError('bad -dM output line: %s' % line)
  101. args = match.group(1).split(',')
  102. value = match.group(2)
  103. else:
  104. raise ValueError('bad -dM output line: %s' % line)
  105. if name in macros_exp:
  106. raise ValueError('duplicate macro: %s' % line)
  107. macros_exp[name] = value
  108. macros_args[name] = args
  109. return macros_exp, macros_args
  110. def compute_macro_consts(source_text, cc, macro_re, exclude_re=None):
  111. """Compute the integer constant values of macros defined by source_text.
  112. Macros must match the regular expression macro_re, and if
  113. exclude_re is defined they must not match exclude_re. Values are
  114. computed with compute_c_consts.
  115. """
  116. macros_exp, macros_args = list_macros(source_text, cc)
  117. macros_set = {m for m in macros_exp
  118. if (macros_args[m] is None
  119. and re.fullmatch(macro_re, m)
  120. and (exclude_re is None
  121. or not re.fullmatch(exclude_re, m)))}
  122. sym_data = [source_text, 'START']
  123. sym_data.extend(sorted((m, m) for m in macros_set))
  124. return compute_c_consts(sym_data, cc)
  125. def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None,
  126. allow_extra_1=False, allow_extra_2=False):
  127. """Compare the values of macros defined by two different sources.
  128. The sources would typically be includes of a glibc header and a
  129. kernel header. If allow_extra_1, the first source may define
  130. extra macros (typically if the kernel headers are older than the
  131. version glibc has taken definitions from); if allow_extra_2, the
  132. second source may define extra macros (typically if the kernel
  133. headers are newer than the version glibc has taken definitions
  134. from). Return 1 if there were any differences other than those
  135. allowed, 0 if the macro values were the same apart from any
  136. allowed differences.
  137. """
  138. macros_1 = compute_macro_consts(source_1, cc, macro_re, exclude_re)
  139. macros_2 = compute_macro_consts(source_2, cc, macro_re, exclude_re)
  140. if macros_1 == macros_2:
  141. return 0
  142. print('First source:\n%s\n' % source_1)
  143. print('Second source:\n%s\n' % source_2)
  144. ret = 0
  145. for name, value in sorted(macros_1.items()):
  146. if name not in macros_2:
  147. print('Only in first source: %s' % name)
  148. if not allow_extra_1:
  149. ret = 1
  150. elif macros_1[name] != macros_2[name]:
  151. print('Different values for %s: %s != %s'
  152. % (name, macros_1[name], macros_2[name]))
  153. ret = 1
  154. for name in sorted(macros_2.keys()):
  155. if name not in macros_1:
  156. print('Only in second source: %s' % name)
  157. if not allow_extra_2:
  158. ret = 1
  159. return ret
  160. CompileResult = collections.namedtuple("CompileResult", "returncode output")
  161. def compile_c_snippet(snippet, cc, extra_cc_args=''):
  162. """Compile and return whether the SNIPPET can be build with CC along
  163. EXTRA_CC_ARGS compiler flags. Return a CompileResult with RETURNCODE
  164. being 0 for success, or the failure value and the compiler output.
  165. """
  166. with tempfile.TemporaryDirectory() as temp_dir:
  167. c_file_name = os.path.join(temp_dir, 'test.c')
  168. obj_file_name = os.path.join(temp_dir, 'test.o')
  169. with open(c_file_name, 'w') as c_file:
  170. c_file.write(snippet + '\n')
  171. cmd = cc.split() + extra_cc_args.split() + ['-c', '-o', obj_file_name,
  172. c_file_name]
  173. r = subprocess.run(cmd, check=False, stdout=subprocess.PIPE,
  174. stderr=subprocess.STDOUT)
  175. return CompileResult(r.returncode, r.stdout)