23using namespace object;
27 std::string StringMsg =
"truncated or malformed fat file (" + Msg.
str() +
")";
28 return make_error<GenericBinaryError>(std::move(StringMsg),
29 object_error::parse_failed);
35 memcpy(&Res,
Ptr,
sizeof(
T));
54 Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
58 Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
67 "called when Parent is a nullptr");
73 ObjectData = ParentData.
substr(Header.offset, Header.size);
74 cputype = Header.cputype;
76 ObjectData = ParentData.
substr(Header64.offset, Header64.size);
77 cputype = Header64.cputype;
79 StringRef ObjectName = Parent->getFileName();
88 "called when Parent is a nullptr");
93 ObjectData = ParentData.
substr(Header.offset, Header.size);
95 ObjectData = ParentData.
substr(Header64.offset, Header64.size);
97 StringRef ObjectName = Parent->getFileName();
107 "called when Parent is a nullptr");
109 StringRef ParentData = Parent->getData();
112 ObjectData = ParentData.
substr(Header.offset, Header.size);
114 ObjectData = ParentData.
substr(Header64.offset, Header64.size);
115 StringRef ObjectName = Parent->getFileName();
120void MachOUniversalBinary::anchor() { }
125 std::unique_ptr<MachOUniversalBinary> Ret(
128 return std::move(Err);
129 return std::move(Ret);
137 Err = make_error<GenericBinaryError>(
"File too small to be a Mach-O "
145 getUniversalBinaryStruct<MachO::fat_header>(Buf.
begin());
147 NumberOfObjects =
H.nfat_arch;
148 if (NumberOfObjects == 0) {
161 if (Buf.
size() < MinSize) {
164 " structs would extend past the end of the file");
167 for (
uint32_t i = 0; i < NumberOfObjects; i++) {
170 bigSize +=
A.getSize();
171 if (bigSize > Buf.
size()) {
173 Twine(
A.getCPUType()) +
") cpusubtype (" +
175 ") extends past the end of the file");
181 ") too large for cputype (" +
Twine(
A.getCPUType()) +
187 if(
A.getOffset() % (1ull <<
A.getAlign()) != 0){
189 " for cputype (" +
Twine(
A.getCPUType()) +
") cpusubtype (" +
191 ") not aligned on it's alignment (2^" +
Twine(
A.getAlign()) +
")");
194 if (
A.getOffset() < MinSize) {
197 ") offset " +
Twine(
A.getOffset()) +
" overlaps universal headers");
201 for (
uint32_t i = 0; i < NumberOfObjects; i++) {
203 for (
uint32_t j = i + 1; j < NumberOfObjects; j++) {
205 if (
A.getCPUType() ==
B.getCPUType() &&
208 Err =
malformedError(
"contains two of the same architecture (cputype "
209 "(" +
Twine(
A.getCPUType()) +
") cpusubtype (" +
213 if ((
A.getOffset() >=
B.getOffset() &&
214 A.getOffset() <
B.getOffset() +
B.getSize()) ||
215 (
A.getOffset() +
A.getSize() >
B.getOffset() &&
216 A.getOffset() +
A.getSize() <
B.getOffset() +
B.getSize()) ||
217 (
A.getOffset() <=
B.getOffset() &&
218 A.getOffset() +
A.getSize() >=
B.getOffset() +
B.getSize())) {
221 ") at offset " +
Twine(
A.getOffset()) +
" with a size of " +
222 Twine(
A.getSize()) +
", overlaps cputype (" +
Twine(
B.getCPUType()) +
224 +
") at offset " +
Twine(
B.getOffset()) +
" with a size of "
236 return make_error<GenericBinaryError>(
"Unknown architecture "
240 for (
const auto &Obj :
objects())
241 if (Obj.getArchFlagName() == ArchName)
243 return make_error<GenericBinaryError>(
"fat file does not "
253 return O.takeError();
254 return O->getAsObjectFile();
262 return O.takeError();
263 return O->getAsIRObject(Ctx);
270 return O.takeError();
271 return O->getAsArchive();
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static Error malformedError(Twine Msg)
static T getUniversalBinaryStruct(const char *Ptr)
static Error malformedError(Twine Msg)
Helper for Errors used as out-parameters.
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.
This is an important class for using LLVM in a threaded context.
size_t getBufferSize() const
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr size_t size() const
size - Get the string size.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::string str() const
Return the twine contents as a std::string.
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
StringRef getData() const
@ ID_MachOUniversalBinary
static Expected< std::unique_ptr< IRObjectFile > > create(MemoryBufferRef Object, LLVMContext &Context)
Expected< std::unique_ptr< MachOObjectFile > > getAsObjectFile() const
ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index)
Expected< std::unique_ptr< IRObjectFile > > getAsIRObject(LLVMContext &Ctx) const
Expected< std::unique_ptr< Archive > > getAsArchive() const
Expected< ObjectForArch > getObjectForArch(StringRef ArchName) const
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
Expected< std::unique_ptr< IRObjectFile > > getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const
Expected< std::unique_ptr< MachOObjectFile > > getMachOObjectForArch(StringRef ArchName) const
Expected< std::unique_ptr< Archive > > getArchiveForArch(StringRef ArchName) const
MachOUniversalBinary(MemoryBufferRef Souce, Error &Err)
uint32_t getMagic() const
iterator_range< object_iterator > objects() const
uint32_t getNumberOfObjects() const
static constexpr uint32_t MaxSectionAlignment
static Expected< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
Create a MachOObjectFile instance from a given buffer.
static const bool IsLittleEndianHost
This is an optimization pass for GlobalISel generic memory operations.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.