Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / contrib / devtools / symbol-check.py
blob98daa1bd76f94a76f85df6fc3d2f7d4f0ed37fe8
1 #!/usr/bin/env python
2 # Copyright (c) 2014 Wladimir J. van der Laan
3 # Distributed under the MIT 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.
10 Example usage:
12 find ../gitian-builder/build -type f -executable | xargs python contrib/devtools/symbol-check.py
13 '''
14 from __future__ import division, print_function, unicode_literals
15 import subprocess
16 import re
17 import sys
18 import os
20 # Debian 6.0.9 (Squeeze) has:
22 # - g++ version 4.4.5 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=g%2B%2B)
23 # - libc version 2.11.3 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=libc6)
24 # - libstdc++ version 4.4.5 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=libstdc%2B%2B6)
26 # Ubuntu 10.04.4 (Lucid Lynx) has:
28 # - g++ version 4.4.3 (http://packages.ubuntu.com/search?keywords=g%2B%2B&searchon=names&suite=lucid&section=all)
29 # - libc version 2.11.1 (http://packages.ubuntu.com/search?keywords=libc6&searchon=names&suite=lucid&section=all)
30 # - libstdc++ version 4.4.3 (http://packages.ubuntu.com/search?suite=lucid&section=all&arch=any&keywords=libstdc%2B%2B&searchon=names)
32 # Taking the minimum of these as our target.
34 # According to GNU ABI document (http://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html) this corresponds to:
35 # GCC 4.4.0: GCC_4.4.0
36 # GCC 4.4.2: GLIBCXX_3.4.13, CXXABI_1.3.3
37 # (glibc) GLIBC_2_11
39 MAX_VERSIONS = {
40 'GCC': (4,4,0),
41 'CXXABI': (1,3,3),
42 'GLIBCXX': (3,4,13),
43 'GLIBC': (2,11)
45 # See here for a description of _IO_stdin_used:
46 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=634261#109
48 # Ignore symbols that are exported as part of every executable
49 IGNORE_EXPORTS = {
50 b'_edata', b'_end', b'_init', b'__bss_start', b'_fini', b'_IO_stdin_used'
52 READELF_CMD = os.getenv('READELF', '/usr/bin/readelf')
53 CPPFILT_CMD = os.getenv('CPPFILT', '/usr/bin/c++filt')
54 # Allowed NEEDED libraries
55 ALLOWED_LIBRARIES = {
56 # bitcoind and bitcoin-qt
57 b'libgcc_s.so.1', # GCC base support
58 b'libc.so.6', # C library
59 b'libpthread.so.0', # threading
60 b'libanl.so.1', # DNS resolve
61 b'libm.so.6', # math library
62 b'librt.so.1', # real-time (clock)
63 b'ld-linux-x86-64.so.2', # 64-bit dynamic linker
64 b'ld-linux.so.2', # 32-bit dynamic linker
65 # bitcoin-qt only
66 b'libX11-xcb.so.1', # part of X11
67 b'libX11.so.6', # part of X11
68 b'libxcb.so.1', # part of X11
69 b'libfontconfig.so.1', # font support
70 b'libfreetype.so.6', # font parsing
71 b'libdl.so.2' # programming interface to dynamic linker
74 class CPPFilt(object):
75 '''
76 Demangle C++ symbol names.
78 Use a pipe to the 'c++filt' command.
79 '''
80 def __init__(self):
81 self.proc = subprocess.Popen(CPPFILT_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
83 def __call__(self, mangled):
84 self.proc.stdin.write(mangled + b'\n')
85 self.proc.stdin.flush()
86 return self.proc.stdout.readline().rstrip()
88 def close(self):
89 self.proc.stdin.close()
90 self.proc.stdout.close()
91 self.proc.wait()
93 def read_symbols(executable, imports=True):
94 '''
95 Parse an ELF executable and return a list of (symbol,version) tuples
96 for dynamic, imported symbols.
97 '''
98 p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
99 (stdout, stderr) = p.communicate()
100 if p.returncode:
101 raise IOError('Could not read symbols for %s: %s' % (executable, stderr.strip()))
102 syms = []
103 for line in stdout.split(b'\n'):
104 line = line.split()
105 if len(line)>7 and re.match(b'[0-9]+:$', line[0]):
106 (sym, _, version) = line[7].partition(b'@')
107 is_import = line[6] == b'UND'
108 if version.startswith(b'@'):
109 version = version[1:]
110 if is_import == imports:
111 syms.append((sym, version))
112 return syms
114 def check_version(max_versions, version):
115 if b'_' in version:
116 (lib, _, ver) = version.rpartition(b'_')
117 else:
118 lib = version
119 ver = '0'
120 ver = tuple([int(x) for x in ver.split(b'.')])
121 if not lib in max_versions:
122 return False
123 return ver <= max_versions[lib]
125 def read_libraries(filename):
126 p = subprocess.Popen([READELF_CMD, '-d', '-W', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
127 (stdout, stderr) = p.communicate()
128 if p.returncode:
129 raise IOError('Error opening file')
130 libraries = []
131 for line in stdout.split(b'\n'):
132 tokens = line.split()
133 if len(tokens)>2 and tokens[1] == b'(NEEDED)':
134 match = re.match(b'^Shared library: \[(.*)\]$', b' '.join(tokens[2:]))
135 if match:
136 libraries.append(match.group(1))
137 else:
138 raise ValueError('Unparseable (NEEDED) specification')
139 return libraries
141 if __name__ == '__main__':
142 cppfilt = CPPFilt()
143 retval = 0
144 for filename in sys.argv[1:]:
145 # Check imported symbols
146 for sym,version in read_symbols(filename, True):
147 if version and not check_version(MAX_VERSIONS, version):
148 print('%s: symbol %s from unsupported version %s' % (filename, cppfilt(sym).decode('utf-8'), version.decode('utf-8')))
149 retval = 1
150 # Check exported symbols
151 for sym,version in read_symbols(filename, False):
152 if sym in IGNORE_EXPORTS:
153 continue
154 print('%s: export of symbol %s not allowed' % (filename, cppfilt(sym).decode('utf-8')))
155 retval = 1
156 # Check dependency libraries
157 for library_name in read_libraries(filename):
158 if library_name not in ALLOWED_LIBRARIES:
159 print('%s: NEEDED library %s is not allowed' % (filename, library_name.decode('utf-8')))
160 retval = 1
162 sys.exit(retval)