16#ifndef LIEF_MACHO_BINARY_PARSER_H
17#define LIEF_MACHO_BINARY_PARSER_H
24#include <unordered_map>
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;
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)();
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());
90 static std::unique_ptr<Binary>
parse(
const std::vector<uint8_t>& data, uint64_t fat_offset,
91 const ParserConfig& conf = ParserConfig::deep());
93 static std::unique_ptr<Binary>
parse(std::unique_ptr<BinaryStream> stream, uint64_t fat_offset,
94 const ParserConfig& conf);
96 BinaryParser&
operator=(
const BinaryParser& copy) =
delete;
102 using exports_list_t = std::vector<std::unique_ptr<ExportInfo>>;
107 template<
class MACHO_T>
110 template<
class MACHO_T>
113 template<
class MACHO_T>
116 template<
class MACHO_T>
124 template<
class MACHO_T>
129 template<
class MACHO_T>
132 template<
class MACHO_T>
135 template<
class MACHO_T>
138 template<
class MACHO_T>
141 template<
class MACHO_T>
144 template<
class MACHO_T>
146 SpanStream& string_s);
149 DynamicSymbolCommand& cmd, std::vector<Symbol*>& symtab,
150 BinaryStream& indirect_stream);
153 template<
class MACHO_T>
156 using it_opaque_segments =
void*;
158 template<
class MACHO_T>
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);
165 template<
class MACHO_T>
167 uint8_t type, uint8_t segment_idx, uint64_t segment_offset,
168 it_opaque_segments segments);
174 template<
class MACHO_T>
177 template<
class MACHO_T>
179 const details::dyld_chained_fixups_header& header, SpanStream& stream,
180 SpanStream& symbol_pool);
182 template<
class MACHO_T>
184 const details::dyld_chained_fixups_header& header, SpanStream& stream);
186 template<
class MACHO_T>
188 SpanStream& stream, uint32_t seg_info_offset, uint64_t offset,
191 template<
class MACHO_T>
193 DYLD_CHAINED_FORMAT fmt, int32_t ord,
const std::string& symbol_name,
194 int64_t addend,
bool is_weak);
196 template<
class MACHO_T>
198 SegmentCommand& segment, uint64_t chain_address, uint64_t chain_offset,
199 const details::dyld_chained_starts_in_segment& seg_info);
201 template<
class MACHO_T>
203 uint64_t& chain_address, uint64_t chain_offset,
204 const details::dyld_chained_starts_in_segment& seg_info);
206 template<
class MACHO_T>
208 SegmentCommand& segment, uint64_t chain_address, uint64_t chain_offset,
209 const details::dyld_chained_starts_in_segment& seg_info);
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);
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);
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);
226 template<
class MACHO_T>
229 template<
class MACHO_T>
232 template<
class MACHO_T>
235 template<
class MACHO_T>
238 template<
class MACHO_T>
241 template<
class MACHO_T>
244 template<
class MACHO_T>
247 template<
class MACHO_T>
250 template<
class MACHO_T>
261 exports_list_t& exports, BinaryStream& stream,
262 uint64_t start,
const std::string& prefix,
bool* invalid_names);
264 LIEF_LOCAL void copy_from(ChainedBindingInfo& to, ChainedBindingInfo& from);
266 std::unique_ptr<BinaryStream> stream_;
267 std::unique_ptr<Binary> binary_;
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_;
275 std::vector<DylibCommand*> binding_libs_;
276 std::set<uint64_t> dyld_reloc_addrs_;
279 DyldChainedFixups* chained_fixups_ =
nullptr;