Skip to content

Commit b0fd12b

Browse files
committed
[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 llvm-svn: 361705
1 parent d87eced commit b0fd12b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

llvm/include/llvm-c/Object.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR);
102102
*/
103103
LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR);
104104

105+
/*
106+
* For a Mach-O universal binary file, retrieves the object file corresponding
107+
* to the given architecture if it is present as a slice.
108+
*
109+
* If NULL is returned, the \p ErrorMessage parameter is populated with the
110+
* error's description. It is then the caller's responsibility to free this
111+
* message by calling \c LLVMDisposeMessage.
112+
*
113+
* It is the responsiblity of the caller to free the returned object file by
114+
* calling \c LLVMDisposeBinary.
115+
*/
116+
LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
117+
const char *Arch,
118+
size_t ArchLen,
119+
char **ErrorMessage);
120+
105121
/**
106122
* Retrieve a copy of the section iterator for this object file.
107123
*

llvm/lib/Object/Object.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/SmallVector.h"
1616
#include "llvm/IR/LLVMContext.h"
1717
#include "llvm/Object/ObjectFile.h"
18+
#include "llvm/Object/MachOUniversal.h"
1819

1920
using namespace llvm;
2021
using namespace object;
@@ -131,6 +132,20 @@ LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR) {
131132
return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType());
132133
}
133134

135+
LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
136+
const char *Arch,
137+
size_t ArchLen,
138+
char **ErrorMessage) {
139+
auto universal = cast<MachOUniversalBinary>(unwrap(BR));
140+
Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
141+
universal->getObjectForArch({Arch, ArchLen}));
142+
if (!ObjOrErr) {
143+
*ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
144+
return nullptr;
145+
}
146+
return wrap(ObjOrErr.get().release());
147+
}
148+
134149
LLVMSectionIteratorRef LLVMObjectFileCopySectionIterator(LLVMBinaryRef BR) {
135150
auto OF = cast<ObjectFile>(unwrap(BR));
136151
auto sections = OF->sections();

0 commit comments

Comments
 (0)