LCOV - code coverage report
Current view: top level - lib/Object - MachOUniversal.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 134 138 97.1 %
Date: 2017-09-14 15:23:50 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MachOUniversal.cpp - Mach-O universal binary -------------*- 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             : // This file defines the MachOUniversalBinary class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Object/MachOUniversal.h"
      15             : #include "llvm/Object/Archive.h"
      16             : #include "llvm/Object/MachO.h"
      17             : #include "llvm/Object/ObjectFile.h"
      18             : #include "llvm/Support/Casting.h"
      19             : #include "llvm/Support/Host.h"
      20             : #include "llvm/Support/MemoryBuffer.h"
      21             : 
      22             : using namespace llvm;
      23             : using namespace object;
      24             : 
      25             : static Error
      26         294 : malformedError(Twine Msg) {
      27        1176 :   std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
      28         294 :   return make_error<GenericBinaryError>(std::move(StringMsg),
      29        1176 :                                         object_error::parse_failed);
      30             : }
      31             : 
      32             : template<typename T>
      33             : static T getUniversalBinaryStruct(const char *Ptr) {
      34             :   T Res;
      35        1330 :   memcpy(&Res, Ptr, sizeof(T));
      36             :   // Universal binary headers have big-endian byte order.
      37             :   if (sys::IsLittleEndianHost)
      38        1330 :     swapStruct(Res);
      39             :   return Res;
      40             : }
      41             : 
      42        1110 : MachOUniversalBinary::ObjectForArch::ObjectForArch(
      43        1110 :     const MachOUniversalBinary *Parent, uint32_t Index)
      44        1110 :     : Parent(Parent), Index(Index) {
      45             :   // The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
      46        1110 :   if (!Parent || Index >= Parent->getNumberOfObjects()) {
      47         167 :     clear();
      48             :   } else {
      49             :     // Parse object header.
      50         943 :     StringRef ParentData = Parent->getData();
      51         943 :     if (Parent->getMagic() == MachO::FAT_MAGIC) {
      52        1840 :       const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
      53        1840 :                               Index * sizeof(MachO::fat_arch);
      54         920 :       Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
      55             :     } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
      56          46 :       const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
      57          46 :                               Index * sizeof(MachO::fat_arch_64);
      58          23 :       Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
      59             :     }
      60             :   }
      61        1110 : }
      62             : 
      63             : Expected<std::unique_ptr<MachOObjectFile>>
      64          72 : MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
      65          72 :   if (!Parent)
      66           0 :     report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
      67             :                        "called when Parent is a nullptr");
      68             : 
      69          72 :   StringRef ParentData = Parent->getData();
      70          72 :   StringRef ObjectData;
      71             :   uint32_t cputype;
      72          72 :   if (Parent->getMagic() == MachO::FAT_MAGIC) {
      73         134 :     ObjectData = ParentData.substr(Header.offset, Header.size);
      74          67 :     cputype = Header.cputype;
      75             :   } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
      76          10 :     ObjectData = ParentData.substr(Header64.offset, Header64.size);
      77           5 :     cputype = Header64.cputype;
      78             :   }
      79          72 :   StringRef ObjectName = Parent->getFileName();
      80          72 :   MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
      81          72 :   return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
      82             : }
      83             : 
      84             : Expected<std::unique_ptr<Archive>>
      85          27 : MachOUniversalBinary::ObjectForArch::getAsArchive() const {
      86          27 :   if (!Parent)
      87           0 :     report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
      88             :                        "called when Parent is a nullptr");
      89             : 
      90          27 :   StringRef ParentData = Parent->getData();
      91          27 :   StringRef ObjectData;
      92          27 :   if (Parent->getMagic() == MachO::FAT_MAGIC)
      93          50 :     ObjectData = ParentData.substr(Header.offset, Header.size);
      94             :   else // Parent->getMagic() == MachO::FAT_MAGIC_64
      95           4 :     ObjectData = ParentData.substr(Header64.offset, Header64.size);
      96          27 :   StringRef ObjectName = Parent->getFileName();
      97          27 :   MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
      98          27 :   return Archive::create(ObjBuffer);
      99             : }
     100             : 
     101           0 : void MachOUniversalBinary::anchor() { }
     102             : 
     103             : Expected<std::unique_ptr<MachOUniversalBinary>>
     104         387 : MachOUniversalBinary::create(MemoryBufferRef Source) {
     105        1161 :   Error Err = Error::success();
     106             :   std::unique_ptr<MachOUniversalBinary> Ret(
     107        1161 :       new MachOUniversalBinary(Source, Err));
     108         387 :   if (Err)
     109        1176 :     return std::move(Err);
     110          93 :   return std::move(Ret);
     111             : }
     112             : 
     113         387 : MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
     114             :     : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
     115         387 :       NumberOfObjects(0) {
     116         480 :   ErrorAsOutParameter ErrAsOutParam(&Err);
     117         774 :   if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
     118           0 :     Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
     119             :                                          "universal file",
     120             :                                          object_error::invalid_file_type);
     121         294 :     return;
     122             :   }
     123             :   // Check for magic value and sufficient header size.
     124         387 :   StringRef Buf = getData();
     125             :   MachO::fat_header H =
     126         774 :       getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
     127         387 :   Magic = H.magic;
     128         387 :   NumberOfObjects = H.nfat_arch;
     129         387 :   if (NumberOfObjects == 0) {
     130           3 :     Err = malformedError("contains zero architecture types");
     131           1 :     return;
     132             :   }
     133         386 :   uint32_t MinSize = sizeof(MachO::fat_header);
     134         386 :   if (Magic == MachO::FAT_MAGIC)
     135          96 :     MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
     136         290 :   else if (Magic == MachO::FAT_MAGIC_64)
     137           4 :     MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
     138             :   else {
     139         858 :     Err = malformedError("bad magic number");
     140         286 :     return;
     141             :   }
     142         100 :   if (Buf.size() < MinSize) {
     143           6 :     Err = malformedError("fat_arch" +
     144           4 :                          Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
     145             :                          " structs would extend past the end of the file");
     146           1 :     return;
     147             :   }
     148         585 :   for (uint32_t i = 0; i < NumberOfObjects; i++) {
     149         247 :     ObjectForArch A(this, i);
     150         247 :     uint64_t bigSize = A.getOffset();
     151         247 :     bigSize += A.getSize();
     152         247 :     if (bigSize > Buf.size()) {
     153           7 :       Err = malformedError("offset plus size of cputype (" +
     154           8 :         Twine(A.getCPUType()) + ") cpusubtype (" +
     155           3 :         Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
     156             :         ") extends past the end of the file");
     157           5 :       return;
     158             :     }
     159             : #define MAXSECTALIGN 15 /* 2**15 or 0x8000 */
     160         246 :     if (A.getAlign() > MAXSECTALIGN) {
     161          11 :       Err = malformedError("align (2^" + Twine(A.getAlign()) + ") too large "
     162           9 :         "for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
     163           5 :         Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
     164           5 :         ") (maximum 2^" + Twine(MAXSECTALIGN) + ")");
     165           1 :       return;
     166             :     }
     167         490 :     if(A.getOffset() % (1 << A.getAlign()) != 0){
     168          11 :       Err = malformedError("offset: " + Twine(A.getOffset()) +
     169           9 :         " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
     170           5 :         Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
     171           6 :         ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
     172           1 :       return;
     173             :     }
     174         244 :     if (A.getOffset() < MinSize) {
     175          11 :       Err =  malformedError("cputype (" + Twine(A.getCPUType()) + ") "
     176           7 :         "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
     177           6 :         ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
     178           1 :       return;
     179             :     }
     180             :   }
     181         571 :   for (uint32_t i = 0; i < NumberOfObjects; i++) {
     182         240 :     ObjectForArch A(this, i);
     183         459 :     for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
     184         221 :       ObjectForArch B(this, j);
     185         508 :       if (A.getCPUType() == B.getCPUType() &&
     186         132 :           (A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
     187          66 :           (B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
     188           5 :         Err = malformedError("contains two of the same architecture (cputype "
     189           8 :           "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
     190           3 :           Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
     191           3 :         return;
     192             :       }
     193         480 :       if ((A.getOffset() >= B.getOffset() &&
     194         340 :            A.getOffset() < B.getOffset() + B.getSize()) ||
     195         701 :           (A.getOffset() + A.getSize() > B.getOffset() &&
     196         603 :            A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
     197         617 :           (A.getOffset() <= B.getOffset() &&
     198         716 :            A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
     199           9 :         Err =  malformedError("cputype (" + Twine(A.getCPUType()) + ") "
     200           7 :           "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
     201           9 :           ") at offset " + Twine(A.getOffset()) + " with a size of " +
     202          10 :           Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
     203           6 :           ") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
     204           9 :           + ") at offset " + Twine(B.getOffset()) + " with a size of "
     205           3 :           + Twine(B.getSize()));
     206           1 :         return;
     207             :       }
     208             :     }
     209             :   }
     210         279 :   Err = Error::success();
     211             : }
     212             : 
     213             : Expected<std::unique_ptr<MachOObjectFile>>
     214          16 : MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
     215          32 :   if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
     216           4 :     return make_error<GenericBinaryError>("Unknown architecture "
     217           2 :                                           "named: " +
     218             :                                               ArchName,
     219           1 :                                           object_error::arch_not_found);
     220             : 
     221          73 :   for (auto &Obj : objects())
     222         112 :     if (Obj.getArchFlagName() == ArchName)
     223          14 :       return Obj.getAsObjectFile();
     224           3 :   return make_error<GenericBinaryError>("fat file does not "
     225           2 :                                         "contain " +
     226             :                                             ArchName,
     227           1 :                                         object_error::arch_not_found);
     228             : }

Generated by: LCOV version 1.13