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