PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/devtools/symbol-check.py

https://gitlab.com/Ltaimao/bitcoin
Python | 119 lines | 81 code | 2 blank | 36 comment | 1 complexity | 3077bdb9d8e8ad0370ad58cc224d381a MD5 | raw file
  1. #!/usr/bin/python
  2. # Copyright (c) 2014 Wladimir J. van der Laan
  3. # Distributed under the MIT/X11 software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. '''
  6. A script to check that the (Linux) executables produced by gitian only contain
  7. allowed gcc, glibc and libstdc++ version symbols. This makes sure they are
  8. still compatible with the minimum supported Linux distribution versions.
  9. Example usage:
  10. find ../gitian-builder/build -type f -executable | xargs python contrib/devtools/symbol-check.py
  11. '''
  12. from __future__ import division, print_function
  13. import subprocess
  14. import re
  15. import sys
  16. # Debian 6.0.9 (Squeeze) has:
  17. #
  18. # - g++ version 4.4.5 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=g%2B%2B)
  19. # - libc version 2.11.3 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=libc6)
  20. # - libstdc++ version 4.4.5 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=libstdc%2B%2B6)
  21. #
  22. # Ubuntu 10.04.4 (Lucid Lynx) has:
  23. #
  24. # - g++ version 4.4.3 (http://packages.ubuntu.com/search?keywords=g%2B%2B&searchon=names&suite=lucid&section=all)
  25. # - libc version 2.11.1 (http://packages.ubuntu.com/search?keywords=libc6&searchon=names&suite=lucid&section=all)
  26. # - libstdc++ version 4.4.3 (http://packages.ubuntu.com/search?suite=lucid&section=all&arch=any&keywords=libstdc%2B%2B&searchon=names)
  27. #
  28. # Taking the minimum of these as our target.
  29. #
  30. # According to GNU ABI document (http://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html) this corresponds to:
  31. # GCC 4.4.0: GCC_4.4.0
  32. # GCC 4.4.2: GLIBCXX_3.4.13, CXXABI_1.3.3
  33. # (glibc) GLIBC_2_11
  34. #
  35. MAX_VERSIONS = {
  36. 'GCC': (4,4,0),
  37. 'CXXABI': (1,3,3),
  38. 'GLIBCXX': (3,4,13),
  39. 'GLIBC': (2,11)
  40. }
  41. # Ignore symbols that are exported as part of every executable
  42. IGNORE_EXPORTS = {
  43. '_edata', '_end', '_init', '__bss_start', '_fini'
  44. }
  45. READELF_CMD = '/usr/bin/readelf'
  46. CPPFILT_CMD = '/usr/bin/c++filt'
  47. class CPPFilt(object):
  48. '''
  49. Demangle C++ symbol names.
  50. Use a pipe to the 'c++filt' command.
  51. '''
  52. def __init__(self):
  53. self.proc = subprocess.Popen(CPPFILT_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  54. def __call__(self, mangled):
  55. self.proc.stdin.write(mangled + '\n')
  56. return self.proc.stdout.readline().rstrip()
  57. def close(self):
  58. self.proc.stdin.close()
  59. self.proc.stdout.close()
  60. self.proc.wait()
  61. def read_symbols(executable, imports=True):
  62. '''
  63. Parse an ELF executable and return a list of (symbol,version) tuples
  64. for dynamic, imported symbols.
  65. '''
  66. p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  67. (stdout, stderr) = p.communicate()
  68. if p.returncode:
  69. raise IOError('Could not read symbols for %s: %s' % (executable, stderr.strip()))
  70. syms = []
  71. for line in stdout.split('\n'):
  72. line = line.split()
  73. if len(line)>7 and re.match('[0-9]+:$', line[0]):
  74. (sym, _, version) = line[7].partition('@')
  75. is_import = line[6] == 'UND'
  76. if version.startswith('@'):
  77. version = version[1:]
  78. if is_import == imports:
  79. syms.append((sym, version))
  80. return syms
  81. def check_version(max_versions, version):
  82. if '_' in version:
  83. (lib, _, ver) = version.rpartition('_')
  84. else:
  85. lib = version
  86. ver = '0'
  87. ver = tuple([int(x) for x in ver.split('.')])
  88. if not lib in max_versions:
  89. return False
  90. return ver <= max_versions[lib]
  91. if __name__ == '__main__':
  92. cppfilt = CPPFilt()
  93. retval = 0
  94. for filename in sys.argv[1:]:
  95. # Check imported symbols
  96. for sym,version in read_symbols(filename, True):
  97. if version and not check_version(MAX_VERSIONS, version):
  98. print('%s: symbol %s from unsupported version %s' % (filename, cppfilt(sym), version))
  99. retval = 1
  100. # Check exported symbols
  101. for sym,version in read_symbols(filename, False):
  102. if sym in IGNORE_EXPORTS:
  103. continue
  104. print('%s: export of symbol %s not allowed' % (filename, cppfilt(sym)))
  105. retval = 1
  106. exit(retval)