blob: 93d13706b7c56f8e6afa4ff3f8c2b5024680e504 [file] [log] [blame]
sophiezc80a2b32020-11-12 16:39:19 +00001#!/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.
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000022
sophiezc80a2b32020-11-12 16:39:19 +000023printHelp() {
24 echo "**************************** Usage Instructions ****************************"
25 echo "This script is used to generate the Mainline modules used-by NDK symbols."
26 echo ""
27 echo "To run this script use: ./ndk_usedby_module.sh \$BINARY_IMAGE_DIRECTORY \$BINARY_LLVM_PATH \$OUTPUT_FILE_PATH"
28 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 echo "./ndk_usedby_module.sh /myModule \$BINARY_LLVM_PATH /myModule.txt"
30}
31
32parseReadelfOutput() {
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000033 local readelfOutput=$1; shift
34 local ndkApisOutput=$1; shift
sophiezc80a2b32020-11-12 16:39:19 +000035 while IFS= read -r line
36 do
37 if [[ $line = *FUNC*GLOBAL*UND*@* ]] ;
38 then
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000039 echo "$line" | sed -r 's/.*UND (.*@.*)/\1/g' >> "${ndkApisOutput}"
sophiezc80a2b32020-11-12 16:39:19 +000040 fi
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000041 done < "${readelfOutput}"
42 echo "" >> "${ndkApisOutput}"
sophiezc80a2b32020-11-12 16:39:19 +000043}
44
45unzipJarAndApk() {
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000046 local dir="$1"; shift
47 local tmpUnzippedDir="$1"; shift
48 mkdir -p "${tmpUnzippedDir}"
49 find "$dir" -name "*.jar" -exec unzip -o {} -d "${tmpUnzippedDir}" \;
50 find "$dir" -name "*.apk" -exec unzip -o {} -d "${tmpUnzippedDir}" \;
51 find "${tmpUnzippedDir}" -name "*.MF" -exec rm {} \;
sophiezc80a2b32020-11-12 16:39:19 +000052}
53
54lookForExecFile() {
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000055 local dir="$1"; shift
56 local readelf="$1"; shift
57 local tmpOutput="$1"; shift
58 find -L "$dir" -type f -name "*.so" -exec "${readelf}" --dyn-symbols {} >> "${tmpOutput}" \;
59 find -L "$dir" -type f -perm /111 ! -name "*.so" -exec "${readelf}" --dyn-symbols {} >> "${tmpOutput}" \;
sophiezc80a2b32020-11-12 16:39:19 +000060}
61
62if [[ "$1" == "help" ]]
63then
64 printHelp
65elif [[ "$#" -ne 3 ]]
66then
67 echo "Wrong argument length. Expecting 3 argument representing image file directory, llvm-readelf tool path, output path."
68else
Jingwen Chen2d61a7d2022-11-03 10:38:12 +000069 imageDir="$1"; shift
70 readelf="$1"; shift
71 outputFile="$1"; shift
72
73 tmpReadelfOutput=$(mktemp /tmp/temporary-file.XXXXXXXX)
74 tmpUnzippedDir=$(mktemp -d /tmp/temporary-dir.XXXXXXXX)
75 trap 'rm -rf -- "${tmpReadelfOutput}" "${tmpUnzippedDir}"' EXIT
76
77 # If there are any jars or apks, unzip them to surface native files.
78 unzipJarAndApk "${imageDir}" "${tmpUnzippedDir}"
79 # Analyze the unzipped files.
80 lookForExecFile "${tmpUnzippedDir}" "${readelf}" "${tmpReadelfOutput}"
81
82 # Analyze the apex image staging dir itself.
83 lookForExecFile "${imageDir}" "${readelf}" "${tmpReadelfOutput}"
84
85 [[ -e "${outputFile}" ]] && rm "${outputFile}"
86 parseReadelfOutput "${tmpReadelfOutput}" "${outputFile}"
87fi