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