1#!/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13# Copyright 2022, Richard Lowe.
14
15TESTDIR=$(dirname $0)
16
17tmpdir=/tmp/test.$$
18mkdir $tmpdir
19cd $tmpdir
20
21cleanup() {
22	cd /
23	rm -fr $tmpdir
24}
25
26trap 'cleanup' EXIT
27
28if [[ $PWD != $tmpdir ]]; then
29	print -u2 "Failed to create temporary directory: $tmpdir"
30	exit 1;
31fi
32
33if [[ -n $PROTO ]]; then
34	export LD_ALTEXEC=$PROTO/bin/ld
35fi
36
37gas -c ${TESTDIR}/sections.s -o obj1.o
38if (( $? != 0 )); then
39	print -u2 "Couldn't assemble ${TESTDIR}/sections.s (obj1)"
40	exit 1;
41fi
42
43gas -c ${TESTDIR}/sections.s -o obj2.o
44if (( $? != 0 )); then
45	print -u2 "Couldn't assemble ${TESTDIR}/sections.s (obj2)"
46	exit 1;
47fi
48
49/bin/ld -r obj1.o obj2.o -o test-obj.o
50if (( $? != 0 )); then
51	print -u2 "Couldn't link ${TESTDIR}/test-obj.o"
52	exit 1;
53fi
54
55# section_content <index> <file>
56section_content() {
57	elfdump -I$1 -w /dev/stdout $2 | tr '\0' '\n'
58}
59
60# find_in_group <group> <section> <file>
61find_in_group() {
62	elfdump -g $3 | awk -v group="${1}\$" -v section=$2 '
63		BEGIN { slurp = 0 };
64		$0 ~ group { slurp = 1 };
65		slurp && $0 ~ section {
66			gsub(/[\[\]]/, "", $3);
67			print $3;
68			exit;
69		}' | read index
70	if [[ -z $index ]] || (( index <= 0 )); then
71		print -u2 "Couldn't find $2 in $1"
72		exit 1
73	fi
74	print $index;
75}
76
77# The first test_data_conflict, a member of group1 unmerged with only one
78# copy kept.
79GROUP1_INDEX=$(find_in_group group1 test_data_conflict test-obj.o)
80
81# The first test_data_conflict, a member of group2 unmerged with only one
82# copy kept.
83GROUP2_INDEX=$(find_in_group group2 test_data_conflict test-obj.o)
84
85# The un-grouped test_data_conflict, with both copies kept
86elfdump -cN.test_data_conflict test-obj.o | \
87    awk	 -v group1=$GROUP1_INDEX -v group2=$GROUP2_INDEX '
88	/^Section Header/ {
89		gsub(/[^0-9]/, "", $2);
90		if (($2 != group1) && ($2 != group2)) {
91			print $2
92		}
93	}' | read UNGROUP_INDEX
94if [[ -z $UNGROUP_INDEX ]] || (( UNGROUP_INDEX <= 0 )); then
95	print -u2 "Couldn't find ungrouped .test_data_conflict"
96	exit 1
97fi
98
99if (( GROUP1_INDEX == GROUP2_INDEX )); then
100	print -u2 "FAIL: group1 and group2 contain the same section";
101	exit 1
102fi
103
104cmp -s <(section_content $GROUP1_INDEX test-obj.o) /dev/stdin <<EOF
1052: test_data_conflict (group 1)
106EOF
107if (( $? != 0 )); then
108	print -u2 "FAIL: the .test_data_conflict section in group1 has the wrong content"
109	exit 1;
110fi
111
112cmp -s <(section_content $GROUP2_INDEX test-obj.o) /dev/stdin <<EOF
1133: test_data_conflict (group 2)
114EOF
115if (( $? != 0 )); then
116	print -u2 "FAIL: the .test_data_conflict section in group2 has the wrong content"
117	exit 1;
118fi
119
120cmp -s <(section_content $UNGROUP_INDEX test-obj.o) /dev/stdin <<EOF
1214: test_data_conflict (two copies not in group)
1224: test_data_conflict (two copies not in group)
123EOF
124if (( $? != 0 )); then
125	print -u2 "FAIL: the ungrouped .test_data_conflict has the wrong content"
126	exit 1;
127fi
128