Bartleby 0.1.0
A symbol renaming toolkit
Loading...
Searching...
No Matches
Bartleby.h
Go to the documentation of this file.
1// Copyright 2023 SandboxAQ
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
20
21#pragma once
22
23#include "Bartleby/Symbol.h"
24
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringMap.h"
27#include "llvm/Object/Binary.h"
28
29#include <unordered_set>
30#include <variant>
31
32namespace saq::bartleby {
33
42 llvm::Triple::ArchType Arch;
43
45 llvm::Triple::SubArchType SubArch;
46
48 llvm::Triple::ObjectFormatType FormatType;
49
53 ObjectFormat(const llvm::Triple &Triple) noexcept;
54
59 [[nodiscard]] uint64_t pack() const noexcept;
60
61 bool operator==(const ObjectFormat &Other) const noexcept;
62
70 bool matches(const llvm::Triple &Triple) const noexcept;
71
73 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
74 const ObjectFormat &Format) noexcept;
75
77 struct Hash {
78 inline size_t operator()(const ObjectFormat &ObjFormat) const noexcept {
79 return static_cast<size_t>(ObjFormat.pack());
80 }
81 };
82};
83
85class Bartleby {
86public:
88 using SymbolMap = llvm::StringMap<Symbol>;
89
91 Bartleby() noexcept;
92
93 Bartleby(const Bartleby &) noexcept = delete;
94 Bartleby(Bartleby &&) noexcept = default;
95 Bartleby &operator=(const Bartleby &) noexcept = delete;
96 Bartleby &operator=(Bartleby &&) noexcept = default;
97 ~Bartleby() noexcept = default;
98
104 [[nodiscard]] llvm::Error
105 addBinary(llvm::object::OwningBinary<llvm::object::Binary> Binary) noexcept;
106
110 [[nodiscard]] const SymbolMap &getSymbols() const noexcept { return Symbols; }
111
117 size_t prefixGlobalAndDefinedSymbols(llvm::StringRef Prefix) noexcept;
118
125 [[nodiscard]] static llvm::Error
126 buildFinalArchive(Bartleby &&B, llvm::StringRef OutFilepath) noexcept;
127
133 [[nodiscard]] static llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
134 buildFinalArchive(Bartleby &&B) noexcept;
135
136private:
138 struct ObjectFile {
140 llvm::object::ObjectFile *Handle;
141
149 std::unique_ptr<llvm::object::Binary> Owner;
150
152 llvm::SmallString<32> Name;
153
157 uint32_t Alignment;
158 };
159
161 using ObjectFormatSet = std::unordered_set<ObjectFormat, ObjectFormat::Hash>;
162
168 using ObjectFormatVariant =
169 std::variant<std::monostate, ObjectFormat, ObjectFormatSet>;
170
174 [[nodiscard]] bool isMachOUniversalBinary() const noexcept;
175
185 [[nodiscard]] bool
186 objectFormatMatches(const ObjectFormat &ObjFmt) const noexcept;
187
193 [[nodiscard]] llvm::Error addMachOUniversalBinary(
194 llvm::object::OwningBinary<llvm::object::Binary> OwningBinary) noexcept;
195
197 SymbolMap Symbols;
198
200 llvm::SmallVector<ObjectFile, 128> Objects;
201
203 llvm::SmallVector<llvm::object::OwningBinary<llvm::object::Binary>, 128>
204 OwnedBinaries;
205
210 ObjectFormatVariant ObjFormat;
211
212 // Forward declaration.
213 class ArchiveWriter;
214};
215
216} // end namespace saq::bartleby
Bartleby library specification.
Bartleby handle.
Definition: Bartleby.h:85
const SymbolMap & getSymbols() const noexcept
Gets a const reference to the map of symbols.
Definition: Bartleby.h:110
llvm::Error addBinary(llvm::object::OwningBinary< llvm::object::Binary > Binary) noexcept
Adds a new binary to Bartleby.
Definition: Bartleby.cpp:152
size_t prefixGlobalAndDefinedSymbols(llvm::StringRef Prefix) noexcept
Applies a prefix to all global and defined symbols.
Definition: Bartleby.cpp:218
Bartleby() noexcept
Constructs an empty Bartleby handle.
static llvm::Error buildFinalArchive(Bartleby &&B, llvm::StringRef OutFilepath) noexcept
Builds the final archive and writes its content to a file.
Definition: ArchiveWriter.cpp:327
llvm::StringMap< Symbol > SymbolMap
Symbol map type.
Definition: Bartleby.h:88
Custom error info for Bartleby.
Definition: Error.h:34
std::hash implementation for ObjectFormat.
Definition: Bartleby.h:77
Object format.
Definition: Bartleby.h:40
llvm::Triple::ObjectFormatType FormatType
The object format file.
Definition: Bartleby.h:48
bool matches(const llvm::Triple &Triple) const noexcept
Verifies that a llvm::Triple matches the value from an ObjectFormat.
Definition: Bartleby.cpp:140
llvm::Triple::SubArchType SubArch
The sub-architecture.
Definition: Bartleby.h:45
llvm::Triple::ArchType Arch
The architecture.
Definition: Bartleby.h:42
uint64_t pack() const noexcept
Packs the architecture, sub-architecture and object format file enums into a 8-bytes unsigned integer...
Definition: Bartleby.cpp:130