LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
MachO/Builder.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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 Binary;
30class BuildVersion;
31class CodeSignature;
33class DataInCode;
35class DyldEnvironment;
36class DyldExportsTrie;
37class DyldInfo;
38class DylibCommand;
39class DylinkerCommand;
41class FatBinary;
42class FunctionStarts;
43class LinkerOptHint;
44class MainCommand;
45class Routine;
47class SourceVersion;
48class SubFramework;
49class SubClient;
50class SymbolCommand;
51class ThreadCommand;
52class TwoLevelHints;
53class VersionMin;
54class RPathCommand;
55class LIEF_API Builder {
58 public: struct config_t {
61 bool linkedit = true;
62 };
63
64 Builder() = delete;
65
66 static ok_error_t write(Binary& binary, const std::string& filename);
67 static ok_error_t write(Binary& binary, const std::string& filename, config_t config);
68
69 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out);
70 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out, config_t config);
71
72 static ok_error_t write(Binary& binary, std::ostream& out);
73 static ok_error_t write(Binary& binary, std::ostream& out, config_t config);
74
75 static ok_error_t write(FatBinary& fat, const std::string& filename);
76 static ok_error_t write(FatBinary& fat, const std::string& filename, config_t config);
77
78 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out);
79 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out, config_t config);
80
81 static ok_error_t write(FatBinary& fat, std::ostream& out);
82 static ok_error_t write(FatBinary& fat, std::ostream& out, config_t config);
83
85 private:
86 LIEF_LOCAL ok_error_t build();
87
88 LIEF_LOCAL const std::vector<uint8_t>& get_build();
89 LIEF_LOCAL ok_error_t write(const std::string& filename) const;
90 LIEF_LOCAL ok_error_t write(std::ostream& os) const;
91
92 LIEF_LOCAL Builder(Binary& binary, config_t config);
93 LIEF_LOCAL Builder(std::vector<Binary*> binaries, config_t config);
94
95 LIEF_LOCAL static std::vector<uint8_t> build_raw(Binary& binary, config_t config);
96 LIEF_LOCAL static std::vector<uint8_t> build_raw(FatBinary& binary, config_t config);
97
98 template<typename T>
99 LIEF_LOCAL ok_error_t build();
100
101 LIEF_LOCAL ok_error_t build_fat();
102 LIEF_LOCAL ok_error_t build_fat_header();
103 LIEF_LOCAL ok_error_t build_load_commands();
104
105 template<typename T>
106 LIEF_LOCAL ok_error_t build_header();
107
108 template<typename T>
109 LIEF_LOCAL ok_error_t build_linkedit();
110
111 template<typename T>
112 LIEF_LOCAL ok_error_t build(DylibCommand& library);
113
114 template<typename T>
115 LIEF_LOCAL ok_error_t build(DylinkerCommand& linker);
116
117 template<class T>
118 LIEF_LOCAL ok_error_t build(VersionMin& version_min);
119
120 template<class T>
121 LIEF_LOCAL ok_error_t build(SourceVersion& source_version);
122
123 template<class T>
124 LIEF_LOCAL ok_error_t build(FunctionStarts& function_starts);
125
126 template<class T>
127 LIEF_LOCAL ok_error_t build(MainCommand& main_cmd);
128
129 template<class T>
130 LIEF_LOCAL ok_error_t build(Routine& routine);
131
132 template<class T>
133 LIEF_LOCAL ok_error_t build(RPathCommand& rpath_cmd);
134
135 template<class T>
136 LIEF_LOCAL ok_error_t build(DyldInfo& dyld_info);
137
138 template<class T>
139 LIEF_LOCAL ok_error_t build(SymbolCommand& symbol_command);
140
141 template<class T>
142 LIEF_LOCAL ok_error_t build(DynamicSymbolCommand& symbol_command);
143
144 template<class T>
145 LIEF_LOCAL ok_error_t build(DataInCode& datacode);
146
147 template<class T>
148 LIEF_LOCAL ok_error_t build(CodeSignature& code_signature);
149
150 template<class T>
151 LIEF_LOCAL ok_error_t build(SegmentSplitInfo& ssi);
152
153 template<class T>
154 LIEF_LOCAL ok_error_t build(SubFramework& sf);
155
156 template<class T>
157 LIEF_LOCAL ok_error_t build(SubClient& sf);
158
159 template<class T>
160 LIEF_LOCAL ok_error_t build(DyldEnvironment& de);
161
162 template<class T>
163 LIEF_LOCAL ok_error_t build(ThreadCommand& tc);
164
165 template<class T>
166 LIEF_LOCAL ok_error_t build(DyldChainedFixups& fixups);
167
168 template<class T>
169 LIEF_LOCAL ok_error_t build(DyldExportsTrie& exports);
170
171 template<class T>
172 LIEF_LOCAL ok_error_t build(TwoLevelHints& two);
173
174 template<class T>
175 LIEF_LOCAL ok_error_t build(LinkerOptHint& opt);
176
177 template<class T>
178 LIEF_LOCAL ok_error_t build(CodeSignatureDir& sig);
179
180 template <typename T>
181 LIEF_LOCAL ok_error_t build_segments();
182
183 template<class T>
184 LIEF_LOCAL ok_error_t build(BuildVersion& bv);
185
186 template <typename T>
187 LIEF_LOCAL ok_error_t build_symbols();
188
189 LIEF_LOCAL ok_error_t build_uuid();
190
191 template <typename T>
192 LIEF_LOCAL ok_error_t update_fixups(DyldChainedFixups& fixups);
193
194 std::vector<Binary*> binaries_;
195 Binary* binary_ = nullptr;
196 mutable vector_iostream raw_;
197 uint64_t linkedit_offset_ = 0;
198 mutable vector_iostream linkedit_;
199 config_t config_;
200};
201
202} // namespace MachO
203} // namespace LIEF
204#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:85
Definition BuildVersion.hpp:34
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:57
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)
~Builder()
Builder()=delete
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 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_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.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 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:41
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:33
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42