LLVM 20.0.0git
MachOUniversalWriter.h
Go to the documentation of this file.
1//===- MachOUniversalWriter.h - 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// Declares the Slice class and writeUniversalBinary function for writing a
10// MachO universal binary file.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECT_MACHOUNIVERSALWRITER_H
15#define LLVM_OBJECT_MACHOUNIVERSALWRITER_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
21#include "llvm/Support/Error.h"
22#include <cstdint>
23#include <string>
24
25namespace llvm {
26class LLVMContext;
27
28namespace object {
29class Archive;
30class Binary;
31class IRObjectFile;
32class MachOObjectFile;
33
34class Slice {
35 const Binary *B;
36 uint32_t CPUType;
37 uint32_t CPUSubType;
38 std::string ArchName;
39
40 // P2Alignment field stores slice alignment values from universal
41 // binaries. This is also needed to order the slices so the total
42 // file size can be calculated before creating the output buffer.
43 uint32_t P2Alignment;
44
45 Slice(const IRObjectFile &IRO, uint32_t CPUType, uint32_t CPUSubType,
46 std::string ArchName, uint32_t Align);
47
48public:
49 explicit Slice(const MachOObjectFile &O);
50
52
53 /// This constructor takes pre-specified \param CPUType , \param CPUSubType ,
54 /// \param ArchName , \param Align instead of inferring them from the archive
55 /// members.
56 Slice(const Archive &A, uint32_t CPUType, uint32_t CPUSubType,
57 std::string ArchName, uint32_t Align);
58
59 static Expected<Slice> create(const Archive &A,
60 LLVMContext *LLVMCtx = nullptr);
61
63
64 void setP2Alignment(uint32_t Align) { P2Alignment = Align; }
65
66 const Binary *getBinary() const { return B; }
67
68 uint32_t getCPUType() const { return CPUType; }
69
70 uint32_t getCPUSubType() const { return CPUSubType; }
71
72 uint32_t getP2Alignment() const { return P2Alignment; }
73
75 return static_cast<uint64_t>(CPUType) << 32 | CPUSubType;
76 }
77
78 std::string getArchString() const {
79 if (!ArchName.empty())
80 return ArchName;
81 return ("unknown(" + Twine(CPUType) + "," +
82 Twine(CPUSubType & ~MachO::CPU_SUBTYPE_MASK) + ")")
83 .str();
84 }
85
86 friend bool operator<(const Slice &Lhs, const Slice &Rhs) {
87 if (Lhs.CPUType == Rhs.CPUType)
88 return Lhs.CPUSubType < Rhs.CPUSubType;
89 // force arm64-family to follow after all other slices for
90 // compatibility with cctools lipo
91 if (Lhs.CPUType == MachO::CPU_TYPE_ARM64)
92 return false;
93 if (Rhs.CPUType == MachO::CPU_TYPE_ARM64)
94 return true;
95 // Sort by alignment to minimize file size
96 return Lhs.P2Alignment < Rhs.P2Alignment;
97 }
98};
99
101
104
106 ArrayRef<Slice> Slices, raw_ostream &Out,
108
109} // end namespace object
110
111} // end namespace llvm
112
113#endif // LLVM_OBJECT_MACHOUNIVERSALWRITER_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
uint32_t getP2Alignment() const
uint32_t getCPUType() const
std::string getArchString() const
const Binary * getBinary() const
void setP2Alignment(uint32_t Align)
uint32_t getCPUSubType() const
static Expected< Slice > create(const Archive &A, LLVMContext *LLVMCtx=nullptr)
uint64_t getCPUID() const
friend bool operator<(const Slice &Lhs, const Slice &Rhs)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ CPU_SUBTYPE_MASK
Definition: MachO.h:1579
@ CPU_TYPE_ARM64
Definition: MachO.h:1570
Error writeUniversalBinary(ArrayRef< Slice > Slices, StringRef OutputFileName, FatHeaderType FatHeader=FatHeaderType::FatHeader)
Error writeUniversalBinaryToStream(ArrayRef< Slice > Slices, raw_ostream &Out, FatHeaderType FatHeader=FatHeaderType::FatHeader)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39