xref: /illumos-gate/usr/src/test/elf-tests/tests/groups/common.sh (revision d227ea68051b5404410ee1880d0bba1ebb33a4cc)
1*d227ea68SRichard Lowe#! /usr/bin/sh
2*d227ea68SRichard Lowe#
3*d227ea68SRichard Lowe#
4*d227ea68SRichard Lowe# This file and its contents are supplied under the terms of the
5*d227ea68SRichard Lowe# Common Development and Distribution License ("CDDL"), version 1.0.
6*d227ea68SRichard Lowe# You may only use this file in accordance with the terms of version
7*d227ea68SRichard Lowe# 1.0 of the CDDL.
8*d227ea68SRichard Lowe#
9*d227ea68SRichard Lowe# A full copy of the text of the CDDL should have accompanied this
10*d227ea68SRichard Lowe# source.  A copy of the CDDL is also available via the Internet at
11*d227ea68SRichard Lowe# http://www.illumos.org/license/CDDL.
12*d227ea68SRichard Lowe#
13*d227ea68SRichard Lowe
14*d227ea68SRichard Lowe# Copyright 2023 Richard Lowe
15*d227ea68SRichard Lowe
16*d227ea68SRichard Lowe# find_in_group <group> <section> <file>
17*d227ea68SRichard Lowe# errors if not present, else returns the group index
18*d227ea68SRichard Lowefind_in_group() {
19*d227ea68SRichard Lowe	elfdump -g $3 | awk -v group="${1}\$" -v section=$2 '
20*d227ea68SRichard Lowe		BEGIN { slurp = 0 };
21*d227ea68SRichard Lowe		$0 ~ group { slurp = 1 };
22*d227ea68SRichard Lowe		slurp && $0 ~ section {
23*d227ea68SRichard Lowe			gsub(/[\[\]]/, "", $3);
24*d227ea68SRichard Lowe			print $3;
25*d227ea68SRichard Lowe			exit;
26*d227ea68SRichard Lowe		}' | read index
27*d227ea68SRichard Lowe	if [[ -z $index ]] || (( index <= 0 )); then
28*d227ea68SRichard Lowe		print -u2 "Couldn't find $2 in $1"
29*d227ea68SRichard Lowe		exit 1
30*d227ea68SRichard Lowe	fi
31*d227ea68SRichard Lowe	print $index;
32*d227ea68SRichard Lowe}
33