1 //===- MachOUniversal.cpp - Mach-O universal binary -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOUniversalBinary class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/MachOUniversal.h"
15 #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"
20 #include "llvm/Support/MemoryBuffer.h"
23 using namespace object;
26 static void SwapStruct(T &Value);
29 void SwapStruct(MachO::fat_header &H) {
30 sys::swapByteOrder(H.magic);
31 sys::swapByteOrder(H.nfat_arch);
35 void SwapStruct(MachO::fat_arch &H) {
36 sys::swapByteOrder(H.cputype);
37 sys::swapByteOrder(H.cpusubtype);
38 sys::swapByteOrder(H.offset);
39 sys::swapByteOrder(H.size);
40 sys::swapByteOrder(H.align);
44 static T getUniversalBinaryStruct(const char *Ptr) {
46 memcpy(&Res, Ptr, sizeof(T));
47 // Universal binary headers have big-endian byte order.
48 if (sys::IsLittleEndianHost)
53 MachOUniversalBinary::ObjectForArch::ObjectForArch(
54 const MachOUniversalBinary *Parent, uint32_t Index)
55 : Parent(Parent), Index(Index) {
56 if (!Parent || Index >= Parent->getNumberOfObjects()) {
59 // Parse object header.
60 StringRef ParentData = Parent->getData();
61 const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
62 Index * sizeof(MachO::fat_arch);
63 Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
64 if (ParentData.size() < Header.offset + Header.size) {
70 ErrorOr<std::unique_ptr<MachOObjectFile>>
71 MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
73 StringRef ParentData = Parent->getData();
74 StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
75 StringRef ObjectName = Parent->getFileName();
76 MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
77 return ObjectFile::createMachOObjectFile(ObjBuffer);
79 return object_error::parse_failed;
82 ErrorOr<std::unique_ptr<Archive>>
83 MachOUniversalBinary::ObjectForArch::getAsArchive() const {
85 return object_error::parse_failed;
87 StringRef ParentData = Parent->getData();
88 StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
89 StringRef ObjectName = Parent->getFileName();
90 MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
91 return Archive::create(ObjBuffer);
94 void MachOUniversalBinary::anchor() { }
96 ErrorOr<std::unique_ptr<MachOUniversalBinary>>
97 MachOUniversalBinary::create(MemoryBufferRef Source) {
99 std::unique_ptr<MachOUniversalBinary> Ret(
100 new MachOUniversalBinary(Source, EC));
103 return std::move(Ret);
106 MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source,
108 : Binary(Binary::ID_MachOUniversalBinary, Source), NumberOfObjects(0) {
109 if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
110 ec = object_error::invalid_file_type;
113 // Check for magic value and sufficient header size.
114 StringRef Buf = getData();
115 MachO::fat_header H= getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
116 NumberOfObjects = H.nfat_arch;
117 uint32_t MinSize = sizeof(MachO::fat_header) +
118 sizeof(MachO::fat_arch) * NumberOfObjects;
119 if (H.magic != MachO::FAT_MAGIC || Buf.size() < MinSize) {
120 ec = object_error::parse_failed;
123 ec = std::error_code();
126 ErrorOr<std::unique_ptr<MachOObjectFile>>
127 MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
128 if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
129 return object_error::arch_not_found;
131 for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
132 if (I->getArchTypeName() == ArchName)
133 return I->getAsObjectFile();
135 return object_error::arch_not_found;