From: Robert Widmann Date: Sat, 25 May 2019 16:47:27 +0000 (+0000) Subject: [LLVM-C] Add Accessor for Mach-O Universal Binary Slices X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4d7830256941e048e4d674e9b61a651f335f0fdd;p=llvm [LLVM-C] Add Accessor for Mach-O Universal Binary Slices Summary: Allow for retrieving an object file corresponding to an architecture-specific slice in a Mach-O universal binary file. Reviewers: whitequark, deadalnix Reviewed By: whitequark Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D60378 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361705 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm-c/Object.h b/include/llvm-c/Object.h index a32f6514094..1e9b703a68f 100644 --- a/include/llvm-c/Object.h +++ b/include/llvm-c/Object.h @@ -102,6 +102,22 @@ LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR); */ LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR); +/* + * For a Mach-O universal binary file, retrieves the object file corresponding + * to the given architecture if it is present as a slice. + * + * If NULL is returned, the \p ErrorMessage parameter is populated with the + * error's description. It is then the caller's responsibility to free this + * message by calling \c LLVMDisposeMessage. + * + * It is the responsiblity of the caller to free the returned object file by + * calling \c LLVMDisposeBinary. + */ +LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR, + const char *Arch, + size_t ArchLen, + char **ErrorMessage); + /** * Retrieve a copy of the section iterator for this object file. * diff --git a/lib/Object/Object.cpp b/lib/Object/Object.cpp index e2511b7aed0..d84798cc6dd 100644 --- a/lib/Object/Object.cpp +++ b/lib/Object/Object.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/IR/LLVMContext.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Object/MachOUniversal.h" using namespace llvm; using namespace object; @@ -131,6 +132,20 @@ LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR) { return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType()); } +LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR, + const char *Arch, + size_t ArchLen, + char **ErrorMessage) { + auto universal = cast(unwrap(BR)); + Expected> ObjOrErr( + universal->getObjectForArch({Arch, ArchLen})); + if (!ObjOrErr) { + *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str()); + return nullptr; + } + return wrap(ObjOrErr.get().release()); +} + LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) { auto OF = cast(unwrap(BR)); auto sections = OF->sections();