Bartleby 0.1.0
A symbol renaming toolkit
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
saq::bartleby::Bartleby Class Reference

Bartleby handle. More...

#include <Bartleby.h>

Classes

class  ArchiveWriter
 Archive builder that implements our multi format config. More...
 

Public Types

using SymbolMap = llvm::StringMap< Symbol >
 Symbol map type.
 

Public Member Functions

 Bartleby () noexcept
 Constructs an empty Bartleby handle.
 
 Bartleby (const Bartleby &) noexcept=delete
 
 Bartleby (Bartleby &&) noexcept=default
 
Bartlebyoperator= (const Bartleby &) noexcept=delete
 
Bartlebyoperator= (Bartleby &&) noexcept=default
 
llvm::Error addBinary (llvm::object::OwningBinary< llvm::object::Binary > Binary) noexcept
 Adds a new binary to Bartleby.
 
const SymbolMapgetSymbols () const noexcept
 Gets a const reference to the map of symbols.
 
size_t prefixGlobalAndDefinedSymbols (llvm::StringRef Prefix) noexcept
 Applies a prefix to all global and defined symbols.
 

Static Public Member Functions

static llvm::Error buildFinalArchive (Bartleby &&B, llvm::StringRef OutFilepath) noexcept
 Builds the final archive and writes its content to a file.
 
static llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > buildFinalArchive (Bartleby &&B) noexcept
 Builds the final archive and returns its content.
 

Detailed Description

Bartleby handle.

Member Function Documentation

◆ addBinary()

BARTLEBY_API llvm::Error Bartleby::addBinary ( llvm::object::OwningBinary< llvm::object::Binary >  Binary)
noexcept

Adds a new binary to Bartleby.

Parameters
[in]BinaryBinary.
Returns
An error.
153 {
154 auto *Binary = OwningBinary.getBinary();
155 llvm::Error E = llvm::Error::success();
156
157 if (auto *Obj = llvm::dyn_cast<llvm::object::ObjectFile>(Binary)) {
158 const auto Triple = Obj->makeTriple();
159 if (!objectFormatMatches(Triple)) {
160 return llvm::make_error<Error>(Error::ObjectFormatTypeMismatchReason{
161 .Constraint = std::get<ObjectFormat>(ObjFormat), .Found = {Triple}});
162 }
163 ObjFormat = Triple;
164 ProcessObjectFile(Obj, Symbols);
165 auto &Entry = Objects.emplace_back(ObjectFile{.Handle = Obj});
166 (llvm::Twine(llvm::utostr(Objects.size())) + ".o")
167 .toNullTerminatedStringRef(Entry.Name);
168 } else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Binary)) {
169 llvm::Error E = llvm::Error::success();
170 for (const auto &Ch : Archive->children(E)) {
171 auto BinOrErr = Ch.getAsBinary();
172 if (!BinOrErr) {
173 return BinOrErr.takeError();
174 }
175 if (auto *Obj =
176 llvm::dyn_cast<llvm::object::ObjectFile>(BinOrErr.get().get())) {
177 const auto Triple = Obj->makeTriple();
178 if (!objectFormatMatches(Triple)) {
179 return llvm::make_error<Error>(Error::ObjectFormatTypeMismatchReason{
180 .Constraint = std::get<ObjectFormat>(ObjFormat),
181 .Found = {Triple}});
182 }
183 ObjFormat = Triple;
184
185 ProcessObjectFile(Obj, Symbols);
186 auto &Entry = Objects.emplace_back(ObjectFile{
187 .Owner = std::move(BinOrErr.get()),
188 });
189 Entry.Handle = Obj;
190 if (auto NameOrErr = Ch.getName()) {
191 Entry.Name = *NameOrErr;
192 } else {
193 (llvm::Twine(llvm::utostr(Objects.size())) + ".o")
194 .toNullTerminatedStringRef(Entry.Name);
195 }
196 } else {
198 llvm::raw_svector_ostream OS(Reason.Msg);
199 OS << "unsupported binary '" << Binary->getType()
200 << "' (triple: " << Binary->getTripleObjectFormat() << ')';
201 return llvm::make_error<Error>(std::move(Reason));
202 }
203 }
204 } else if (Binary->isMachOUniversalBinary()) {
205 return addMachOUniversalBinary(std::move(OwningBinary));
206 } else {
208 llvm::raw_svector_ostream OS(Reason.Msg);
209 OS << "unsupported binary '" << Binary->getType()
210 << "' (triple: " << Binary->getTripleObjectFormat() << ')';
211 return llvm::make_error<Error>(std::move(Reason));
212 }
213 OwnedBinaries.push_back(std::move(OwningBinary));
214 return llvm::Error::success();
215}
Mismatch between object format type.
Definition: Error.h:49
ObjectFormat Constraint
Object type from the Bartleby handle.
Definition: Error.h:51
Error from std::error_code.
Definition: Error.h:40
llvm::SmallString< 32 > Msg
Error message.
Definition: Error.h:42

◆ buildFinalArchive() [1/2]

BARTLEBY_API llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > Bartleby::buildFinalArchive ( Bartleby &&  B)
staticnoexcept

Builds the final archive and returns its content.

Parameters
[in]BBartleby handle.
Returns
The memory buffer containing the archive, or an error.
334 {
335 ArchiveWriter Builder(std::move(B));
336 return Builder.build();
337}

◆ buildFinalArchive() [2/2]

BARTLEBY_API llvm::Error Bartleby::buildFinalArchive ( Bartleby &&  B,
llvm::StringRef  OutFilepath 
)
staticnoexcept

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

Parameters
[in]BBartleby handle.
OutFilepathPath to out file.
Returns
An error.
328 {
329 ArchiveWriter Builder(std::move(B));
330 return Builder.build(OutFilepath);
331}

◆ getSymbols()

const SymbolMap & saq::bartleby::Bartleby::getSymbols ( ) const
inlinenoexcept

Gets a const reference to the map of symbols.

Returns
The map of symbols.
110{ return Symbols; }

◆ prefixGlobalAndDefinedSymbols()

BARTLEBY_API size_t Bartleby::prefixGlobalAndDefinedSymbols ( llvm::StringRef  Prefix)
noexcept

Applies a prefix to all global and defined symbols.

Parameters
PrefixPrefix.
Returns
The number of symbols that have been prefixed.
218 {
219 size_t N = 0;
220 const auto End = Symbols.end();
221 for (auto Entry = Symbols.begin(); Entry != End; ++Entry) {
222 const auto &Name = Entry->first();
223 auto &Sym = Entry->getValue();
224 if (Sym.isGlobal() && Sym.isDefined()) {
225 std::string NewName;
226 if (Sym.isMachO()) {
227 NewName += '_';
228 }
229 NewName += Prefix;
230
231 if (Sym.isMachO()) {
232 NewName += Name.substr(1);
233 } else {
234 NewName += Name;
235 }
236 Sym.setName(std::move(NewName));
237 ++N;
238 }
239 }
240
241 return N;
242}

The documentation for this class was generated from the following files: