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.
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.
12 find ../gitian-builder/build -type f -executable | xargs python contrib/devtools/symbol-check.py
14 from __future__
import division
, print_function
, unicode_literals
20 # Debian 6.0.9 (Squeeze) has:
22 # - g++ version 4.4.5 (https://packages.debian.org/search?suite=default§ion=all&arch=any&searchon=names&keywords=g%2B%2B)
23 # - libc version 2.11.3 (https://packages.debian.org/search?suite=default§ion=all&arch=any&searchon=names&keywords=libc6)
24 # - libstdc++ version 4.4.5 (https://packages.debian.org/search?suite=default§ion=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§ion=all)
29 # - libc version 2.11.1 (http://packages.ubuntu.com/search?keywords=libc6&searchon=names&suite=lucid§ion=all)
30 # - libstdc++ version 4.4.3 (http://packages.ubuntu.com/search?suite=lucid§ion=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
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
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
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
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):
76 Demangle C++ symbol names.
78 Use a pipe to the 'c++filt' command.
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()
89 self
.proc
.stdin
.close()
90 self
.proc
.stdout
.close()
93 def read_symbols(executable
, imports
=True):
95 Parse an ELF executable and return a list of (symbol,version) tuples
96 for dynamic, imported symbols.
98 p
= subprocess
.Popen([READELF_CMD
, '--dyn-syms', '-W', executable
], stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
99 (stdout
, stderr
) = p
.communicate()
101 raise IOError('Could not read symbols for %s: %s' % (executable
, stderr
.strip()))
103 for line
in stdout
.split(b
'\n'):
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
))
114 def check_version(max_versions
, version
):
116 (lib
, _
, ver
) = version
.rpartition(b
'_')
120 ver
= tuple([int(x
) for x
in ver
.split(b
'.')])
121 if not lib
in max_versions
:
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()
129 raise IOError('Error opening file')
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:]))
136 libraries
.append(match
.group(1))
138 raise ValueError('Unparseable (NEEDED) specification')
141 if __name__
== '__main__':
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')))
150 # Check exported symbols
151 for sym
,version
in read_symbols(filename
, False):
152 if sym
in IGNORE_EXPORTS
:
154 print('%s: export of symbol %s not allowed' % (filename
, cppfilt(sym
).decode('utf-8')))
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')))