llvm.org GIT mirror llvm / 49009c9
[LLVM-C] Begin to Expose A More General Binary Interface Summary: Provides a new type, `LLVMBinaryRef`, and a binding to `llvm::object::createBinary` for more general interoperation with binary files than `LLVMObjectFileRef`. It also provides the proper non-consuming API for input buffers and populates an out parameter for error handling if necessary - two things the previous API did not do. In a follow-up, I'll define section and symbol iterators and begin to build upon the existing test infrastructure. This patch is a first step towards deprecating that API and replacing it with something more robust. Reviewers: deadalnix, whitequark Reviewed By: whitequark Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D60322 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357822 91177308-0d34-0410-b5e6-96231b3b80d8 Robert Widmann 4 months ago
4 changed file(s) with 76 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1212 #ifndef LLVM_OBJECT_BINARY_H
1313 #define LLVM_OBJECT_BINARY_H
1414
15 #include "llvm-c/Types.h"
1516 #include "llvm/ADT/Triple.h"
1617 #include "llvm/Object/Error.h"
1718 #include "llvm/Support/Error.h"
161162 }
162163 };
163164
165 // Create wrappers for C Binding types (see CBindingWrapping.h).
166 DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
167
164168 /// Create a Binary from Source, autodetecting the file type.
165169 ///
166170 /// @param Source The data to create the Binary from.
3737 typedef struct LLVMOpaqueSectionIterator *LLVMSectionIteratorRef;
3838 typedef struct LLVMOpaqueSymbolIterator *LLVMSymbolIteratorRef;
3939 typedef struct LLVMOpaqueRelocationIterator *LLVMRelocationIteratorRef;
40
41 /**
42 * Create a binary file from the given memory buffer.
43 *
44 * The exact type of the binary file will be inferred automatically, and the
45 * appropriate implementation selected. The context may be NULL except if
46 * the resulting file is an LLVM IR file.
47 *
48 * The memory buffer is not consumed by this function. It is the responsibilty
49 * of the caller to free it with \c LLVMDisposeMemoryBuffer.
50 *
51 * If NULL is returned, the \p ErrorMessage parameter is populated with the
52 * error's description. It is then the caller's responsibility to free this
53 * message by calling \c LLVMDisposeMessage.
54 *
55 * @see llvm::object::createBinary
56 */
57 LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
58 LLVMContextRef Context,
59 char **ErrorMessage);
60
61 /**
62 * Dispose of a binary file.
63 *
64 * The binary file does not own its backing buffer. It is the responsibilty
65 * of the caller to free it with \c LLVMDisposeMemoryBuffer.
66 */
67 void LLVMDisposeBinary(LLVMBinaryRef BR);
68
69 /**
70 * Retrieves a copy of the memory buffer associated with this object file.
71 *
72 * The returned buffer is merely a shallow copy and does not own the actual
73 * backing buffer of the binary. Nevertheless, it is the responsibility of the
74 * caller to free it with \c LLVMDisposeMemoryBuffer.
75 *
76 * @see llvm::object::getMemoryBufferRef
77 */
78 LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR);
4079
4180 // ObjectFile creation
4281 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf);
163163 typedef struct LLVMOpaqueJITEventListener *LLVMJITEventListenerRef;
164164
165165 /**
166 * @see llvm::object::Binary
167 */
168 typedef struct LLVMOpaqueBinary *LLVMBinaryRef;
169
170 /**
166171 * @}
167172 */
168173
1212
1313 #include "llvm-c/Object.h"
1414 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/IR/LLVMContext.h"
1516 #include "llvm/Object/ObjectFile.h"
1617
1718 using namespace llvm;
5455 wrap(const relocation_iterator *SI) {
5556 return reinterpret_cast
5657 (const_cast(SI));
58 }
59
60 /*--.. Operations on binary files ..........................................--*/
61
62 LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf,
63 LLVMContextRef Context,
64 char **ErrorMessage) {
65 auto maybeContext = Context ? unwrap(Context) : nullptr;
66 Expected> ObjOrErr(
67 createBinary(unwrap(MemBuf)->getMemBufferRef(), maybeContext));
68 if (!ObjOrErr) {
69 *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
70 return nullptr;
71 }
72
73 return wrap(ObjOrErr.get().release());
74 }
75
76 LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR) {
77 auto Buf = unwrap(BR)->getMemoryBufferRef();
78 return wrap(llvm::MemoryBuffer::getMemBuffer(
79 Buf.getBuffer(), Buf.getBufferIdentifier(),
80 /*RequiresNullTerminator*/false).release());
81 }
82
83 void LLVMDisposeBinary(LLVMBinaryRef BR) {
84 delete unwrap(BR);
5785 }
5886
5987 // ObjectFile creation