diff options
author | Robert Widmann <devteam.codafi@gmail.com> | 2019-04-05 21:36:50 +0000 |
---|---|---|
committer | Robert Widmann <devteam.codafi@gmail.com> | 2019-04-05 21:36:50 +0000 |
commit | c76b6215302f42c1ebdc5dfff3a875403f6abb67 (patch) | |
tree | 7449c13329f3c69dc35bb7680f368b75be066d02 /llvm/lib/Object | |
parent | 40442658db9461ab86bf33d7889689482a27c874 (diff) | |
download | bcm5719-llvm-c76b6215302f42c1ebdc5dfff3a875403f6abb67.tar.gz bcm5719-llvm-c76b6215302f42c1ebdc5dfff3a875403f6abb67.zip |
[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
Diffstat (limited to 'llvm/lib/Object')
-rw-r--r-- | llvm/lib/Object/Object.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
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<relocation_iterator*>(SI)); } +/*--.. Operations on binary files ..........................................--*/ + +LLVMBinaryRef LLVMCreateBinary(LLVMMemoryBufferRef MemBuf, + LLVMContextRef Context, + char **ErrorMessage) { + auto maybeContext = Context ? unwrap(Context) : nullptr; + Expected<std::unique_ptr<Binary>> 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<MemoryBuffer> Buf(unwrap(MemBuf)); |