|
| 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§ion=all&arch=any&searchon=names&keywords=g%2B%2B) |
| 22 | +# - libc version 2.11.3 (https://packages.debian.org/search?suite=default§ion=all&arch=any&searchon=names&keywords=libc6) |
| 23 | +# - libstdc++ version 4.4.5 (https://packages.debian.org/search?suite=default§ion=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§ion=all) |
| 28 | +# - libc version 2.11.1 (http://packages.ubuntu.com/search?keywords=libc6&searchon=names&suite=lucid§ion=all) |
| 29 | +# - libstdc++ version 4.4.3 (http://packages.ubuntu.com/search?suite=lucid§ion=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