[Support][FileSystem] Fix identify_magic for big endian ELF.
[oota-llvm.git] / lib / Support / PathV2.cpp
index 58a6ea720e733a41f3159220132d14591e367a62..ac53a9e9e6b41ec985281e3ed94314f1c6115085 100644 (file)
@@ -789,8 +789,11 @@ file_magic identify_magic(StringRef magic) {
 
     case '\177':
       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
-        if (magic.size() >= 18 && magic[17] == 0)
-          switch (magic[16]) {
+        bool Data2MSB = magic[5] == 2;
+        unsigned high = Data2MSB ? 16 : 17;
+        unsigned low  = Data2MSB ? 17 : 16;
+        if (magic.size() >= 18 && magic[high] == 0)
+          switch (magic[low]) {
             default: break;
             case 1: return file_magic::elf_relocatable;
             case 2: return file_magic::elf_executable;