1 #!/bin/sh 2 # 3 # Copyright (C) 2011 The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 # gen-system-symbols.sh 18 # 19 # This tool is used to read the shared library from a source directory 20 # (SRC) and extract the list of functions and variables. 21 # 22 # Then, for each library, it will generate in (DST) two text files 23 # named <library>.functions.txt and <library>.variables.txt 24 # 25 26 # Only runs on Linux because it requires the "readelf" utility 27 # 28 29 . `dirname $0`/prebuilt-common.sh 30 31 VERBOSE=no 32 VERBOSE2=no 33 34 PROGRAM_PARAMETERS="<src-dir> <dst-dir>" 35 PROGRAM_DESCRIPTION=\ 36 "This program is used to parse all shared libraries in <src-dir> 37 and extract, for each one of them, the list of functions and variables 38 that it exports. 39 40 For some of these libraries, it will remove symbols that are not meant 41 to be imported (unless you use --no-symbol-filtering) 42 43 These lists will then be saved into two files: 44 45 <dst-dir>/<libname>.functions.txt 46 <dst-dir>/<libname>.variables.txt 47 " 48 49 NO_FILTERING= 50 register_var_option "--no-symbol-filtering" NO_FILTERING "Disable symbol filtering" 51 52 extract_parameters "$@" 53 54 parse_params () 55 { 56 SRCDIR=$1 57 DSTDIR=$2 58 59 if [ -z "$SRCDIR" ]; then 60 dump "ERROR: Missing first parameter (source directory path), see --help" 61 exit 1 62 fi 63 64 if [ -z "$DSTDIR" ]; then 65 dump "ERROR: Missing second parameter (destination directory path), see --help" 66 exit 1 67 fi 68 69 if [ ! -d "$SRCDIR" ]; then 70 dump "ERROR: Not a source directory: $SRCDIR" 71 exit 1 72 fi 73 74 mkdir -p $DSTDIR 75 fail_panic "Could not create destination directory: $DSTDIR" 76 } 77 78 parse_params $PARAMETERS 79 80 READELF=readelf 81 82 # $1: shared library path 83 get_library_functions () 84 { 85 $READELF -s -D -W $1 | awk '$5 ~ /FUNC/ && $6 ~ /GLOBAL|WEAK/ && $8 !~ /UND/ { print $9; }' | sort -u 86 } 87 88 # $1: shared library path 89 get_library_variables () 90 { 91 $READELF -s -D -W $1 | awk '$5 ~ /OBJECT/ && $6 ~ /GLOBAL|WEAK/ && $8 !~ /UND/ { print $9; }' | sort -u 92 } 93 94 # Temp file used to list shared library symbol exclusions 95 # See set_symbol_excludes and extract_shared_library_xxxx functions below 96 SYMBOL_EXCLUDES=/tmp/ndk-$USER/ndk-symbol-excludes.txt 97 98 # Temp file used to list shared library symbol inclusions, these 99 # are essentially overrides to the content of SYMBOL_EXCLUDES 100 SYMBOL_INCLUDES=/tmp/ndk-$USER/ndk-symbol-includes.txt 101 102 # Temp file used to filter symbols 103 SYMBOL_TMPFILE=/tmp/ndk-$USER/ndk-symbols-list.txt 104 105 # Reset the symbol exclusion list to its default 106 reset_symbol_excludes () 107 { 108 # By default, do not export C++ mangled symbol, which all start with _Z 109 echo '^_Z' > $SYMBOL_EXCLUDES 110 111 # __INIT_ARRAY__ and __FINI_ARRAY__ are special symbols that should 112 # normally be hidden. 113 echo "^__INIT_ARRAY__" >> $SYMBOL_EXCLUDES 114 echo "^__FINI_ARRAY__" >> $SYMBOL_EXCLUDES 115 > $SYMBOL_INCLUDES 116 } 117 118 # Add new exclusion patterns to SYMBOL_EXCLUDES 119 set_symbol_excludes () 120 { 121 (echo "$@" | tr ' ' '\n') >> $SYMBOL_EXCLUDES 122 } 123 124 # Add new inclusion patterns to SYMBOL_INCLUDES 125 set_symbol_includes () 126 { 127 (echo "$@" | tr ' ' '\n') >> $SYMBOL_INCLUDES 128 } 129 130 # Clear symbol exclusion/inclusion files 131 clear_symbol_excludes () 132 { 133 rm -f $SYMBOL_EXCLUDES $SYMBOL_INCLUDES 134 } 135 136 # Filter the list of symbols from a file 137 # $1: path to symbol list file 138 filter_symbols () 139 { 140 (grep -v -f $SYMBOL_EXCLUDES $1 ; grep -f $SYMBOL_INCLUDES $1) | sort -u 141 } 142 143 # $1: Library name 144 # $2+: List of symbols (functions or variables) 145 # Out: sorted list of filtered symbols, based on library name 146 filter_library_symbols () 147 { 148 local LIB=$1 149 shift 150 local SYMBOLS="$@" 151 (echo "$SYMBOLS" | tr ' ' '\n' | sort -u) > $SYMBOL_TMPFILE 152 153 reset_symbol_excludes 154 155 case $LIB in 156 libc.so) 157 # Remove a few internal symbols that should not be exposed 158 # from the C library (we plan to clean that up soon by using the 159 # "hidden" visibility attribute in the near future). 160 # 161 set_symbol_excludes \ 162 '^the_' '^dns_' 'load_domain_search_list' 'res_get_dns_changed' \ 163 '^_resolv_cache' '^_dns_getht' '^_thread_atexit' \ 164 '^free_malloc_leak_info' 'fake_gmtime_r' 'fake_localtime_r' \ 165 '^gAllocationsMutex' '^gHashTable' '^gMallocLeakZygoteChild' 166 ;; 167 libstdc++.so) 168 # This is the only library that is allowed to export C++ symbols for now. 169 set_symbol_includes '^_Z.*' 170 ;; 171 liblog.so) 172 set_symbol_excludes '^.*' # exclude everything 173 set_symbol_includes '^__android_' # except __android_xxxx functions 174 ;; 175 libOpenSLES.so) 176 set_symbol_excludes '^_' '^MPH_' # remove MPH_to_xxx definitions 177 ;; 178 libGLESv*.so) 179 # Exclude non-OES extension entry points 180 set_symbol_excludes 'EXT$' 181 set_symbol_excludes 'AMD$' 182 set_symbol_excludes 'ANGLE$' 183 set_symbol_excludes 'APPLE$' 184 set_symbol_excludes 'IMG$' 185 set_symbol_excludes 'NV$' 186 set_symbol_excludes 'QCOM$' 187 ;; 188 esac 189 filter_symbols "$SYMBOL_TMPFILE" 190 } 191 192 for LIB in $(cd $SRCDIR && ls lib*.so); do 193 SRCLIB=$SRCDIR/$LIB 194 log "Extracting symbols from $LIB" 195 FUNCS=$(get_library_functions $SRCLIB) 196 VARS=$(get_library_variables $SRCLIB) 197 if [ -z "$NO_FILTERING" ]; then 198 FUNCS=$(filter_library_symbols $LIB $FUNCS) 199 VARS=$(filter_library_symbols $LIB $VARS) 200 fi 201 NUMFUNCS=$(echo $FUNCS | wc -w) 202 NUMVARS=$(echo $VARS | wc -w) 203 log " Found $NUMFUNCS functions and $NUMVARS variables" 204 (echo "$FUNCS" | tr ' ' '\n') > $DSTDIR/$LIB.functions.txt 205 (echo "$VARS" | tr ' ' '\n') > $DSTDIR/$LIB.variables.txt 206 done 207