LLVM 22.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"
22#include "llvm/Support/Error.h"
23#include <cstdint>
24#include <string>
25
26namespace llvm {
27class LLVMContext;
28
29namespace object {
30class Archive;
31class Binary;
32class IRObjectFile;
33class MachOObjectFile;
34
35class Slice {
36 const Binary *B;
37 uint32_t CPUType;
38 uint32_t CPUSubType;
39 std::string ArchName;
40
41 // P2Alignment field stores slice alignment values from universal
42 // binaries. This is also needed to order the slices so the total
43 // file size can be calculated before creating the output buffer.
44 uint32_t P2Alignment;
45
46 Slice(const IRObjectFile &IRO, uint32_t CPUType, uint32_t CPUSubType,
47 std::string ArchName, uint32_t Align);
48
49public:
50 LLVM_ABI explicit Slice(const MachOObjectFile &O);
51
53
54 /// This constructor takes pre-specified \param CPUType , \param CPUSubType ,
55 /// \param ArchName , \param Align instead of inferring them from the archive
56 /// members.
57 LLVM_ABI Slice(const Archive &A, uint32_t CPUType, uint32_t CPUSubType,
58 std::string ArchName, uint32_t Align);
59
61 LLVMContext *LLVMCtx = nullptr);
62
65
66 void setP2Alignment(uint32_t Align) { P2Alignment = Align; }
67
68 const Binary *getBinary() const { return B; }
69
70 uint32_t getCPUType() const { return CPUType; }
71
72 uint32_t getCPUSubType() const { return CPUSubType; }
73
74 uint32_t getP2Alignment() const { return P2Alignment; }
75
77 return static_cast<uint64_t>(CPUType) << 32 | CPUSubType;
78 }
79
80 std::string getArchString() const {
81 if (!ArchName.empty())
82 return ArchName;
83 return ("unknown(" + Twine(CPUType) + "," +
84 Twine(CPUSubType & ~MachO::CPU_SUBTYPE_MASK) + ")")
85 .str();
86 }
87
88 friend bool operator<(const Slice &Lhs, const Slice &Rhs) {
89 if (Lhs.CPUType == Rhs.CPUType)
90 return Lhs.CPUSubType < Rhs.CPUSubType;
91 // force arm64-family to follow after all other slices for
92 // compatibility with cctools lipo
93 if (Lhs.CPUType == MachO::CPU_TYPE_ARM64)
94 return false;
95 if (Rhs.CPUType == MachO::CPU_TYPE_ARM64)
96 return true;
97 // Sort by alignment to minimize file size
98 return Lhs.P2Alignment < Rhs.P2Alignment;
99 }
100};
101
103
105writeUniversalBinary(ArrayRef<Slice> Slices, StringRef OutputFileName,
107
109 ArrayRef<Slice> Slices, raw_ostream &Out,
111
112} // end namespace object
113
114} // end namespace llvm
115
116#endif // LLVM_OBJECT_MACHOUNIVERSALWRITER_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition: Compiler.h:213
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:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
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 LLVM_ABI 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:53
@ CPU_SUBTYPE_MASK
Definition: MachO.h:1581
@ CPU_TYPE_ARM64
Definition: MachO.h:1570
LLVM_ABI Error writeUniversalBinary(ArrayRef< Slice > Slices, StringRef OutputFileName, FatHeaderType FatHeader=FatHeaderType::FatHeader)
LLVM_ABI 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