2 # ===- lib/dfsan/scripts/build-libc-list.py ---------------------------------===#
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
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.
14 # Typical usage will list runtime libraries which are not instrumented by dfsan.
15 # This would include libc, and compiler builtins.
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 \
41 from optparse
import OptionParser
44 def defined_function_list(lib
):
45 """Get non-local function symbols from lib."""
47 readelf_proc
= subprocess
.Popen(
48 ["readelf", "-s", "-W", lib
], stdout
=subprocess
.PIPE
50 readelf
= readelf_proc
.communicate()[0].decode().split("\n")
51 if readelf_proc
.returncode
!= 0:
52 raise subprocess
.CalledProcessError(readelf_proc
.returncode
, "readelf")
55 (line
[31:35] == "FUNC" or line
[31:36] == "IFUNC")
56 and line
[39:44] != "LOCAL"
57 and line
[55:58] != "UND"
59 function_name
= line
[59:].split("@")[0]
60 functions
.append(function_name
)
70 help="Specific library files to add.",
75 "--error-missing-lib",
77 help="Make this script exit with an error code if any library is missing.",
78 dest
="error_missing_lib",
82 (options
, args
) = p
.parse_args()
84 libs
= options
.lib_file
86 print("No libraries provided.", file=sys
.stderr
)
93 functions
+= defined_function_list(l
)
96 print("warning: library %s not found" % l
, file=sys
.stderr
)
98 if options
.error_missing_lib
and missing_lib
:
99 print("Exiting with failure code due to missing library.", file=sys
.stderr
)
102 for f
in sorted(set(functions
)):
103 print("fun:%s=uninstrumented" % f
)