• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -eu
2
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
17# Script to handle generating a .toc file from a .so file
18# Inputs:
19#  Environment:
20#   CLANG_BIN: path to the clang bin directory
21#  Arguments:
22#   -i ${file}: input file (required)
23#   -o ${file}: output file (required)
24#   -d ${file}: deps file (required)
25#   --elf | --macho | --pe: format (required)
26
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() {
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"
40
41    cat <<EOF > "${depsfile}"
42${outfile}: \\
43  ${CLANG_BIN}/llvm-readelf \\
44EOF
45}
46
47do_macho() {
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)
50
51    cat <<EOF > "${depsfile}"
52${outfile}: \\
53  ${CLANG_BIN}/llvm-objdump \\
54  ${CLANG_BIN}/llvm-nm \\
55EOF
56}
57
58do_pe() {
59    "${CLANG_BIN}/llvm-objdump" -x "${infile}" | grep "^Name" | cut -f3 -d" " > "${outfile}.tmp"
60    "${CLANG_BIN}/llvm-nm" -gP "${infile}" | cut -f1-2 -d" " >> "${outfile}.tmp"
61
62    cat <<EOF > "${depsfile}"
63${outfile}: \\
64  ${CLANG_BIN}/llvm-objdump \\
65  ${CLANG_BIN}/llvm-nm \\
66EOF
67}
68
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
76                elf) elf=1 ;;
77                macho) macho=1 ;;
78                pe) pe=1 ;;
79                *) echo "Unknown option --${OPTARG}"; usage ;;
80            esac;;
81        ?) usage ;;
82        *) echo "'${opt}' '${OPTARG}'"
83    esac
84done
85
86if [ -z "${infile:-}" ]; then
87    echo "-i argument is required"
88    usage
89fi
90
91if [ -z "${outfile:-}" ]; then
92    echo "-o argument is required"
93    usage
94fi
95
96if [ -z "${depsfile:-}" ]; then
97    echo "-d argument is required"
98    usage
99fi
100
101if [ -z "${CLANG_BIN:-}" ]; then
102    echo "CLANG_BIN environment variable must be set"
103    usage
104fi
105
106rm -f "${outfile}.tmp"
107
108cat <<EOF > "${depsfile}"
109${outfile}: \\
110  ${CLANG_BIN}/llvm-readelf \\
111EOF
112
113if [ -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
122
123if cmp "${outfile}" "${outfile}.tmp" > /dev/null 2> /dev/null; then
124    rm -f "${outfile}.tmp"
125else
126    mv -f "${outfile}.tmp" "${outfile}"
127fi
128