32using namespace object;
40 const bool Is64Bit = O.is64Bit();
42 for (
const auto &LC : O.load_commands()) {
43 if (LC.C.cmd != (Is64Bit ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT))
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));
58 : O.getSegmentLoadCommand(LC).vmaddr);
60 P2MinAlignment = std::min(P2MinAlignment, P2CurrentAlignment);
65 std::min(P2MinAlignment,
static_cast<uint32_t>(
87 :
B(&
A), CPUType(CPUType), CPUSubType(CPUSubType),
91 :
B(&O), CPUType(O.getHeader().cputype),
92 CPUSubType(O.getHeader().cpusubtype),
93 ArchName(
std::
string(O.getArchTriple().getArchName())),
98 :
B(&IRO), CPUType(CPUType), CPUSubType(CPUSubType),
108 return CPU.first.takeError();
111 return CPU.second.takeError();
113 return std::make_pair(*CPU.first, *CPU.second);
122 std::unique_ptr<MachOObjectFile> MFO =
nullptr;
123 std::unique_ptr<IRObjectFile> IRFO =
nullptr;
129 if (
Bin->isMachOUniversalBinary())
131 (
"archive member " +
Bin->getFileName() +
132 " is a fat file (not allowed in an archive)")
135 if (
Bin->isMachO()) {
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());
145 std::tie(MFO->getHeader().cputype, MFO->getHeader().cpusubtype) !=
146 std::tie(O->getHeader().cputype, O->getHeader().cpusubtype)) {
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())
160 ChildOrErr.
get().release();
163 }
else if (
Bin->isIR()) {
167 "archive member '%s' is an LLVM IR object, "
168 "while previous archive member "
170 O->getFileName().str().c_str(),
171 MFO->getFileName().str().c_str());
181 if (*CPUO != *CPUFO) {
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) " +
194 ChildOrErr.
get().release();
199 (
"archive member " +
Bin->getFileName() +
200 " is neither a MachO file or an LLVM IR file "
201 "(not allowed in an archive)")
209 std::errc::invalid_argument,
210 (
"empty archive with no architecture specification: " +
211 A.getFileName() +
" (can't determine architecture for it)")
216 Slice ArchiveSlice(*(MFO), MFO->is64Bit() ? 3 : 2);
223 if (!ArchiveSliceOrErr)
225 auto &ArchiveSlice = ArchiveSliceOrErr.
get();
227 return std::move(ArchiveSlice);
234 unsigned CPUType, CPUSubType;
235 std::tie(CPUType, CPUSubType) = CPUOrErr.
get();
238 std::string ArchName(
240 return Slice{IRO, CPUType, CPUSubType, std::move(ArchName),
Align};
249 for (
const auto &S : Slices) {
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.")
262 FatArch.
cputype = S.getCPUType();
265 FatArch.
size = S.getBinary()->getMemoryBufferRef().getBufferSize();
266 FatArch.
align = S.getP2Alignment();
281 if (!FatArchListOrErr)
287 Out.
write(
reinterpret_cast<const char *
>(&FatHeader),
293 Out.
write(
reinterpret_cast<const char *
>(FatArchList.
data()),
316 const bool IsExecutable =
any_of(Slices, [](
Slice S) {
323 OutputFileName +
".temp-universal-%%%%%%", Mode);
328 if (
Error DiscardError = Temp->discard())
329 return joinErrors(std::move(
E), std::move(DiscardError));
332 return Temp->keep(OutputFileName);
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static Expected< SmallVector< MachO::fat_arch, 2 > > buildFatArchList(ArrayRef< Slice > Slices)
static uint32_t calculateFileAlignment(const MachOObjectFile &O)
std::pair< unsigned, unsigned > MachoCPUTy
static Expected< MachoCPUTy > getMachoCPUFromTriple(Triple TT)
static uint32_t calculateAlignment(const MachOObjectFile &ObjectFile)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
This is an important class for using LLVM in a threaded context.
size_t getBufferSize() const
const char * getBufferStart() const
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringRef getFileName() const
StringRef getTargetTriple() const
Triple getArchTriple(const char **McpuDefault=nullptr) const
static constexpr uint32_t MaxSectionAlignment
This class is the base class for all object file types.
const Binary * getBinary() const
static Expected< Slice > create(const Archive &A, LLVMContext *LLVMCtx=nullptr)
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream & write(unsigned char C)
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...
Expected< uint32_t > getCPUSubType(const Triple &T)
void swapStruct(fat_header &mh)
Expected< uint32_t > getCPUType(const Triple &T)
Error writeUniversalBinary(ArrayRef< Slice > Slices, StringRef OutputFileName)
Error writeUniversalBinaryToStream(ArrayRef< Slice > Slices, raw_ostream &Out)
bool can_execute(const Twine &Path)
Can we execute this file?
static const bool IsLittleEndianHost
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
This struct is a compact representation of a valid (non-zero power of two) alignment.