Skip to content

Commit 7cbe636

Browse files
committed
Merge pull request #4089
74fc254 devtools: add script to check symbols from Linux gitian executables (Wladimir J. van der Laan)
2 parents 814df91 + 74fc254 commit 7cbe636

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

contrib/devtools/README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contents
33
This directory contains tools for developers working on this repository.
44

55
github-merge.sh
6-
----------------
6+
==================
77

88
A small script to automate merging pull-requests securely and sign them with GPG.
99

@@ -36,7 +36,8 @@ Configuring the github-merge tool for the bitcoin repository is done in the foll
3636
git config githubmerge.testcmd "make -j4 check" (adapt to whatever you want to use for testing)
3737
git config --global user.signingkey mykeyid (if you want to GPG sign)
3838

39-
## fix-copyright-headers.py
39+
fix-copyright-headers.py
40+
===========================
4041

4142
Every year newly updated files need to have its copyright headers updated to reflect the current year.
4243
If you run this script from src/ it will automatically update the year on the copyright header for all
@@ -46,4 +47,25 @@ For example a file changed in 2014 (with 2014 being the current year):
4647
```// Copyright (c) 2009-2013 The Bitcoin developers```
4748

4849
would be changed to:
49-
```// Copyright (c) 2009-2014 The Bitcoin developers```
50+
```// Copyright (c) 2009-2014 The Bitcoin developers```
51+
52+
symbol-check.py
53+
==================
54+
55+
A script to check that the (Linux) executables produced by gitian only contain
56+
allowed gcc, glibc and libstdc++ version symbols. This makes sure they are
57+
still compatible with the minimum supported Linux distribution versions.
58+
59+
Example usage after a gitian build:
60+
61+
find ../gitian-builder/build -type f -executable | xargs python contrib/devtools/symbol-check.py
62+
63+
If only supported symbols are used the return value will be 0 and the output will be empty.
64+
65+
If there are 'unsupported' symbols, the return value will be 1 a list like this will be printed:
66+
67+
.../64/test_bitcoin: symbol memcpy from unsupported version GLIBC_2.14
68+
.../64/test_bitcoin: symbol __fdelt_chk from unsupported version GLIBC_2.15
69+
.../64/test_bitcoin: symbol std::out_of_range::~out_of_range() from unsupported version GLIBCXX_3.4.15
70+
.../64/test_bitcoin: symbol _ZNSt8__detail15_List_nod from unsupported version GLIBCXX_3.4.15
71+

contrib/devtools/symbol-check.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
10+
Example usage:
11+
12+
find ../gitian-builder/build -type f -executable | xargs python contrib/devtools/symbol-check.py
13+
'''
14+
from __future__ import division, print_function
15+
import subprocess
16+
import re
17+
import sys
18+
19+
# Debian 6.0.9 (Squeeze) has:
20+
#
21+
# - g++ version 4.4.5 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=g%2B%2B)
22+
# - libc version 2.11.3 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=libc6)
23+
# - libstdc++ version 4.4.5 (https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=names&keywords=libstdc%2B%2B6)
24+
#
25+
# Ubuntu 10.04.4 (Lucid Lynx) has:
26+
#
27+
# - g++ version 4.4.3 (http://packages.ubuntu.com/search?keywords=g%2B%2B&searchon=names&suite=lucid&section=all)
28+
# - libc version 2.11.1 (http://packages.ubuntu.com/search?keywords=libc6&searchon=names&suite=lucid&section=all)
29+
# - libstdc++ version 4.4.3 (http://packages.ubuntu.com/search?suite=lucid&section=all&arch=any&keywords=libstdc%2B%2B&searchon=names)
30+
#
31+
# Taking the minimum of these as our target.
32+
#
33+
# According to GNU ABI document (http://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html) this corresponds to:
34+
# GCC 4.4.0: GCC_4.4.0
35+
# GCC 4.4.2: GLIBCXX_3.4.13, CXXABI_1.3.3
36+
# (glibc) GLIBC_2_11
37+
#
38+
MAX_VERSIONS = {
39+
'GCC': (4,4,0),
40+
'CXXABI': (1,3,3),
41+
'GLIBCXX': (3,4,13),
42+
'GLIBC': (2,11)
43+
}
44+
READELF_CMD = '/usr/bin/readelf'
45+
CPPFILT_CMD = '/usr/bin/c++filt'
46+
47+
class CPPFilt(object):
48+
'''
49+
Demangle C++ symbol names.
50+
51+
Use a pipe to the 'c++filt' command.
52+
'''
53+
def __init__(self):
54+
self.proc = subprocess.Popen(CPPFILT_CMD, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
55+
56+
def __call__(self, mangled):
57+
self.proc.stdin.write(mangled + '\n')
58+
return self.proc.stdout.readline().rstrip()
59+
60+
def close(self):
61+
self.proc.stdin.close()
62+
self.proc.stdout.close()
63+
self.proc.wait()
64+
65+
def read_symbols(executable, imports=True):
66+
'''
67+
Parse an ELF executable and return a list of (symbol,version) tuples
68+
for dynamic, imported symbols.
69+
'''
70+
p = subprocess.Popen([READELF_CMD, '--dyn-syms', '-W', executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
71+
(stdout, stderr) = p.communicate()
72+
if p.returncode:
73+
raise IOError('Could not read symbols for %s: %s' % (executable, stderr.strip()))
74+
syms = []
75+
for line in stdout.split('\n'):
76+
line = line.split()
77+
if len(line)>7 and re.match('[0-9]+:$', line[0]):
78+
(sym, _, version) = line[7].partition('@')
79+
is_import = line[6] == 'UND'
80+
if version.startswith('@'):
81+
version = version[1:]
82+
if is_import == imports:
83+
syms.append((sym, version))
84+
return syms
85+
86+
def check_version(max_versions, version):
87+
if '_' in version:
88+
(lib, _, ver) = version.rpartition('_')
89+
else:
90+
lib = version
91+
ver = '0'
92+
ver = tuple([int(x) for x in ver.split('.')])
93+
if not lib in max_versions:
94+
return False
95+
return ver <= max_versions[lib]
96+
97+
if __name__ == '__main__':
98+
cppfilt = CPPFilt()
99+
retval = 0
100+
for filename in sys.argv[1:]:
101+
for sym,version in read_symbols(filename, True):
102+
if version and not check_version(MAX_VERSIONS, version):
103+
print('%s: symbol %s from unsupported version %s' % (filename, cppfilt(sym), version))
104+
retval = 1
105+
106+
exit(retval)
107+
108+

0 commit comments

Comments
 (0)