From 1ef8e9cd745fcddcaa6a6f03cdadcdc76d2b8663 Mon Sep 17 00:00:00 2001
From: Nagajyothi Eggone <Nagajyothi.E@amd.com>
Date: Sun, 16 May 2021 16:09:58 +0530
Subject: [PATCH 2/2] [testsuite] Use llvm-dwarfdump when compiler is clang

This checks if $CC/$CXX is clang/clang++ and uses llvm-dwarfdump in place of
readelf in the test scripts. This is needed as readelf produces errors/warnings
with clang-generated DWARF5 sections/forms, causing many test failures. The
latest llvm-dwarfdump (27 April, 2021) is to be used, which has a fix in
printing headers of DW_UT_partial type.

	Changelog:
        testsuite/scripts/smaller-than.sh: Use llvm-dwarfdump when compiler
                                           is clang.
	testsuite/dwz.tests/cycle.sh: Use llvm-dwarfdump when compiler is clang.
        testsuite/dwz.tests/devel-ignore-locus.sh         : Likewise.
        testsuite/dwz.tests/devel-ignore-size.sh          : Likewise.
        testsuite/dwz.tests/low-mem-die-limit-at-limit.sh : Likewise.
        testsuite/dwz.tests/low-mem-die-limit-one-above.sh: Likewise.
        testsuite/dwz.tests/low-mem-die-limit-one-below.sh: Likewise.
	testsuite/dwz.tests/odr-class-ns.sh		  : Likewise.
        testsuite/dwz.tests/odr-class.sh		  : Likewise.
        testsuite/dwz.tests/odr-def-decl.sh		  : Likewise.
	testsuite/dwz.tests/odr-loc.sh			  : Likewise.
        testsuite/dwz.tests/odr-struct-multifile.sh	  : Likewise.
	testsuite/dwz.tests/odr-struct-ns.sh		  : Likewise.
	testsuite/dwz.tests/odr-struct.sh		  : Likewise.
	testsuite/dwz.tests/odr-union-ns.sh		  : Likewise.
	testsuite/dwz.tests/odr-union.sh		  : Likewise.
	testsuite/dwz.tests/two-files-too-many-dies-2.sh  : Likewise.
---
 testsuite/dwz.tests/cycle.sh                  | 16 ++++++--
 testsuite/dwz.tests/devel-ignore-locus.sh     | 38 +++++++++++++-----
 testsuite/dwz.tests/devel-ignore-size.sh      | 33 +++++++++++----
 .../dwz.tests/low-mem-die-limit-at-limit.sh   | 12 +++++-
 .../dwz.tests/low-mem-die-limit-one-above.sh  | 12 +++++-
 .../dwz.tests/low-mem-die-limit-one-below.sh  | 12 +++++-
 testsuite/dwz.tests/odr-class-ns.sh           | 40 +++++++++++++++----
 testsuite/dwz.tests/odr-class.sh              | 40 +++++++++++++++----
 testsuite/dwz.tests/odr-def-decl.sh           | 16 ++++++--
 testsuite/dwz.tests/odr-loc.sh                | 22 ++++++++--
 testsuite/dwz.tests/odr-struct-multifile.sh   | 36 ++++++++++++++---
 testsuite/dwz.tests/odr-struct-ns.sh          | 40 +++++++++++++++----
 testsuite/dwz.tests/odr-struct.sh             | 40 +++++++++++++++----
 testsuite/dwz.tests/odr-union-ns.sh           | 40 +++++++++++++++----
 testsuite/dwz.tests/odr-union.sh              | 40 +++++++++++++++----
 .../dwz.tests/two-files-too-many-dies-2.sh    | 12 +++++-
 testsuite/scripts/smaller-than.sh             | 20 ++++++++--
 17 files changed, 383 insertions(+), 86 deletions(-)

diff --git a/testsuite/dwz.tests/cycle.sh b/testsuite/dwz.tests/cycle.sh
index 85aa556..d0aadda 100644
--- a/testsuite/dwz.tests/cycle.sh
+++ b/testsuite/dwz.tests/cycle.sh
@@ -1,3 +1,4 @@
+echo $CC > comp 2>/dev/null
 readelf_flags=""
 if readelf -h 2>&1 | grep -q "\-wN,"; then
     readelf_flags=-wN
@@ -23,10 +24,19 @@ rm -f DUMP.1 DUMP.2 LINE.1 LINE.2
 cp 1 2
 $execs/dwz-for-test -m 3 1 2 --devel-no-checksum-cycle-opt --devel-ignore-size
 
-cnt=$(readelf -wi 3 | grep -c "DW_AT_name.*: s$")
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 3 | grep -c "DW_AT_name.*s")
+else
+    cnt=$(readelf -wi 3 | grep -c "DW_AT_name.*: s$")
+fi
+
 [ $cnt -eq 1 ]
 
-cnt=$(readelf -wi $readelf_flags 1 | grep -c "DW_AT_name.*: s$" || true)
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_name.*s" || true)
+else
+    cnt=$(readelf -wi $readelf_flags 1 | grep -c "DW_AT_name.*: s$" || true)
+fi
 [ $cnt -eq 0 ]
 
-rm -f 1 2 3
+rm -f 1 2 3 comp
diff --git a/testsuite/dwz.tests/devel-ignore-locus.sh b/testsuite/dwz.tests/devel-ignore-locus.sh
index caa9d78..16245c9 100644
--- a/testsuite/dwz.tests/devel-ignore-locus.sh
+++ b/testsuite/dwz.tests/devel-ignore-locus.sh
@@ -1,27 +1,45 @@
 cp $execs/two-typedef 1
 
-cnt=$(readelf -wi 1 \
-	    | grep 'DW_AT_name.*: aaa' \
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_AT_name*' | grep 'aaa' \
+	    | wc -l)
+else
+    cnt=$(readelf -wi 1 \
+	    | grep 'DW_AT_name.*: aaa'        \
 	    | wc -l)
+fi
 
 [ $cnt -eq 2 ]
 
  $execs/dwz-for-test 1 2>/dev/null
 
-cnt=$(readelf -wi 1 \
-	    | grep 'DW_AT_name.*: aaa' \
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_AT_name*' | grep 'aaa' \
 	    | wc -l)
-
+else
+    cnt=$(readelf -wi 1 \
+	    | grep 'DW_AT_name.*: aaa'        \
+	    | wc -l)
+fi
 [ $cnt -eq 2 ]
 
 cp $execs/two-typedef 1
 
- $execs/dwz-for-test --devel-ignore-locus --devel-ignore-size 1
+$execs/dwz-for-test --devel-ignore-locus --devel-ignore-size 1
 
-cnt=$(readelf -wi 1 \
-	    | grep 'DW_AT_name.*: aaa' \
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_AT_name*' | grep 'aaa' \
 	    | wc -l)
-
+else
+    cnt=$(readelf -wi 1 \
+	    | grep 'DW_AT_name.*: aaa'        \
+	    | wc -l)
+fi
 [ $cnt -eq 1 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/devel-ignore-size.sh b/testsuite/dwz.tests/devel-ignore-size.sh
index 78d28a2..70c421c 100644
--- a/testsuite/dwz.tests/devel-ignore-size.sh
+++ b/testsuite/dwz.tests/devel-ignore-size.sh
@@ -1,16 +1,29 @@
 cp $execs/min 1
 
-cnt=$(readelf -wi 1 \
-	    | grep '(DW_TAG_partial_unit' \
+echo $CC > comp 2>/dev/null
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_TAG_partial_unit' \
 	    | wc -l)
+else
+    cnt=$(readelf -wi 1 \
+	    | grep 'DW_TAG_partial_unit' \
+	    | wc -l)
+fi
 
 [ $cnt -eq 0 ]
 
  $execs/dwz-for-test 1 2>/dev/null
 
-cnt=$(readelf -wi 1 \
-	    | grep '(DW_TAG_partial_unit' \
+if grep -q "clang" comp; then
+    cnt=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_TAG_partial_unit' \
+	    | wc -l)
+else
+    cnt=$(readelf -wi 1 \
+	    | grep 'DW_TAG_partial_unit' \
 	    | wc -l)
+fi
 
 if [ $cnt -ne 0 ]; then
     exit 77
@@ -20,10 +33,16 @@ cp $execs/min 1
 
  $execs/dwz-for-test --devel-ignore-size 1
 
-cnt=$(readelf -wi 1 \
-	    | grep '(DW_TAG_partial_unit' \
+if grep -q "clang" comp; then
+    cnt=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_TAG_partial_unit' \
+	    | wc -l)
+else
+    cnt=$(readelf -wi 1 \
+	    | grep 'DW_TAG_partial_unit' \
 	    | wc -l)
+fi
 
 [ $cnt -gt 0 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/low-mem-die-limit-at-limit.sh b/testsuite/dwz.tests/low-mem-die-limit-at-limit.sh
index 03ff273..0cfc6b5 100644
--- a/testsuite/dwz.tests/low-mem-die-limit-at-limit.sh
+++ b/testsuite/dwz.tests/low-mem-die-limit-at-limit.sh
@@ -1,8 +1,16 @@
 cp $execs/hello 1
 
-count=$(readelf -wi 1 \
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    count=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_TAG' \
+	    | wc -l)
+else
+    count=$(readelf -wi 1 \
 	    | grep '(DW_TAG' \
 	    | wc -l)
+fi
 limit=$count
 
 $execs/dwz-for-test \
@@ -15,4 +23,4 @@ if grep -q "Compressing 1 in low-mem mode" dwz.err; then
     exit 1
 fi
 
-rm -f 1 dwz.err
+rm -f 1 dwz.err comp
diff --git a/testsuite/dwz.tests/low-mem-die-limit-one-above.sh b/testsuite/dwz.tests/low-mem-die-limit-one-above.sh
index 9b9a6ff..f33353d 100644
--- a/testsuite/dwz.tests/low-mem-die-limit-one-above.sh
+++ b/testsuite/dwz.tests/low-mem-die-limit-one-above.sh
@@ -1,8 +1,16 @@
 cp $execs/hello 1
 
-count=$(readelf -wi 1 \
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    count=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_TAG' \
+	    | wc -l)
+else
+    count=$(readelf -wi 1 \
 	    | grep '(DW_TAG' \
 	    | wc -l)
+fi
 limit=$((count + 1))
 
 $execs/dwz-for-test \
@@ -15,4 +23,4 @@ if grep -q "Compressing 1 in low-mem mode" dwz.err; then
     exit 1
 fi
 
-rm -f 1 dwz.err
+rm -f 1 dwz.err comp
diff --git a/testsuite/dwz.tests/low-mem-die-limit-one-below.sh b/testsuite/dwz.tests/low-mem-die-limit-one-below.sh
index 4136336..f2cb1a4 100644
--- a/testsuite/dwz.tests/low-mem-die-limit-one-below.sh
+++ b/testsuite/dwz.tests/low-mem-die-limit-one-below.sh
@@ -1,8 +1,16 @@
 cp $execs/hello 1
 
-count=$(readelf -wi 1 \
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    count=$(llvm-dwarfdump --debug-info 1 \
+	    | grep 'DW_TAG' \
+	    | wc -l)
+else
+    count=$(readelf -wi 1 \
 	    | grep '(DW_TAG' \
 	    | wc -l)
+fi
 limit=$((count - 1))
 
 $execs/dwz-for-test \
@@ -15,4 +23,4 @@ if ! grep -q "Compressing 1 in low-mem mode" dwz.err; then
     exit 1
 fi
 
-rm -f 1 dwz.err
+rm -f 1 dwz.err comp
diff --git a/testsuite/dwz.tests/odr-class-ns.sh b/testsuite/dwz.tests/odr-class-ns.sh
index 4b890a3..b5ab455 100644
--- a/testsuite/dwz.tests/odr-class-ns.sh
+++ b/testsuite/dwz.tests/odr-class-ns.sh
@@ -6,24 +6,50 @@ cp $execs/odr-class-ns 1
 
 verify-dwarf.sh 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 2 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 2 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 2 ]
 
 $execs/dwz-for-test --odr 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 1 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 1 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 1 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-class.sh b/testsuite/dwz.tests/odr-class.sh
index 872c3ca..103400f 100644
--- a/testsuite/dwz.tests/odr-class.sh
+++ b/testsuite/dwz.tests/odr-class.sh
@@ -6,24 +6,50 @@ cp $execs/odr-class 1
 
 verify-dwarf.sh 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 2 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 2 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 2 ]
 
 $execs/dwz-for-test --odr 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 1 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 1 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep 'DW_AT_name*' | grep -c 'ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 1 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-def-decl.sh b/testsuite/dwz.tests/odr-def-decl.sh
index 8905e52..00658d5 100644
--- a/testsuite/dwz.tests/odr-def-decl.sh
+++ b/testsuite/dwz.tests/odr-def-decl.sh
@@ -6,14 +6,24 @@ cp $execs/def-decl 1
 
 verify-dwarf.sh 1
 
-cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*ao_ref" || true)
+echo $CC > comp 2>/dev/null
+
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_name.*ao_ref" || true)
+else
+    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*ao_ref" || true)
+fi
 [ $cnt -eq 5 ]
 
 $execs/dwz-for-test --odr 1 --devel-ignore-size
 
 verify-dwarf.sh 1
 
-cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*ao_ref" || true)
+if (grep -q "clang" comp); then
+    cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_name.*ao_ref" || true)
+else
+    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*ao_ref" || true)
+fi
 [ $cnt -eq 2 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-loc.sh b/testsuite/dwz.tests/odr-loc.sh
index a8b3318..a05488a 100644
--- a/testsuite/dwz.tests/odr-loc.sh
+++ b/testsuite/dwz.tests/odr-loc.sh
@@ -2,10 +2,16 @@ if ! $execs/dwz-for-test --odr -v 2>/dev/null; then
     exit 77
 fi
 
+echo $CC > comp 2>/dev/null
+
 cp $execs/odr-loc 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 2 ]
 done
 
@@ -14,7 +20,11 @@ $execs/dwz-for-test --odr 1 --devel-ignore-size
 verify-dwarf.sh 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+       cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     case $name in
 	aaa)
 	    [ $cnt -eq 2 ]
@@ -32,8 +42,12 @@ $execs/dwz-for-test --odr --devel-ignore-locus 1
 verify-dwarf.sh 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-struct-multifile.sh b/testsuite/dwz.tests/odr-struct-multifile.sh
index 20b682b..68b8156 100644
--- a/testsuite/dwz.tests/odr-struct-multifile.sh
+++ b/testsuite/dwz.tests/odr-struct-multifile.sh
@@ -7,16 +7,26 @@ if readelf -h 2>&1 | grep -q "\-wN,"; then
     readelf_flags=-wN
 fi
 
+echo $CC > comp 2>/dev/null
+
 cp $execs/odr-struct 1
 cp 1 2
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 2 ]
 done
 
 for name in member_one member_two member_three member_four; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     case $name in
 	member_one|member_two)
 	    [ $cnt -eq 2 ]
@@ -27,7 +37,11 @@ for name in member_one member_two member_three member_four; do
 	esac
 done
 
-decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+if (grep -q "clang" comp); then
+    decl_cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_declaration" || true)
+else
+    decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+fi
 
 $execs/dwz-for-test --odr 1 2 -m 3
 
@@ -35,24 +49,34 @@ verify-dwarf.sh 1
 verify-dwarf.sh 3
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 3 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 3 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 3 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
 for name in member_one member_two member_three member_four; do
-    cnt=$(readelf -wi 3 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 3 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 3 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
 
 for name in aaa bbb ccc; do
+#   There is no flag equivalent to "-wN" in llvm-dwarfdump, so use readelf
     cnt=$(readelf -wi $readelf_flags 1 | grep -c "DW_AT_name.*:.*$name" || true)
     [ $cnt -eq 0 ]
 done
 
 for name in member_one member_two member_three member_four; do
+#   There is no flag equivalent to "-wN" in llvm-dwarfdump, so use readelf
     cnt=$(readelf -wi $readelf_flags 1 | grep -c "DW_AT_name.*:.*$name" || true)
     [ $cnt -eq 0 ]
 done
 
-rm -f 1 2 3
+rm -f 1 2 3 comp
diff --git a/testsuite/dwz.tests/odr-struct-ns.sh b/testsuite/dwz.tests/odr-struct-ns.sh
index 4fd00af..0de93c4 100644
--- a/testsuite/dwz.tests/odr-struct-ns.sh
+++ b/testsuite/dwz.tests/odr-struct-ns.sh
@@ -2,15 +2,25 @@ if ! $execs/dwz-for-test --odr -v 2>/dev/null; then
     exit 77
 fi
 
+echo $CC > comp 2>/dev/null
+
 cp $execs/odr-struct-ns 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 2 ]
 done
 
 for name in member_one member_two member_three member_four; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     case $name in
 	member_one|member_two)
 	    [ $cnt -eq 2 ]
@@ -21,25 +31,41 @@ for name in member_one member_two member_three member_four; do
 	esac
 done
 
-decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+if (grep -q "clang" comp); then
+    decl_cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_declaration" || true)
+else
+    decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+fi
 
 $execs/dwz-for-test --odr 1
 
 verify-dwarf.sh 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
 for name in member_one member_two member_three member_four; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
 # We expect two decls to be removed, for bbb and ccc.
 expected_decl_cnt=$(($decl_cnt - 2))
-decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+if (grep -q "clang" comp); then
+    decl_cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_declaration" || true)
+else
+    decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+fi
 [ $expected_decl_cnt -eq $decl_cnt ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-struct.sh b/testsuite/dwz.tests/odr-struct.sh
index d0fddf9..20e6125 100644
--- a/testsuite/dwz.tests/odr-struct.sh
+++ b/testsuite/dwz.tests/odr-struct.sh
@@ -2,15 +2,25 @@ if ! $execs/dwz-for-test --odr -v 2>/dev/null; then
     exit 77
 fi
 
+echo $CC > comp 2>/dev/null
+
 cp $execs/odr-struct 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 2 ]
 done
 
 for name in member_one member_two member_three member_four; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     case $name in
 	member_one|member_two)
 	    [ $cnt -eq 2 ]
@@ -21,25 +31,41 @@ for name in member_one member_two member_three member_four; do
 	esac
 done
 
-decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+if (grep -q "clang" comp); then
+    decl_cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_declaration" || true)
+else
+    decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+fi
 
 $execs/dwz-for-test --odr 1
 
 verify-dwarf.sh 1
 
 for name in aaa bbb ccc; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
 for name in member_one member_two member_three member_four; do
-    cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    if (grep -q "clang" comp); then
+        cnt=$(llvm-dwarfdump --debug-info 1 | grep "DW_AT_name*" | grep -c "$name" || true)
+    else
+        cnt=$(readelf -wi 1 | grep -c "DW_AT_name.*:.*$name" || true)
+    fi
     [ $cnt -eq 1 ]
 done
 
 # We expect two decls to be removed, for bbb and ccc.
 expected_decl_cnt=$(($decl_cnt - 2))
-decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+if (grep -q "clang" comp); then
+    decl_cnt=$(llvm-dwarfdump --debug-info 1 | grep -c "DW_AT_declaration" || true)
+else
+    decl_cnt=$(readelf -wi 1 | grep -c "DW_AT_declaration" || true)
+fi
 [ $expected_decl_cnt -eq $decl_cnt ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-union-ns.sh b/testsuite/dwz.tests/odr-union-ns.sh
index 7d86dba..44c0969 100644
--- a/testsuite/dwz.tests/odr-union-ns.sh
+++ b/testsuite/dwz.tests/odr-union-ns.sh
@@ -2,28 +2,54 @@ if ! $execs/dwz-for-test --odr -v 2>/dev/null; then
     exit 77
 fi
 
+echo $CC > comp 2>/dev/null
+
 cp $execs/odr-union-ns 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 2 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 2 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 2 ]
 
 $execs/dwz-for-test --odr 1
 
 verify-dwarf.sh 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 1 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 1 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 1 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/odr-union.sh b/testsuite/dwz.tests/odr-union.sh
index a32a46c..f0ed1e7 100644
--- a/testsuite/dwz.tests/odr-union.sh
+++ b/testsuite/dwz.tests/odr-union.sh
@@ -2,28 +2,54 @@ if ! $execs/dwz-for-test --odr -v 2>/dev/null; then
     exit 77
 fi
 
+echo $CC > comp 2>/dev/null
+
 cp $execs/odr-union 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 2 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 2 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 2 ]
 
 $execs/dwz-for-test --odr 1
 
 verify-dwarf.sh 1
 
-aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+if (grep -q "clang" comp); then
+    aaa=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*aaa')
+else
+    aaa=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*aaa')
+fi
 [ $aaa -eq 1 ]
 
-bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+if (grep -q "clang" comp); then
+    bbb=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*bbb')
+else
+    bbb=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*bbb')
+fi
 [ $bbb -eq 1 ]
 
-ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+if (grep -q "clang" comp); then
+    ccc=$(llvm-dwarfdump --debug-info 1 | grep -c 'DW_AT_name.*ccc')
+else
+    ccc=$(readelf -wi 1 | grep -c 'DW_AT_name.*:.*ccc')
+fi
 [ $ccc -eq 1 ]
 
-rm -f 1
+rm -f 1 comp
diff --git a/testsuite/dwz.tests/two-files-too-many-dies-2.sh b/testsuite/dwz.tests/two-files-too-many-dies-2.sh
index 91b5ccc..5b73e8a 100644
--- a/testsuite/dwz.tests/two-files-too-many-dies-2.sh
+++ b/testsuite/dwz.tests/two-files-too-many-dies-2.sh
@@ -1,9 +1,17 @@
+echo $CC > comp 2>/dev/null
+
 cp $execs/hello 1
 cp $execs/dwz-for-test 2
 
-limit=$(readelf -wi 2 \
+if (grep -q "clang" comp); then
+    limit=$(llvm-dwarfdump --debug-info 2 \
+	    | grep ':[ ]*DW_TAG' \
+	    | wc -l)
+else
+    limit=$(readelf -wi 2 \
 	    | grep '(DW_TAG' \
 	    | wc -l)
+fi
 limit=$((limit - 1))
 
 if dwz -L$limit 2 1 2>dwz.err; status=$?; then
@@ -19,4 +27,4 @@ fi
 smaller-than.sh 1 $execs/hello
 cmp 2 $execs/dwz-for-test
 
-rm -f 1 2 dwz.err
+rm -f 1 2 dwz.err comp
diff --git a/testsuite/scripts/smaller-than.sh b/testsuite/scripts/smaller-than.sh
index 3b452dc..539f5fd 100755
--- a/testsuite/scripts/smaller-than.sh
+++ b/testsuite/scripts/smaller-than.sh
@@ -3,16 +3,26 @@
 f1=$1
 f2=$2
 
+echo $CC > comp 2>/dev/null
+
 section_size ()
 {
     local f="$1"
     local section="$2"
 
     local s
-    s=$(readelf -S -W $f \
+
+    if (grep -q "clang" comp); then
+        s=$(llvm-dwarfdump --show-section-sizes $f \
+		| grep -w "\.debug_$section" \
+		| sed 's/.*\.debug_//' \
+		| awk '{print $2}')
+    else
+        s=$(readelf -S -W $f \
 	    | grep "\.debug_$section" \
 	    | sed 's/.*\.debug_//' \
-	    | awk '{print $5}')
+      	    | awk '{print $5}')
+    fi
 
     if [ "$s" = "" ]; then
        echo 0
@@ -20,7 +30,9 @@ section_size ()
     fi
 
     # Convert hex to decimal.
-    s=$(printf "%d" $((16#$s)))
+    if ( ! grep -q "clang" comp); then
+        s=$(printf "%d" $((16#$s)))
+    fi
 
     echo $s
 }
@@ -45,4 +57,6 @@ if [ $s1 -ge $s2 ]; then
     exit 1
 fi
 
+rm -f comp
+
 exit 0
-- 
2.25.1

