File: build-libc-list.py

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (92 lines) | stat: -rwxr-xr-x 3,444 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
#===- lib/dfsan/scripts/build-libc-list.py ---------------------------------===#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===------------------------------------------------------------------------===#
# The purpose of this script is to identify every function symbol in a set of
# libraries (in this case, libc and libgcc) so that they can be marked as
# uninstrumented, thus allowing the instrumentation pass to treat calls to those
# functions correctly.

# Typical usage will list runtime libraries which are not instrumented by dfsan.
# This would include libc, and compiler builtins.
#
# ./build-libc-list.py \
#    --lib-file=/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 \
#    --lib-file=/lib/x86_64-linux-gnu/libanl.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libBrokenLocale.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libcidn.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libcrypt.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libc.so.6 \
#    --lib-file=/lib/x86_64-linux-gnu/libdl.so.2 \
#    --lib-file=/lib/x86_64-linux-gnu/libm.so.6 \
#    --lib-file=/lib/x86_64-linux-gnu/libnsl.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libpthread.so.0 \
#    --lib-file=/lib/x86_64-linux-gnu/libresolv.so.2 \
#    --lib-file=/lib/x86_64-linux-gnu/librt.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libthread_db.so.1 \
#    --lib-file=/lib/x86_64-linux-gnu/libutil.so.1 \
#    --lib-file=/usr/lib/x86_64-linux-gnu/libc_nonshared.a \
#    --lib-file=/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a \
#    --lib-file=/lib/x86_64-linux-gnu/libgcc_s.so.1 \
#    --lib-file=/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.a \
#    --error-missing-lib

import os
import subprocess
import sys
from optparse import OptionParser

def defined_function_list(object):
  functions = []
  readelf_proc = subprocess.Popen(['readelf', '-s', '-W', object],
                                  stdout=subprocess.PIPE)
  readelf = readelf_proc.communicate()[0].split('\n')
  if readelf_proc.returncode != 0:
    raise subprocess.CalledProcessError(readelf_proc.returncode, 'readelf')
  for line in readelf:
    if (line[31:35] == 'FUNC' or line[31:36] == 'IFUNC') and \
       line[39:44] != 'LOCAL' and \
       line[55:58] != 'UND':
      function_name = line[59:].split('@')[0]
      functions.append(function_name)
  return functions

p = OptionParser()

p.add_option('--lib-file', action='append', metavar='PATH',
             help='Specific library files to add.',
             default=[])

p.add_option('--error-missing-lib', action='store_true',
             help='Make this script exit with an error code if any library is missing.',
             dest='error_missing_lib', default=False)

(options, args) = p.parse_args()

libs = options.lib_file
if not libs:
    print >> sys.stderr, 'No libraries provided.'
    exit(1)

missing_lib = False
functions = []
for l in libs:
  if os.path.exists(l):
    functions += defined_function_list(l)
  else:
    missing_lib = True
    print >> sys.stderr, 'warning: library %s not found' % l

if options.error_missing_lib and missing_lib:
    print >> sys.stderr, 'Exiting with failure code due to missing library.'
    exit(1)

functions = list(set(functions))
functions.sort()

for f in functions:
  print 'fun:%s=uninstrumented' % f