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 template<typename T>
26 static void SwapStruct(T &Value);
27 
28 template<>
SwapStruct(MachO::fat_header & H)29 void SwapStruct(MachO::fat_header &H) {
30   sys::swapByteOrder(H.magic);
31   sys::swapByteOrder(H.nfat_arch);
32 }
33 
34 template<>
SwapStruct(MachO::fat_arch & H)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>
getUniversalBinaryStruct(const char * Ptr)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 
ObjectForArch(const MachOUniversalBinary * Parent,uint32_t Index)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<MachOObjectFile>>
getAsObjectFile() const71 MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
72   if (Parent) {
73     StringRef ParentData = Parent->getData();
74     StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
75     StringRef ObjectName = Parent->getFileName();
76     MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
77     return ObjectFile::createMachOObjectFile(ObjBuffer);
78   }
79   return object_error::parse_failed;
80 }
81 
82 ErrorOr<std::unique_ptr<Archive>>
getAsArchive() const83 MachOUniversalBinary::ObjectForArch::getAsArchive() const {
84   if (!Parent)
85     return object_error::parse_failed;
86 
87   StringRef ParentData = Parent->getData();
88   StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
89   StringRef ObjectName = Parent->getFileName();
90   MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
91   return Archive::create(ObjBuffer);
92 }
93 
anchor()94 void MachOUniversalBinary::anchor() { }
95 
96 ErrorOr<std::unique_ptr<MachOUniversalBinary>>
create(MemoryBufferRef Source)97 MachOUniversalBinary::create(MemoryBufferRef Source) {
98   std::error_code EC;
99   std::unique_ptr<MachOUniversalBinary> Ret(
100       new MachOUniversalBinary(Source, EC));
101   if (EC)
102     return EC;
103   return std::move(Ret);
104 }
105 
MachOUniversalBinary(MemoryBufferRef Source,std::error_code & ec)106 MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source,
107                                            std::error_code &ec)
108     : Binary(Binary::ID_MachOUniversalBinary, Source), NumberOfObjects(0) {
109   if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
110     ec = object_error::invalid_file_type;
111     return;
112   }
113   // Check for magic value and sufficient header size.
114   StringRef Buf = getData();
115   MachO::fat_header H= getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
116   NumberOfObjects = H.nfat_arch;
117   uint32_t MinSize = sizeof(MachO::fat_header) +
118                      sizeof(MachO::fat_arch) * NumberOfObjects;
119   if (H.magic != MachO::FAT_MAGIC || Buf.size() < MinSize) {
120     ec = object_error::parse_failed;
121     return;
122   }
123   ec = object_error::success;
124 }
125 
getCTMForArch(Triple::ArchType Arch,MachO::CPUType & CTM)126 static bool getCTMForArch(Triple::ArchType Arch, MachO::CPUType &CTM) {
127   switch (Arch) {
128     case Triple::x86:    CTM = MachO::CPU_TYPE_I386; return true;
129     case Triple::x86_64: CTM = MachO::CPU_TYPE_X86_64; return true;
130     case Triple::arm:    CTM = MachO::CPU_TYPE_ARM; return true;
131     case Triple::sparc:  CTM = MachO::CPU_TYPE_SPARC; return true;
132     case Triple::ppc:    CTM = MachO::CPU_TYPE_POWERPC; return true;
133     case Triple::ppc64:  CTM = MachO::CPU_TYPE_POWERPC64; return true;
134     default: return false;
135   }
136 }
137 
138 ErrorOr<std::unique_ptr<MachOObjectFile>>
getObjectForArch(Triple::ArchType Arch) const139 MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch) const {
140   MachO::CPUType CTM;
141   if (!getCTMForArch(Arch, CTM))
142     return object_error::arch_not_found;
143   for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
144     if (I->getCPUType() == static_cast<uint32_t>(CTM))
145       return I->getAsObjectFile();
146   }
147   return object_error::arch_not_found;
148 }
149