xref: /aosp_15_r20/external/strace/mpers.sh (revision cf84ac9a129d8ea9952db616b4e9b904c4bdde56)
1*cf84ac9aSAndroid Build Coastguard Worker#!/bin/sh -e
2*cf84ac9aSAndroid Build Coastguard Worker#
3*cf84ac9aSAndroid Build Coastguard Worker# Copyright (c) 2015 Elvira Khabirova <[email protected]>
4*cf84ac9aSAndroid Build Coastguard Worker# Copyright (c) 2015-2018 The strace developers.
5*cf84ac9aSAndroid Build Coastguard Worker# All rights reserved.
6*cf84ac9aSAndroid Build Coastguard Worker#
7*cf84ac9aSAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*cf84ac9aSAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions
9*cf84ac9aSAndroid Build Coastguard Worker# are met:
10*cf84ac9aSAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright
11*cf84ac9aSAndroid Build Coastguard Worker#    notice, this list of conditions and the following disclaimer.
12*cf84ac9aSAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright
13*cf84ac9aSAndroid Build Coastguard Worker#    notice, this list of conditions and the following disclaimer in the
14*cf84ac9aSAndroid Build Coastguard Worker#    documentation and/or other materials provided with the distribution.
15*cf84ac9aSAndroid Build Coastguard Worker# 3. The name of the author may not be used to endorse or promote products
16*cf84ac9aSAndroid Build Coastguard Worker#    derived from this software without specific prior written permission.
17*cf84ac9aSAndroid Build Coastguard Worker#
18*cf84ac9aSAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*cf84ac9aSAndroid Build Coastguard Worker# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*cf84ac9aSAndroid Build Coastguard Worker# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*cf84ac9aSAndroid Build Coastguard Worker# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*cf84ac9aSAndroid Build Coastguard Worker# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*cf84ac9aSAndroid Build Coastguard Worker# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*cf84ac9aSAndroid Build Coastguard Worker# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*cf84ac9aSAndroid Build Coastguard Worker# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*cf84ac9aSAndroid Build Coastguard Worker# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*cf84ac9aSAndroid Build Coastguard Worker# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*cf84ac9aSAndroid Build Coastguard Worker
29*cf84ac9aSAndroid Build Coastguard Workerexport LC_ALL=C
30*cf84ac9aSAndroid Build Coastguard Worker
31*cf84ac9aSAndroid Build Coastguard WorkerMPERS_AWK="${0%/*}/mpers.awk"
32*cf84ac9aSAndroid Build Coastguard WorkerARCH_FLAG=$1
33*cf84ac9aSAndroid Build Coastguard WorkerCC_ARCH_FLAG=$2
34*cf84ac9aSAndroid Build Coastguard WorkerPARSER_FILE=$3
35*cf84ac9aSAndroid Build Coastguard Worker
36*cf84ac9aSAndroid Build Coastguard WorkerREADELF="${READELF:-readelf}"
37*cf84ac9aSAndroid Build Coastguard WorkerCC="${CC-gcc}"
38*cf84ac9aSAndroid Build Coastguard WorkerCFLAGS="$CFLAGS -gdwarf-2 -c"
39*cf84ac9aSAndroid Build Coastguard WorkerCPP="${CPP-$CC -E}"
40*cf84ac9aSAndroid Build Coastguard WorkerCPPFLAGS="$CPPFLAGS -MM -MG"
41*cf84ac9aSAndroid Build Coastguard Worker
42*cf84ac9aSAndroid Build Coastguard WorkerVAR_NAME='mpers_target_var'
43*cf84ac9aSAndroid Build Coastguard WorkerBITS_DIR="mpers-${ARCH_FLAG}"
44*cf84ac9aSAndroid Build Coastguard Worker
45*cf84ac9aSAndroid Build Coastguard Workermkdir -p ${BITS_DIR}
46*cf84ac9aSAndroid Build Coastguard Workerset -- $(sed -r -n \
47*cf84ac9aSAndroid Build Coastguard Worker	's/^#[[:space:]]*include[[:space:]]+DEF_MPERS_TYPE\(([^)[:space:]]*)\)$/\1/p' \
48*cf84ac9aSAndroid Build Coastguard Worker		"${PARSER_FILE}")
49*cf84ac9aSAndroid Build Coastguard Workerfor m_type; do
50*cf84ac9aSAndroid Build Coastguard Worker	f_h="${BITS_DIR}/${m_type}.h"
51*cf84ac9aSAndroid Build Coastguard Worker	f_c="${BITS_DIR}/${m_type}.c"
52*cf84ac9aSAndroid Build Coastguard Worker	f_i="${BITS_DIR}/${m_type}.i"
53*cf84ac9aSAndroid Build Coastguard Worker	f_o="${BITS_DIR}/${m_type}.o"
54*cf84ac9aSAndroid Build Coastguard Worker	f_d1="${BITS_DIR}/${m_type}.d1"
55*cf84ac9aSAndroid Build Coastguard Worker	f_d2="${BITS_DIR}/${m_type}.d2"
56*cf84ac9aSAndroid Build Coastguard Worker	sed -e '
57*cf84ac9aSAndroid Build Coastguard Worker		/DEF_MPERS_TYPE('"${m_type}"')$/n
58*cf84ac9aSAndroid Build Coastguard Worker		/DEF_MPERS_TYPE/d
59*cf84ac9aSAndroid Build Coastguard Worker		/^[[:space:]]*#[[:space:]]*include[[:space:]]*"xlat\//d
60*cf84ac9aSAndroid Build Coastguard Worker		/^#[[:space:]]*include[[:space:]][[:space:]]*MPERS_DEFS$/ {s//'"${m_type} ${VAR_NAME}"';/;q}
61*cf84ac9aSAndroid Build Coastguard Worker		' "${PARSER_FILE}" > "${f_c}"
62*cf84ac9aSAndroid Build Coastguard Worker	$CPP $CPPFLAGS "${f_c}" > "${f_i}"
63*cf84ac9aSAndroid Build Coastguard Worker	grep -F -q "${m_type}.h" "${f_i}" ||
64*cf84ac9aSAndroid Build Coastguard Worker		continue
65*cf84ac9aSAndroid Build Coastguard Worker	sed -i -e '/DEF_MPERS_TYPE/d' "${f_c}"
66*cf84ac9aSAndroid Build Coastguard Worker	$CC $CFLAGS $CC_ARCH_FLAG "${f_c}" -o "${f_o}"
67*cf84ac9aSAndroid Build Coastguard Worker	$READELF --debug-dump=info "${f_o}" > "${f_d1}"
68*cf84ac9aSAndroid Build Coastguard Worker	sed -r -n '
69*cf84ac9aSAndroid Build Coastguard Worker		/^[[:space:]]*<1>/,/^[[:space:]]*<1><[^>]+>: Abbrev Number: 0/!d
70*cf84ac9aSAndroid Build Coastguard Worker		/^[[:space:]]*<[^>]*><[^>]*>: Abbrev Number: 0/d
71*cf84ac9aSAndroid Build Coastguard Worker		s/^[[:space:]]*<[[:xdigit:]]+>[[:space:]]+//
72*cf84ac9aSAndroid Build Coastguard Worker		s/^[[:space:]]*((<[[:xdigit:]]+>){2}):[[:space:]]+/\1\n/
73*cf84ac9aSAndroid Build Coastguard Worker		s/[[:space:]]+$//
74*cf84ac9aSAndroid Build Coastguard Worker		p' "${f_d1}" > "${f_d2}"
75*cf84ac9aSAndroid Build Coastguard Worker	gawk -v VAR_NAME="$VAR_NAME" -v ARCH_FLAG="${ARCH_FLAG}" \
76*cf84ac9aSAndroid Build Coastguard Worker		-f "$MPERS_AWK" "${f_d2}" > "${f_h}"
77*cf84ac9aSAndroid Build Coastguard Workerdone
78