LLVM  16.0.0git
MachOUniversalWriter.cpp
Go to the documentation of this file.
1 //===- MachOUniversalWriter.cpp - MachO universal binary writer---*- 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 // Defines the Slice class and writeUniversalBinary function for writing a MachO
10 // universal binary file.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/MachO.h"
23 #include "llvm/Support/Casting.h"
30 
31 using namespace llvm;
32 using namespace object;
33 
34 // For compatibility with cctools lipo, a file's alignment is calculated as the
35 // minimum aligment of all segments. For object files, the file's alignment is
36 // the maximum alignment of its sections.
37 static uint32_t calculateFileAlignment(const MachOObjectFile &O) {
38  uint32_t P2CurrentAlignment;
40  const bool Is64Bit = O.is64Bit();
41 
42  for (const auto &LC : O.load_commands()) {
43  if (LC.C.cmd != (Is64Bit ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT))
44  continue;
45  if (O.getHeader().filetype == MachO::MH_OBJECT) {
46  unsigned NumberOfSections =
47  (Is64Bit ? O.getSegment64LoadCommand(LC).nsects
48  : O.getSegmentLoadCommand(LC).nsects);
49  P2CurrentAlignment = NumberOfSections ? 2 : P2MinAlignment;
50  for (unsigned SI = 0; SI < NumberOfSections; ++SI) {
51  P2CurrentAlignment = std::max(P2CurrentAlignment,
52  (Is64Bit ? O.getSection64(LC, SI).align
53  : O.getSection(LC, SI).align));
54  }
55  } else {
56  P2CurrentAlignment =
57  countTrailingZeros(Is64Bit ? O.getSegment64LoadCommand(LC).vmaddr
58  : O.getSegmentLoadCommand(LC).vmaddr);
59  }
60  P2MinAlignment = std::min(P2MinAlignment, P2CurrentAlignment);
61  }
62  // return a value >= 4 byte aligned, and less than MachO MaxSectionAlignment
63  return std::max(
64  static_cast<uint32_t>(2),
65  std::min(P2MinAlignment, static_cast<uint32_t>(
67 }
68 
69 static uint32_t calculateAlignment(const MachOObjectFile &ObjectFile) {
70  switch (ObjectFile.getHeader().cputype) {
75  return 12; // log2 value of page size(4k) for x86 and PPC
79  return 14; // log2 value of page size(16k) for Darwin ARM
80  default:
81  return calculateFileAlignment(ObjectFile);
82  }
83 }
84 
85 Slice::Slice(const Archive &A, uint32_t CPUType, uint32_t CPUSubType,
86  std::string ArchName, uint32_t Align)
87  : B(&A), CPUType(CPUType), CPUSubType(CPUSubType),
88  ArchName(std::move(ArchName)), P2Alignment(Align) {}
89 
90 Slice::Slice(const MachOObjectFile &O, uint32_t Align)
91  : B(&O), CPUType(O.getHeader().cputype),
92  CPUSubType(O.getHeader().cpusubtype),
93  ArchName(std::string(O.getArchTriple().getArchName())),
94  P2Alignment(Align) {}
95 
96 Slice::Slice(const IRObjectFile &IRO, uint32_t CPUType, uint32_t CPUSubType,
97  std::string ArchName, uint32_t Align)
98  : B(&IRO), CPUType(CPUType), CPUSubType(CPUSubType),
99  ArchName(std::move(ArchName)), P2Alignment(Align) {}
100 
101 Slice::Slice(const MachOObjectFile &O) : Slice(O, calculateAlignment(O)) {}
102 
103 using MachoCPUTy = std::pair<unsigned, unsigned>;
104 
106  auto CPU = std::make_pair(MachO::getCPUType(TT), MachO::getCPUSubType(TT));
107  if (!CPU.first) {
108  return CPU.first.takeError();
109  }
110  if (!CPU.second) {
111  return CPU.second.takeError();
112  }
113  return std::make_pair(*CPU.first, *CPU.second);
114 }
115 
117  return getMachoCPUFromTriple(Triple{TT});
118 }
119 
120 Expected<Slice> Slice::create(const Archive &A, LLVMContext *LLVMCtx) {
121  Error Err = Error::success();
122  std::unique_ptr<MachOObjectFile> MFO = nullptr;
123  std::unique_ptr<IRObjectFile> IRFO = nullptr;
124  for (const Archive::Child &Child : A.children(Err)) {
125  Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(LLVMCtx);
126  if (!ChildOrErr)
127  return createFileError(A.getFileName(), ChildOrErr.takeError());
128  Binary *Bin = ChildOrErr.get().get();
129  if (Bin->isMachOUniversalBinary())
130  return createStringError(std::errc::invalid_argument,
131  ("archive member " + Bin->getFileName() +
132  " is a fat file (not allowed in an archive)")
133  .str()
134  .c_str());
135  if (Bin->isMachO()) {
136  MachOObjectFile *O = cast<MachOObjectFile>(Bin);
137  if (IRFO) {
138  return createStringError(
139  std::errc::invalid_argument,
140  "archive member %s is a MachO, while previous archive member "
141  "%s was an IR LLVM object",
142  O->getFileName().str().c_str(), IRFO->getFileName().str().c_str());
143  }
144  if (MFO &&
145  std::tie(MFO->getHeader().cputype, MFO->getHeader().cpusubtype) !=
146  std::tie(O->getHeader().cputype, O->getHeader().cpusubtype)) {
147  return createStringError(
148  std::errc::invalid_argument,
149  ("archive member " + O->getFileName() + " cputype (" +
150  Twine(O->getHeader().cputype) + ") and cpusubtype(" +
151  Twine(O->getHeader().cpusubtype) +
152  ") does not match previous archive members cputype (" +
153  Twine(MFO->getHeader().cputype) + ") and cpusubtype(" +
154  Twine(MFO->getHeader().cpusubtype) +
155  ") (all members must match) " + MFO->getFileName())
156  .str()
157  .c_str());
158  }
159  if (!MFO) {
160  ChildOrErr.get().release();
161  MFO.reset(O);
162  }
163  } else if (Bin->isIR()) {
164  IRObjectFile *O = cast<IRObjectFile>(Bin);
165  if (MFO) {
166  return createStringError(std::errc::invalid_argument,
167  "archive member '%s' is an LLVM IR object, "
168  "while previous archive member "
169  "'%s' was a MachO",
170  O->getFileName().str().c_str(),
171  MFO->getFileName().str().c_str());
172  }
173  if (IRFO) {
174  Expected<MachoCPUTy> CPUO = getMachoCPUFromTriple(O->getTargetTriple());
175  Expected<MachoCPUTy> CPUFO =
176  getMachoCPUFromTriple(IRFO->getTargetTriple());
177  if (!CPUO)
178  return CPUO.takeError();
179  if (!CPUFO)
180  return CPUFO.takeError();
181  if (*CPUO != *CPUFO) {
182  return createStringError(
183  std::errc::invalid_argument,
184  ("archive member " + O->getFileName() + " cputype (" +
185  Twine(CPUO->first) + ") and cpusubtype(" + Twine(CPUO->second) +
186  ") does not match previous archive members cputype (" +
187  Twine(CPUFO->first) + ") and cpusubtype(" +
188  Twine(CPUFO->second) + ") (all members must match) " +
189  IRFO->getFileName())
190  .str()
191  .c_str());
192  }
193  } else {
194  ChildOrErr.get().release();
195  IRFO.reset(O);
196  }
197  } else
198  return createStringError(std::errc::invalid_argument,
199  ("archive member " + Bin->getFileName() +
200  " is neither a MachO file or an LLVM IR file "
201  "(not allowed in an archive)")
202  .str()
203  .c_str());
204  }
205  if (Err)
206  return createFileError(A.getFileName(), std::move(Err));
207  if (!MFO && !IRFO)
208  return createStringError(
209  std::errc::invalid_argument,
210  ("empty archive with no architecture specification: " +
211  A.getFileName() + " (can't determine architecture for it)")
212  .str()
213  .c_str());
214 
215  if (MFO) {
216  Slice ArchiveSlice(*(MFO), MFO->is64Bit() ? 3 : 2);
217  ArchiveSlice.B = &A;
218  return ArchiveSlice;
219  }
220 
221  // For IR objects
222  Expected<Slice> ArchiveSliceOrErr = Slice::create(*IRFO, 0);
223  if (!ArchiveSliceOrErr)
224  return createFileError(A.getFileName(), ArchiveSliceOrErr.takeError());
225  auto &ArchiveSlice = ArchiveSliceOrErr.get();
226  ArchiveSlice.B = &A;
227  return std::move(ArchiveSlice);
228 }
229 
232  if (!CPUOrErr)
233  return CPUOrErr.takeError();
234  unsigned CPUType, CPUSubType;
235  std::tie(CPUType, CPUSubType) = CPUOrErr.get();
236  // We don't directly use the architecture name of the target triple T, as,
237  // for instance, thumb is treated as ARM by the MachOUniversal object.
238  std::string ArchName(
239  MachOObjectFile::getArchTriple(CPUType, CPUSubType).getArchName());
240  return Slice{IRO, CPUType, CPUSubType, std::move(ArchName), Align};
241 }
242 
246  uint64_t Offset =
247  sizeof(MachO::fat_header) + Slices.size() * sizeof(MachO::fat_arch);
248 
249  for (const auto &S : Slices) {
250  Offset = alignTo(Offset, 1ull << S.getP2Alignment());
251  if (Offset > UINT32_MAX)
252  return createStringError(
253  std::errc::invalid_argument,
254  ("fat file too large to be created because the offset "
255  "field in struct fat_arch is only 32-bits and the offset " +
256  Twine(Offset) + " for " + S.getBinary()->getFileName() +
257  " for architecture " + S.getArchString() + "exceeds that.")
258  .str()
259  .c_str());
260 
261  MachO::fat_arch FatArch;
262  FatArch.cputype = S.getCPUType();
263  FatArch.cpusubtype = S.getCPUSubType();
264  FatArch.offset = Offset;
265  FatArch.size = S.getBinary()->getMemoryBufferRef().getBufferSize();
266  FatArch.align = S.getP2Alignment();
267  Offset += FatArch.size;
268  FatArchList.push_back(FatArch);
269  }
270  return FatArchList;
271 }
272 
274  raw_ostream &Out) {
275  MachO::fat_header FatHeader;
276  FatHeader.magic = MachO::FAT_MAGIC;
277  FatHeader.nfat_arch = Slices.size();
278 
279  Expected<SmallVector<MachO::fat_arch, 2>> FatArchListOrErr =
280  buildFatArchList(Slices);
281  if (!FatArchListOrErr)
282  return FatArchListOrErr.takeError();
283  SmallVector<MachO::fat_arch, 2> FatArchList = *FatArchListOrErr;
284 
286  MachO::swapStruct(FatHeader);
287  Out.write(reinterpret_cast<const char *>(&FatHeader),
288  sizeof(MachO::fat_header));
289 
291  for (MachO::fat_arch &FA : FatArchList)
292  MachO::swapStruct(FA);
293  Out.write(reinterpret_cast<const char *>(FatArchList.data()),
294  sizeof(MachO::fat_arch) * FatArchList.size());
295 
297  for (MachO::fat_arch &FA : FatArchList)
298  MachO::swapStruct(FA);
299 
300  size_t Offset =
301  sizeof(MachO::fat_header) + sizeof(MachO::fat_arch) * FatArchList.size();
302  for (size_t Index = 0, Size = Slices.size(); Index < Size; ++Index) {
303  MemoryBufferRef BufferRef = Slices[Index].getBinary()->getMemoryBufferRef();
304  assert((Offset <= FatArchList[Index].offset) && "Incorrect slice offset");
305  Out.write_zeros(FatArchList[Index].offset - Offset);
306  Out.write(BufferRef.getBufferStart(), BufferRef.getBufferSize());
307  Offset = FatArchList[Index].offset + BufferRef.getBufferSize();
308  }
309 
310  Out.flush();
311  return Error::success();
312 }
313 
315  StringRef OutputFileName) {
316  const bool IsExecutable = any_of(Slices, [](Slice S) {
317  return sys::fs::can_execute(S.getBinary()->getFileName());
318  });
320  if (IsExecutable)
323  OutputFileName + ".temp-universal-%%%%%%", Mode);
324  if (!Temp)
325  return Temp.takeError();
326  raw_fd_ostream Out(Temp->FD, false);
327  if (Error E = writeUniversalBinaryToStream(Slices, Out)) {
328  if (Error DiscardError = Temp->discard())
329  return joinErrors(std::move(E), std::move(DiscardError));
330  return E;
331  }
332  return Temp->keep(OutputFileName);
333 }
MachOUniversalWriter.h
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
llvm::MemoryBufferRef::getBufferStart
const char * getBufferStart() const
Definition: MemoryBufferRef.h:35
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FileSystem.h
llvm::MachO::fat_header::nfat_arch
uint32_t nfat_arch
Definition: MachO.h:957
llvm::raw_ostream::write_zeros
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
Definition: raw_ostream.cpp:499
llvm::MachO::fat_arch::align
uint32_t align
Definition: MachO.h:965
calculateFileAlignment
static uint32_t calculateFileAlignment(const MachOObjectFile &O)
Definition: MachOUniversalWriter.cpp:37
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
ErrorHandling.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
SwapByteOrder.h
llvm::MachO::CPU_TYPE_ARM
@ CPU_TYPE_ARM
Definition: MachO.h:1567
MemoryBufferRef.h
llvm::MachO::FAT_MAGIC
@ FAT_MAGIC
Definition: MachO.h:34
llvm::object::Slice::create
static Expected< Slice > create(const Archive &A, LLVMContext *LLVMCtx=nullptr)
llvm::MachO::CPUType
CPUType
Definition: MachO.h:1560
llvm::MemoryBufferRef::getBufferSize
size_t getBufferSize() const
Definition: MemoryBufferRef.h:37
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
STLExtras.h
llvm::msgpack::Type::Binary
@ Binary
llvm::sys::fs::all_write
@ all_write
Definition: FileSystem.h:101
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::MachO::CPU_TYPE_X86_64
@ CPU_TYPE_X86_64
Definition: MachO.h:1564
llvm::MachO::getCPUType
Expected< uint32_t > getCPUType(const Triple &T)
Definition: MachO.cpp:77
MachO.h
llvm::SubDirectoryType::Bin
@ Bin
llvm::MachO::fat_arch::size
uint32_t size
Definition: MachO.h:964
llvm::MachO::CPU_TYPE_POWERPC64
@ CPU_TYPE_POWERPC64
Definition: MachO.h:1572
llvm::MachO::fat_arch
Definition: MachO.h:960
llvm::object::Archive
Definition: Archive.h:159
llvm::createFileError
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1320
llvm::object::IRObjectFile::getTargetTriple
StringRef getTargetTriple() const
Definition: IRObjectFile.cpp:67
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
MachOUniversal.h
llvm::MachO::CPU_TYPE_POWERPC
@ CPU_TYPE_POWERPC
Definition: MachO.h:1571
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::MachO::fat_header::magic
uint32_t magic
Definition: MachO.h:956
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::raw_ostream::write
raw_ostream & write(unsigned char C)
Definition: raw_ostream.cpp:218
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:185
llvm::MachO::getCPUSubType
Expected< uint32_t > getCPUSubType(const Triple &T)
Definition: MachO.cpp:95
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
getMachoCPUFromTriple
static Expected< MachoCPUTy > getMachoCPUFromTriple(Triple TT)
Definition: MachOUniversalWriter.cpp:105
llvm::object::MachOObjectFile
Definition: MachO.h:406
IRObjectFile.h
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
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:82
uint64_t
MachoCPUTy
std::pair< unsigned, unsigned > MachoCPUTy
Definition: MachOUniversalWriter.cpp:103
buildFatArchList
static Expected< SmallVector< MachO::fat_arch, 2 > > buildFatArchList(ArrayRef< Slice > Slices)
Definition: MachOUniversalWriter.cpp:244
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::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::MachO::MH_OBJECT
@ MH_OBJECT
Definition: MachO.h:43
llvm::MachO::fat_arch::offset
uint32_t offset
Definition: MachO.h:963
llvm::AArch64::getArchName
StringRef getArchName(ArchKind AK)
Definition: AArch64TargetParser.cpp:97
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:426
llvm::MachO::CPU_TYPE_ARM64
@ CPU_TYPE_ARM64
Definition: MachO.h:1568
llvm::object::Slice
Definition: MachOUniversalWriter.h:34
llvm::object::MachOObjectFile::getArchTriple
Triple getArchTriple(const char **McpuDefault=nullptr) const
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::MachOUniversalBinary::MaxSectionAlignment
static constexpr uint32_t MaxSectionAlignment
Definition: MachOUniversal.h:36
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1666
llvm::object::IRObjectFile
Definition: IRObjectFile.h:27
SI
StandardInstrumentations SI(Debug, VerifyEach)
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
Triple.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1597
llvm::countTrailingZeros
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: MathExtras.h:152
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
Archive.h
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:440
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1239
std
Definition: BitVector.h:851
llvm::object::writeUniversalBinaryToStream
Error writeUniversalBinaryToStream(ArrayRef< Slice > Slices, raw_ostream &Out)
Definition: MachOUniversalWriter.cpp:273
llvm::sys::fs::all_exe
@ all_exe
Definition: FileSystem.h:102
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::MachO::CPU_TYPE_ARM64_32
@ CPU_TYPE_ARM64_32
Definition: MachO.h:1569
Casting.h
llvm::sys::fs::all_read
@ all_read
Definition: FileSystem.h:100
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
Binary.h
SmallVector.h
llvm::sys::IsLittleEndianHost
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:101
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::MachO::CPU_TYPE_I386
@ CPU_TYPE_I386
Definition: MachO.h:1563
llvm::object::writeUniversalBinary
Error writeUniversalBinary(ArrayRef< Slice > Slices, StringRef OutputFileName)
Definition: MachOUniversalWriter.cpp:314
calculateAlignment
static uint32_t calculateAlignment(const MachOObjectFile &ObjectFile)
Definition: MachOUniversalWriter.cpp:69
llvm::MachO::fat_arch::cpusubtype
uint32_t cpusubtype
Definition: MachO.h:962
llvm::sys::fs::TempFile::create
static Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write, OpenFlags ExtraFlags=OF_None)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Definition: Path.cpp:1337
raw_ostream.h
llvm::MachO::swapStruct
void swapStruct(fat_header &mh)
Definition: MachO.h:1139
llvm::MachO::fat_header
Definition: MachO.h:955
llvm::sys::fs::can_execute
bool can_execute(const Twine &Path)
Can we execute this file?
llvm::MachO::fat_arch::cputype
uint32_t cputype
Definition: MachO.h:961
SpecialSubKind::string
@ string