LCOV - code coverage report
Current view: top level - lib/BinaryFormat - Magic.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 75 77 97.4 %
Date: 2018-10-20 13:21:21 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/BinaryFormat/Magic.cpp - File magic identification --*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/BinaryFormat/Magic.h"
      11             : 
      12             : #include "llvm/BinaryFormat/COFF.h"
      13             : #include "llvm/BinaryFormat/ELF.h"
      14             : #include "llvm/BinaryFormat/MachO.h"
      15             : #include "llvm/Support/Endian.h"
      16             : #include "llvm/Support/FileSystem.h"
      17             : #include "llvm/Support/MemoryBuffer.h"
      18             : 
      19             : #if !defined(_MSC_VER) && !defined(__MINGW32__)
      20             : #include <unistd.h>
      21             : #else
      22             : #include <io.h>
      23             : #endif
      24             : 
      25             : using namespace llvm;
      26             : using namespace llvm::support::endian;
      27             : using namespace llvm::sys::fs;
      28             : 
      29             : template <size_t N>
      30             : static bool startswith(StringRef Magic, const char (&S)[N]) {
      31             :   return Magic.startswith(StringRef(S, N - 1));
      32             : }
      33             : 
      34             : /// Identify the magic in magic.
      35       21698 : file_magic llvm::identify_magic(StringRef Magic) {
      36       21698 :   if (Magic.size() < 4)
      37          67 :     return file_magic::unknown;
      38       43262 :   switch ((unsigned char)Magic[0]) {
      39        1331 :   case 0x00: {
      40             :     // COFF bigobj, CL.exe's LTO object file, or short import library file
      41             :     if (startswith(Magic, "\0\0\xFF\xFF")) {
      42             :       size_t MinSize =
      43             :           offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
      44         599 :       if (Magic.size() < MinSize)
      45           1 :         return file_magic::coff_import_library;
      46             : 
      47         598 :       const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
      48         598 :       if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
      49           2 :         return file_magic::coff_object;
      50         596 :       if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
      51           2 :         return file_magic::coff_cl_gl_object;
      52         594 :       return file_magic::coff_import_library;
      53             :     }
      54             :     // Windows resource file
      55         732 :     if (Magic.size() >= sizeof(COFF::WinResMagic) &&
      56         723 :         memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
      57          47 :       return file_magic::windows_resource;
      58             :     // 0x0000 = COFF unknown machine type
      59         685 :     if (Magic[1] == 0)
      60          26 :       return file_magic::coff_object;
      61             :     if (startswith(Magic, "\0asm"))
      62         659 :       return file_magic::wasm_object;
      63             :     break;
      64             :   }
      65          51 :   case 0xDE: // 0x0B17C0DE = BC wraper
      66             :     if (startswith(Magic, "\xDE\xC0\x17\x0B"))
      67          51 :       return file_magic::bitcode;
      68             :     break;
      69        1672 :   case 'B':
      70             :     if (startswith(Magic, "BC\xC0\xDE"))
      71        1672 :       return file_magic::bitcode;
      72             :     break;
      73         343 :   case '!':
      74             :     if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
      75         343 :       return file_magic::archive;
      76             :     break;
      77             : 
      78       12634 :   case '\177':
      79       12634 :     if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
      80             :       bool Data2MSB = Magic[5] == 2;
      81       12634 :       unsigned high = Data2MSB ? 16 : 17;
      82       12634 :       unsigned low = Data2MSB ? 17 : 16;
      83       25268 :       if (Magic[high] == 0) {
      84       25258 :         switch (Magic[low]) {
      85             :         default:
      86           1 :           return file_magic::elf;
      87             :         case 1:
      88       10017 :           return file_magic::elf_relocatable;
      89             :         case 2:
      90        1458 :           return file_magic::elf_executable;
      91             :         case 3:
      92        1153 :           return file_magic::elf_shared_object;
      93             :         case 4:
      94           0 :           return file_magic::elf_core;
      95             :         }
      96             :       }
      97             :       // It's still some type of ELF file.
      98           5 :       return file_magic::elf;
      99             :     }
     100             :     break;
     101             : 
     102          70 :   case 0xCA:
     103             :     if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
     104             :         startswith(Magic, "\xCA\xFE\xBA\xBF")) {
     105             :       // This is complicated by an overlap with Java class files.
     106             :       // See the Mach-O section in /usr/share/file/magic for details.
     107          70 :       if (Magic.size() >= 8 && Magic[7] < 43)
     108          70 :         return file_magic::macho_universal_binary;
     109             :     }
     110             :     break;
     111             : 
     112             :   // The two magic numbers for mach-o are:
     113             :   // 0xfeedface - 32-bit mach-o
     114             :   // 0xfeedfacf - 64-bit mach-o
     115        1846 :   case 0xFE:
     116             :   case 0xCE:
     117             :   case 0xCF: {
     118             :     uint16_t type = 0;
     119             :     if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
     120             :         startswith(Magic, "\xFE\xED\xFA\xCF")) {
     121             :       /* Native endian */
     122             :       size_t MinSize;
     123          24 :       if (Magic[3] == char(0xCE))
     124             :         MinSize = sizeof(MachO::mach_header);
     125             :       else
     126             :         MinSize = sizeof(MachO::mach_header_64);
     127          24 :       if (Magic.size() >= MinSize)
     128          72 :         type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
     129             :     } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
     130             :                startswith(Magic, "\xCF\xFA\xED\xFE")) {
     131             :       /* Reverse endian */
     132             :       size_t MinSize;
     133        1821 :       if (Magic[0] == char(0xCE))
     134             :         MinSize = sizeof(MachO::mach_header);
     135             :       else
     136             :         MinSize = sizeof(MachO::mach_header_64);
     137        1821 :       if (Magic.size() >= MinSize)
     138        5460 :         type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
     139             :     }
     140             :     switch (type) {
     141             :     default:
     142             :       break;
     143             :     case 1:
     144        1311 :       return file_magic::macho_object;
     145             :     case 2:
     146         292 :       return file_magic::macho_executable;
     147             :     case 3:
     148           1 :       return file_magic::macho_fixed_virtual_memory_shared_lib;
     149             :     case 4:
     150           3 :       return file_magic::macho_core;
     151             :     case 5:
     152           2 :       return file_magic::macho_preload_executable;
     153             :     case 6:
     154         130 :       return file_magic::macho_dynamically_linked_shared_lib;
     155             :     case 7:
     156           1 :       return file_magic::macho_dynamic_linker;
     157             :     case 8:
     158           5 :       return file_magic::macho_bundle;
     159             :     case 9:
     160           2 :       return file_magic::macho_dynamically_linked_shared_lib_stub;
     161             :     case 10:
     162          91 :       return file_magic::macho_dsym_companion;
     163             :     case 11:
     164           6 :       return file_magic::macho_kext_bundle;
     165             :     }
     166             :     break;
     167             :   }
     168         676 :   case 0xF0: // PowerPC Windows
     169             :   case 0x83: // Alpha 32-bit
     170             :   case 0x84: // Alpha 64-bit
     171             :   case 0x66: // MPS R4000 Windows
     172             :   case 0x50: // mc68K
     173             :   case 0x4c: // 80386 Windows
     174             :   case 0xc4: // ARMNT Windows
     175         676 :     if (Magic[1] == 0x01)
     176         650 :       return file_magic::coff_object;
     177             :     LLVM_FALLTHROUGH;
     178             : 
     179             :   case 0x90: // PA-RISC Windows
     180             :   case 0x68: // mc68K Windows
     181          28 :     if (Magic[1] == 0x02)
     182           0 :       return file_magic::coff_object;
     183             :     break;
     184             : 
     185         379 :   case 'M': // Possible MS-DOS stub on Windows PE file or MSF/PDB file.
     186         330 :     if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
     187             :       uint32_t off = read32le(Magic.data() + 0x3c);
     188             :       // PE/COFF file, either EXE or DLL.
     189         658 :       if (Magic.substr(off).startswith(
     190             :               StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
     191         328 :         return file_magic::pecoff_executable;
     192             :     }
     193             :     if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
     194          49 :       return file_magic::pdb;
     195             :     break;
     196             : 
     197        2036 :   case 0x64: // x86-64 or ARM64 Windows.
     198        2036 :     if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
     199        2036 :       return file_magic::coff_object;
     200             :     break;
     201             : 
     202             :   default:
     203             :     break;
     204             :   }
     205         623 :   return file_magic::unknown;
     206             : }
     207             : 
     208          55 : std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
     209          55 :   auto FileOrError = MemoryBuffer::getFile(Path);
     210          55 :   if (!FileOrError)
     211             :     return FileOrError.getError();
     212             : 
     213             :   std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
     214          55 :   Result = identify_magic(FileBuffer->getBuffer());
     215             : 
     216          55 :   return std::error_code();
     217             : }

Generated by: LCOV version 1.13