xref: /openbsd-src/gnu/llvm/compiler-rt/lib/dfsan/scripts/build-libc-list.py (revision 810390e339a5425391477d5d41c78d7cab2424ac)
1#!/usr/bin/env python
2#===- lib/dfsan/scripts/build-libc-list.py ---------------------------------===#
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===------------------------------------------------------------------------===#
9# The purpose of this script is to identify every function symbol in a set of
10# libraries (in this case, libc and libgcc) so that they can be marked as
11# uninstrumented, thus allowing the instrumentation pass to treat calls to those
12# functions correctly.
13
14# Typical usage will list runtime libraries which are not instrumented by dfsan.
15# This would include libc, and compiler builtins.
16#
17# ./build-libc-list.py \
18#    --lib-file=/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 \
19#    --lib-file=/lib/x86_64-linux-gnu/libanl.so.1 \
20#    --lib-file=/lib/x86_64-linux-gnu/libBrokenLocale.so.1 \
21#    --lib-file=/lib/x86_64-linux-gnu/libcidn.so.1 \
22#    --lib-file=/lib/x86_64-linux-gnu/libcrypt.so.1 \
23#    --lib-file=/lib/x86_64-linux-gnu/libc.so.6 \
24#    --lib-file=/lib/x86_64-linux-gnu/libdl.so.2 \
25#    --lib-file=/lib/x86_64-linux-gnu/libm.so.6 \
26#    --lib-file=/lib/x86_64-linux-gnu/libnsl.so.1 \
27#    --lib-file=/lib/x86_64-linux-gnu/libpthread.so.0 \
28#    --lib-file=/lib/x86_64-linux-gnu/libresolv.so.2 \
29#    --lib-file=/lib/x86_64-linux-gnu/librt.so.1 \
30#    --lib-file=/lib/x86_64-linux-gnu/libthread_db.so.1 \
31#    --lib-file=/lib/x86_64-linux-gnu/libutil.so.1 \
32#    --lib-file=/usr/lib/x86_64-linux-gnu/libc_nonshared.a \
33#    --lib-file=/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a \
34#    --lib-file=/lib/x86_64-linux-gnu/libgcc_s.so.1 \
35#    --lib-file=/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.a \
36#    --error-missing-lib
37
38import os
39import subprocess
40import sys
41from optparse import OptionParser
42
43def defined_function_list(object):
44  functions = []
45  readelf_proc = subprocess.Popen(['readelf', '-s', '-W', object],
46                                  stdout=subprocess.PIPE)
47  readelf = readelf_proc.communicate()[0].split('\n')
48  if readelf_proc.returncode != 0:
49    raise subprocess.CalledProcessError(readelf_proc.returncode, 'readelf')
50  for line in readelf:
51    if (line[31:35] == 'FUNC' or line[31:36] == 'IFUNC') and \
52       line[39:44] != 'LOCAL' and \
53       line[55:58] != 'UND':
54      function_name = line[59:].split('@')[0]
55      functions.append(function_name)
56  return functions
57
58p = OptionParser()
59
60p.add_option('--lib-file', action='append', metavar='PATH',
61             help='Specific library files to add.',
62             default=[])
63
64p.add_option('--error-missing-lib', action='store_true',
65             help='Make this script exit with an error code if any library is missing.',
66             dest='error_missing_lib', default=False)
67
68(options, args) = p.parse_args()
69
70libs = options.lib_file
71if not libs:
72    print >> sys.stderr, 'No libraries provided.'
73    exit(1)
74
75missing_lib = False
76functions = []
77for l in libs:
78  if os.path.exists(l):
79    functions += defined_function_list(l)
80  else:
81    missing_lib = True
82    print >> sys.stderr, 'warning: library %s not found' % l
83
84if options.error_missing_lib and missing_lib:
85    print >> sys.stderr, 'Exiting with failure code due to missing library.'
86    exit(1)
87
88functions = list(set(functions))
89functions.sort()
90
91for f in functions:
92  print 'fun:%s=uninstrumented' % f
93