LLVM  8.0.1
MachOUniversal.cpp
Go to the documentation of this file.
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 
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"
21 
22 using namespace llvm;
23 using namespace object;
24 
25 static Error
27  std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
28  return make_error<GenericBinaryError>(std::move(StringMsg),
30 }
31 
32 template<typename T>
33 static T getUniversalBinaryStruct(const char *Ptr) {
34  T Res;
35  memcpy(&Res, Ptr, sizeof(T));
36  // Universal binary headers have big-endian byte order.
38  swapStruct(Res);
39  return Res;
40 }
41 
43  const MachOUniversalBinary *Parent, uint32_t Index)
44  : Parent(Parent), Index(Index) {
45  // The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
46  if (!Parent || Index >= Parent->getNumberOfObjects()) {
47  clear();
48  } else {
49  // Parse object header.
50  StringRef ParentData = Parent->getData();
51  if (Parent->getMagic() == MachO::FAT_MAGIC) {
52  const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
53  Index * sizeof(MachO::fat_arch);
54  Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
55  } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
56  const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
57  Index * sizeof(MachO::fat_arch_64);
58  Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
59  }
60  }
61 }
62 
65  if (!Parent)
66  report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
67  "called when Parent is a nullptr");
68 
69  StringRef ParentData = Parent->getData();
70  StringRef ObjectData;
71  uint32_t cputype;
72  if (Parent->getMagic() == MachO::FAT_MAGIC) {
73  ObjectData = ParentData.substr(Header.offset, Header.size);
74  cputype = Header.cputype;
75  } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
76  ObjectData = ParentData.substr(Header64.offset, Header64.size);
77  cputype = Header64.cputype;
78  }
79  StringRef ObjectName = Parent->getFileName();
80  MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
81  return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
82 }
83 
86  if (!Parent)
87  report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
88  "called when Parent is a nullptr");
89 
90  StringRef ParentData = Parent->getData();
91  StringRef ObjectData;
92  if (Parent->getMagic() == MachO::FAT_MAGIC)
93  ObjectData = ParentData.substr(Header.offset, Header.size);
94  else // Parent->getMagic() == MachO::FAT_MAGIC_64
95  ObjectData = ParentData.substr(Header64.offset, Header64.size);
96  StringRef ObjectName = Parent->getFileName();
97  MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
98  return Archive::create(ObjBuffer);
99 }
100 
101 void MachOUniversalBinary::anchor() { }
102 
105  Error Err = Error::success();
106  std::unique_ptr<MachOUniversalBinary> Ret(
107  new MachOUniversalBinary(Source, Err));
108  if (Err)
109  return std::move(Err);
110  return std::move(Ret);
111 }
112 
114  : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
115  NumberOfObjects(0) {
116  ErrorAsOutParameter ErrAsOutParam(&Err);
117  if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
118  Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
119  "universal file",
121  return;
122  }
123  // Check for magic value and sufficient header size.
124  StringRef Buf = getData();
126  getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
127  Magic = H.magic;
128  NumberOfObjects = H.nfat_arch;
129  if (NumberOfObjects == 0) {
130  Err = malformedError("contains zero architecture types");
131  return;
132  }
133  uint32_t MinSize = sizeof(MachO::fat_header);
134  if (Magic == MachO::FAT_MAGIC)
135  MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
136  else if (Magic == MachO::FAT_MAGIC_64)
137  MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
138  else {
139  Err = malformedError("bad magic number");
140  return;
141  }
142  if (Buf.size() < MinSize) {
143  Err = malformedError("fat_arch" +
144  Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
145  " structs would extend past the end of the file");
146  return;
147  }
148  for (uint32_t i = 0; i < NumberOfObjects; i++) {
149  ObjectForArch A(this, i);
150  uint64_t bigSize = A.getOffset();
151  bigSize += A.getSize();
152  if (bigSize > Buf.size()) {
153  Err = malformedError("offset plus size of cputype (" +
154  Twine(A.getCPUType()) + ") cpusubtype (" +
155  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
156  ") extends past the end of the file");
157  return;
158  }
159 #define MAXSECTALIGN 15 /* 2**15 or 0x8000 */
160  if (A.getAlign() > MAXSECTALIGN) {
161  Err = malformedError("align (2^" + Twine(A.getAlign()) + ") too large "
162  "for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
163  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
164  ") (maximum 2^" + Twine(MAXSECTALIGN) + ")");
165  return;
166  }
167  if(A.getOffset() % (1 << A.getAlign()) != 0){
168  Err = malformedError("offset: " + Twine(A.getOffset()) +
169  " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
170  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
171  ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
172  return;
173  }
174  if (A.getOffset() < MinSize) {
175  Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
176  "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
177  ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
178  return;
179  }
180  }
181  for (uint32_t i = 0; i < NumberOfObjects; i++) {
182  ObjectForArch A(this, i);
183  for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
184  ObjectForArch B(this, j);
185  if (A.getCPUType() == B.getCPUType() &&
186  (A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
187  (B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
188  Err = malformedError("contains two of the same architecture (cputype "
189  "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
190  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
191  return;
192  }
193  if ((A.getOffset() >= B.getOffset() &&
194  A.getOffset() < B.getOffset() + B.getSize()) ||
195  (A.getOffset() + A.getSize() > B.getOffset() &&
196  A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
197  (A.getOffset() <= B.getOffset() &&
198  A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
199  Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
200  "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
201  ") at offset " + Twine(A.getOffset()) + " with a size of " +
202  Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
203  ") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
204  + ") at offset " + Twine(B.getOffset()) + " with a size of "
205  + Twine(B.getSize()));
206  return;
207  }
208  }
209  }
210  Err = Error::success();
211 }
212 
215  if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
216  return make_error<GenericBinaryError>("Unknown architecture "
217  "named: " +
218  ArchName,
220 
221  for (auto &Obj : objects())
222  if (Obj.getArchFlagName() == ArchName)
223  return Obj.getAsObjectFile();
224  return make_error<GenericBinaryError>("fat file does not "
225  "contain " +
226  ArchName,
228 }
void swapStruct(fat_header &mh)
Definition: MachO.h:991
static T getUniversalBinaryStruct(const char *Ptr)
Expected< std::unique_ptr< Archive > > getAsArchive() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringRef getFileName() const
Definition: Binary.cpp:41
uint32_t nfat_arch
Definition: MachO.h:922
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
static Error malformedError(Twine Msg)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
static const bool IsLittleEndianHost
Definition: Host.h:50
Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
MachOUniversalBinary(MemoryBufferRef Souce, Error &Err)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:531
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
size_t getBufferSize() const
Definition: MemoryBuffer.h:279
#define H(x, y, z)
Definition: MD5.cpp:57
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static const char *const Magic
Definition: Archive.cpp:42
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
iterator_range< object_iterator > objects() const
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
Expected< std::unique_ptr< MachOObjectFile > > getObjectForArch(StringRef ArchName) const
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0)
MemoryBufferRef Data
Definition: Binary.h:37
uint32_t cputype
Definition: MachO.h:926
Helper for Errors used as out-parameters.
Definition: Error.h:1022
#define MAXSECTALIGN
iterator begin() const
Definition: StringRef.h:106
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
uint32_t offset
Definition: MachO.h:928
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:18
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef getData() const
Definition: Binary.cpp:39