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;
48class LinkerOptHint;
49class LoadCommand;
50class MainCommand;
51class NoteCommand;
52class RPathCommand;
53class Routine;
55class SourceVersion;
56class SubClient;
57class SubFramework;
58class SymbolCommand;
59class ThreadCommand;
60class TwoLevelHints;
61class VersionMin;
62
65 public:
67 struct config_t {
68 bool linkedit = true;
69 };
70
71 Builder() = delete;
72
73 static ok_error_t write(Binary& binary, const std::string& filename);
74 static ok_error_t write(Binary& binary, const std::string& filename,
75 config_t config);
76
77 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out);
78 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out,
79 config_t config);
80
81 static ok_error_t write(Binary& binary, std::ostream& out);
82 static ok_error_t write(Binary& binary, std::ostream& out, config_t config);
83
84 static ok_error_t write(FatBinary& fat, const std::string& filename);
85 static ok_error_t write(FatBinary& fat, const std::string& filename,
86 config_t config);
87
88 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out);
89 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out,
90 config_t config);
91
92 static ok_error_t write(FatBinary& fat, std::ostream& out);
93 static ok_error_t write(FatBinary& fat, std::ostream& out, config_t config);
94
96
97 private:
98 LIEF_LOCAL ok_error_t build();
99
100 LIEF_LOCAL bool should_swap() const;
101
102 template<class T>
103 LIEF_LOCAL void swap_endian_if_needed(T& s) const {
104 if (should_swap()) {
106 }
107 }
108
109 LIEF_LOCAL const std::vector<uint8_t>& get_build();
110 LIEF_LOCAL ok_error_t write(const std::string& filename) const;
111 LIEF_LOCAL ok_error_t write(std::ostream& os) const;
112
113 LIEF_LOCAL Builder(Binary& binary, config_t config);
114 LIEF_LOCAL Builder(std::vector<Binary*> binaries, config_t config);
115
116 LIEF_LOCAL static std::vector<uint8_t> build_raw(Binary& binary,
117 config_t config);
118 LIEF_LOCAL static std::vector<uint8_t> build_raw(FatBinary& binary,
119 config_t config);
120
121 template<class T>
122 LIEF_LOCAL static size_t get_cmd_size(const LoadCommand& cmd);
123
124 template<typename T>
125 LIEF_LOCAL ok_error_t build();
126
127 LIEF_LOCAL ok_error_t build_fat();
128 LIEF_LOCAL ok_error_t build_fat_header();
129 LIEF_LOCAL ok_error_t build_load_commands();
130
131 template<typename T>
132 LIEF_LOCAL ok_error_t build_header();
133
134 template<typename T>
135 LIEF_LOCAL ok_error_t build_linkedit();
136
137 template<typename T>
138 LIEF_LOCAL ok_error_t build(DylibCommand& library);
139
140 template<typename T>
141 LIEF_LOCAL ok_error_t build(DylinkerCommand& linker);
142
143 template<class T>
144 LIEF_LOCAL ok_error_t build(VersionMin& version_min);
145
146 template<class T>
147 LIEF_LOCAL ok_error_t build(SourceVersion& source_version);
148
149 template<class T>
150 LIEF_LOCAL ok_error_t build(FunctionStarts& function_starts);
151
152 template<class T>
153 LIEF_LOCAL ok_error_t build(MainCommand& main_cmd);
154
155 template<class T>
156 LIEF_LOCAL ok_error_t build(NoteCommand& main_cmd);
157
158 template<class T>
159 LIEF_LOCAL ok_error_t build(Routine& routine);
160
161 template<class T>
162 LIEF_LOCAL ok_error_t build(RPathCommand& rpath_cmd);
163
164 template<class T>
165 LIEF_LOCAL ok_error_t build(DyldInfo& dyld_info);
166
167 template<class T>
168 LIEF_LOCAL ok_error_t build(SymbolCommand& symbol_command);
169
170 template<class T>
171 LIEF_LOCAL ok_error_t build(DynamicSymbolCommand& symbol_command);
172
173 template<class T>
174 LIEF_LOCAL ok_error_t build(DataInCode& datacode);
175
176 template<class T>
177 LIEF_LOCAL ok_error_t build(CodeSignature& code_signature);
178
179 template<class T>
180 LIEF_LOCAL ok_error_t build(SegmentSplitInfo& ssi);
181
182 template<class T>
183 LIEF_LOCAL ok_error_t build(SubFramework& sf);
184
185 template<class T>
186 LIEF_LOCAL ok_error_t build(SubClient& sf);
187
188 template<class T>
189 LIEF_LOCAL ok_error_t build(DyldEnvironment& de);
190
191 template<class T>
192 LIEF_LOCAL ok_error_t build(ThreadCommand& tc);
193
194 template<class T>
195 LIEF_LOCAL ok_error_t build(DyldChainedFixups& fixups);
196
197 template<class T>
198 LIEF_LOCAL ok_error_t build(DyldExportsTrie& exports);
199
200 template<class T>
201 LIEF_LOCAL ok_error_t build(TwoLevelHints& two);
202
203 template<class T>
204 LIEF_LOCAL ok_error_t build(LinkerOptHint& opt);
205
206 template<class T>
207 LIEF_LOCAL ok_error_t build(AtomInfo& opt);
208
209 template<class T>
210 LIEF_LOCAL ok_error_t build(CodeSignatureDir& sig);
211
212 template<class T>
213 LIEF_LOCAL ok_error_t build(EncryptionInfo& tc);
214
215 template<class T>
216 LIEF_LOCAL ok_error_t build(FunctionVariants& func);
217
218 template<class T>
219 LIEF_LOCAL ok_error_t build(FunctionVariantFixups& func);
220
221 template<class T>
222 LIEF_LOCAL ok_error_t build(LazyLoadDylibInfo& cmd);
223
224 template<typename T>
225 LIEF_LOCAL ok_error_t build_segments();
226
227 template<class T>
228 LIEF_LOCAL ok_error_t build(BuildVersion& bv);
229
230 template<typename T>
231 LIEF_LOCAL ok_error_t build_symbols();
232
233 LIEF_LOCAL ok_error_t build_uuid();
234
235 template<typename T>
236 LIEF_LOCAL ok_error_t update_fixups(DyldChainedFixups& fixups);
237
238 std::vector<Binary*> binaries_;
239 Binary* binary_ = nullptr;
240 mutable vector_iostream raw_;
241 uint64_t linkedit_offset_ = 0;
242 mutable vector_iostream linkedit_;
243 config_t config_;
244};
245
246} // namespace MachO
247} // namespace LIEF
248#endif
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
Class which represents the LC_ATOM_INFO command.
Definition AtomInfo.hpp:36
Class which represents a MachO binary.
Definition MachO/Binary.hpp:91
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:37
Definition CodeSignature.hpp:38
Interface of the LC_DATA_IN_CODE command This command is used to list slices of code sections that co...
Definition DataInCode.hpp:43
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:50
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:43
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:51
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 represents a Mach-O (fat) binary This object is also used for representing Mach-O binarie...
Definition FatBinary.hpp:36
Class which represents the LC_FUNCTION_STARTS command.
Definition FunctionStarts.hpp:40
Class which represents the LC_FUNCTION_VARIANT_FIXUPS command.
Definition FunctionVariantFixups.hpp:60
Class representing the LC_FUNCTION_VARIANTS load command.
Definition FunctionVariants.hpp:69
Class representing the LC_LAZY_LOAD_DYLIB_INFO load command.
Definition LazyLoadDylibInfo.hpp:49
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:38
Base class for the Mach-O load commands.
Definition LoadCommand.hpp:38
Class that represents the LC_MAIN command. This kind of command can be used to determine the entrypoi...
Definition MainCommand.hpp:33
Class that represents the LC_NOTE command.
Definition NoteCommand.hpp:38
Class that represents the LC_RPATH command.
Definition RPathCommand.hpp:36
Class that represents the LC_ROUTINE/LC_ROUTINE64 commands. According to the Mach-O loader....
Definition Routine.hpp:38
Class that represents the LoadCommand::TYPE::SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:36
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. According to the Mach-O loader.h documentation:
Definition SubClient.hpp:43
Class that represents the SubFramework command. According to the Mach-O loader.h documentation:
Definition SubFramework.hpp:46
Class that represents the LC_SYMTAB command.
Definition SymbolCommand.hpp:36
Class that represents the LC_THREAD / LC_UNIXTHREAD commands and that can be used to get the binary e...
Definition ThreadCommand.hpp:43
Class which represents the LC_TWOLEVEL_HINTS command.
Definition TwoLevelHints.hpp:40
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:118
Definition iostream.hpp:32
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:41
void swap_endian(T *)
Definition endianness_support.hpp:118
Options to tweak the building process.
Definition MachO/Builder.hpp:67
bool linkedit
Definition MachO/Builder.hpp:68
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46