summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2013-04-05 20:10:04 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2013-04-05 20:10:04 +0000
commitc0a74e28cf937f0e847b1bb907c396411e2aac36 (patch)
tree4adae1662b66cc3fd51dcaa2b99fcf7bc14ea600
parent3455b32b3e795ea27a31b6cb1c225812515e3e2c (diff)
downloadllvm-c0a74e28cf937f0e847b1bb907c396411e2aac36.tar.gz
llvm-c0a74e28cf937f0e847b1bb907c396411e2aac36.tar.bz2
llvm-c0a74e28cf937f0e847b1bb907c396411e2aac36.tar.xz
[Support][FileSystem] Fix identify_magic for big endian ELF.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178905 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Support/PathV2.cpp7
-rw-r--r--unittests/Support/Path.cpp12
2 files changed, 15 insertions, 4 deletions
diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp
index 58a6ea720e..ac53a9e9e6 100644
--- a/lib/Support/PathV2.cpp
+++ b/lib/Support/PathV2.cpp
@@ -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;
diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp
index 4511259797..eec8c62d8c 100644
--- a/unittests/Support/Path.cpp
+++ b/unittests/Support/Path.cpp
@@ -298,12 +298,19 @@ TEST_F(FileSystemTest, DirectoryIteration) {
ASSERT_LT(z0, za1);
}
+const char elf[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
+
TEST_F(FileSystemTest, Magic) {
struct type {
const char *filename;
const char *magic_str;
- size_t magic_str_len;
- } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
+ size_t magic_str_len;
+ fs::file_magic magic;
+ } types [] = {
+ {"magic.archive", "!<arch>\x0A", 8, fs::file_magic::archive},
+ {"magic.elf", elf, sizeof(elf),
+ fs::file_magic::elf_relocatable}
+ };
// Create some files filled with magic.
for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
@@ -320,6 +327,7 @@ TEST_F(FileSystemTest, Magic) {
bool res = false;
ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
EXPECT_TRUE(res);
+ EXPECT_EQ(i->magic, fs::identify_magic(magic));
}
}