LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
BinaryParser.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_BINARY_PARSER_H
17#define LIEF_MACHO_BINARY_PARSER_H
18#include <memory>
19#include <string>
20#include <vector>
21#include <limits>
22#include <set>
23#include <map>
24#include <unordered_map>
25
26#include "LIEF/visibility.h"
27#include "LIEF/errors.hpp"
28
30
31#include "LIEF/MachO/enums.hpp"
35
36namespace LIEF {
37class BinaryStream;
38class SpanStream;
39
40namespace MachO {
42class CodeSignature;
44class DataInCode;
46class DylibCommand;
48class ExportInfo;
49class FunctionStarts;
50class LinkerOptHint;
51class Parser;
52class Section;
53class SegmentCommand;
55class Symbol;
56class SymbolCommand;
57class TwoLevelHints;
58struct ParserConfig;
59
60
61namespace details {
62struct dyld_chained_starts_in_segment;
63struct dyld_chained_fixups_header;
64union dyld_chained_ptr_arm64e;
65union dyld_chained_ptr_generic64;
66union dyld_chained_ptr_generic32;
67}
68class LIEF_API BinaryParser : public LIEF::Parser {
75
76 friend class MachO::Parser;
77 constexpr static size_t MAX_RELOCATIONS = (std::numeric_limits<uint16_t>::max)();
80 constexpr static size_t MAX_COMMANDS = (std::numeric_limits<uint16_t>::max)();
83
84 public:
85 static std::unique_ptr<Binary> parse(const std::string& file);
86 static std::unique_ptr<Binary> parse(const std::string& file, const ParserConfig& conf);
87 static std::unique_ptr<Binary> parse(const std::vector<uint8_t>& data,
88 const ParserConfig& conf = ParserConfig::deep());
89
90 static std::unique_ptr<Binary> parse(const std::vector<uint8_t>& data, uint64_t fat_offset,
91 const ParserConfig& conf = ParserConfig::deep());
92
93 static std::unique_ptr<Binary> parse(std::unique_ptr<BinaryStream> stream, uint64_t fat_offset,
94 const ParserConfig& conf);
95
96 BinaryParser& operator=(const BinaryParser& copy) = delete;
97 BinaryParser(const BinaryParser& copy) = delete;
98
99 ~BinaryParser() override;
100
101 private:
102 using exports_list_t = std::vector<std::unique_ptr<ExportInfo>>;
103 LIEF_LOCAL BinaryParser();
104
105 LIEF_LOCAL ok_error_t init_and_parse();
106
107 template<class MACHO_T>
108 LIEF_LOCAL ok_error_t parse();
109
110 template<class MACHO_T>
111 LIEF_LOCAL ok_error_t parse_header();
112
113 template<class MACHO_T>
114 LIEF_LOCAL ok_error_t parse_load_commands();
115
116 template<class MACHO_T>
117 LIEF_LOCAL ok_error_t parse_relocations(Section& section);
118
119 // Dyld info parser
120 // ================
121
122 // Rebase
123 // ------
124 template<class MACHO_T>
125 LIEF_LOCAL ok_error_t parse_dyldinfo_rebases();
126
127 // Bindings
128 // --------
129 template<class MACHO_T>
130 LIEF_LOCAL ok_error_t parse_dyldinfo_binds();
131
132 template<class MACHO_T>
133 LIEF_LOCAL ok_error_t parse_dyldinfo_generic_bind();
134
135 template<class MACHO_T>
136 LIEF_LOCAL ok_error_t parse_dyldinfo_weak_bind();
137
138 template<class MACHO_T>
139 LIEF_LOCAL ok_error_t parse_dyldinfo_lazy_bind();
140
141 template<class MACHO_T>
142 LIEF_LOCAL ok_error_t infer_indirect_bindings();
143
144 template<class MACHO_T>
145 LIEF_LOCAL ok_error_t parse_symtab(SymbolCommand& cmd, SpanStream& nlist_s,
146 SpanStream& string_s);
147
148 LIEF_LOCAL ok_error_t parse_indirect_symbols(
149 DynamicSymbolCommand& cmd, std::vector<Symbol*>& symtab,
150 BinaryStream& indirect_stream);
151
152
153 template<class MACHO_T>
154 LIEF_LOCAL ok_error_t parse_data_in_code(DataInCode& cmd, BinaryStream& stream);
155
156 using it_opaque_segments = void*; // To avoid including Binary.hpp. It must contains it_opaque_segments
157
158 template<class MACHO_T>
159 LIEF_LOCAL ok_error_t do_bind(DyldBindingInfo::CLASS cls, uint8_t type, uint8_t segment_idx,
160 uint64_t segment_offset, const std::string& symbol_name,
161 int32_t ord, int64_t addend, bool is_weak,
162 bool is_non_weak_definition, it_opaque_segments segments_ptr, uint64_t offset = 0);
163
164
165 template<class MACHO_T>
166 LIEF_LOCAL ok_error_t do_rebase(
167 uint8_t type, uint8_t segment_idx, uint64_t segment_offset,
168 it_opaque_segments segments);
169
170 /*
171 * This set of functions are related to the parsing of LC_DYLD_CHAINED_FIXUPS
172 */
173
174 template<class MACHO_T>
175 LIEF_LOCAL ok_error_t parse_chained_payload(SpanStream& stream);
176
177 template<class MACHO_T>
178 LIEF_LOCAL ok_error_t parse_chained_import(
179 const details::dyld_chained_fixups_header& header, SpanStream& stream,
180 SpanStream& symbol_pool);
181
182 template<class MACHO_T>
183 LIEF_LOCAL ok_error_t parse_chained_fixup(
184 const details::dyld_chained_fixups_header& header, SpanStream& stream);
185
186 template<class MACHO_T>
187 LIEF_LOCAL ok_error_t parse_fixup_seg(
188 SpanStream& stream, uint32_t seg_info_offset, uint64_t offset,
189 uint32_t seg_idx);
190
191 template<class MACHO_T>
192 LIEF_LOCAL ok_error_t do_fixup(
193 DYLD_CHAINED_FORMAT fmt, int32_t ord, const std::string& symbol_name,
194 int64_t addend, bool is_weak);
195
196 template<class MACHO_T>
197 LIEF_LOCAL ok_error_t process_fixup(
198 SegmentCommand& segment, uint64_t chain_address, uint64_t chain_offset,
199 const details::dyld_chained_starts_in_segment& seg_info);
200
201 template<class MACHO_T>
202 LIEF_LOCAL result<uint64_t> next_chain(
203 uint64_t& chain_address, uint64_t chain_offset,
204 const details::dyld_chained_starts_in_segment& seg_info);
205
206 template<class MACHO_T>
207 LIEF_LOCAL ok_error_t walk_chain(
208 SegmentCommand& segment, uint64_t chain_address, uint64_t chain_offset,
209 const details::dyld_chained_starts_in_segment& seg_info);
210
211 LIEF_LOCAL ok_error_t do_chained_fixup(
212 SegmentCommand& segment, uint64_t chain_address, uint32_t chain_offset,
213 const details::dyld_chained_starts_in_segment& seg_info,
214 const details::dyld_chained_ptr_arm64e& fixup);
215
216 LIEF_LOCAL ok_error_t do_chained_fixup(
217 SegmentCommand& segment, uint64_t chain_address, uint32_t chain_offset,
218 const details::dyld_chained_starts_in_segment& seg_info,
219 const details::dyld_chained_ptr_generic64& fixup);
220
221 LIEF_LOCAL ok_error_t do_chained_fixup(
222 SegmentCommand& segment, uint64_t chain_address, uint32_t chain_offset,
223 const details::dyld_chained_starts_in_segment& seg_info,
224 const details::dyld_chained_ptr_generic32 & fixup);
225
226 template<class MACHO_T>
227 LIEF_LOCAL ok_error_t post_process(SymbolCommand& cmd);
228
229 template<class MACHO_T>
230 LIEF_LOCAL ok_error_t post_process(FunctionStarts& cmd);
231
232 template<class MACHO_T>
233 LIEF_LOCAL ok_error_t post_process(DataInCode& cmd);
234
235 template<class MACHO_T>
236 LIEF_LOCAL ok_error_t post_process(SegmentSplitInfo& cmd);
237
238 template<class MACHO_T>
239 LIEF_LOCAL ok_error_t post_process(DynamicSymbolCommand& cmd);
240
241 template<class MACHO_T>
242 LIEF_LOCAL ok_error_t post_process(LinkerOptHint& cmd);
243
244 template<class MACHO_T>
245 LIEF_LOCAL ok_error_t post_process(TwoLevelHints& cmd);
246
247 template<class MACHO_T>
248 LIEF_LOCAL ok_error_t post_process(CodeSignature& cmd);
249
250 template<class MACHO_T>
251 LIEF_LOCAL ok_error_t post_process(CodeSignatureDir& cmd);
252
253 LIEF_LOCAL ok_error_t parse_overlay();
254
255 // Exports
256 // -------
257 LIEF_LOCAL ok_error_t parse_dyldinfo_export();
258 LIEF_LOCAL ok_error_t parse_dyld_exports();
259
260 LIEF_LOCAL ok_error_t parse_export_trie(
261 exports_list_t& exports, BinaryStream& stream,
262 uint64_t start, const std::string& prefix, bool* invalid_names);
263
264 LIEF_LOCAL void copy_from(ChainedBindingInfo& to, ChainedBindingInfo& from);
265
266 std::unique_ptr<BinaryStream> stream_;
267 std::unique_ptr<Binary> binary_;
268 MACHO_TYPES type_ = MACHO_TYPES::MH_MAGIC_64;
269 bool is64_ = true;
270 ParserConfig config_;
271 std::set<uint64_t> visited_;
272 std::unordered_map<std::string, Symbol*> memoized_symbols_;
273 std::map<uint64_t, Symbol*> memoized_symbols_by_address_;
274
275 std::vector<DylibCommand*> binding_libs_;
276 std::set<uint64_t> dyld_reloc_addrs_;
277
278 // Cache of DyldChainedFixups
279 DyldChainedFixups* chained_fixups_ = nullptr;
280};
281
282
283} // namespace MachO
284} // namespace LIEF
285#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
BinaryParser(const BinaryParser &copy)=delete
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(const std::string &file)
static std::unique_ptr< Binary > parse(const std::string &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:48
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 which represents a library dependency.
Definition DylibCommand.hpp:34
Class that represents the LC_DYSYMTAB command.
Definition DynamicSymbolCommand.hpp:40
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
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
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:46
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
Class that represents the LoadCommand::TYPE::SEGMENT_SPLIT_INFO command.
Definition SegmentSplitInfo.hpp:35
Class that represents the LC_SYMTAB command.
Definition SymbolCommand.hpp:35
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
Class which represents the LC_TWOLEVEL_HINTS command.
Definition TwoLevelHints.hpp:39
Definition SpanStream.hpp:32
Definition endianness_support.hpp:59
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
MACHO_TYPES
Definition MachO/enums.hpp:24
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
This structure is used to tweak the MachO Parser (MachO::Parser)
Definition MachO/ParserConfig.hpp:24
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42