1*333d2b36SAndroid Build Coastguard Worker#!/bin/bash -e 2*333d2b36SAndroid Build Coastguard Worker 3*333d2b36SAndroid Build Coastguard Worker# Copyright 2020 Google Inc. All rights reserved. 4*333d2b36SAndroid Build Coastguard Worker# 5*333d2b36SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*333d2b36SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*333d2b36SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*333d2b36SAndroid Build Coastguard Worker# 9*333d2b36SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*333d2b36SAndroid Build Coastguard Worker# 11*333d2b36SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*333d2b36SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*333d2b36SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*333d2b36SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*333d2b36SAndroid Build Coastguard Worker# limitations under the License. 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Worker# Generates NDK API txt file used by Mainline modules. NDK APIs would have value 18*333d2b36SAndroid Build Coastguard Worker# "UND" in Ndx column and have suffix "@LIB_NAME" in Name column. 19*333d2b36SAndroid Build Coastguard Worker# For example, current line llvm-readelf output is: 20*333d2b36SAndroid Build Coastguard Worker# 1: 00000000 0 FUNC GLOBAL DEFAULT UND dlopen@LIBC 21*333d2b36SAndroid Build Coastguard Worker# After the parse function below "dlopen" would be write to the output file. 22*333d2b36SAndroid Build Coastguard Worker 23*333d2b36SAndroid Build Coastguard WorkerprintHelp() { 24*333d2b36SAndroid Build Coastguard Worker echo "**************************** Usage Instructions ****************************" 25*333d2b36SAndroid Build Coastguard Worker echo "This script is used to generate the Mainline modules used-by NDK symbols." 26*333d2b36SAndroid Build Coastguard Worker echo "" 27*333d2b36SAndroid Build Coastguard Worker echo "To run this script use: ./ndk_usedby_module.sh \$BINARY_IMAGE_DIRECTORY \$BINARY_LLVM_PATH \$OUTPUT_FILE_PATH" 28*333d2b36SAndroid Build Coastguard Worker 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:" 29*333d2b36SAndroid Build Coastguard Worker echo "./ndk_usedby_module.sh /myModule \$BINARY_LLVM_PATH /myModule.txt" 30*333d2b36SAndroid Build Coastguard Worker} 31*333d2b36SAndroid Build Coastguard Worker 32*333d2b36SAndroid Build Coastguard WorkerparseReadelfOutput() { 33*333d2b36SAndroid Build Coastguard Worker local readelfOutput=$1; shift 34*333d2b36SAndroid Build Coastguard Worker local ndkApisOutput=$1; shift 35*333d2b36SAndroid Build Coastguard Worker while IFS= read -r line 36*333d2b36SAndroid Build Coastguard Worker do 37*333d2b36SAndroid Build Coastguard Worker if [[ $line = *FUNC*GLOBAL*UND*@* ]] ; 38*333d2b36SAndroid Build Coastguard Worker then 39*333d2b36SAndroid Build Coastguard Worker echo "$line" | sed -r 's/.*UND (.*@.*)/\1/g' >> "${ndkApisOutput}" 40*333d2b36SAndroid Build Coastguard Worker fi 41*333d2b36SAndroid Build Coastguard Worker done < "${readelfOutput}" 42*333d2b36SAndroid Build Coastguard Worker echo "" >> "${ndkApisOutput}" 43*333d2b36SAndroid Build Coastguard Worker} 44*333d2b36SAndroid Build Coastguard Worker 45*333d2b36SAndroid Build Coastguard WorkerunzipJarAndApk() { 46*333d2b36SAndroid Build Coastguard Worker local dir="$1"; shift 47*333d2b36SAndroid Build Coastguard Worker local tmpUnzippedDir="$1"; shift 48*333d2b36SAndroid Build Coastguard Worker mkdir -p "${tmpUnzippedDir}" 49*333d2b36SAndroid Build Coastguard Worker find "$dir" -name "*.jar" -exec unzip -o {} -d "${tmpUnzippedDir}" \; 50*333d2b36SAndroid Build Coastguard Worker find "$dir" -name "*.apk" -exec unzip -o {} -d "${tmpUnzippedDir}" \; 51*333d2b36SAndroid Build Coastguard Worker find "${tmpUnzippedDir}" -name "*.MF" -exec rm {} \; 52*333d2b36SAndroid Build Coastguard Worker} 53*333d2b36SAndroid Build Coastguard Worker 54*333d2b36SAndroid Build Coastguard WorkerlookForExecFile() { 55*333d2b36SAndroid Build Coastguard Worker local dir="$1"; shift 56*333d2b36SAndroid Build Coastguard Worker local readelf="$1"; shift 57*333d2b36SAndroid Build Coastguard Worker local tmpOutput="$1"; shift 58*333d2b36SAndroid Build Coastguard Worker find -L "$dir" -type f -name "*.so" -exec "${readelf}" --dyn-symbols {} >> "${tmpOutput}" \; 59*333d2b36SAndroid Build Coastguard Worker find -L "$dir" -type f -perm /111 ! -name "*.so" -exec "${readelf}" --dyn-symbols {} >> "${tmpOutput}" \; 60*333d2b36SAndroid Build Coastguard Worker} 61*333d2b36SAndroid Build Coastguard Worker 62*333d2b36SAndroid Build Coastguard Workerif [[ "$1" == "help" ]] 63*333d2b36SAndroid Build Coastguard Workerthen 64*333d2b36SAndroid Build Coastguard Worker printHelp 65*333d2b36SAndroid Build Coastguard Workerelif [[ "$#" -ne 3 ]] 66*333d2b36SAndroid Build Coastguard Workerthen 67*333d2b36SAndroid Build Coastguard Worker echo "Wrong argument length. Expecting 3 argument representing image file directory, llvm-readelf tool path, output path." 68*333d2b36SAndroid Build Coastguard Workerelse 69*333d2b36SAndroid Build Coastguard Worker imageDir="$1"; shift 70*333d2b36SAndroid Build Coastguard Worker readelf="$1"; shift 71*333d2b36SAndroid Build Coastguard Worker outputFile="$1"; shift 72*333d2b36SAndroid Build Coastguard Worker 73*333d2b36SAndroid Build Coastguard Worker tmpReadelfOutput=$(mktemp /tmp/temporary-file.XXXXXXXX) 74*333d2b36SAndroid Build Coastguard Worker tmpUnzippedDir=$(mktemp -d /tmp/temporary-dir.XXXXXXXX) 75*333d2b36SAndroid Build Coastguard Worker trap 'rm -rf -- "${tmpReadelfOutput}" "${tmpUnzippedDir}"' EXIT 76*333d2b36SAndroid Build Coastguard Worker 77*333d2b36SAndroid Build Coastguard Worker # If there are any jars or apks, unzip them to surface native files. 78*333d2b36SAndroid Build Coastguard Worker unzipJarAndApk "${imageDir}" "${tmpUnzippedDir}" 79*333d2b36SAndroid Build Coastguard Worker # Analyze the unzipped files. 80*333d2b36SAndroid Build Coastguard Worker lookForExecFile "${tmpUnzippedDir}" "${readelf}" "${tmpReadelfOutput}" 81*333d2b36SAndroid Build Coastguard Worker 82*333d2b36SAndroid Build Coastguard Worker # Analyze the apex image staging dir itself. 83*333d2b36SAndroid Build Coastguard Worker lookForExecFile "${imageDir}" "${readelf}" "${tmpReadelfOutput}" 84*333d2b36SAndroid Build Coastguard Worker 85*333d2b36SAndroid Build Coastguard Worker [[ -e "${outputFile}" ]] && rm "${outputFile}" 86*333d2b36SAndroid Build Coastguard Worker parseReadelfOutput "${tmpReadelfOutput}" "${outputFile}" 87*333d2b36SAndroid Build Coastguard Workerfi 88