1#!/bin/bash -e 2 3# Copyright 2020 Google Inc. All rights reserved. 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# Generates NDK API txt file used by Mainline modules. NDK APIs would have value 18# "UND" in Ndx column and have suffix "@LIB_NAME" in Name column. 19# For example, current line llvm-readelf output is: 20# 1: 00000000 0 FUNC GLOBAL DEFAULT UND dlopen@LIBC 21# After the parse function below "dlopen" would be write to the output file. 22printHelp() { 23 echo "**************************** Usage Instructions ****************************" 24 echo "This script is used to generate the Mainline modules used-by NDK symbols." 25 echo "" 26 echo "To run this script use: ./ndk_usedby_module.sh \$BINARY_IMAGE_DIRECTORY \$BINARY_LLVM_PATH \$OUTPUT_FILE_PATH" 27 echo "For example: If all the module image files that you would like to run is under directory '/myModule' and output write to /myModule.txt then the command would be:" 28 echo "./ndk_usedby_module.sh /myModule \$BINARY_LLVM_PATH /myModule.txt" 29} 30 31parseReadelfOutput() { 32 while IFS= read -r line 33 do 34 if [[ $line = *FUNC*GLOBAL*UND*@* ]] ; 35 then 36 echo "$line" | sed -r 's/.*UND (.*@.*)/\1/g' >> "$2" 37 fi 38 done < "$1" 39 echo "" >> "$2" 40} 41 42unzipJarAndApk() { 43 tmpUnzippedDir="$1"/tmpUnzipped 44 [[ -e "$tmpUnzippedDir" ]] && rm -rf "$tmpUnzippedDir" 45 mkdir -p "$tmpUnzippedDir" 46 find "$1" -name "*.jar" -exec unzip -o {} -d "$tmpUnzippedDir" \; 47 find "$1" -name "*.apk" -exec unzip -o {} -d "$tmpUnzippedDir" \; 48 find "$tmpUnzippedDir" -name "*.MF" -exec rm {} \; 49} 50 51lookForExecFile() { 52 dir="$1" 53 readelf="$2" 54 find "$dir" -type f -name "*.so" -exec "$2" --dyn-symbols {} >> "$dir"/../tmpReadelf.txt \; 55 find "$dir" -type f -perm /111 ! -name "*.so" -exec "$2" --dyn-symbols {} >> "$dir"/../tmpReadelf.txt \; 56} 57 58if [[ "$1" == "help" ]] 59then 60 printHelp 61elif [[ "$#" -ne 3 ]] 62then 63 echo "Wrong argument length. Expecting 3 argument representing image file directory, llvm-readelf tool path, output path." 64else 65 unzipJarAndApk "$1" 66 lookForExecFile "$1" "$2" 67 tmpReadelfOutput="$1/../tmpReadelf.txt" 68 [[ -e "$3" ]] && rm "$3" 69 parseReadelfOutput "$tmpReadelfOutput" "$3" 70 [[ -e "$tmpReadelfOutput" ]] && rm "$tmpReadelfOutput" 71 rm -rf "$1/tmpUnzipped" 72fi