LLVM  14.0.0git
MachOUniversal.cpp
Go to the documentation of this file.
1 //===- MachOUniversal.cpp - Mach-O universal binary -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MachOUniversalBinary class.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #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::getAsIRObject() "
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  }
97  StringRef ObjectName = Parent->getFileName();
98  MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
99 
100  return IRObjectFile::create(ObjBuffer, Ctx);
101 }
102 
105  if (!Parent)
106  report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
107  "called when Parent is a nullptr");
108 
109  StringRef ParentData = Parent->getData();
110  StringRef ObjectData;
111  if (Parent->getMagic() == MachO::FAT_MAGIC)
112  ObjectData = ParentData.substr(Header.offset, Header.size);
113  else // Parent->getMagic() == MachO::FAT_MAGIC_64
114  ObjectData = ParentData.substr(Header64.offset, Header64.size);
115  StringRef ObjectName = Parent->getFileName();
116  MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
117  return Archive::create(ObjBuffer);
118 }
119 
120 void MachOUniversalBinary::anchor() { }
121 
124  Error Err = Error::success();
125  std::unique_ptr<MachOUniversalBinary> Ret(
126  new MachOUniversalBinary(Source, Err));
127  if (Err)
128  return std::move(Err);
129  return std::move(Ret);
130 }
131 
134  NumberOfObjects(0) {
135  ErrorAsOutParameter ErrAsOutParam(&Err);
136  if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
137  Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
138  "universal file",
140  return;
141  }
142  // Check for magic value and sufficient header size.
143  StringRef Buf = getData();
145  getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
146  Magic = H.magic;
147  NumberOfObjects = H.nfat_arch;
148  if (NumberOfObjects == 0) {
149  Err = malformedError("contains zero architecture types");
150  return;
151  }
152  uint32_t MinSize = sizeof(MachO::fat_header);
153  if (Magic == MachO::FAT_MAGIC)
154  MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
155  else if (Magic == MachO::FAT_MAGIC_64)
156  MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
157  else {
158  Err = malformedError("bad magic number");
159  return;
160  }
161  if (Buf.size() < MinSize) {
162  Err = malformedError("fat_arch" +
163  Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
164  " structs would extend past the end of the file");
165  return;
166  }
167  for (uint32_t i = 0; i < NumberOfObjects; i++) {
168  ObjectForArch A(this, i);
169  uint64_t bigSize = A.getOffset();
170  bigSize += A.getSize();
171  if (bigSize > Buf.size()) {
172  Err = malformedError("offset plus size of cputype (" +
173  Twine(A.getCPUType()) + ") cpusubtype (" +
174  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
175  ") extends past the end of the file");
176  return;
177  }
178 
179  if (A.getAlign() > MaxSectionAlignment) {
180  Err = malformedError("align (2^" + Twine(A.getAlign()) +
181  ") too large for cputype (" + Twine(A.getCPUType()) +
182  ") cpusubtype (" +
183  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
184  ") (maximum 2^" + Twine(MaxSectionAlignment) + ")");
185  return;
186  }
187  if(A.getOffset() % (1ull << A.getAlign()) != 0){
188  Err = malformedError("offset: " + Twine(A.getOffset()) +
189  " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
190  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
191  ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
192  return;
193  }
194  if (A.getOffset() < MinSize) {
195  Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
196  "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
197  ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
198  return;
199  }
200  }
201  for (uint32_t i = 0; i < NumberOfObjects; i++) {
202  ObjectForArch A(this, i);
203  for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
204  ObjectForArch B(this, j);
205  if (A.getCPUType() == B.getCPUType() &&
206  (A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
207  (B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
208  Err = malformedError("contains two of the same architecture (cputype "
209  "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
210  Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
211  return;
212  }
213  if ((A.getOffset() >= B.getOffset() &&
214  A.getOffset() < B.getOffset() + B.getSize()) ||
215  (A.getOffset() + A.getSize() > B.getOffset() &&
216  A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
217  (A.getOffset() <= B.getOffset() &&
218  A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
219  Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
220  "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
221  ") at offset " + Twine(A.getOffset()) + " with a size of " +
222  Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
223  ") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
224  + ") at offset " + Twine(B.getOffset()) + " with a size of "
225  + Twine(B.getSize()));
226  return;
227  }
228  }
229  }
230  Err = Error::success();
231 }
232 
235  if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
236  return make_error<GenericBinaryError>("Unknown architecture "
237  "named: " +
238  ArchName,
240  for (const auto &Obj : objects())
241  if (Obj.getArchFlagName() == ArchName)
242  return Obj;
243  return make_error<GenericBinaryError>("fat file does not "
244  "contain " +
245  ArchName,
247 }
248 
252  if (!O)
253  return O.takeError();
254  return O->getAsObjectFile();
255 }
256 
259  LLVMContext &Ctx) const {
261  if (!O)
262  return O.takeError();
263  return O->getAsIRObject(Ctx);
264 }
265 
269  if (!O)
270  return O.takeError();
271  return O->getAsArchive();
272 }
i
i
Definition: README.txt:29
MemoryBuffer.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Host.h
llvm::object::ObjectFile::createMachOObjectFile
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0)
Definition: MachOObjectFile.cpp:4657
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::object::MachOUniversalBinary::getNumberOfObjects
uint32_t getNumberOfObjects() const
Definition: MachOUniversal.h:149
llvm::object::Binary::Data
MemoryBufferRef Data
Definition: Binary.h:37
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::MemoryBufferRef::getBufferSize
size_t getBufferSize() const
Definition: MemoryBufferRef.h:37
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::MachOUniversalBinary::getArchiveForArch
Expected< std::unique_ptr< Archive > > getArchiveForArch(StringRef ArchName) const
Definition: MachOUniversal.cpp:267
llvm::object::MachOUniversalBinary::MachOUniversalBinary
MachOUniversalBinary(MemoryBufferRef Souce, Error &Err)
Definition: MachOUniversal.cpp:132
MachO.h
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:611
llvm::object::MachOUniversalBinary::ObjectForArch
Definition: MachOUniversal.h:39
llvm::object::MachOUniversalBinary::ObjectForArch::getAsObjectFile
Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
Definition: MachOUniversal.cpp:64
llvm::MachO::fat_arch
Definition: MachO.h:939
MachOUniversal.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::ErrorAsOutParameter
Helper for Errors used as out-parameters.
Definition: Error.h:1089
llvm::object::object_error::parse_failed
@ parse_failed
llvm::object::object_error::invalid_file_type
@ invalid_file_type
llvm::object::MachOUniversalBinary::getMachOObjectForArch
Expected< std::unique_ptr< MachOObjectFile > > getMachOObjectForArch(StringRef ArchName) const
Definition: MachOUniversal.cpp:250
llvm::Twine::str
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
llvm::object::MachOUniversalBinary::getObjectForArch
Expected< ObjectForArch > getObjectForArch(StringRef ArchName) const
Definition: MachOUniversal.cpp:234
IRObjectFile.h
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
uint64_t
malformedError
static Error malformedError(Twine Msg)
Definition: MachOUniversal.cpp:26
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::object::MachOUniversalBinary::ObjectForArch::getAsIRObject
Expected< std::unique_ptr< IRObjectFile > > getAsIRObject(LLVMContext &Ctx) const
Definition: MachOUniversal.cpp:85
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Magic
const char Magic[]
Definition: Archive.cpp:41
Archive.h
llvm::object::Binary::ID_MachOUniversalBinary
@ ID_MachOUniversalBinary
Definition: Binary.h:43
llvm::object::MachOUniversalBinary::MaxSectionAlignment
static constexpr uint32_t MaxSectionAlignment
Definition: MachOUniversal.h:37
ObjectFile.h
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::object::MachOUniversalBinary::objects
iterator_range< object_iterator > objects() const
Definition: MachOUniversal.h:144
llvm::MachO::CPU_SUBTYPE_MASK
@ CPU_SUBTYPE_MASK
Definition: MachO.h:1435
llvm::object::MachOUniversalBinary::ObjectForArch::ObjectForArch
ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
Definition: MachOUniversal.cpp:42
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary
Definition: Binary.h:32
A
* A
Definition: README_ALTIVEC.txt:89
llvm::object::MachOUniversalBinary::getIRObjectForArch
Expected< std::unique_ptr< IRObjectFile > > getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const
Definition: MachOUniversal.cpp:258
uint32_t
llvm::object::MachOUniversalBinary::ObjectForArch::clear
void clear()
Definition: MachOUniversal.h:50
j
return j(j<< 16)
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::object::MachOUniversalBinary::create
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
Definition: MachOUniversal.cpp:123
llvm::object::MachOUniversalBinary::ObjectForArch::getAsArchive
Expected< std::unique_ptr< Archive > > getAsArchive() const
Definition: MachOUniversal.cpp:104
H
#define H(x, y, z)
Definition: MD5.cpp:58
llvm::MachO::FAT_MAGIC_64
@ FAT_MAGIC_64
Definition: MachO.h:36
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Casting.h
llvm::MachO::FAT_MAGIC
@ FAT_MAGIC
Definition: MachO.h:34
llvm::object::IRObjectFile::create
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
Definition: IRObjectFile.cpp:111
llvm::sys::IsLittleEndianHost
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:101
llvm::object::object_error::arch_not_found
@ arch_not_found
llvm::MachO::fat_arch_64
Definition: MachO.h:947
llvm::object::Binary::getData
StringRef getData() const
Definition: Binary.cpp:40
llvm::MachO::swapStruct
void swapStruct(fat_header &mh)
Definition: MachO.h:1005
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
llvm::object::Archive::create
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:538
llvm::MachO::fat_header
Definition: MachO.h:934
llvm::object::MachOUniversalBinary::getMagic
uint32_t getMagic() const
Definition: MachOUniversal.h:148
llvm::object::MachOUniversalBinary
Definition: MachOUniversal.h:31
getUniversalBinaryStruct
static T getUniversalBinaryStruct(const char *Ptr)
Definition: MachOUniversal.cpp:33