LCOV - code coverage report
Current view: top level - lib/BinaryFormat - Magic.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 100 105 95.2 %
Date: 2017-09-14 15:23:50 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             : 
      18             : #if !defined(_MSC_VER) && !defined(__MINGW32__)
      19             : #include <unistd.h>
      20             : #else
      21             : #include <io.h>
      22             : #endif
      23             : 
      24             : using namespace llvm;
      25             : using namespace llvm::support::endian;
      26             : using namespace llvm::sys::fs;
      27             : 
      28             : template <size_t N>
      29             : static bool startswith(StringRef Magic, const char (&S)[N]) {
      30       28963 :   return Magic.startswith(StringRef(S, N - 1));
      31             : }
      32             : 
      33             : /// @brief Identify the magic in magic.
      34       13123 : file_magic llvm::identify_magic(StringRef Magic) {
      35       13123 :   if (Magic.size() < 4)
      36          39 :     return file_magic::unknown;
      37       26168 :   switch ((unsigned char)Magic[0]) {
      38             :   case 0x00: {
      39             :     // COFF bigobj, CL.exe's LTO object file, or short import library file
      40         279 :     if (startswith(Magic, "\0\0\xFF\xFF")) {
      41         211 :       size_t MinSize =
      42             :           offsetof(COFF::BigObjHeader, UUID) + sizeof(COFF::BigObjMagic);
      43         211 :       if (Magic.size() < MinSize)
      44           1 :         return file_magic::coff_import_library;
      45             : 
      46         210 :       const char *Start = Magic.data() + offsetof(COFF::BigObjHeader, UUID);
      47         210 :       if (memcmp(Start, COFF::BigObjMagic, sizeof(COFF::BigObjMagic)) == 0)
      48           2 :         return file_magic::coff_object;
      49         208 :       if (memcmp(Start, COFF::ClGlObjMagic, sizeof(COFF::BigObjMagic)) == 0)
      50           2 :         return file_magic::coff_cl_gl_object;
      51         206 :       return file_magic::coff_import_library;
      52             :     }
      53             :     // Windows resource file
      54         128 :     if (Magic.size() >= sizeof(COFF::WinResMagic) &&
      55          60 :         memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
      56          19 :       return file_magic::windows_resource;
      57             :     // 0x0000 = COFF unknown machine type
      58          98 :     if (Magic[1] == 0)
      59          16 :       return file_magic::coff_object;
      60          33 :     if (startswith(Magic, "\0asm"))
      61          33 :       return file_magic::wasm_object;
      62             :     break;
      63             :   }
      64             :   case 0xDE: // 0x0B17C0DE = BC wraper
      65          49 :     if (startswith(Magic, "\xDE\xC0\x17\x0B"))
      66          49 :       return file_magic::bitcode;
      67             :     break;
      68             :   case 'B':
      69        1142 :     if (startswith(Magic, "BC\xC0\xDE"))
      70        1142 :       return file_magic::bitcode;
      71             :     break;
      72         233 :   case '!':
      73             :     if (startswith(Magic, "!<arch>\n") || startswith(Magic, "!<thin>\n"))
      74         233 :       return file_magic::archive;
      75             :     break;
      76             : 
      77             :   case '\177':
      78        7857 :     if (startswith(Magic, "\177ELF") && Magic.size() >= 18) {
      79       15714 :       bool Data2MSB = Magic[5] == 2;
      80        7857 :       unsigned high = Data2MSB ? 16 : 17;
      81        7857 :       unsigned low = Data2MSB ? 17 : 16;
      82       15714 :       if (Magic[high] == 0) {
      83       15708 :         switch (Magic[low]) {
      84           1 :         default:
      85           1 :           return file_magic::elf;
      86        6098 :         case 1:
      87        6098 :           return file_magic::elf_relocatable;
      88         960 :         case 2:
      89         960 :           return file_magic::elf_executable;
      90         795 :         case 3:
      91         795 :           return file_magic::elf_shared_object;
      92           0 :         case 4:
      93           0 :           return file_magic::elf_core;
      94             :         }
      95             :       }
      96             :       // It's still some type of ELF file.
      97           3 :       return file_magic::elf;
      98             :     }
      99             :     break;
     100             : 
     101             :   case 0xCA:
     102           4 :     if (startswith(Magic, "\xCA\xFE\xBA\xBE") ||
     103             :         startswith(Magic, "\xCA\xFE\xBA\xBF")) {
     104             :       // This is complicated by an overlap with Java class files.
     105             :       // See the Mach-O section in /usr/share/file/magic for details.
     106         118 :       if (Magic.size() >= 8 && Magic[7] < 43)
     107          59 :         return file_magic::macho_universal_binary;
     108             :     }
     109             :     break;
     110             : 
     111             :   // The two magic numbers for mach-o are:
     112             :   // 0xfeedface - 32-bit mach-o
     113             :   // 0xfeedfacf - 64-bit mach-o
     114             :   case 0xFE:
     115             :   case 0xCE:
     116             :   case 0xCF: {
     117        1512 :     uint16_t type = 0;
     118        1491 :     if (startswith(Magic, "\xFE\xED\xFA\xCE") ||
     119        1486 :         startswith(Magic, "\xFE\xED\xFA\xCF")) {
     120             :       /* Native endian */
     121             :       size_t MinSize;
     122          52 :       if (Magic[3] == char(0xCE))
     123             :         MinSize = sizeof(MachO::mach_header);
     124             :       else
     125           5 :         MinSize = sizeof(MachO::mach_header_64);
     126          26 :       if (Magic.size() >= MinSize)
     127         104 :         type = Magic[12] << 24 | Magic[13] << 12 | Magic[14] << 8 | Magic[15];
     128        1015 :     } else if (startswith(Magic, "\xCE\xFA\xED\xFE") ||
     129             :                startswith(Magic, "\xCF\xFA\xED\xFE")) {
     130             :       /* Reverse endian */
     131             :       size_t MinSize;
     132        1485 :       if (Magic[0] == char(0xCE))
     133             :         MinSize = sizeof(MachO::mach_header);
     134             :       else
     135        1014 :         MinSize = sizeof(MachO::mach_header_64);
     136        1485 :       if (Magic.size() >= MinSize)
     137        5936 :         type = Magic[15] << 24 | Magic[14] << 12 | Magic[13] << 8 | Magic[12];
     138             :     }
     139        1510 :     switch (type) {
     140             :     default:
     141             :       break;
     142        1085 :     case 1:
     143        1085 :       return file_magic::macho_object;
     144         241 :     case 2:
     145         241 :       return file_magic::macho_executable;
     146           1 :     case 3:
     147           1 :       return file_magic::macho_fixed_virtual_memory_shared_lib;
     148           4 :     case 4:
     149           4 :       return file_magic::macho_core;
     150           1 :     case 5:
     151           1 :       return file_magic::macho_preload_executable;
     152         127 :     case 6:
     153         127 :       return file_magic::macho_dynamically_linked_shared_lib;
     154           1 :     case 7:
     155           1 :       return file_magic::macho_dynamic_linker;
     156           5 :     case 8:
     157           5 :       return file_magic::macho_bundle;
     158           2 :     case 9:
     159           2 :       return file_magic::macho_dynamically_linked_shared_lib_stub;
     160          39 :     case 10:
     161          39 :       return file_magic::macho_dsym_companion;
     162           4 :     case 11:
     163           4 :       return file_magic::macho_kext_bundle;
     164             :     }
     165             :     break;
     166             :   }
     167         357 :   case 0xF0: // PowerPC Windows
     168             :   case 0x83: // Alpha 32-bit
     169             :   case 0x84: // Alpha 64-bit
     170             :   case 0x66: // MPS R4000 Windows
     171             :   case 0x50: // mc68K
     172             :   case 0x4c: // 80386 Windows
     173             :   case 0xc4: // ARMNT Windows
     174         714 :     if (Magic[1] == 0x01)
     175         339 :       return file_magic::coff_object;
     176             :     LLVM_FALLTHROUGH;
     177             : 
     178             :   case 0x90: // PA-RISC Windows
     179             :   case 0x68: // mc68K Windows
     180          38 :     if (Magic[1] == 0x02)
     181           0 :       return file_magic::coff_object;
     182             :     break;
     183             : 
     184             :   case 'M': // Possible MS-DOS stub on Windows PE file
     185         203 :     if (startswith(Magic, "MZ") && Magic.size() >= 0x3c + 4) {
     186         404 :       uint32_t off = read32le(Magic.data() + 0x3c);
     187             :       // PE/COFF file, either EXE or DLL.
     188         404 :       if (off < Magic.size() &&
     189         202 :           memcmp(Magic.data() + off, COFF::PEMagic, sizeof(COFF::PEMagic)) == 0)
     190         202 :         return file_magic::pecoff_executable;
     191             :     }
     192             :     break;
     193             : 
     194         842 :   case 0x64: // x86-64 or ARM64 Windows.
     195        1684 :     if (Magic[1] == char(0x86) || Magic[1] == char(0xaa))
     196         842 :       return file_magic::coff_object;
     197             :     break;
     198             : 
     199             :   default:
     200             :     break;
     201             :   }
     202         572 :   return file_magic::unknown;
     203             : }
     204             : 
     205          37 : std::error_code llvm::identify_magic(const Twine &Path, file_magic &Result) {
     206             :   int FD;
     207          37 :   if (std::error_code EC = openFileForRead(Path, FD))
     208           0 :     return EC;
     209             : 
     210             :   char Buffer[32];
     211          37 :   int Length = read(FD, Buffer, sizeof(Buffer));
     212          37 :   if (close(FD) != 0 || Length < 0)
     213           0 :     return std::error_code(errno, std::generic_category());
     214             : 
     215          74 :   Result = identify_magic(StringRef(Buffer, Length));
     216          37 :   return std::error_code();
     217             : }

Generated by: LCOV version 1.13