Home | History | Annotate | Download | only in Object
      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/MachO.h"
     16 #include "llvm/Object/ObjectFile.h"
     17 #include "llvm/Object/Archive.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 template<typename T>
     26 static void SwapStruct(T &Value);
     27 
     28 template<>
     29 void SwapStruct(MachO::fat_header &H) {
     30   sys::swapByteOrder(H.magic);
     31   sys::swapByteOrder(H.nfat_arch);
     32 }
     33 
     34 template<>
     35 void SwapStruct(MachO::fat_arch &H) {
     36   sys::swapByteOrder(H.cputype);
     37   sys::swapByteOrder(H.cpusubtype);
     38   sys::swapByteOrder(H.offset);
     39   sys::swapByteOrder(H.size);
     40   sys::swapByteOrder(H.align);
     41 }
     42 
     43 template<typename T>
     44 static T getUniversalBinaryStruct(const char *Ptr) {
     45   T Res;
     46   memcpy(&Res, Ptr, sizeof(T));
     47   // Universal binary headers have big-endian byte order.
     48   if (sys::IsLittleEndianHost)
     49     SwapStruct(Res);
     50   return Res;
     51 }
     52 
     53 MachOUniversalBinary::ObjectForArch::ObjectForArch(
     54     const MachOUniversalBinary *Parent, uint32_t Index)
     55     : Parent(Parent), Index(Index) {
     56   if (!Parent || Index >= Parent->getNumberOfObjects()) {
     57     clear();
     58   } else {
     59     // Parse object header.
     60     StringRef ParentData = Parent->getData();
     61     const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
     62                             Index * sizeof(MachO::fat_arch);
     63     Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
     64     if (ParentData.size() < Header.offset + Header.size) {
     65       clear();
     66     }
     67   }
     68 }
     69 
     70 ErrorOr<std::unique_ptr<ObjectFile>>
     71 MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
     72   if (Parent) {
     73     StringRef ParentData = Parent->getData();
     74     StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
     75     std::string ObjectName = Parent->getFileName().str();
     76     std::unique_ptr<MemoryBuffer> ObjBuffer(
     77         MemoryBuffer::getMemBuffer(ObjectData, ObjectName, false));
     78     return ObjectFile::createMachOObjectFile(ObjBuffer);
     79   }
     80   return object_error::parse_failed;
     81 }
     82 
     83 std::error_code MachOUniversalBinary::ObjectForArch::getAsArchive(
     84     std::unique_ptr<Archive> &Result) const {
     85   if (Parent) {
     86     StringRef ParentData = Parent->getData();
     87     StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
     88     std::string ObjectName = Parent->getFileName().str();
     89     std::unique_ptr<MemoryBuffer> ObjBuffer(
     90         MemoryBuffer::getMemBuffer(ObjectData, ObjectName, false));
     91     ErrorOr<Archive *> Obj = Archive::create(std::move(ObjBuffer));
     92     if (std::error_code EC = Obj.getError())
     93       return EC;
     94     Result.reset(Obj.get());
     95     return object_error::success;
     96   }
     97   return object_error::parse_failed;
     98 }
     99 
    100 void MachOUniversalBinary::anchor() { }
    101 
    102 ErrorOr<MachOUniversalBinary *>
    103 MachOUniversalBinary::create(std::unique_ptr<MemoryBuffer> Source) {
    104   std::error_code EC;
    105   std::unique_ptr<MachOUniversalBinary> Ret(
    106       new MachOUniversalBinary(std::move(Source), EC));
    107   if (EC)
    108     return EC;
    109   return Ret.release();
    110 }
    111 
    112 MachOUniversalBinary::MachOUniversalBinary(std::unique_ptr<MemoryBuffer> Source,
    113                                            std::error_code &ec)
    114     : Binary(Binary::ID_MachOUniversalBinary, std::move(Source)),
    115       NumberOfObjects(0) {
    116   if (Data->getBufferSize() < sizeof(MachO::fat_header)) {
    117     ec = object_error::invalid_file_type;
    118     return;
    119   }
    120   // Check for magic value and sufficient header size.
    121   StringRef Buf = getData();
    122   MachO::fat_header H= getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
    123   NumberOfObjects = H.nfat_arch;
    124   uint32_t MinSize = sizeof(MachO::fat_header) +
    125                      sizeof(MachO::fat_arch) * NumberOfObjects;
    126   if (H.magic != MachO::FAT_MAGIC || Buf.size() < MinSize) {
    127     ec = object_error::parse_failed;
    128     return;
    129   }
    130   ec = object_error::success;
    131 }
    132 
    133 static bool getCTMForArch(Triple::ArchType Arch, MachO::CPUType &CTM) {
    134   switch (Arch) {
    135     case Triple::x86:    CTM = MachO::CPU_TYPE_I386; return true;
    136     case Triple::x86_64: CTM = MachO::CPU_TYPE_X86_64; return true;
    137     case Triple::arm:    CTM = MachO::CPU_TYPE_ARM; return true;
    138     case Triple::sparc:  CTM = MachO::CPU_TYPE_SPARC; return true;
    139     case Triple::ppc:    CTM = MachO::CPU_TYPE_POWERPC; return true;
    140     case Triple::ppc64:  CTM = MachO::CPU_TYPE_POWERPC64; return true;
    141     default: return false;
    142   }
    143 }
    144 
    145 ErrorOr<std::unique_ptr<ObjectFile>>
    146 MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch) const {
    147   MachO::CPUType CTM;
    148   if (!getCTMForArch(Arch, CTM))
    149     return object_error::arch_not_found;
    150   for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
    151     if (I->getCPUType() == static_cast<uint32_t>(CTM))
    152       return I->getAsObjectFile();
    153   }
    154   return object_error::arch_not_found;
    155 }
    156