From b850a05f2c914fef74dbafbf61470512a976c632 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 14 Nov 2013 22:09:08 +0000 Subject: [PATCH] Recognize 0x0000 as a COFF file magic. Summary: Some machine-type-neutral object files containing only undefined symbols actually do exist in the Windows standard library. Need to recognize them as COFF files. Reviewers: Bigcheese CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D2164 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194734 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Path.cpp | 3 +++ .../llvm-readobj/Inputs/magic.coff-unknown | Bin 0 -> 450 bytes test/tools/llvm-readobj/file-headers.test | 16 ++++++++++++++++ unittests/Support/Path.cpp | 2 ++ 4 files changed, 21 insertions(+) create mode 100644 test/tools/llvm-readobj/Inputs/magic.coff-unknown diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 7fc9ab89672..dfa18aaa6d2 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -853,6 +853,9 @@ error_code has_magic(const Twine &path, const Twine &magic, bool &result) { if (Magic.size() >= sizeof(Expected) && memcmp(Magic.data(), Expected, sizeof(Expected)) == 0) return file_magic::windows_resource; + // 0x0000 = COFF unknown machine type + if (Magic[1] == 0) + return file_magic::coff_object; break; } case 0xDE: // 0x0B17C0DE = BC wraper diff --git a/test/tools/llvm-readobj/Inputs/magic.coff-unknown b/test/tools/llvm-readobj/Inputs/magic.coff-unknown new file mode 100644 index 0000000000000000000000000000000000000000..7b3b4619a8cde277a42dffaeca18ff43906b4f79 GIT binary patch literal 450 zcmZQzU}i|LYYqCrz`(!_#2}zol3Gy$Vd5ke7y=UXQW8rNajJkwI4}ep&`U{8Dos}j zhAGVfGJ0SfDDA)?;KTxCe+1%qAXZ^u6a+~*Tg4<-rk7{t#bo5?rp6SNW|qd3=B1>j z>E$P7F_<&h2r@G9054!WegFUf literal 0 HcmV?d00001 diff --git a/test/tools/llvm-readobj/file-headers.test b/test/tools/llvm-readobj/file-headers.test index b900e365f20..d88029adddf 100644 --- a/test/tools/llvm-readobj/file-headers.test +++ b/test/tools/llvm-readobj/file-headers.test @@ -8,6 +8,8 @@ RUN: llvm-readobj -h %p/Inputs/trivial.obj.elf-i386 \ RUN: | FileCheck %s -check-prefix ELF32 RUN: llvm-readobj -h %p/Inputs/trivial.obj.elf-x86-64 \ RUN: | FileCheck %s -check-prefix ELF64 +RUN: llvm-readobj -h %p/Inputs/magic.coff-unknown \ +RUN: | FileCheck %s -check-prefix COFF-UNKNOWN COFF32: File: {{(.*[/\\])?}}trivial.obj.coff-i386 COFF32-NEXT: Format: COFF-i386 @@ -183,3 +185,17 @@ PE32-NEXT: ReservedRVA: 0x0 PE32-NEXT: ReservedSize: 0x0 PE32-NEXT: } PE32-NEXT: } + +COFF-UNKNOWN: Format: COFF- +COFF-UNKNOWN-NEXT: Arch: unknown +COFF-UNKNOWN-NEXT: AddressSize: 32bit +COFF-UNKNOWN-NEXT: ImageFileHeader { +COFF-UNKNOWN-NEXT: Machine: IMAGE_FILE_MACHINE_UNKNOWN (0x0) +COFF-UNKNOWN-NEXT: SectionCount: 3 +COFF-UNKNOWN-NEXT: TimeDateStamp: 2013-11-14 21:19:28 (0x52853E60) +COFF-UNKNOWN-NEXT: PointerToSymbolTable: 0xF8 +COFF-UNKNOWN-NEXT: SymbolCount: 11 +COFF-UNKNOWN-NEXT: OptionalHeaderSize: 0 +COFF-UNKNOWN-NEXT: Characteristics [ (0x0) +COFF-UNKNOWN-NEXT: ] +COFF-UNKNOWN-NEXT: } diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index caf17b9b225..2323029b589 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -418,6 +418,7 @@ TEST_F(FileSystemTest, DirectoryIteration) { const char archive[] = "!\x0A"; const char bitcode[] = "\xde\xc0\x17\x0b"; +const char coff_object[] = "\x00\x00......"; const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; @@ -445,6 +446,7 @@ TEST_F(FileSystemTest, Magic) { { #magic, magic, sizeof(magic), fs::file_magic::magic } DEFINE(archive), DEFINE(bitcode), + DEFINE(coff_object), DEFINE(elf_relocatable), DEFINE(macho_universal_binary), DEFINE(macho_object),