LIEF: Library to Instrument Executable Formats Version
Loading...
Searching...
No Matches
MachO/Builder.hpp
1/* Copyright 2017 - 2023 R. Thomas
2 * Copyright 2017 - 2023 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 <algorithm>
20#include <functional>
21#include <memory>
22#include <unordered_map>
23#include <vector>
24
25#include "LIEF/errors.hpp"
26#include "LIEF/visibility.h"
27
28#include "LIEF/iostream.hpp"
29
30namespace LIEF {
31namespace MachO {
32
33class Binary;
34class BuildVersion;
35class CodeSignature;
36class CodeSignatureDir;
37class DataInCode;
38class DyldChainedFixups;
39class DyldEnvironment;
40class DyldExportsTrie;
41class DyldInfo;
42class DylibCommand;
43class DylinkerCommand;
44class DynamicSymbolCommand;
45class FatBinary;
46class FunctionStarts;
47class LinkerOptHint;
48class MainCommand;
49class SegmentSplitInfo;
50class SourceVersion;
51class SubFramework;
52class SymbolCommand;
53class ThreadCommand;
54class TwoLevelHints;
55class VersionMin;
56
58class LIEF_API Builder {
59 public:
61 struct config_t {
62 bool linkedit = true;
63 };
64
65 Builder() = delete;
66
67 static ok_error_t write(Binary& binary, const std::string& filename);
68 static ok_error_t write(Binary& binary, const std::string& filename, config_t config);
69
70 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out);
71 static ok_error_t write(Binary& binary, std::vector<uint8_t>& out, config_t config);
72
73 static ok_error_t write(Binary& binary, std::ostream& out);
74 static ok_error_t write(Binary& binary, std::ostream& out, config_t config);
75
76 static ok_error_t write(FatBinary& fat, const std::string& filename);
77 static ok_error_t write(FatBinary& fat, const std::string& filename, config_t config);
78
79 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out);
80 static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out, config_t config);
81
82 static ok_error_t write(FatBinary& fat, std::ostream& out);
83 static ok_error_t write(FatBinary& fat, std::ostream& out, config_t config);
84
85 ~Builder();
86 private:
87 ok_error_t build();
88
89 const std::vector<uint8_t>& get_build();
90 ok_error_t write(const std::string& filename) const;
91 ok_error_t write(std::ostream& os) const;
92
93 Builder(Binary& binary, config_t config);
94 Builder(std::vector<Binary*> binaries, config_t config);
95
96 static std::vector<uint8_t> build_raw(Binary& binary, config_t config);
97 static std::vector<uint8_t> build_raw(FatBinary& binary, config_t config);
98
99 template<typename T>
100 ok_error_t build();
101
102 ok_error_t build_fat();
103 ok_error_t build_fat_header();
104 ok_error_t build_header();
105 ok_error_t build_load_commands();
106
107 template<typename T>
108 ok_error_t build_linkedit();
109
110 template<typename T>
111 ok_error_t build(DylibCommand& library);
112
113 template<typename T>
114 ok_error_t build(DylinkerCommand& linker);
115
116 template<class T>
117 ok_error_t build(VersionMin& version_min);
118
119 template<class T>
120 ok_error_t build(SourceVersion& source_version);
121
122 template<class T>
123 ok_error_t build(FunctionStarts& function_starts);
124
125 template<class T>
126 ok_error_t build(MainCommand& main_cmd);
127
128 template<class T>
129 ok_error_t build(DyldInfo& dyld_info);
130
131 template<class T>
132 ok_error_t build(SymbolCommand& symbol_command);
133
134 template<class T>
135 ok_error_t build(DynamicSymbolCommand& symbol_command);
136
137 template<class T>
138 ok_error_t build(DataInCode& datacode);
139
140 template<class T>
141 ok_error_t build(CodeSignature& code_signature);
142
143 template<class T>
144 ok_error_t build(SegmentSplitInfo& ssi);
145
146 template<class T>
147 ok_error_t build(SubFramework& sf);
148
149 template<class T>
150 ok_error_t build(DyldEnvironment& de);
151
152 template<class T>
153 ok_error_t build(ThreadCommand& tc);
154
155 template<class T>
156 ok_error_t build(DyldChainedFixups& fixups);
157
158 template<class T>
159 ok_error_t build(DyldExportsTrie& exports);
160
161 template<class T>
162 ok_error_t build(TwoLevelHints& two);
163
164 template<class T>
165 ok_error_t build(LinkerOptHint& opt);
166
167 template<class T>
168 ok_error_t build(CodeSignatureDir& sig);
169
170 template <typename T>
171 ok_error_t build_segments();
172
173 template<class T>
174 ok_error_t build(BuildVersion& bv);
175
176 template <typename T>
177 ok_error_t build_symbols();
178
179 ok_error_t build_uuid();
180
181 template <typename T>
182 ok_error_t update_fixups(DyldChainedFixups& fixups);
183
184
185
186 std::vector<Binary*> binaries_;
187 Binary* binary_ = nullptr;
188 mutable vector_iostream raw_;
189 uint64_t linkedit_offset_ = 0;
190 mutable vector_iostream linkedit_;
191 config_t config_;
192};
193
194} // namespace MachO
195} // namespace LIEF
196#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:74
Definition BuildVersion.hpp:35
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:58
Definition CodeSignatureDir.hpp:38
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:44
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:45
Class that represents a LC_DYLD_ENVIRONMENT which is used by the Mach-O linker/loader to initialize a...
Definition DyldEnvironment.hpp:35
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:35
Class that represents the Mach-O linker, also named loader Most of the time, DylinkerCommand::name() ...
Definition DylinkerCommand.hpp:36
Class that represents the LC_DYSYMTAB command.
Definition DynamicSymbolCommand.hpp:41
Class which represent a Mach-O (fat) binary This object is also used for representing Mach-O binaries...
Definition FatBinary.hpp:38
Class which represents the LC_FUNCTION_STARTS command.
Definition FunctionStarts.hpp:42
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:39
Class that represent the LC_MAIN command. This kind of command can be used to determine the entrypoin...
Definition MainCommand.hpp:34
Class that represents the LOAD_COMMAND_TYPES::LC_SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:37
Class that represents the MachO LOAD_COMMAND_TYPES::LC_SOURCE_VERSION This command is used to provide...
Definition SourceVersion.hpp:35
Class that represents the SubFramework command. Accodring to the Mach-O loader.h documentation:
Definition SubFramework.hpp:47
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:41
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
Definition iostream.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:106
Options to tweak the building process.
Definition MachO/Builder.hpp:61