blob: bd6425be1312dd6d05c34c962d47ac7625d89913 [file] [log] [blame]
Colin Cross26c34ed2016-09-30 17:10:16 -07001#!/bin/bash -eu
2
Colin Crossd00350c2017-11-17 10:55:38 -08003# 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 Cross26c34ed2016-09-30 17:10:16 -070017# 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
26OPTSTRING=d:i:o:-:
27
28usage() {
29 cat <<EOF
30Usage: toc.sh [options] -i in-file -o out-file -d deps-file
31Options:
32EOF
33 exit 1
34}
35
36do_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
41do_macho() {
Dan Willemsenfdb20d02018-06-03 16:13:40 -070042 "${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 Cross26c34ed2016-09-30 17:10:16 -070044}
45
46
47while 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
59done
60
61if [ -z "${infile}" ]; then
62 echo "-i argument is required"
63 usage
64fi
65
66if [ -z "${outfile}" ]; then
67 echo "-o argument is required"
68 usage
69fi
70
71if [ -z "${depsfile}" ]; then
72 echo "-d argument is required"
73 usage
74fi
75
76rm -f "${outfile}.tmp"
77
78cat <<EOF > "${depsfile}"
79${outfile}: \\
80 ${CROSS_COMPILE}readelf \\
81EOF
82
83do_elf
84
85if cmp "${outfile}" "${outfile}.tmp" > /dev/null 2> /dev/null; then
86 rm -f "${outfile}.tmp"
87else
88 mv -f "${outfile}.tmp" "${outfile}"
89fi