gen_ndk_usedby_apex.sh 3.3 KB

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