LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
MachO/Builder.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_MACHO_BUIDLER_H
17#define LIEF_MACHO_BUIDLER_H
18
19#include <vector>
20
21#include "LIEF/errors.hpp"
22#include "LIEF/visibility.h"
23
24#include "LIEF/iostream.hpp"
25
26namespace LIEF {
27namespace MachO {
28
29class AtomInfo;
30class Binary;
31class BuildVersion;
32class CodeSignature;
34class DataInCode;
36class DyldEnvironment;
37class DyldExportsTrie;
38class DyldInfo;
39class DylibCommand;
40class DylinkerCommand;
42class EncryptionInfo;
43class FatBinary;
44class FunctionStarts;
47class LinkerOptHint;
48class LoadCommand;
49class MainCommand;
50class NoteCommand;
51class RPathCommand;
52class Routine;
54class SourceVersion;
55class SubClient;
56class SubFramework;
57class SymbolCommand;
58class ThreadCommand;
59class TwoLevelHints;
60class VersionMin;
61
64 public:
66 struct config_t {
67 bool linkedit = true;
68 };
69
70 Builder() = delete;
71
72 static ok_error_t write(Binary& binary, const std::string& filename);
73 static ok_error_t write(Binary& binary, const std::string& filename,
74 config_t config);
75
76 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out);
77 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out,
78 config_t config);
79
80 static ok_error_t write(Binary& binary, std::ostream& out);
81 static ok_error_t write(Binary& binary, std::ostream& out, config_t config);
82
83 static ok_error_t write(FatBinary& fat, const std::string& filename);
84 static ok_error_t write(FatBinary& fat, const std::string& filename,
85 config_t config);
86
87 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out);
88 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out,
89 config_t config);
90
91 static ok_error_t write(FatBinary& fat, std::ostream& out);
92 static ok_error_t write(FatBinary& fat, std::ostream& out, config_t config);
93
95
96 private:
97 LIEF_LOCAL ok_error_t build();
98
99 LIEF_LOCAL const std::vector<uint8_t>& get_build();
100 LIEF_LOCAL ok_error_t write(const std::string& filename) const;
101 LIEF_LOCAL ok_error_t write(std::ostream& os) const;
102
103 LIEF_LOCAL Builder(Binary& binary, config_t config);
104 LIEF_LOCAL Builder(std::vector<Binary*> binaries, config_t config);
105
106 LIEF_LOCAL static std::vector<uint8_t> build_raw(Binary& binary,
107 config_t config);
108 LIEF_LOCAL static std::vector<uint8_t> build_raw(FatBinary& binary,
109 config_t config);
110
111 template<class T>
112 LIEF_LOCAL static size_t get_cmd_size(const LoadCommand& cmd);
113
114 template<typename T>
115 LIEF_LOCAL ok_error_t build();
116
117 LIEF_LOCAL ok_error_t build_fat();
118 LIEF_LOCAL ok_error_t build_fat_header();
119 LIEF_LOCAL ok_error_t build_load_commands();
120
121 template<typename T>
122 LIEF_LOCAL ok_error_t build_header();
123
124 template<typename T>
125 LIEF_LOCAL ok_error_t build_linkedit();
126
127 template<typename T>
128 LIEF_LOCAL ok_error_t build(DylibCommand& library);
129
130 template<typename T>
132
133 template<class T>
134 LIEF_LOCAL ok_error_t build(VersionMin& version_min);
135
136 template<class T>
137 LIEF_LOCAL ok_error_t build(SourceVersion& source_version);
138
139 template<class T>
140 LIEF_LOCAL ok_error_t build(FunctionStarts& function_starts);
141
142 template<class T>
143 LIEF_LOCAL ok_error_t build(MainCommand& main_cmd);
144
145 template<class T>
146 LIEF_LOCAL ok_error_t build(NoteCommand& main_cmd);
147
148 template<class T>
149 LIEF_LOCAL ok_error_t build(Routine& routine);
150
151 template<class T>
152 LIEF_LOCAL ok_error_t build(RPathCommand& rpath_cmd);
153
154 template<class T>
155 LIEF_LOCAL ok_error_t build(DyldInfo& dyld_info);
156
157 template<class T>
158 LIEF_LOCAL ok_error_t build(SymbolCommand& symbol_command);
159
160 template<class T>
161 LIEF_LOCAL ok_error_t build(DynamicSymbolCommand& symbol_command);
162
163 template<class T>
164 LIEF_LOCAL ok_error_t build(DataInCode& datacode);
165
166 template<class T>
167 LIEF_LOCAL ok_error_t build(CodeSignature& code_signature);
168
169 template<class T>
171
172 template<class T>
174
175 template<class T>
177
178 template<class T>
180
181 template<class T>
183
184 template<class T>
186
187 template<class T>
188 LIEF_LOCAL ok_error_t build(DyldExportsTrie& exports);
189
190 template<class T>
192
193 template<class T>
195
196 template<class T>
197 LIEF_LOCAL ok_error_t build(AtomInfo& opt);
198
199 template<class T>
201
202 template<class T>
204
205 template<class T>
207
208 template<class T>
210
211 template<typename T>
212 LIEF_LOCAL ok_error_t build_segments();
213
214 template<class T>
216
217 template<typename T>
218 LIEF_LOCAL ok_error_t build_symbols();
219
220 LIEF_LOCAL ok_error_t build_uuid();
221
222 template<typename T>
223 LIEF_LOCAL ok_error_t update_fixups(DyldChainedFixups& fixups);
224
225 std::vector<Binary*> binaries_;
226 Binary* binary_ = nullptr;
227 mutable vector_iostream raw_;
228 uint64_t linkedit_offset_ = 0;
229 mutable vector_iostream linkedit_;
230 config_t config_;
231};
232
233} // namespace MachO
234} // namespace LIEF
235#endif
Class which represents the LC_ATOM_INFO command.
Definition AtomInfo.hpp:35
Class which represents a MachO binary.
Definition MachO/Binary.hpp:88
Definition BuildVersion.hpp:34
static ok_error_t write(FatBinary &fat, std::ostream &out)
static ok_error_t write(Binary &binary, const std::string &filename)
static ok_error_t write(FatBinary &fat, std::vector< uint8_t > &out, config_t config)
static ok_error_t write(FatBinary &fat, const std::string &filename, config_t config)
static ok_error_t write(FatBinary &fat, const std::string &filename)
static ok_error_t write(FatBinary &fat, std::vector< uint8_t > &out)
static ok_error_t write(Binary &binary, std::ostream &out, config_t config)
static ok_error_t write(FatBinary &fat, std::ostream &out, config_t config)
static ok_error_t write(Binary &binary, std::vector< uint8_t > &out, config_t config)
static ok_error_t write(Binary &binary, std::ostream &out)
static ok_error_t write(Binary &binary, std::vector< uint8_t > &out)
static ok_error_t write(Binary &binary, const std::string &filename, config_t config)
Definition CodeSignatureDir.hpp:36
Definition CodeSignature.hpp:37
Interface of the LC_DATA_IN_CODE command This command is used to list slices of code sections that co...
Definition DataInCode.hpp:42
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:49
Class that represents a LC_DYLD_ENVIRONMENT command which is used by the Mach-O linker/loader to init...
Definition DyldEnvironment.hpp:34
Class that represents the LC_DYLD_EXPORTS_TRIE command.
Definition DyldExportsTrie.hpp:40
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:50
Class which represents a library dependency.
Definition DylibCommand.hpp:34
Class that represents the Mach-O linker, also named loader. Most of the time, DylinkerCommand::name()...
Definition DylinkerCommand.hpp:34
Class that represents the LC_DYSYMTAB command.
Definition DynamicSymbolCommand.hpp:40
Class that represents the LC_ENCRYPTION_INFO / LC_ENCRYPTION_INFO_64 commands.
Definition EncryptionInfo.hpp:35
Class which represent a Mach-O (fat) binary This object is also used for representing Mach-O binaries...
Definition FatBinary.hpp:36
Class which represents the LC_FUNCTION_STARTS command.
Definition FunctionStarts.hpp:39
Class which represents the LC_FUNCTION_VARIANT_FIXUPS command.
Definition FunctionVariantFixups.hpp:34
Class representing the LC_FUNCTION_VARIANTS load command.
Definition FunctionVariants.hpp:59
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:37
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:37
Class that represent the LC_MAIN command. This kind of command can be used to determine the entrypoin...
Definition MainCommand.hpp:33
Class that represent the LC_NOTE command.
Definition NoteCommand.hpp:35
Class that represents the LC_RPATH command.
Definition RPathCommand.hpp:36
Class that represents the LC_ROUTINE/LC_ROUTINE64 commands. Accodring to the Mach-O loader....
Definition Routine.hpp:38
Class that represents the LoadCommand::TYPE::SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:35
Class that represents the MachO LoadCommand::TYPE::SOURCE_VERSION This command is used to provide the...
Definition SourceVersion.hpp:35
Class that represents the SubClient command. Accodring to the Mach-O loader.h documentation:
Definition SubClient.hpp:43
Class that represents the SubFramework command. Accodring to the Mach-O loader.h documentation:
Definition SubFramework.hpp:46
Class that represents the LC_SYMTAB command.
Definition SymbolCommand.hpp:35
Class that represents the LC_THREAD / LC_UNIXTHREAD commands and that can be used to get the binary e...
Definition ThreadCommand.hpp:42
Class which represents the LC_TWOLEVEL_HINTS command.
Definition TwoLevelHints.hpp:39
Class that wraps the LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, ... commands.
Definition VersionMin.hpp:34
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:114
Definition iostream.hpp:32
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:40
Options to tweak the building process.
Definition MachO/Builder.hpp:66
bool linkedit
Definition MachO/Builder.hpp:67
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44