Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

symbols-check.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python
  2. import argparse
  3. import os
  4. import platform
  5. import subprocess
  6. # This list contains symbols that _might_ be exported for some platforms
  7. PLATFORM_SYMBOLS = [
  8. '__bss_end__',
  9. '__bss_start__',
  10. '__bss_start',
  11. '__end__',
  12. '_bss_end__',
  13. '_edata',
  14. '_end',
  15. '_fini',
  16. '_init',
  17. ]
  18. def get_symbols_nm(nm, lib):
  19. '''
  20. List all the (non platform-specific) symbols exported by the library
  21. using `nm`
  22. '''
  23. symbols = []
  24. platform_name = platform.system()
  25. output = subprocess.check_output([nm, '-gP', lib],
  26. stderr=open(os.devnull, 'w')).decode("ascii")
  27. for line in output.splitlines():
  28. fields = line.split()
  29. if len(fields) == 2 or fields[1] == 'U':
  30. continue
  31. symbol_name = fields[0]
  32. if platform_name == 'Linux':
  33. if symbol_name in PLATFORM_SYMBOLS:
  34. continue
  35. elif platform_name == 'Darwin':
  36. assert symbol_name[0] == '_'
  37. symbol_name = symbol_name[1:]
  38. symbols.append(symbol_name)
  39. return symbols
  40. def get_symbols_dumpbin(dumpbin, lib):
  41. '''
  42. List all the (non platform-specific) symbols exported by the library
  43. using `dumpbin`
  44. '''
  45. symbols = []
  46. output = subprocess.check_output([dumpbin, '/exports', lib],
  47. stderr=open(os.devnull, 'w')).decode("ascii")
  48. for line in output.splitlines():
  49. fields = line.split()
  50. # The lines with the symbols are made of at least 4 columns; see details below
  51. if len(fields) < 4:
  52. continue
  53. try:
  54. # Making sure the first 3 columns are a dec counter, a hex counter
  55. # and a hex address
  56. _ = int(fields[0], 10)
  57. _ = int(fields[1], 16)
  58. _ = int(fields[2], 16)
  59. except ValueError:
  60. continue
  61. symbol_name = fields[3]
  62. # De-mangle symbols
  63. if symbol_name[0] == '_':
  64. symbol_name = symbol_name[1:].split('@')[0]
  65. symbols.append(symbol_name)
  66. return symbols
  67. def main():
  68. parser = argparse.ArgumentParser()
  69. parser.add_argument('--symbols-file',
  70. action='store',
  71. required=True,
  72. help='path to file containing symbols')
  73. parser.add_argument('--lib',
  74. action='store',
  75. required=True,
  76. help='path to library')
  77. parser.add_argument('--nm',
  78. action='store',
  79. help='path to binary (or name in $PATH)')
  80. parser.add_argument('--dumpbin',
  81. action='store',
  82. help='path to binary (or name in $PATH)')
  83. args = parser.parse_args()
  84. try:
  85. if platform.system() == 'Windows':
  86. if not args.dumpbin:
  87. parser.error('--dumpbin is mandatory')
  88. lib_symbols = get_symbols_dumpbin(args.dumpbin, args.lib)
  89. else:
  90. if not args.nm:
  91. parser.error('--nm is mandatory')
  92. lib_symbols = get_symbols_nm(args.nm, args.lib)
  93. except:
  94. # We can't run this test, but we haven't technically failed it either
  95. # Return the GNU "skip" error code
  96. exit(77)
  97. mandatory_symbols = []
  98. optional_symbols = []
  99. with open(args.symbols_file) as symbols_file:
  100. qualifier_optional = '(optional)'
  101. for line in symbols_file.readlines():
  102. # Strip comments
  103. line = line.split('#')[0]
  104. line = line.strip()
  105. if not line:
  106. continue
  107. # Line format:
  108. # [qualifier] symbol
  109. qualifier = None
  110. symbol = None
  111. fields = line.split()
  112. if len(fields) == 1:
  113. symbol = fields[0]
  114. elif len(fields) == 2:
  115. qualifier = fields[0]
  116. symbol = fields[1]
  117. else:
  118. print(args.symbols_file + ': invalid format: ' + line)
  119. exit(1)
  120. # The only supported qualifier is 'optional', which means the
  121. # symbol doesn't have to be exported by the library
  122. if qualifier and not qualifier == qualifier_optional:
  123. print(args.symbols_file + ': invalid qualifier: ' + qualifier)
  124. exit(1)
  125. if qualifier == qualifier_optional:
  126. optional_symbols.append(symbol)
  127. else:
  128. mandatory_symbols.append(symbol)
  129. unknown_symbols = []
  130. for symbol in lib_symbols:
  131. if symbol in mandatory_symbols:
  132. continue
  133. if symbol in optional_symbols:
  134. continue
  135. if symbol[:2] == '_Z':
  136. # Ignore random C++ symbols
  137. #TODO: figure out if there's any way to avoid exporting them in the first place
  138. continue
  139. unknown_symbols.append(symbol)
  140. missing_symbols = [
  141. sym for sym in mandatory_symbols if sym not in lib_symbols
  142. ]
  143. for symbol in unknown_symbols:
  144. print(args.lib + ': unknown symbol exported: ' + symbol)
  145. for symbol in missing_symbols:
  146. print(args.lib + ': missing symbol: ' + symbol)
  147. if unknown_symbols or missing_symbols:
  148. exit(1)
  149. exit(0)
  150. if __name__ == '__main__':
  151. main()