LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
MachO/Builder.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
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 */
15#ifndef LIEF_MACHO_BUIDLER_H
16#define LIEF_MACHO_BUIDLER_H
17
18#include <vector>
19
20#include "LIEF/errors.hpp"
21#include "LIEF/visibility.h"
22
23#include "LIEF/iostream.hpp"
24
25
26namespace LIEF::MachO {
27
28class AtomInfo;
29class Binary;
30class BuildVersion;
31class CodeSignature;
33class DataInCode;
35class DyldEnvironment;
36class DyldExportsTrie;
37class DyldInfo;
38class DylibCommand;
39class DylinkerCommand;
41class EncryptionInfo;
42class FatBinary;
43class 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 bool should_swap() const;
100
101 template<class T>
102 LIEF_LOCAL void swap_endian_if_needed(T& s) const {
103 if (should_swap()) {
105 }
106 }
107
108 LIEF_LOCAL const std::vector<uint8_t>& get_build();
109 LIEF_LOCAL ok_error_t write(const std::string& filename) const;
110 LIEF_LOCAL ok_error_t write(std::ostream& os) const;
111
112 LIEF_LOCAL Builder(Binary& binary, config_t config);
113 LIEF_LOCAL Builder(std::vector<Binary*> binaries, config_t config);
114
115 LIEF_LOCAL static std::vector<uint8_t> build_raw(Binary& binary,
116 config_t config);
117 LIEF_LOCAL static std::vector<uint8_t> build_raw(FatBinary& binary,
118 config_t config);
119
120 template<class T>
121 LIEF_LOCAL static size_t get_cmd_size(const LoadCommand& cmd);
122
123 template<typename T>
124 LIEF_LOCAL ok_error_t build();
125
126 LIEF_LOCAL ok_error_t build_fat();
127 LIEF_LOCAL ok_error_t build_fat_header();
128 LIEF_LOCAL ok_error_t build_load_commands();
129
130 template<typename T>
131 LIEF_LOCAL ok_error_t build_header();
132
133 template<typename T>
134 LIEF_LOCAL ok_error_t build_linkedit();
135
136 template<typename T>
137 LIEF_LOCAL ok_error_t build(DylibCommand& library);
138
139 template<typename T>
140 LIEF_LOCAL ok_error_t build(DylinkerCommand& linker);
141
142 template<class T>
143 LIEF_LOCAL ok_error_t build(VersionMin& version_min);
144
145 template<class T>
146 LIEF_LOCAL ok_error_t build(SourceVersion& source_version);
147
148 template<class T>
149 LIEF_LOCAL ok_error_t build(FunctionStarts& function_starts);
150
151 template<class T>
152 LIEF_LOCAL ok_error_t build(MainCommand& main_cmd);
153
154 template<class T>
155 LIEF_LOCAL ok_error_t build(NoteCommand& main_cmd);
156
157 template<class T>
158 LIEF_LOCAL ok_error_t build(Routine& routine);
159
160 template<class T>
161 LIEF_LOCAL ok_error_t build(RPathCommand& rpath_cmd);
162
163 template<class T>
164 LIEF_LOCAL ok_error_t build(DyldInfo& dyld_info);
165
166 template<class T>
167 LIEF_LOCAL ok_error_t build(SymbolCommand& symbol_command);
168
169 template<class T>
170 LIEF_LOCAL ok_error_t build(DynamicSymbolCommand& symbol_command);
171
172 template<class T>
173 LIEF_LOCAL ok_error_t build(DataInCode& datacode);
174
175 template<class T>
176 LIEF_LOCAL ok_error_t build(CodeSignature& code_signature);
177
178 template<class T>
179 LIEF_LOCAL ok_error_t build(SegmentSplitInfo& ssi);
180
181 template<class T>
182 LIEF_LOCAL ok_error_t build(SubFramework& sf);
183
184 template<class T>
185 LIEF_LOCAL ok_error_t build(SubClient& sf);
186
187 template<class T>
188 LIEF_LOCAL ok_error_t build(DyldEnvironment& de);
189
190 template<class T>
191 LIEF_LOCAL ok_error_t build(ThreadCommand& tc);
192
193 template<class T>
194 LIEF_LOCAL ok_error_t build(DyldChainedFixups& fixups);
195
196 template<class T>
197 LIEF_LOCAL ok_error_t build(DyldExportsTrie& exports);
198
199 template<class T>
200 LIEF_LOCAL ok_error_t build(TwoLevelHints& two);
201
202 template<class T>
203 LIEF_LOCAL ok_error_t build(LinkerOptHint& opt);
204
205 template<class T>
206 LIEF_LOCAL ok_error_t build(AtomInfo& opt);
207
208 template<class T>
209 LIEF_LOCAL ok_error_t build(CodeSignatureDir& sig);
210
211 template<class T>
212 LIEF_LOCAL ok_error_t build(EncryptionInfo& tc);
213
214 template<class T>
215 LIEF_LOCAL ok_error_t build(FunctionVariants& func);
216
217 template<class T>
218 LIEF_LOCAL ok_error_t build(FunctionVariantFixups& func);
219
220 template<class T>
221 LIEF_LOCAL ok_error_t build(LazyLoadDylibInfo& cmd);
222
223 template<typename T>
224 LIEF_LOCAL ok_error_t build_segments();
225
226 template<class T>
227 LIEF_LOCAL ok_error_t build(BuildVersion& bv);
228
229 template<typename T>
230 LIEF_LOCAL ok_error_t build_symbols();
231
232 LIEF_LOCAL ok_error_t build_uuid();
233
234 template<typename T>
235 LIEF_LOCAL ok_error_t update_fixups(DyldChainedFixups& fixups);
236
237 std::vector<Binary*> binaries_;
238 Binary* binary_ = nullptr;
239 mutable vector_iostream raw_;
240 uint64_t linkedit_offset_ = 0;
241 mutable vector_iostream linkedit_;
242 config_t config_;
243};
244
245}
246
247#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:93
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:49
Class that represents a LC_DYLD_ENVIRONMENT command which is used by the Mach-O linker/loader to init...
Definition DyldEnvironment.hpp:35
Class that represents the LC_DYLD_EXPORTS_TRIE command.
Definition DyldExportsTrie.hpp:42
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:35
Class that represents the Mach-O linker, also named loader. Most of the time, DylinkerCommand::name()...
Definition DylinkerCommand.hpp:35
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:35
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:50
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:38
Base class for the Mach-O load commands.
Definition LoadCommand.hpp:37
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:38
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:46
Class that represents the SubFramework command. According to the Mach-O loader.h documentation:
Definition SubFramework.hpp:48
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:33
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:35
void swap_endian(T *)
Definition endianness_support.hpp:120
Options to tweak the building process.
Definition MachO/Builder.hpp:66
bool linkedit
Definition MachO/Builder.hpp:67
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45