| 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: | 
|  | 20 | #   CROSS_COMPILE: prefix added to readelf tool | 
|  | 21 | #  Arguments: | 
|  | 22 | #   -i ${file}: input file (required) | 
|  | 23 | #   -o ${file}: output file (required) | 
|  | 24 | #   -d ${file}: deps file (required) | 
|  | 25 |  | 
|  | 26 | OPTSTRING=d:i:o:-: | 
|  | 27 |  | 
|  | 28 | usage() { | 
|  | 29 | cat <<EOF | 
|  | 30 | Usage: toc.sh [options] -i in-file -o out-file -d deps-file | 
|  | 31 | Options: | 
|  | 32 | EOF | 
|  | 33 | exit 1 | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | do_elf() { | 
|  | 37 | ("${CROSS_COMPILE}readelf" -d "${infile}" | grep SONAME || echo "No SONAME for ${infile}") > "${outfile}.tmp" | 
|  | 38 | "${CROSS_COMPILE}readelf" --dyn-syms "${infile}" | awk '{$2=""; $3=""; print}' >> "${outfile}.tmp" | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | do_macho() { | 
| Dan Willemsen | fdb20d0 | 2018-06-03 16:13:40 -0700 | [diff] [blame] | 42 | "${CROSS_COMPILE}/otool" -l "${infile}" | grep LC_ID_DYLIB -A 5 > "${outfile}.tmp" | 
|  | 43 | "${CROSS_COMPILE}/nm" -gP "${infile}" | cut -f1-2 -d" " | grep -v 'U$' >> "${outfile}.tmp" | 
| Colin Cross | 26c34ed | 2016-09-30 17:10:16 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
|  | 46 |  | 
|  | 47 | while getopts $OPTSTRING opt; do | 
|  | 48 | case "$opt" in | 
|  | 49 | d) depsfile="${OPTARG}" ;; | 
|  | 50 | i) infile="${OPTARG}" ;; | 
|  | 51 | o) outfile="${OPTARG}" ;; | 
|  | 52 | -) | 
|  | 53 | case "${OPTARG}" in | 
|  | 54 | *) echo "Unknown option --${OPTARG}"; usage ;; | 
|  | 55 | esac;; | 
|  | 56 | ?) usage ;; | 
|  | 57 | *) echo "'${opt}' '${OPTARG}'" | 
|  | 58 | esac | 
|  | 59 | done | 
|  | 60 |  | 
|  | 61 | if [ -z "${infile}" ]; then | 
|  | 62 | echo "-i argument is required" | 
|  | 63 | usage | 
|  | 64 | fi | 
|  | 65 |  | 
|  | 66 | if [ -z "${outfile}" ]; then | 
|  | 67 | echo "-o argument is required" | 
|  | 68 | usage | 
|  | 69 | fi | 
|  | 70 |  | 
|  | 71 | if [ -z "${depsfile}" ]; then | 
|  | 72 | echo "-d argument is required" | 
|  | 73 | usage | 
|  | 74 | fi | 
|  | 75 |  | 
|  | 76 | rm -f "${outfile}.tmp" | 
|  | 77 |  | 
|  | 78 | cat <<EOF > "${depsfile}" | 
|  | 79 | ${outfile}: \\ | 
|  | 80 | ${CROSS_COMPILE}readelf \\ | 
|  | 81 | EOF | 
|  | 82 |  | 
|  | 83 | do_elf | 
|  | 84 |  | 
|  | 85 | if cmp "${outfile}" "${outfile}.tmp" > /dev/null 2> /dev/null; then | 
|  | 86 | rm -f "${outfile}.tmp" | 
|  | 87 | else | 
|  | 88 | mv -f "${outfile}.tmp" "${outfile}" | 
|  | 89 | fi |