LCOV - code coverage report
Current view: top level - lib/BinaryFormat - Magic.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 76 78 97.4 %
Date: 2018-07-13 00:08:38 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       18689 : file_magic llvm::identify_magic(StringRef Magic) {
      36       18689 :   if (Magic.size() < 4)
      37          39 :     return file_magic::unknown;
      38       37300 :   switch ((unsigned char)Magic[0]) {
      39         861 :   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         353 :       if (Magic.size() < MinSize)
      45           1 :         return file_magic::coff_import_library;
      46             : 
      47         352 :       const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
      48         352 :       if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
      49           2 :         return file_magic::coff_object;
      50         350 :       if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
      51           2 :         return file_magic::coff_cl_gl_object;
      52         348 :       return file_magic::coff_import_library;
      53             :     }
      54             :     // Windows resource file
      55        1007 :     if (Magic.size() >= sizeof(COFF::WinResMagic) &&
      56         499 :         memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
      57          46 :       return file_magic::windows_resource;
      58             :     // 0x0000 = COFF unknown machine type
      59         462 :     if (Magic[1] == 0)
      60          20 :       return file_magic::coff_object;
      61             :     if (startswith(Magic, "\0asm"))
      62         442 :       return file_magic::wasm_object;
      63             :     break;
      64             :   }
      65          48 :   case 0xDE: // 0x0B17C0DE = BC wraper
      66             :     if (startswith(Magic, "\xDE\xC0\x17\x0B"))
      67          48 :       return file_magic::bitcode;
      68             :     break;
      69        1544 :   case 'B':
      70             :     if (startswith(Magic, "BC\xC0\xDE"))
      71        1544 :       return file_magic::bitcode;
      72             :     break;
      73         310 :   case '!':
      74             :     if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
      75         310 :       return file_magic::archive;
      76             :     break;
      77             : 
      78       11354 :   case '\177':
      79       11354 :     if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
      80             :       bool Data2MSB = Magic[5] == 2;
      81       11354 :       unsigned high = Data2MSB ? 16 : 17;
      82       11354 :       unsigned low = Data2MSB ? 17 : 16;
      83       22708 :       if (Magic[high] == 0) {
      84       22698 :         switch (Magic[low]) {
      85             :         default:
      86           1 :           return file_magic::elf;
      87             :         case 1:
      88        8858 :           return file_magic::elf_relocatable;
      89             :         case 2:
      90        1373 :           return file_magic::elf_executable;
      91             :         case 3:
      92        1117 :           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          69 :   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         138 :       if (Magic.size() >= 8 && Magic[7] < 43)
     108          69 :         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        1767 :   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          28 :       if (Magic[3] == char(0xCE))
     124             :         MinSize = sizeof(MachO::mach_header);
     125             :       else
     126             :         MinSize = sizeof(MachO::mach_header_64);
     127          28 :       if (Magic.size() >= MinSize)
     128          84 :         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        1738 :       if (Magic[0] == char(0xCE))
     134             :         MinSize = sizeof(MachO::mach_header);
     135             :       else
     136             :         MinSize = sizeof(MachO::mach_header_64);
     137        1738 :       if (Magic.size() >= MinSize)
     138        5211 :         type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
     139             :     }
     140        1765 :     switch (type) {
     141             :     default:
     142             :       break;
     143             :     case 1:
     144        1252 :       return file_magic::macho_object;
     145             :     case 2:
     146         285 :       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         131 :       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          77 :       return file_magic::macho_dsym_companion;
     163             :     case 11:
     164           6 :       return file_magic::macho_kext_bundle;
     165             :     }
     166             :     break;
     167             :   }
     168         491 :   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         491 :     if (Magic[1] == 0x01)
     176         473 :       return file_magic::coff_object;
     177             :     LLVM_FALLTHROUGH;
     178             : 
     179             :   case 0x90: // PA-RISC Windows
     180             :   case 0x68: // mc68K Windows
     181          19 :     if (Magic[1] == 0x02)
     182           0 :       return file_magic::coff_object;
     183             :     break;
     184             : 
     185         342 :   case 'M': // Possible MS-DOS stub on Windows PE file or MSF/PDB file.
     186         298 :     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         297 :       if (Magic.substr(off).startswith(
     190             :               StringRef(COFF::PEMagic, sizeof(COFF::PEMagic))))
     191         296 :         return file_magic::pecoff_executable;
     192             :     }
     193             :     if (Magic.startswith("Microsoft C/C++ MSF 7.00\r\n"))
     194          44 :       return file_magic::pdb;
     195             :     break;
     196             : 
     197        1333 :   case 0x64: // x86-64 or ARM64 Windows.
     198        1333 :     if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
     199        1333 :       return file_magic::coff_object;
     200             :     break;
     201             : 
     202             :   default:
     203             :     break;
     204             :   }
     205         553 :   return file_magic::unknown;
     206             : }
     207             : 
     208          48 : std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
     209          48 :   auto FileOrError = MemoryBuffer::getFile(Path);
     210          48 :   if (!FileOrError)
     211             :     return FileOrError.getError();
     212             : 
     213             :   std::unique_ptr<MemoryBuffer> FileBuffer = std::move(*FileOrError);
     214          48 :   Result = identify_magic(FileBuffer->getBuffer());
     215             : 
     216          48 :   return std::error_code();
     217             : }

Generated by: LCOV version 1.13