From c76b6215302f42c1ebdc5dfff3a875403f6abb67 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Fri, 5 Apr 2019 21:36:50 +0000 Subject: [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 llvm-svn: 357822 --- llvm/lib/Object/Object.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'llvm/lib/Object/Object.cpp') diff --git a/llvm/lib/Object/Object.cpp b/llvm/lib/Object/Object.cpp index 5032509a47c..f5bedfd7920 100644 --- a/llvm/lib/Object/Object.cpp +++ b/llvm/lib/Object/Object.cpp @@ -13,6 +13,7 @@ #include "llvm-c/Object.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/Object/ObjectFile.h" using namespace llvm; @@ -57,6 +58,33 @@ wrap(const relocation_iterator *SI) { (const_cast(SI)); } +/*--.. Operations on binary files ..........................................--*/ + +LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf, + LLVMContextRef Context, + char **ErrorMessage) { + auto maybeContext = Context ? unwrap(Context) : nullptr; + Expected> ObjOrErr( + createBinary(unwrap(MemBuf)->getMemBufferRef(), maybeContext)); + if (!ObjOrErr) { + *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str()); + return nullptr; + } + + return wrap(ObjOrErr.get().release()); +} + +LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR) { + auto Buf = unwrap(BR)->getMemoryBufferRef(); + return wrap(llvm::MemoryBuffer::getMemBuffer( + Buf.getBuffer(), Buf.getBufferIdentifier(), + /*RequiresNullTerminator*/false).release()); +} + +void LLVMDisposeBinary(LLVMBinaryRef BR) { + delete unwrap(BR); +} + // ObjectFile creation LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) { std::unique_ptr Buf(unwrap(MemBuf)); -- cgit v1.2.3