104 lines · python
1#!/usr/bin/env python32# ===- 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-exception7#8# ===------------------------------------------------------------------------===#9# The purpose of this script is to identify every function symbol in a set of10# libraries (in this case, libc and libgcc) so that they can be marked as11# uninstrumented, thus allowing the instrumentation pass to treat calls to those12# 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-lib37 38import os39import subprocess40import sys41from optparse import OptionParser42 43 44def defined_function_list(lib):45 """Get non-local function symbols from lib."""46 functions = []47 readelf_proc = subprocess.Popen(48 ["readelf", "-s", "-W", lib], stdout=subprocess.PIPE49 )50 readelf = readelf_proc.communicate()[0].decode().split("\n")51 if readelf_proc.returncode != 0:52 raise subprocess.CalledProcessError(readelf_proc.returncode, "readelf")53 for line in readelf:54 if (55 (line[31:35] == "FUNC" or line[31:36] == "IFUNC")56 and line[39:44] != "LOCAL"57 and line[55:58] != "UND"58 ):59 function_name = line[59:].split("@")[0]60 functions.append(function_name)61 return functions62 63 64p = OptionParser()65 66p.add_option(67 "--lib-file",68 action="append",69 metavar="PATH",70 help="Specific library files to add.",71 default=[],72)73 74p.add_option(75 "--error-missing-lib",76 action="store_true",77 help="Make this script exit with an error code if any library is missing.",78 dest="error_missing_lib",79 default=False,80)81 82(options, args) = p.parse_args()83 84libs = options.lib_file85if not libs:86 print("No libraries provided.", file=sys.stderr)87 exit(1)88 89missing_lib = False90functions = []91for l in libs:92 if os.path.exists(l):93 functions += defined_function_list(l)94 else:95 missing_lib = True96 print("warning: library %s not found" % l, file=sys.stderr)97 98if options.error_missing_lib and missing_lib:99 print("Exiting with failure code due to missing library.", file=sys.stderr)100 exit(1)101 102for f in sorted(set(functions)):103 print("fun:%s=uninstrumented" % f)104