LLVM  3.7.0
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 template<typename T>
26 static void SwapStruct(T &Value);
27 
28 template<>
32 }
33 
34 template<>
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.
49  SwapStruct(Res);
50  return Res;
51 }
52 
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 
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  }
80 }
81 
84  if (!Parent)
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 
94 void MachOUniversalBinary::anchor() { }
95 
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 
107  std::error_code &ec)
108  : Binary(Binary::ID_MachOUniversalBinary, Source), NumberOfObjects(0) {
109  if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
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) {
121  return;
122  }
123  ec = std::error_code();
124 }
125 
128  if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
130 
131  for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
132  if (I->getArchTypeName() == ArchName)
133  return I->getAsObjectFile();
134  }
136 }
static ErrorOr< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object)
ErrorOr< std::unique_ptr< Archive > > getAsArchive() const
Represents either an error or a value T.
Definition: ErrorOr.h:82
static T getUniversalBinaryStruct(const char *Ptr)
void swapByteOrder(T &Value)
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
static void SwapStruct(T &Value)
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
StringRef getData() const
Definition: Binary.cpp:33
static const bool IsLittleEndianHost
Definition: Host.h:38
iterator begin() const
Definition: StringRef.h:90
ErrorOr< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
ErrorOr< std::unique_ptr< MachOObjectFile > > getObjectForArch(StringRef ArchName) const
#define H(x, y, z)
Definition: MD5.cpp:53
static ErrorOr< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
size_t getBufferSize() const
Definition: MemoryBuffer.h:163
object_iterator end_objects() const
MemoryBufferRef Data
Definition: Binary.h:37
static ErrorOr< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:222
#define I(x, y, z)
Definition: MD5.cpp:54
MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC)
object_iterator begin_objects() const
LLVM Value Representation.
Definition: Value.h:69
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40