PR13013: ELF Type identification fails for MSB type ELF files.
authorMeador Inge <meadori@codesourcery.com>
Mon, 25 Jun 2012 14:48:43 +0000 (14:48 +0000)
committerMeador Inge <meadori@codesourcery.com>
Mon, 25 Jun 2012 14:48:43 +0000 (14:48 +0000)
Fix 'sys::IdentifyFileType' to work with big and little endian byte orderings
when reading the ELF object file type.

Initial patch by Stefan Hepp.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@159138 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Path.cpp
test/MC/Mips/elf-objdump.s [new file with mode: 0644]

index dcddeda977d11a0a2c5aa79c9a09e951f3340af9..db4a56b6928c295857868d06a1819eed4941e4d3 100644 (file)
@@ -60,8 +60,11 @@ sys::IdentifyFileType(const char *magic, unsigned length) {
 
     case '\177':
       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
-        if (length >= 18 && magic[17] == 0)
-          switch (magic[16]) {
+        bool Data2MSB = magic[5] == 2;
+        unsigned high = Data2MSB ? 16 : 17;
+        unsigned low  = Data2MSB ? 17 : 16;
+        if (length >= 18 && magic[high] == 0)
+          switch (magic[low]) {
             default: break;
             case 1: return ELF_Relocatable_FileType;
             case 2: return ELF_Executable_FileType;
diff --git a/test/MC/Mips/elf-objdump.s b/test/MC/Mips/elf-objdump.s
new file mode 100644 (file)
index 0000000..6a5c2a5
--- /dev/null
@@ -0,0 +1,11 @@
+// 32 bit big endian
+// RUN: llvm-mc -filetype=obj -triple mips-unknown-linux %s -o - | llvm-objdump -d -triple mips-unknown-linux  - | FileCheck %s
+// 32 bit little endian
+// RUN: llvm-mc -filetype=obj -triple mipsel-unknown-linux %s -o - | llvm-objdump -d -triple mips-unknown-linux  - | FileCheck %s
+// 64 bit big endian
+// RUN: llvm-mc -filetype=obj -arch=mips64 -triple mips64-unknown-linux %s -o - | llvm-objdump -d -triple mips-unknown-linux - | FileCheck %s
+// 64 bit little endian
+// RUN: llvm-mc -filetype=obj -arch=mips64el -triple mips64el-unknown-linux %s -o - | llvm-objdump -d -triple mips-unknown-linux - | FileCheck %s
+
+// We just want to see if llvm-objdump works at all.
+// CHECK: .text