blob: c6b78668c68175fce819552d0bebee963dc143fb [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:
Yi Kong4ad44e72021-04-01 17:45:42 +080020# CLANG_BIN: path to the clang bin directory
Colin Cross26c34ed2016-09-30 17:10:16 -070021# Arguments:
22# -i ${file}: input file (required)
23# -o ${file}: output file (required)
24# -d ${file}: deps file (required)
Colin Crossb496cfd2018-09-10 16:50:05 -070025# --elf | --macho | --pe: format (required)
Colin Cross26c34ed2016-09-30 17:10:16 -070026
27OPTSTRING=d:i:o:-:
28
29usage() {
30 cat <<EOF
31Usage: toc.sh [options] -i in-file -o out-file -d deps-file
32Options:
33EOF
34 exit 1
35}
36
37do_elf() {
Yi Kong4ad44e72021-04-01 17:45:42 +080038 ("${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 Crossb496cfd2018-09-10 16:50:05 -070040
41 cat <<EOF > "${depsfile}"
42${outfile}: \\
Yi Kong4ad44e72021-04-01 17:45:42 +080043 ${CLANG_BIN}/llvm-readelf \\
Colin Crossb496cfd2018-09-10 16:50:05 -070044EOF
Colin Cross26c34ed2016-09-30 17:10:16 -070045}
46
47do_macho() {
Yi Kong4ad44e72021-04-01 17:45:42 +080048 "${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 Crossb496cfd2018-09-10 16:50:05 -070050
51 cat <<EOF > "${depsfile}"
52${outfile}: \\
Yi Kong4ad44e72021-04-01 17:45:42 +080053 ${CLANG_BIN}/llvm-objdump \\
54 ${CLANG_BIN}/llvm-nm \\
Colin Crossb496cfd2018-09-10 16:50:05 -070055EOF
Colin Cross26c34ed2016-09-30 17:10:16 -070056}
57
Colin Crossb496cfd2018-09-10 16:50:05 -070058do_pe() {
Yi Kong4ad44e72021-04-01 17:45:42 +080059 "${CLANG_BIN}/llvm-objdump" -x "${infile}" | grep "^Name" | cut -f3 -d" " > "${outfile}.tmp"
Yi Kong6f43f542021-04-03 03:20:12 +080060 "${CLANG_BIN}/llvm-nm" -gP "${infile}" | cut -f1-2 -d" " >> "${outfile}.tmp"
Colin Crossb496cfd2018-09-10 16:50:05 -070061
62 cat <<EOF > "${depsfile}"
63${outfile}: \\
Yi Kong4ad44e72021-04-01 17:45:42 +080064 ${CLANG_BIN}/llvm-objdump \\
65 ${CLANG_BIN}/llvm-nm \\
Colin Crossb496cfd2018-09-10 16:50:05 -070066EOF
67}
Colin Cross26c34ed2016-09-30 17:10:16 -070068
69while 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 Crossb496cfd2018-09-10 16:50:05 -070076 elf) elf=1 ;;
77 macho) macho=1 ;;
78 pe) pe=1 ;;
Colin Cross26c34ed2016-09-30 17:10:16 -070079 *) echo "Unknown option --${OPTARG}"; usage ;;
80 esac;;
81 ?) usage ;;
82 *) echo "'${opt}' '${OPTARG}'"
83 esac
84done
85
Colin Crossb496cfd2018-09-10 16:50:05 -070086if [ -z "${infile:-}" ]; then
Colin Cross26c34ed2016-09-30 17:10:16 -070087 echo "-i argument is required"
88 usage
89fi
90
Colin Crossb496cfd2018-09-10 16:50:05 -070091if [ -z "${outfile:-}" ]; then
Colin Cross26c34ed2016-09-30 17:10:16 -070092 echo "-o argument is required"
93 usage
94fi
95
Colin Crossb496cfd2018-09-10 16:50:05 -070096if [ -z "${depsfile:-}" ]; then
Colin Cross26c34ed2016-09-30 17:10:16 -070097 echo "-d argument is required"
98 usage
99fi
100
Yi Kong4ad44e72021-04-01 17:45:42 +0800101if [ -z "${CLANG_BIN:-}" ]; then
102 echo "CLANG_BIN environment variable must be set"
Colin Crossb496cfd2018-09-10 16:50:05 -0700103 usage
104fi
105
Colin Cross26c34ed2016-09-30 17:10:16 -0700106rm -f "${outfile}.tmp"
107
108cat <<EOF > "${depsfile}"
109${outfile}: \\
Yi Kong4ad44e72021-04-01 17:45:42 +0800110 ${CLANG_BIN}/llvm-readelf \\
Colin Cross26c34ed2016-09-30 17:10:16 -0700111EOF
112
Colin Crossb496cfd2018-09-10 16:50:05 -0700113if [ -n "${elf:-}" ]; then
114 do_elf
115elif [ -n "${macho:-}" ]; then
116 do_macho
117elif [ -n "${pe:-}" ]; then
118 do_pe
119else
120 echo "--elf, --macho or --pe is required"; usage
121fi
Colin Cross26c34ed2016-09-30 17:10:16 -0700122
123if cmp "${outfile}" "${outfile}.tmp" > /dev/null 2> /dev/null; then
124 rm -f "${outfile}.tmp"
125else
126 mv -f "${outfile}.tmp" "${outfile}"
127fi