clang with -flto produces object files that contain bitcode. This
breaks various (self) testcases that expect those .o files are ELF
files. readelf, nm, elfcheck and elfcompress won't work on such files.

Add a helper function is_obj_bitcode in test-subr.sh that tests can
use to possibly skip a check on an .o file.

       * tests/test-subr.sh (is_obj_bitcode): New function.
       (testrun_on_self): Use is_obj_bitcode to possibly skip a file.
       (testrun_on_self_obj): Likewise.
       (testrun_on_self_compressed): Likewise.
       (testrun_on_self_quiet): Likewise.
       * tests/run-elfclassify-self.sh: Check if the object files are
       actually ELF files.
       * tests/run-nm-self.sh: Use is_obj_bitcode to possibly skip file.
       * tests/run-reverse-sections-self.sh: Likewise.
       * tests/run-strip-reloc-self.sh: Likewise.
       * tests/run-strip-strmerge.sh: Likewise.
       * tests/strip-reloc-subr.sh: Likewise.

Signed-off-by: Mark Wielaard <[email protected]>
---
 tests/run-elfclassify-self.sh      |  9 +++++++--
 tests/run-nm-self.sh               |  6 +++++-
 tests/run-reverse-sections-self.sh |  6 +++++-
 tests/run-strip-reloc-self.sh      |  4 ++++
 tests/run-strip-strmerge.sh        |  4 ++++
 tests/strip-reloc-subr.sh          |  5 +++++
 tests/test-subr.sh                 | 24 ++++++++++++++++++++++++
 7 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/tests/run-elfclassify-self.sh b/tests/run-elfclassify-self.sh
index c48ab9c93e92..4d86cb24e711 100755
--- a/tests/run-elfclassify-self.sh
+++ b/tests/run-elfclassify-self.sh
@@ -32,5 +32,10 @@ testrun_on_self_exe ${abs_top_builddir}/src/elfclassify 
--program
 testrun_on_self_exe ${abs_top_builddir}/src/elfclassify --loadable
 testrun_on_self_exe ${abs_top_builddir}/src/elfclassify --not-shared
 
-testrun ${abs_top_builddir}/src/elfclassify --not-shared $self_test_files_obj
-testrun ${abs_top_builddir}/src/elfclassify --not-executable 
$self_test_files_obj
+# Check if the object files are actually ELF files (could be bitcode lto)
+if ${abs_top_builddir}/src/elfclassify -v --elf $self_test_files_obj; then
+  testrun ${abs_top_builddir}/src/elfclassify --not-shared $self_test_files_obj
+  testrun ${abs_top_builddir}/src/elfclassify --not-executable 
$self_test_files_obj
+else
+  echo "*** Skipping testing not ELF files $self_test_files_obj"
+fi
diff --git a/tests/run-nm-self.sh b/tests/run-nm-self.sh
index 6a31afcf6bed..e7d7c58d41d0 100755
--- a/tests/run-nm-self.sh
+++ b/tests/run-nm-self.sh
@@ -28,7 +28,11 @@ for what_arg in --debug-syms --defined-only --dynamic 
--extern-only; do
       for self_file in $ET_REL $ET_EXEC $ET_DYN; do
        # --dynamic doesn't make sense for ET_REL.
        if ! test "$what_arg" = "--dynamic" -a "$self_file" = "$ET_REL"; then
-         testrun ${abs_top_builddir}/src/nm $what_arg $format_arg $out_arg 
$self_file > /dev/null
+         if is_obj_bitcode "$self_file"; then
+           echo "*** skipping bitcode file $self_file"
+         else
+           testrun ${abs_top_builddir}/src/nm $what_arg $format_arg $out_arg 
$self_file > /dev/null
+         fi
        fi
       done
     done
diff --git a/tests/run-reverse-sections-self.sh 
b/tests/run-reverse-sections-self.sh
index 71afd6aca1f1..d241775993d9 100755
--- a/tests/run-reverse-sections-self.sh
+++ b/tests/run-reverse-sections-self.sh
@@ -39,7 +39,11 @@ test_reverse_self ()
 # Only really makes sense for ET_REL files, but try all, just to check
 # it also works if we keep the order for the allocated sections.
 for file in $self_test_files; do
-  test_reverse_self $file
+  if is_obj_bitcode "$file"; then
+    echo "*** skipping bitcode file $file"
+  else
+    test_reverse_self $file
+  fi
 done
 
 exit 0
diff --git a/tests/run-strip-reloc-self.sh b/tests/run-strip-reloc-self.sh
index 68ed4cc2702b..73f5b6c36606 100755
--- a/tests/run-strip-reloc-self.sh
+++ b/tests/run-strip-reloc-self.sh
@@ -8,6 +8,10 @@ runtest ${abs_top_builddir}/src/strip.o 1
 
 # Copy ET_REL file for self-test and make sure to run with/without
 # elf section compression.
+if is_obj_bitcode ${abs_top_builddir}/src/strip.o; then
+      echo "*** skipping bitcode file"
+      exit $runtest_status
+fi
 tempfiles strip-uncompressed.o strip-compressed.o
 testrun ${abs_top_builddir}/src/elfcompress -o strip-uncompressed.o -t none \
   ${abs_top_builddir}/src/strip.o
diff --git a/tests/run-strip-strmerge.sh b/tests/run-strip-strmerge.sh
index aa9c1eb93283..0d24adef90b9 100755
--- a/tests/run-strip-strmerge.sh
+++ b/tests/run-strip-strmerge.sh
@@ -50,6 +50,10 @@ testrun ${abs_top_builddir}/src/elfcmp $merged $remerged
 
 # A random ET_REL file
 input=${abs_top_builddir}/tests/elfstrmerge.o
+if is_obj_bitcode "$input"; then
+  echo "*** skipping bitcode file $self_file"
+  exit 0
+fi
 merged=merged.elf
 stripped=${merged}.stripped
 debugfile=${merged}.debug
diff --git a/tests/strip-reloc-subr.sh b/tests/strip-reloc-subr.sh
index c4d55ced1596..045f5b0798ba 100755
--- a/tests/strip-reloc-subr.sh
+++ b/tests/strip-reloc-subr.sh
@@ -36,6 +36,11 @@ runtest() {
 
   echo "runtest $infile"
 
+  if is_obj_bitcode "$infile"; then
+    echo "*** skipping bitcode file $infile"
+    return
+  fi
+
   rm -f $outfile1 $debugfile1 $outfile2 $debugfile2
 
   testrun ${abs_top_builddir}/src/strip -o $outfile1 -f $debugfile1 $infile ||
diff --git a/tests/test-subr.sh b/tests/test-subr.sh
index 2a956b47de2f..623b5b774511 100644
--- a/tests/test-subr.sh
+++ b/tests/test-subr.sh
@@ -1,5 +1,6 @@
 #! /bin/sh
 # Copyright (C) 2005-2015, 2017 Red Hat, Inc.
+# Copyright (C) 2026 Mark J. Wielaard <[email protected]>
 # This file is part of elfutils.
 #
 # This file is free software; you can redistribute it and/or modify
@@ -118,6 +119,13 @@ program_transform()
   echo "$*" | sed "${program_transform_name}"
 }
 
+is_obj_bitcode()
+{
+  bcfile="$1"
+  BC=$(od -An -tx2 -N2 "$bcfile" | sed -e 's/^[[:space:]]*//')
+  if [ "'$BC'" = "'4342'" ]; then return 0; else return 1; fi
+}
+
 self_test_files_exe=`echo ${abs_top_builddir}/src/addr2line \
 ${abs_top_builddir}/src/elfclassify \
 ${abs_top_builddir}/src/stack \
@@ -137,8 +145,12 @@ testrun_on_self()
   exit_status=0
 
   for file in $self_test_files; do
+    if is_obj_bitcode "$file"; then
+      echo "*** skipping bitcode file in $* $file"
+    else
       testrun $* $file \
          || { echo "*** failure in $* $file"; exit_status=1; }
+    fi
   done
 
   # Only exit if something failed
@@ -176,8 +188,12 @@ testrun_on_self_obj()
   exit_status=0
 
   for file in $self_test_files_obj; do
+    if is_obj_bitcode "$file"; then
+      echo "*** skipping bitcode file in $* $file"
+    else
       testrun $* $file \
          || { echo "*** failure in $* $file"; exit_status=1; }
+    fi
   done
 
   # Only exit if something failed
@@ -190,12 +206,16 @@ testrun_on_self_compressed()
   exit_status=0
 
   for file in $self_test_files; do
+    if is_obj_bitcode "$file"; then
+      echo "*** skipping bitcode file in $* $file"
+    else
       tempfiles ${file}z
       testrun ${abs_top_builddir}/src/elfcompress -f -q -o ${file}z ${file}
       testrun ${abs_top_builddir}/src/elfcompress -f -q --name='.s??tab' 
${file}z
 
       testrun $* ${file}z \
          || { echo "*** failure in $* ${file}z"; exit_status=1; }
+    fi
   done
 
   # Only exit if something failed
@@ -208,8 +228,12 @@ testrun_on_self_quiet()
   exit_status=0
 
   for file in $self_test_files; do
+    if is_obj_bitcode "$file"; then
+      echo "*** skipping bitcode file in $* $file"
+    else
       testrun $* $file > /dev/null \
          || { echo "*** failure in $* $file"; exit_status=1; }
+    fi
   done
 
   # Only exit if something failed
-- 
2.52.0

Reply via email to