sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 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. |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 22 | |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 23 | printHelp() { |
| 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 | |
| 32 | parseReadelfOutput() { |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 33 | local readelfOutput=$1; shift |
| 34 | local ndkApisOutput=$1; shift |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 35 | while IFS= read -r line |
| 36 | do |
| 37 | if [[ $line = *FUNC*GLOBAL*UND*@* ]] ; |
| 38 | then |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 39 | echo "$line" | sed -r 's/.*UND (.*@.*)/\1/g' >> "${ndkApisOutput}" |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 40 | fi |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 41 | done < "${readelfOutput}" |
| 42 | echo "" >> "${ndkApisOutput}" |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | unzipJarAndApk() { |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 46 | 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 {} \; |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | lookForExecFile() { |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 55 | 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}" \; |
sophiez | c80a2b3 | 2020-11-12 16:39:19 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if [[ "$1" == "help" ]] |
| 63 | then |
| 64 | printHelp |
| 65 | elif [[ "$#" -ne 3 ]] |
| 66 | then |
| 67 | echo "Wrong argument length. Expecting 3 argument representing image file directory, llvm-readelf tool path, output path." |
| 68 | else |
Jingwen Chen | 2d61a7d | 2022-11-03 10:38:12 +0000 | [diff] [blame] | 69 | 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}" |
| 87 | fi |