Bartleby 0.1.0
A symbol renaming toolkit
Loading...
Searching...
No Matches
Macros | Functions
Bartleby.h File Reference

Bartleby C API specification. More...

#include <sys/types.h>

Go to the source code of this file.

Macros

#define SAQ_BARTLEBY_API
 

Functions

SAQ_BARTLEBY_API struct BartlebyHandlesaq_bartleby_new (void)
 Allocates a new Bartleby handle.
 
SAQ_BARTLEBY_API void saq_bartleby_free (struct BartlebyHandle *bh)
 Frees a Bartleby handle.
 
SAQ_BARTLEBY_API int saq_bartleby_set_prefix (struct BartlebyHandle *bh, const char *prefix)
 Applies a prefix to all global and defined symbols.
 
SAQ_BARTLEBY_API int saq_bartleby_add_binary (struct BartlebyHandle *bh, const void *s, const size_t n)
 Adds a new binary to Bartleby.
 
SAQ_BARTLEBY_API 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 specification.

Author
thb-sb

Macro Definition Documentation

◆ SAQ_BARTLEBY_API

#define SAQ_BARTLEBY_API

Copyright 2023 SandboxAQ

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http: *www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Function Documentation

◆ saq_bartleby_add_binary()

SAQ_BARTLEBY_API 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()

SAQ_BARTLEBY_API 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()

SAQ_BARTLEBY_API 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()

SAQ_BARTLEBY_API 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()

SAQ_BARTLEBY_API 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