LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
BinaryParser.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_BINARY_PARSER_H
16#define LIEF_MACHO_BINARY_PARSER_H
17#include <string_view>
18#include <unordered_map>
19#include <limits>
20#include <map>
21#include <memory>
22#include <set>
23#include <string>
24#include <vector>
25
26#include "LIEF/errors.hpp"
27#include "LIEF/path.hpp"
28#include "LIEF/visibility.h"
29
31
35#include "LIEF/MachO/enums.hpp"
36
37namespace LIEF {
38class BinaryStream;
39class SpanStream;
40
41namespace MachO {
42class AtomInfo;
44class CodeSignature;
46class DataInCode;
48class DylibCommand;
50class ExportInfo;
51class FunctionStarts;
55class LinkerOptHint;
56class Parser;
57class Section;
58class SegmentCommand;
60class Symbol;
61class SymbolCommand;
62class TwoLevelHints;
63struct ParserConfig;
64
65
66namespace details {
67struct dyld_chained_starts_in_segment;
68struct dyld_chained_fixups_header;
69union dyld_chained_ptr_arm64e;
70union dyld_chained_ptr_generic64;
71union dyld_chained_ptr_generic32;
72union dyld_chained_ptr_arm64e_segmented;
73}
74
81
82 friend class MachO::Parser;
83
85 constexpr static size_t MAX_RELOCATIONS = (std::numeric_limits<uint16_t>::max)();
86
88 constexpr static size_t MAX_COMMANDS = (std::numeric_limits<uint16_t>::max)();
89
90 public:
91 static std::unique_ptr<Binary> parse(std::string_view file);
92 static std::unique_ptr<Binary> parse(std::string_view file,
93 const ParserConfig& conf);
94
97 template<class PathT, enable_if_path_t<PathT> = 0>
98 static std::unique_ptr<Binary>
99 parse(const PathT& file, const ParserConfig& conf = ParserConfig::deep()) {
100 return parse(file.string(), conf);
101 }
102
103 static std::unique_ptr<Binary>
104 parse(const std::vector<uint8_t>& data,
105 const ParserConfig& conf = ParserConfig::deep());
106
107 static std::unique_ptr<Binary>
108 parse(const std::vector<uint8_t>& data, uint64_t fat_offset,
109 const ParserConfig& conf = ParserConfig::deep());
110
111 static std::unique_ptr<Binary> parse(std::unique_ptr<BinaryStream> stream,
112 uint64_t fat_offset,
113 const ParserConfig& conf);
114
115 BinaryParser& operator=(const BinaryParser& copy) = delete;
116 BinaryParser(const BinaryParser& copy) = delete;
117
119
120 private:
121 using exports_list_t = std::vector<std::unique_ptr<ExportInfo>>;
123
124 LIEF_LOCAL ok_error_t init_and_parse();
125
126 template<class MACHO_T>
128
129 template<class MACHO_T>
130 LIEF_LOCAL ok_error_t parse_header();
131
132 template<class MACHO_T>
133 LIEF_LOCAL ok_error_t parse_load_commands();
134
135 template<class MACHO_T>
136 LIEF_LOCAL ok_error_t parse_relocations(Section& section);
137
138 // Dyld info parser
139 // ================
140
141 // Rebase
142 // ------
143 template<class MACHO_T>
144 LIEF_LOCAL ok_error_t parse_dyldinfo_rebases();
145
146 // Bindings
147 // --------
148 template<class MACHO_T>
149 LIEF_LOCAL ok_error_t parse_dyldinfo_binds();
150
151 template<class MACHO_T>
152 LIEF_LOCAL ok_error_t parse_dyldinfo_generic_bind();
153
154 template<class MACHO_T>
155 LIEF_LOCAL ok_error_t parse_dyldinfo_weak_bind();
156
157 template<class MACHO_T>
158 LIEF_LOCAL ok_error_t parse_dyldinfo_lazy_bind();
159
160 template<class MACHO_T>
161 LIEF_LOCAL ok_error_t infer_indirect_bindings();
162
163 template<class MACHO_T>
164 LIEF_LOCAL ok_error_t parse_symtab(SymbolCommand& cmd, SpanStream& nlist_s,
165 SpanStream& string_s);
166
167 LIEF_LOCAL ok_error_t parse_indirect_symbols(DynamicSymbolCommand& cmd,
168 std::vector<Symbol*>& symtab,
169 BinaryStream& indirect_stream);
170
171
172 template<class MACHO_T>
173 LIEF_LOCAL ok_error_t parse_data_in_code(DataInCode& cmd, BinaryStream& stream);
174
175 using it_opaque_segments =
176 void*; // To avoid including Binary.hpp. It must contains it_opaque_segments
177
178 template<class MACHO_T>
179 LIEF_LOCAL ok_error_t do_bind(DyldBindingInfo::CLASS cls, uint8_t type,
180 uint8_t segment_idx, uint64_t segment_offset,
181 const std::string& symbol_name, int32_t ord,
182 int64_t addend, bool is_weak,
183 bool is_non_weak_definition,
184 it_opaque_segments segments_ptr,
185 uint64_t offset = 0);
186
187
188 template<class MACHO_T>
189 LIEF_LOCAL ok_error_t do_rebase(uint8_t type, uint8_t segment_idx,
190 uint64_t segment_offset,
191 it_opaque_segments segments);
192
193 /*
194 * This set of functions are related to the parsing of LC_DYLD_CHAINED_FIXUPS
195 */
196 template<class MACHO_T>
197 LIEF_LOCAL ok_error_t parse_chained_payload(SpanStream& stream);
198
199 template<class MACHO_T>
201 parse_chained_import(const details::dyld_chained_fixups_header& header,
202 SpanStream& stream, SpanStream& symbol_pool);
203
204 template<class MACHO_T>
206 parse_chained_fixup(const details::dyld_chained_fixups_header& header,
207 SpanStream& stream);
208
209 template<class MACHO_T>
210 LIEF_LOCAL ok_error_t parse_fixup_seg(SpanStream& stream,
211 uint32_t seg_info_offset, uint64_t offset,
212 uint32_t seg_idx);
213
214 template<class MACHO_T>
215 LIEF_LOCAL ok_error_t do_fixup(DYLD_CHAINED_FORMAT fmt, int32_t ord,
216 const std::string& symbol_name, int64_t addend,
217 bool is_weak);
218
219 template<class MACHO_T>
221 process_fixup(SegmentCommand& segment, uint64_t chain_address,
222 uint64_t chain_offset,
223 const details::dyld_chained_starts_in_segment& seg_info);
224
225 template<class MACHO_T>
227 next_chain(uint64_t& chain_address, uint64_t chain_offset,
228 const details::dyld_chained_starts_in_segment& seg_info);
229
230 template<class MACHO_T>
232 walk_chain(SegmentCommand& segment, uint64_t chain_address,
233 uint64_t chain_offset,
234 const details::dyld_chained_starts_in_segment& seg_info);
235
237 do_chained_fixup(SegmentCommand& segment, uint64_t chain_address,
238 uint32_t chain_offset,
239 const details::dyld_chained_starts_in_segment& seg_info,
240 const details::dyld_chained_ptr_arm64e& fixup);
241
243 do_chained_fixup(SegmentCommand& segment, uint64_t chain_address,
244 uint32_t chain_offset,
245 const details::dyld_chained_starts_in_segment& seg_info,
246 const details::dyld_chained_ptr_generic64& fixup);
247
249 do_chained_fixup(SegmentCommand& segment, uint64_t chain_address,
250 uint32_t chain_offset,
251 const details::dyld_chained_starts_in_segment& seg_info,
252 const details::dyld_chained_ptr_generic32& fixup);
253
255 do_chained_fixup(SegmentCommand& segment, uint64_t chain_address,
256 uint32_t chain_offset,
257 const details::dyld_chained_starts_in_segment& seg_info,
258 const details::dyld_chained_ptr_arm64e_segmented& fixup);
259
260 template<class MACHO_T>
261 LIEF_LOCAL ok_error_t post_process(SymbolCommand& cmd);
262
263 template<class MACHO_T>
264 LIEF_LOCAL ok_error_t post_process(FunctionStarts& cmd);
265
266 template<class MACHO_T>
267 LIEF_LOCAL ok_error_t post_process(DataInCode& cmd);
268
269 template<class MACHO_T>
270 LIEF_LOCAL ok_error_t post_process(SegmentSplitInfo& cmd);
271
272 template<class MACHO_T>
273 LIEF_LOCAL ok_error_t post_process(DynamicSymbolCommand& cmd);
274
275 template<class MACHO_T>
276 LIEF_LOCAL ok_error_t post_process(LinkerOptHint& cmd);
277
278 template<class MACHO_T>
279 LIEF_LOCAL ok_error_t post_process(AtomInfo& cmd);
280
281 template<class MACHO_T>
282 LIEF_LOCAL ok_error_t post_process(TwoLevelHints& cmd);
283
284 template<class MACHO_T>
285 LIEF_LOCAL ok_error_t post_process(CodeSignature& cmd);
286
287 template<class MACHO_T>
288 LIEF_LOCAL ok_error_t post_process(CodeSignatureDir& cmd);
289
290 template<class MACHO_T>
291 LIEF_LOCAL ok_error_t post_process(FunctionVariants& cmd);
292
293 template<class MACHO_T>
295
296 template<class MACHO_T>
297 LIEF_LOCAL ok_error_t post_process(LazyLoadDylibInfo& cmd);
298
299 LIEF_LOCAL ok_error_t parse_overlay();
300
301 // Exports
302 // -------
303 LIEF_LOCAL ok_error_t parse_dyldinfo_export();
304 LIEF_LOCAL ok_error_t parse_dyld_exports();
305
306 LIEF_LOCAL result<exports_list_t> parse_export_trie(BinaryStream& stream,
307 uint64_t start,
308 const std::string& prefix,
309 uint32_t depth,
310 bool* invalid_names);
311
312 LIEF_LOCAL void copy_from(ChainedBindingInfo& to, ChainedBindingInfo& from);
313
314 std::unique_ptr<BinaryStream> stream_;
315 std::unique_ptr<Binary> binary_;
317 bool is64_ = true;
318 ParserConfig config_;
319 std::set<uint64_t> visited_;
320 std::unordered_map<std::string, Symbol*> memoized_symbols_;
321 std::map<uint64_t, Symbol*> memoized_symbols_by_address_;
322
323 std::vector<DylibCommand*> binding_libs_;
324 std::set<uint64_t> dyld_reloc_addrs_;
325
326 // Cache of DyldChainedFixups
327 DyldChainedFixups* chained_fixups_ = nullptr;
328};
329
330
331}
332}
333#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Class which represents the LC_ATOM_INFO command.
Definition AtomInfo.hpp:36
BinaryParser(const BinaryParser &copy)=delete
static std::unique_ptr< Binary > parse(std::string_view file)
static std::unique_ptr< Binary > parse(const PathT &file, const ParserConfig &conf=ParserConfig::deep())
Same as parse(std::string_view, const ParserConfig&) but the file is given as a std::filesystem::path...
Definition BinaryParser.hpp:99
BinaryParser & operator=(const BinaryParser &copy)=delete
static std::unique_ptr< Binary > parse(std::unique_ptr< BinaryStream > stream, uint64_t fat_offset, const ParserConfig &conf)
static std::unique_ptr< Binary > parse(const std::vector< uint8_t > &data, uint64_t fat_offset, const ParserConfig &conf=ParserConfig::deep())
static std::unique_ptr< Binary > parse(std::string_view file, const ParserConfig &conf)
static std::unique_ptr< Binary > parse(const std::vector< uint8_t > &data, const ParserConfig &conf=ParserConfig::deep())
This class represents a symbol binding operation associated with the LC_DYLD_CHAINED_FIXUPS command.
Definition ChainedBindingInfo.hpp:47
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
Definition DyldBindingInfo.hpp:38
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:49
Class which represents a library dependency.
Definition DylibCommand.hpp:35
Class that represents the LC_DYSYMTAB command.
Definition DynamicSymbolCommand.hpp:40
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:37
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
The main interface to parse a Mach-O binary.
Definition MachO/Parser.hpp:42
Class that represents a Mach-O section.
Definition MachO/Section.hpp:48
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:53
Class that represents the LoadCommand::TYPE::SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:36
Class that represents the LC_SYMTAB command.
Definition SymbolCommand.hpp:36
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:46
Class which represents the LC_TWOLEVEL_HINTS command.
Definition TwoLevelHints.hpp:40
Main interface to parse an executable regardless of its format.
Definition Abstract/Parser.hpp:31
Definition SpanStream.hpp:31
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
Definition endianness_support.hpp:62
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:35
DYLD_CHAINED_FORMAT
Definition DyldChainedFormat.hpp:22
MACHO_TYPES
Definition MachO/enums.hpp:23
@ MAGIC_64
64-bit big-endian magic
Definition MachO/enums.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:41
This structure is used to tweak the MachO Parser (MachO::Parser).
Definition MachO/ParserConfig.hpp:23
static ParserConfig deep()
Return a parser configuration such as all the objects supported by LIEF are parsed.
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45