Skip to contentSkip to conversation

file_test: fix testcase for ppc64le

PATCH In review Jeffrey Bastian 3 participants 9 messages Nov 3, 2014 – Dec 2, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 file_test: fix testcase for ppc64le

The file_test.sh test assumes all PPC systems are big-endian (MSB), but this assumption is incorrect on the new ppc64le little-endian (LSB) systems and file06 and file10 report false failures:

file06      0  TINFO  :  TEST #6: file command recognizes ELF executables
file06      6  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize ELF binary executable. Reason:
/tmp//cprog: ELF 64-bit LSB executable, version 1 (SYSV), dynamically linked (uses shared libs), ...
...
file10      0  TINFO  :  TEST #10: file command recognizes vmlinu file
file10     10  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize vmlinu correctly. Reason:

/boot/vmlinuz: ELF 64-bit LSB shared object, version 1 (SYSV), statically linked, ...

This patch fixes the test so ppc64le is checked for "LSB", not "MSB".

---
 testcases/commands/ade/file/file_test.sh | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
testcases/commands/ade/file/file_test.sh +8 -3
index 101a8cca8bc9..fd7943807bdb 100755
@@ -332,10 +332,10 @@ fi
 
 
 # TEST #6
-# Test if file command can recognize ELF binay executables
+# Test if file command can recognize ELF binary executables
 
-# Check ppc architecture
-  TEST_ARCH=LSB   # Assume the architecture is Intel
+# Check architecture endianness
+  TEST_ARCH=LSB   # Assume the architecture is little-endian
 
   if uname -m |
     grep -qe '\(m68k\)\|\(sparc\)\|\(mips\b\)\|\(mipseb\)\|\(sh.eb\)' \
@@ -343,6 +343,11 @@ fi
      TEST_ARCH=MSB
   fi
 
+  # ppc is usually big-endian, but not necessarily
+  if uname -m | grep -qe '\(ppc64le\)'; then
+     TEST_ARCH=LSB
+  fi
+
 export TCID=file06
 export TST_COUNT=6
 

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#1 lore raw
Quoted 10 lines · Jeffrey Bastian

The file_test.sh test assumes all PPC systems are big-endian (MSB), but this assumption is incorrect on the new ppc64le little-endian (LSB) systems and file06 and file10 report false failures:

file06      0  TINFO  :  TEST #6: file command recognizes ELF executables
file06      6  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize ELF binary executable. Reason:
/tmp//cprog: ELF 64-bit LSB executable, version 1 (SYSV), dynamically linked (uses shared libs), ...
...
file10      0  TINFO  :  TEST #10: file command recognizes vmlinu file
file10     10  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize vmlinu correctly. Reason:
Quoted 3 lines

/boot/vmlinuz: ELF 64-bit LSB shared object, version 1 (SYSV), statically linked, ...

This patch fixes the test so ppc64le is checked for "LSB", not "MSB".

we should delete the uname logic entirely. how about instead we use readelf -h and look at the Data field.
-mike

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#2 lore raw
Quoted 2 lines · Mike Frysinger

we should delete the uname logic entirely. how about instead we use readelf -h and look at the Data field.

That's a good idea, although 'lscpu' is even easier to use. But what if lscpu or readelf are not installed?

~]$ uname -m
ppc64
~]$ lscpu | grep Byte.Order
Byte Order: Big Endian
~]# readelf -h /usr/bin/readelf | grep Data
Data: 2's complement, big endian

~]$ uname -m
x86_64
~]$ lscpu | grep Byte.Order
Byte Order: Little Endian
~]$ readelf -h /usr/bin/readelf | grep Data
Data: 2's complement, little endian

Signature · 1 line
Jeff

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#3 lore raw
Quoted 6 lines · Jeffrey Bastian
Quoted 2 lines · Mike Frysinger

we should delete the uname logic entirely. how about instead we use readelf -h and look at the Data field.

That's a good idea, although 'lscpu' is even easier to use. But what if lscpu or readelf are not installed?

the test requires cc to be installed and fully functional. that means binutils also have to be installed (as and ld). i'm not aware of any distro that splits readelf out from the others.
-mike

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#4 lore raw
Quoted 3 lines · Mike Frysinger

the test requires cc to be installed and fully functional. that means binutils also have to be installed (as and ld). i'm not aware of any distro that splits readelf out from the others.

Ok, it's reasonable to assume readelf is installed then.

The patch below adds two different checks for endiannes: first a simple check of the ASCII character "I" in octal, then the readelf test. If neither test works it logs an error.

           ==================================================

file_test: fix testcase for ppc64le

The file_test.sh test assumes all PPC systems are big-endian (MSB), but this assumption is incorrect on the new ppc64le little-endian (LSB) systems and file06 and file10 report false failures:

file06      0  TINFO  :  TEST #6: file command recognizes ELF executables
file06      6  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize ELF binary executable. Reason:
/tmp//cprog: ELF 64-bit LSB executable, version 1 (SYSV), dynamically linked (uses shared libs), ...
...
file10      0  TINFO  :  TEST #10: file command recognizes vmlinu file
file10     10  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize vmlinu correctly. Reason:

/boot/vmlinuz: ELF 64-bit LSB shared object, version 1 (SYSV), statically linked, ...

This patch adds more robust testing for the endianness of a CPU.

---
 testcases/commands/ade/file/file_test.sh | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)
testcases/commands/ade/file/file_test.sh +22 -10
index 101a8cca8bc9..b94ad2313a31 100755
@@ -332,16 +332,7 @@ fi
 
 
 # TEST #6
-# Test if file command can recognize ELF binay executables
-
-# Check ppc architecture
-  TEST_ARCH=LSB   # Assume the architecture is Intel
-
-  if uname -m |
-    grep -qe '\(m68k\)\|\(sparc\)\|\(mips\b\)\|\(mipseb\)\|\(sh.eb\)' \
-         -e '\(powerpc\)\|\(ppc\)\|\(s390\)\|\(parisc\)'; then
-     TEST_ARCH=MSB
-  fi
+# Test if file command can recognize ELF binary executables
 
 export TCID=file06
 export TST_COUNT=6
@@ -349,6 +340,27 @@ export TST_COUNT=6
 $LTPBIN/tst_resm TINFO \
         "TEST #6: file command recognizes ELF executables"
 
+# check for CPU endianness
+TEST_ARCH=NULL
+ENDIAN=$(echo -n I | od -to2 2>/dev/null |
+             awk '{print substr($2,6,1); exit}' 2>/dev/null)
+if [ "$ENDIAN" = "0" ]; then
+    TEST_ARCH=MSB
+elif [ "$ENDIAN" = "1" ]; then
+    TEST_ARCH=LSB
+else
+    READELF=$(which readelf 2>/dev/null)
+    ENDIAN=$($READELF -h $READELF 2>/dev/null | grep endian)
+    if echo $ENDIAN | grep -q 'Data:.*big endian' ; then
+        TEST_ARCH=MSB
+    elif echo $ENDIAN | grep -q 'Data:.*little endian' ; then
+        TEST_ARCH=LSB
+    fi
+fi
+
+if [ "$TEST_ARCH" = "NULL" ]; then
+    $LTPBIN/tst_resm TFAIL "file: Could not determine CPU endianness"
+fi
 
 cat > $LTPTMP/cprog.c <<EOF
 #include <stdio.h>

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#5 lore raw

testcases/commands/ade/file/file_test.sh export TST_COUNT=6 · line 346

Quoted 4 lines · Jeffrey Bastian
testcases/commands/ade/file/file_test.sh +4 -0
+# check for CPU endianness
+TEST_ARCH=NULL
+ENDIAN=$(echo -n I | od -to2 2>/dev/null |
+             awk '{print substr($2,6,1); exit}' 2>/dev/null)

interesting idea. there's a few issues with this code, but rather than get into that, why don't ew just rely on readelf and be done ? i don't think we want to maintain two code paths here.

testcases/commands/ade/file/file_test.sh export TST_COUNT=6 · line 358

Quoted 7 lines
testcases/commands/ade/file/file_test.sh +7 -0
+    READELF=$(which readelf 2>/dev/null)
+    ENDIAN=$($READELF -h $READELF 2>/dev/null | grep endian)
+    if echo $ENDIAN | grep -q 'Data:.*big endian' ; then
+        TEST_ARCH=MSB
+    elif echo $ENDIAN | grep -q 'Data:.*little endian' ; then
+        TEST_ARCH=LSB
+    fi

you can simplify this a bit:

	READELF=$(which readelf 2>/dev/null)
	case $(${READELF} -h ${READELF}) in
	*Data:*"big endian"*)
		TEST_ARCH=MSB ;;
	*Data:*"little endian"*)
		TEST_ARCH=LSB ;;
	*) <throw an error> ;;
	esac

-mike

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#6 lore raw
Quoted 3 lines · Mike Frysinger

interesting idea. there's a few issues with this code, but rather than get into that, why don't ew just rely on readelf and be done ? i don't think we want to maintain two code paths here.

I was just trying to give the test the best chance of finding the endianness (just-in-case there's a rare system that does not have readelf installed). I suppose it would be simpler, though, to start with just the readelf test and then add the backup check later if there are any bug reports about a missing readelf command.

If a missing readelf command becomes a problem, there are a number of other ways to check:
1. echo -n I | od -to2
2. lscpu | grep "Byte Order"
3. python -c "import sys;print sys.byteorder;"
4. perl -MConfig -e 'print $Config{byteorder};'
5. a small C helper program since we know at least the compiler is installed

The patch below uses just the readelf command, and it uses your simplified code path, and it also updates the comments at the top of the test (I forgot that in my earlier patches).

           ==================================================

file_test: fix testcase for ppc64le

The file_test.sh test assumes all PPC systems are big-endian (MSB), but this assumption is incorrect on the new ppc64le little-endian (LSB) systems and file06 and file10 report false failures:

file06      0  TINFO  :  TEST #6: file command recognizes ELF executables
file06      6  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize ELF binary executable. Reason:
/tmp//cprog: ELF 64-bit LSB executable, version 1 (SYSV), dynamically linked (uses shared libs), ...
...
file10      0  TINFO  :  TEST #10: file command recognizes vmlinu file
file10     10  TFAIL  :  ltpapicmd.c:156: file: Failed to Recognize vmlinu correctly. Reason:

/boot/vmlinuz: ELF 64-bit LSB shared object, version 1 (SYSV), statically linked, ...

This patch adds more robust testing for the endianness of a CPU.

---
 testcases/commands/ade/file/file_test.sh | 44 +++++++++++++++++---------------
 1 file changed, 23 insertions(+), 21 deletions(-)
testcases/commands/ade/file/file_test.sh +23 -21
index 101a8cca8bc9..59ec85e45b31 100755
@@ -75,20 +75,17 @@
 # -------
 #   Similar test(as Test02) is performed with C program text
 #
-# Test06: Test if file command can recognize ELF binay executables
+# Test06: Test if file command can recognize ELF binary executables
 # -------
-# 1) Grep for 'm68k' or 'sparc' or 'mips' or 'mipseb' or 'sh.eb'
-#    or 'powerpc' or 'ppc' or 's390' from the output of the command
-#    'uname -m'
-# 2) If the above step is successful, assign string 'MSB' to variable
-#    TARGET_ARCH else assign string 'LSB'
-# 3) Write small C program to a known '.c' file
-# 4) Compile it using "cc"
+# 1) Use readelf to determine if the host is big- or little-endian
+#    and assign TEST_ARCH the string "MSB" or "LSB" respectively
+# 2) Write small C program to a known '.c' file
+# 3) Compile it using "cc"
 #    Ex: cc xyz xyz.c
-# 5) Use file command to get the type of the object file
-# 6) Grep for the string "ELF .*-bit $TEST_ARCH executable, .*"
+# 4) Use file command to get the type of the object file
+# 5) Grep for the string "ELF .*-bit $TEST_ARCH executable, .*"
 #    in the output of the 'file' command
-# 7) If the above command is successful, declare test as PASS
+# 6) If the above command is successful, declare test as PASS
 #    else declare test as FAIL
 #
 # Test07: Test if file command can recognize tar files
@@ -332,16 +329,7 @@ fi
 
 
 # TEST #6
-# Test if file command can recognize ELF binay executables
-
-# Check ppc architecture
-  TEST_ARCH=LSB   # Assume the architecture is Intel
-
-  if uname -m |
-    grep -qe '\(m68k\)\|\(sparc\)\|\(mips\b\)\|\(mipseb\)\|\(sh.eb\)' \
-         -e '\(powerpc\)\|\(ppc\)\|\(s390\)\|\(parisc\)'; then
-     TEST_ARCH=MSB
-  fi
+# Test if file command can recognize ELF binary executables
 
 export TCID=file06
 export TST_COUNT=6
@@ -349,6 +337,20 @@ export TST_COUNT=6
 $LTPBIN/tst_resm TINFO \
         "TEST #6: file command recognizes ELF executables"
 
+# check for CPU endianness
+READELF=$(which readelf 2>/dev/null)
+case $(${READELF} -h ${READELF}) in
+    *Data:*"big endian"*)
+        TEST_ARCH=MSB
+        ;;
+    *Data:*"little endian"*)
+        TEST_ARCH=LSB
+        ;;
+    *)
+        TEST_ARCH=NULL
+        $LTPBIN/tst_resm TFAIL "file: Could not determine CPU endianness"
+        ;;
+esac
 
 cat > $LTPTMP/cprog.c <<EOF
 #include <stdio.h>

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#7 lore raw
Quoted 3 lines · Jeffrey Bastian

If a missing readelf command becomes a problem, there are a number of other ways to check:
1. echo -n I | od -to2

printf I :)

Quoted 5 lines

2. lscpu | grep "Byte Order"
3. python -c "import sys;print sys.byteorder;"
4. perl -MConfig -e 'print $Config{byteorder};'
5. a small C helper program since we know at least the compiler is installed

the way autoconf does it is to compile a data struct and grep the string:

$ cat test.c
short big_endian[] = { 0x4269, 0x4765, 0x4e64, 0x4961, 0x4e00, 0, };
short little_endian[] = { 0x694c, 0x7454, 0x654c, 0x6e45, 0x6944, 0x6e41, 0, };
extern int foo(short *, short *);
int main(int argc, char *argv[]) {
    return foo(big_endian, little_endian);
}
$ gcc -c test.c
$ grep -q BiGeNdIaN test.o && echo big endian
$ grep -q LiTtLeEnDiAn test.o && echo little endian
Quoted 3 lines

The patch below uses just the readelf command, and it uses your simplified code path, and it also updates the comments at the top of the test (I forgot that in my earlier patches).

-mike

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#8 lore raw
In reply to Jeffrey Bastian

Hi!

testcases/commands/ade/file/file_test.sh export TST_COUNT=6 · line 342

Quoted 3 lines
testcases/commands/ade/file/file_test.sh +3 -0
+# check for CPU endianness
+READELF=$(which readelf 2>/dev/null)
+case $(${READELF} -h ${READELF}) in

I've simplified these two lines to:

case $(readelf -h /bin/sh) in

And pushed, thanks.

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list