16#ifndef LIEF_MACHO_BINARY_PARSER_H
17#define LIEF_MACHO_BINARY_PARSER_H
24#include <unordered_map>
63struct dyld_chained_starts_in_segment;
64struct dyld_chained_fixups_header;
65union dyld_chained_ptr_arm64e;
66union dyld_chained_ptr_generic64;
67union dyld_chained_ptr_generic32;
77 friend class MachO::Parser;
78 constexpr static size_t MAX_RELOCATIONS = (std::numeric_limits<uint16_t>::max)();
81 constexpr static size_t MAX_COMMANDS = (std::numeric_limits<uint16_t>::max)();
86 static std::unique_ptr<Binary>
parse(
const std::string& file);
87 static std::unique_ptr<Binary>
parse(
const std::string& file,
const ParserConfig& conf);
88 static std::unique_ptr<Binary>
parse(
const std::vector<uint8_t>& data,
89 const ParserConfig& conf = ParserConfig::deep());
91 static std::unique_ptr<Binary>
parse(
const std::vector<uint8_t>& data, uint64_t fat_offset,
92 const ParserConfig& conf = ParserConfig::deep());
94 static std::unique_ptr<Binary>
parse(std::unique_ptr<BinaryStream> stream, uint64_t fat_offset,
95 const ParserConfig& conf);
97 BinaryParser&
operator=(
const BinaryParser& copy) =
delete;
103 using exports_list_t = std::vector<std::unique_ptr<ExportInfo>>;
108 template<
class MACHO_T>
111 template<
class MACHO_T>
114 template<
class MACHO_T>
117 template<
class MACHO_T>
125 template<
class MACHO_T>
130 template<
class MACHO_T>
133 template<
class MACHO_T>
136 template<
class MACHO_T>
139 template<
class MACHO_T>
142 template<
class MACHO_T>
145 template<
class MACHO_T>
147 SpanStream& string_s);
150 DynamicSymbolCommand& cmd, std::vector<Symbol*>& symtab,
151 BinaryStream& indirect_stream);
154 template<
class MACHO_T>
157 using it_opaque_segments =
void*;
159 template<
class MACHO_T>
161 uint64_t segment_offset,
const std::string& symbol_name,
162 int32_t ord, int64_t addend,
bool is_weak,
163 bool is_non_weak_definition, it_opaque_segments segments_ptr, uint64_t offset = 0);
166 template<
class MACHO_T>
168 uint8_t type, uint8_t segment_idx, uint64_t segment_offset,
169 it_opaque_segments segments);
175 template<
class MACHO_T>
178 template<
class MACHO_T>
180 const details::dyld_chained_fixups_header& header, SpanStream& stream,
181 SpanStream& symbol_pool);
183 template<
class MACHO_T>
185 const details::dyld_chained_fixups_header& header, SpanStream& stream);
187 template<
class MACHO_T>
189 SpanStream& stream, uint32_t seg_info_offset, uint64_t offset,
192 template<
class MACHO_T>
194 DYLD_CHAINED_FORMAT fmt, int32_t ord,
const std::string& symbol_name,
195 int64_t addend,
bool is_weak);
197 template<
class MACHO_T>
199 SegmentCommand& segment, uint64_t chain_address, uint64_t chain_offset,
200 const details::dyld_chained_starts_in_segment& seg_info);
202 template<
class MACHO_T>
204 uint64_t& chain_address, uint64_t chain_offset,
205 const details::dyld_chained_starts_in_segment& seg_info);
207 template<
class MACHO_T>
209 SegmentCommand& segment, uint64_t chain_address, uint64_t chain_offset,
210 const details::dyld_chained_starts_in_segment& seg_info);
213 SegmentCommand& segment, uint64_t chain_address, uint32_t chain_offset,
214 const details::dyld_chained_starts_in_segment& seg_info,
215 const details::dyld_chained_ptr_arm64e& fixup);
218 SegmentCommand& segment, uint64_t chain_address, uint32_t chain_offset,
219 const details::dyld_chained_starts_in_segment& seg_info,
220 const details::dyld_chained_ptr_generic64& fixup);
223 SegmentCommand& segment, uint64_t chain_address, uint32_t chain_offset,
224 const details::dyld_chained_starts_in_segment& seg_info,
225 const details::dyld_chained_ptr_generic32 & fixup);
227 template<
class MACHO_T>
230 template<
class MACHO_T>
233 template<
class MACHO_T>
236 template<
class MACHO_T>
239 template<
class MACHO_T>
242 template<
class MACHO_T>
245 template<
class MACHO_T>
248 template<
class MACHO_T>
251 template<
class MACHO_T>
254 template<
class MACHO_T>
265 exports_list_t& exports, BinaryStream& stream,
266 uint64_t start,
const std::string& prefix,
bool* invalid_names);
268 LIEF_LOCAL void copy_from(ChainedBindingInfo& to, ChainedBindingInfo& from);
270 std::unique_ptr<BinaryStream> stream_;
271 std::unique_ptr<Binary> binary_;
274 ParserConfig config_;
275 std::set<uint64_t> visited_;
276 std::unordered_map<std::string, Symbol*> memoized_symbols_;
277 std::map<uint64_t, Symbol*> memoized_symbols_by_address_;
279 std::vector<DylibCommand*> binding_libs_;
280 std::set<uint64_t> dyld_reloc_addrs_;
283 DyldChainedFixups* chained_fixups_ =
nullptr;