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     return object_error::parse_failed;
74 
75   StringRef ParentData = Parent->getData();
76   StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
77   StringRef ObjectName = Parent->getFileName();
78   MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
79   return ObjectFile::createMachOObjectFile(ObjBuffer);
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 = std::error_code();
124 }
125 
126 ErrorOr<std::unique_ptr<MachOObjectFile>>
getObjectForArch(StringRef ArchName) const127 MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
128   if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
129     return object_error::arch_not_found;
130 
131   for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
132     if (I->getArchTypeName() == ArchName)
133       return I->getAsObjectFile();
134   }
135   return object_error::arch_not_found;
136 }
137