toc.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash -eu
  2. # Copyright 2017 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. # Script to handle generating a .toc file from a .so file
  16. # Inputs:
  17. # Environment:
  18. # CLANG_BIN: path to the clang bin directory
  19. # Arguments:
  20. # -i ${file}: input file (required)
  21. # -o ${file}: output file (required)
  22. # -d ${file}: deps file (required)
  23. # --elf | --macho | --pe: format (required)
  24. OPTSTRING=d:i:o:-:
  25. usage() {
  26. cat <<EOF
  27. Usage: toc.sh [options] -i in-file -o out-file -d deps-file
  28. Options:
  29. EOF
  30. exit 1
  31. }
  32. do_elf() {
  33. ("${CLANG_BIN}/llvm-readelf" -d "${infile}" | grep SONAME || echo "No SONAME for ${infile}") > "${outfile}.tmp"
  34. "${CLANG_BIN}/llvm-readelf" --dyn-syms "${infile}" | awk '{$2=""; $3=""; print}' >> "${outfile}.tmp"
  35. cat <<EOF > "${depsfile}"
  36. ${outfile}: \\
  37. ${CLANG_BIN}/llvm-readelf \\
  38. EOF
  39. }
  40. do_macho() {
  41. "${CLANG_BIN}/llvm-objdump" -p "${infile}" | grep LC_ID_DYLIB -A 5 > "${outfile}.tmp"
  42. "${CLANG_BIN}/llvm-nm" -gP "${infile}" | cut -f1-2 -d" " | (grep -v 'U$' >> "${outfile}.tmp" || true)
  43. cat <<EOF > "${depsfile}"
  44. ${outfile}: \\
  45. ${CLANG_BIN}/llvm-objdump \\
  46. ${CLANG_BIN}/llvm-nm \\
  47. EOF
  48. }
  49. do_pe() {
  50. "${CLANG_BIN}/llvm-objdump" -x "${infile}" | grep "^Name" | cut -f3 -d" " > "${outfile}.tmp"
  51. "${CLANG_BIN}/llvm-nm" -gP "${infile}" | cut -f1-2 -d" " >> "${outfile}.tmp"
  52. cat <<EOF > "${depsfile}"
  53. ${outfile}: \\
  54. ${CLANG_BIN}/llvm-objdump \\
  55. ${CLANG_BIN}/llvm-nm \\
  56. EOF
  57. }
  58. while getopts $OPTSTRING opt; do
  59. case "$opt" in
  60. d) depsfile="${OPTARG}" ;;
  61. i) infile="${OPTARG}" ;;
  62. o) outfile="${OPTARG}" ;;
  63. -)
  64. case "${OPTARG}" in
  65. elf) elf=1 ;;
  66. macho) macho=1 ;;
  67. pe) pe=1 ;;
  68. *) echo "Unknown option --${OPTARG}"; usage ;;
  69. esac;;
  70. ?) usage ;;
  71. *) echo "'${opt}' '${OPTARG}'"
  72. esac
  73. done
  74. if [ -z "${infile:-}" ]; then
  75. echo "-i argument is required"
  76. usage
  77. fi
  78. if [ -z "${outfile:-}" ]; then
  79. echo "-o argument is required"
  80. usage
  81. fi
  82. if [ -z "${depsfile:-}" ]; then
  83. echo "-d argument is required"
  84. usage
  85. fi
  86. if [ -z "${CLANG_BIN:-}" ]; then
  87. echo "CLANG_BIN environment variable must be set"
  88. usage
  89. fi
  90. rm -f "${outfile}.tmp"
  91. cat <<EOF > "${depsfile}"
  92. ${outfile}: \\
  93. ${CLANG_BIN}/llvm-readelf \\
  94. EOF
  95. if [ -n "${elf:-}" ]; then
  96. do_elf
  97. elif [ -n "${macho:-}" ]; then
  98. do_macho
  99. elif [ -n "${pe:-}" ]; then
  100. do_pe
  101. else
  102. echo "--elf, --macho or --pe is required"; usage
  103. fi
  104. if cmp "${outfile}" "${outfile}.tmp" > /dev/null 2> /dev/null; then
  105. rm -f "${outfile}.tmp"
  106. else
  107. mv -f "${outfile}.tmp" "${outfile}"
  108. fi