Bartleby 0.1.0
A symbol renaming toolkit
Loading...
Searching...
No Matches
Classes | Functions
Bartleby-c.cpp File Reference

Bartleby C API implementation. More...

#include "Bartleby/Bartleby.h"
#include "Bartleby-c/Bartleby.h"
#include "llvm/Object/Binary.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SmallVectorMemoryBuffer.h"
#include <memory>
#include <errno.h>
#include <stdlib.h>

Classes

struct  BartlebyHandle
 Definition of the Bartleby handle. More...
 

Functions

struct BartlebyHandlesaq_bartleby_new (void)
 Allocates a new Bartleby handle.
 
void saq_bartleby_free (struct BartlebyHandle *bh)
 Frees a Bartleby handle.
 
int saq_bartleby_set_prefix (struct BartlebyHandle *bh, const char *prefix)
 Applies a prefix to all global and defined symbols.
 
int saq_bartleby_add_binary (struct BartlebyHandle *bh, const void *s, const size_t n)
 Adds a new binary to Bartleby.
 
int saq_bartleby_build_archive (struct BartlebyHandle *bh, void **s, size_t *n)
 Builds the final archive and writes its content to a buffer.
 

Detailed Description

Bartleby C API implementation.

Author
thb-sb

Function Documentation

◆ saq_bartleby_add_binary()

int saq_bartleby_add_binary ( struct BartlebyHandle bh,
const void *  s,
const size_t  n 
)

Adds a new binary to Bartleby.

Parameters
bhBartleby handle.
sBuffer containing the binary.
nSize of bin_src.

Buffer s is going to be copied, thus it can be freed after.

Returns
0 on success, else an error code.
71 {
72 if (bh == nullptr) {
73 return EINVAL;
74 }
75
76 if (s == nullptr) {
77 return EINVAL;
78 }
79
80 if (n == 0) {
81 return EINVAL;
82 }
83 llvm::SmallVector<char, 2048> ObjContent(static_cast<const uint8_t*>(s), static_cast<const uint8_t*>(s) + n);
84 auto OutBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>(
85 std::move(ObjContent), false);
86
87 if (auto ObjOrErr = llvm::object::ObjectFile::createObjectFile(*OutBuffer)) {
88 if (auto Err = bh->B.addBinary({std::move(*ObjOrErr), std::move(OutBuffer)})) {
89 return EINVAL;
90 }
91 } else {
92 return EINVAL;
93 }
94
95 return 0;
96}
llvm::Error addBinary(llvm::object::OwningBinary< llvm::object::Binary > Binary) noexcept
Adds a new binary to Bartleby.
Definition: Bartleby.cpp:152
bartleby::Bartleby B
Bartleby object.
Definition: Bartleby-c.cpp:42

◆ saq_bartleby_build_archive()

int saq_bartleby_build_archive ( struct BartlebyHandle bh,
void **  s,
size_t *  n 
)

Builds the final archive and writes its content to a buffer.

Warning
This function consumes the input Bartleby handle. Thus, users must not call saq_bartleby_free after calling saq_bartleby_build_archive.
Parameters
bhBartleby handle.
[out]sDestination buffer.
[out]nSize of s.
Returns
0 on success, else an error code.
100 {
101 std::unique_ptr<struct BartlebyHandle> handle(bh);
102
103 if (handle == nullptr) {
104 return EINVAL;
105 }
106
107 if (s == nullptr) {
108 return EINVAL;
109 }
110 *s = nullptr;
111
112 if (n == nullptr) {
113 return EINVAL;
114 }
115 *n = 0;
116
117 if (auto ArOrErr = bartleby::Bartleby::buildFinalArchive(std::move(handle->B))) {
118 const auto MemBuf = std::move(*ArOrErr);
119 *n = MemBuf->getBufferSize();
120 *s = ::malloc(*n);
121 if (*s == nullptr) {
122 return ENOMEM;
123 }
124 ::memcpy(*s, MemBuf->getBufferStart(), *n);
125 return 0;
126 }
127 return EINVAL;
128}

◆ saq_bartleby_free()

void saq_bartleby_free ( struct BartlebyHandle bh)

Frees a Bartleby handle.

Parameters
bhBartleby handle to free. A NULL value here is allowed.
51 {
52 delete bh;
53}

◆ saq_bartleby_new()

struct BartlebyHandle * saq_bartleby_new ( void  )

Allocates a new Bartleby handle.

Returns
A new Bartleby handle, or NULL if an error occurred.
47 {
48 return new BartlebyHandle{};
49}
Definition of the Bartleby handle.
Definition: Bartleby-c.cpp:40

◆ saq_bartleby_set_prefix()

int saq_bartleby_set_prefix ( struct BartlebyHandle bh,
const char *  prefix 
)

Applies a prefix to all global and defined symbols.

Parameters
bhBartleby handle.
prefixPrefix to apply.
Returns
0 on success, else an error code.
55 {
56 if (bh == nullptr) {
57 return EINVAL;
58 }
59
60 if (prefix == nullptr) {
61 return EINVAL;
62 }
63
65
66 return 0;
67}
size_t prefixGlobalAndDefinedSymbols(llvm::StringRef Prefix) noexcept
Applies a prefix to all global and defined symbols.
Definition: Bartleby.cpp:218