LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
MachO/Binary.hpp
1
2/* Copyright 2017 - 2024 R. Thomas
3 * Copyright 2017 - 2024 Quarkslab
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef LIEF_MACHO_BINARY_H
18#define LIEF_MACHO_BINARY_H
19
20#include <vector>
21#include <map>
22#include <memory>
23
24#include "LIEF/MachO/LoadCommand.hpp"
25#include "LIEF/visibility.h"
26
27#include "LIEF/Abstract/Binary.hpp"
28
29#include "LIEF/iterators.hpp"
30#include "LIEF/MachO/Header.hpp"
31#include "LIEF/errors.hpp"
32
33namespace LIEF {
34
36namespace MachO {
37
38class BinaryParser;
39class BuildVersion;
40class Builder;
41class CodeSignature;
43class DataInCode;
45class DyldEnvironment;
46class DyldExportsTrie;
47class DyldInfo;
48class DylibCommand;
49class DylinkerCommand;
51class EncryptionInfo;
52class ExportInfo;
53class FunctionStarts;
54class Header;
55class LinkerOptHint;
56class MainCommand;
57class Parser;
58class RPathCommand;
59class Relocation;
60class Section;
61class SegmentCommand;
63class SourceVersion;
64class SubFramework;
65class Symbol;
66class SymbolCommand;
67class ThreadCommand;
68class TwoLevelHints;
69class UUIDCommand;
70class VersionMin;
71
73class LIEF_API Binary : public LIEF::Binary {
74
75 friend class Parser;
76 friend class BinaryParser;
77 friend class Builder;
78 friend class DyldInfo;
79
80 public:
81 struct range_t {
82 uint64_t start = 0;
83 uint64_t end = 0;
84 };
85
87 using commands_t = std::vector<std::unique_ptr<LoadCommand>>;
88
91
94
96 using symbols_t = std::vector<std::unique_ptr<Symbol>>;
97
100
103
106
109
112
115
117 using sections_cache_t = std::vector<Section*>;
118
121
124
126 using segments_cache_t = std::vector<SegmentCommand*>;
127
130
133
135 using libraries_cache_t = std::vector<DylibCommand*>;
136
139
142
144 using fileset_binaries_t = std::vector<std::unique_ptr<Binary>>;
145
148
151
152 struct KeyCmp {
153 bool operator() (const Relocation* lhs, const Relocation* rhs) const;
154 };
155
159 using relocations_t = std::set<Relocation*, KeyCmp>;
160
163
166
169
172
173 public:
174 Binary(const Binary&) = delete;
175 Binary& operator=(const Binary&) = delete;
176
179 return header_;
180 }
181
182 const Header& header() const {
183 return header_;
184 }
185
189 return commands_;
190 }
191
192 it_const_commands commands() const {
193 return commands_;
194 }
195
199 return filesets_;
200 }
201
202 it_const_fileset_binaries filesets() const {
203 return filesets_;
204 }
205
208 return symbols_;
209 }
210 it_const_symbols symbols() const {
211 return symbols_;
212 }
213
215 bool has_symbol(const std::string& name) const {
216 return get_symbol(name) != nullptr;
217 }
218
221 const Symbol* get_symbol(const std::string& name) const;
222 Symbol* get_symbol(const std::string& name);
223
225 static bool is_exported(const Symbol& symbol);
226
229 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
230 return is_exported(*symbol); }
231 };
232 }
233 it_const_exported_symbols exported_symbols() const {
234 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
235 return is_exported(*symbol);
236 }};
237 }
238
240 static bool is_imported(const Symbol& symbol);
241
244 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
245 return is_imported(*symbol);
246 }};
247 }
248
249 it_const_imported_symbols imported_symbols() const {
250 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
251 return is_imported(*symbol);
252 }};
253 }
254
257 return libraries_;
258 }
259
260 it_const_libraries libraries() const {
261 return libraries_;
262 }
263
266 return segments_;
267 }
268 it_const_segments segments() const {
269 return segments_;
270 }
271
274 return sections_;
275 }
276 it_const_sections sections() const {
277 return sections_;
278 }
279
282 it_const_relocations relocations() const;
283
287 void write(const std::string& filename) override;
288
292 void write(std::ostream& os) override;
293
295 std::vector<uint8_t> raw();
296
298 bool has(LoadCommand::TYPE type) const;
299
302 const LoadCommand* get(LoadCommand::TYPE type) const;
303 LoadCommand* get(LoadCommand::TYPE type);
304
306 LoadCommand* add(const LoadCommand& command);
307
309 LoadCommand* add(const LoadCommand& command, size_t index);
310
312 LoadCommand* add(const DylibCommand& library);
313
316
318 LoadCommand* add_library(const std::string& name);
319
321 Section* add_section(const Section& section);
322
327 Section* add_section(const SegmentCommand& segment, const Section& section);
328
333 void remove_section(const std::string& name, bool clear = false) override;
334
342 void remove_section(const std::string& segname, const std::string& secname, bool clear = false);
343
345 bool remove(const LoadCommand& command);
346
348 bool remove(LoadCommand::TYPE type);
349
351 bool remove_command(size_t index);
352
355
357 bool extend(const LoadCommand& command, uint64_t size);
358
360 bool extend_segment(const SegmentCommand& segment, size_t size);
361
364
366 uint64_t imagebase() const override;
367
369 uint64_t virtual_size() const;
370
373 std::string loader() const;
374
376 bool has_section(const std::string& name) const {
377 return get_section(name) != nullptr;
378 }
379
382 Section* get_section(const std::string& name);
383
386 const Section* get_section(const std::string& name) const;
387
391 Section* get_section(const std::string& segname, const std::string& secname);
392
393 const Section* get_section(const std::string& segname, const std::string& secname) const;
394
396 bool has_segment(const std::string& name) const {
397 return get_segment(name) != nullptr;
398 }
399
401 const SegmentCommand* get_segment(const std::string& name) const;
402
404 SegmentCommand* get_segment(const std::string& name);
405
407 bool remove_symbol(const std::string& name);
408
410 bool remove(const Symbol& sym);
411
413 bool can_remove(const Symbol& sym) const;
414
416 bool can_remove_symbol(const std::string& name) const;
417
419 bool unexport(const std::string& name);
420
422 bool unexport(const Symbol& sym);
423
426 Section* section_from_offset(uint64_t offset);
427 const Section* section_from_offset(uint64_t offset) const;
428
431 Section* section_from_virtual_address(uint64_t virtual_address);
432 const Section* section_from_virtual_address(uint64_t virtual_address) const;
433
435 result<uint64_t> virtual_address_to_offset(uint64_t virtual_address) const;
436
441 result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const override;
442
447 const SegmentCommand* segment_from_offset(uint64_t offset) const;
448
450 size_t segment_index(const SegmentCommand& segment) const;
451
453 uint64_t fat_offset() const {
454 return fat_offset_;
455 }
456
460 const SegmentCommand* segment_from_virtual_address(uint64_t virtual_address) const;
461
464
467
470 bool is_valid_addr(uint64_t address) const;
471
473 void accept(LIEF::Visitor& visitor) const override;
474
475 std::ostream& print(std::ostream& os) const override;
476
483 void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
485
493 void patch_address(uint64_t address, uint64_t patch_value,
494 size_t size = sizeof(uint64_t),
496
499 uint64_t virtual_address, uint64_t size,
500 Binary::VA_TYPES addr_type = Binary::VA_TYPES::AUTO) const override;
501
503 uint64_t entrypoint() const override;
504
506 bool is_pie() const override {
507 return header().has(Header::FLAGS::PIE);
508 }
509
511 bool has_nx() const override {
512 return !has_nx_stack();
513 }
514
516 bool has_nx_stack() const {
517 return !header().has(Header::FLAGS::ALLOW_STACK_EXECUTION);
518 }
519
521 bool has_nx_heap() const {
522 return !header().has(Header::FLAGS::NO_HEAP_EXECUTION);
523 }
524
528 bool has_entrypoint() const {
529 return has_main_command() || has_thread_command();
530 }
531
533 bool has_uuid() const;
534
537 const UUIDCommand* uuid() const;
538
540 bool has_main_command() const;
541
544 const MainCommand* main_command() const;
545
547 bool has_dylinker() const;
548
551 const DylinkerCommand* dylinker() const;
552
554 bool has_dyld_info() const;
555
558 const DyldInfo* dyld_info() const;
559
562
565 const FunctionStarts* function_starts() const;
566
568 bool has_source_version() const;
569
572 const SourceVersion* source_version() const;
573
575 bool has_version_min() const;
576
579 const VersionMin* version_min() const;
580
582 bool has_thread_command() const;
583
586 const ThreadCommand* thread_command() const;
587
589 bool has_rpath() const;
590
593 const RPathCommand* rpath() const;
594
597 it_const_rpaths rpaths() const;
598
600 bool has_symbol_command() const;
601
604 const SymbolCommand* symbol_command() const;
605
608
611 const DynamicSymbolCommand* dynamic_symbol_command() const;
612
614 bool has_code_signature() const;
615
618 const CodeSignature* code_signature() const;
619
622
625 const CodeSignatureDir* code_signature_dir() const;
626
628 bool has_data_in_code() const;
629
632 const DataInCode* data_in_code() const;
633
636
639 const SegmentSplitInfo* segment_split_info() const;
640
642 bool has_sub_framework() const;
643
646
649 const EncryptionInfo* encryption_info() const;
650
653 const SubFramework* sub_framework() const;
654
657
660 const DyldEnvironment* dyld_environment() const;
661
663 bool has_build_version() const;
664
667 const BuildVersion* build_version() const;
668
671
674 const DyldChainedFixups* dyld_chained_fixups() const;
675
678
681 const DyldExportsTrie* dyld_exports_trie() const;
682
685
688 const TwoLevelHints* two_level_hints() const;
689
692
695 const LinkerOptHint* linker_opt_hint() const;
696
698 ExportInfo* add_exported_function(uint64_t address, const std::string& name);
699
701 Symbol* add_local_symbol(uint64_t address, const std::string& name);
702
703 template<class T>
704 LIEF_LOCAL bool has_command() const;
705
706 template<class T>
707 LIEF_LOCAL T* command();
708
709 template<class T>
710 LIEF_LOCAL const T* command() const;
711
712 template<class T>
713 LIEF_LOCAL size_t count_commands() const;
714
715 template<class CMD, class Func>
716 LIEF_LOCAL Binary& for_commands(Func f);
717
718 LoadCommand* operator[](LoadCommand::TYPE type) {
719 return get(type);
720 }
721 const LoadCommand* operator[](LoadCommand::TYPE type) const {
722 return get(type);
723 }
724
726 LIEF::Binary::functions_t ctor_functions() const override;
727
729 LIEF::Binary::functions_t functions() const;
730
732 LIEF::Binary::functions_t unwind_functions() const;
733
735 bool has_filesets() const;
736
738 const std::string& fileset_name() const {
739 return fileset_name_;
740 }
741
742 ~Binary() override;
743
746 ok_error_t shift(size_t value);
747
750
755 uint64_t memory_base_address() const {
756 return in_memory_base_addr_;
757 }
758
759 uint32_t page_size() const;
760
761 static bool classof(const LIEF::Binary* bin) {
762 return bin->format() == Binary::FORMATS::MACHO;
763 }
764
765 span<const uint8_t> overlay() const {
766 return overlay_;
767 }
768
769 private:
771 Binary();
772
773 void shift_command(size_t width, uint64_t from_offset);
774
777 size_t add_cached_segment(SegmentCommand& segment);
778 void refresh_seg_offset();
779
780 template<class T>
781 LIEF_LOCAL ok_error_t patch_relocation(Relocation& relocation, uint64_t from, uint64_t shift);
782
783 LIEF::Header get_abstract_header() const override;
784 LIEF::Binary::sections_t get_abstract_sections() override;
785 LIEF::Binary::symbols_t get_abstract_symbols() override;
786 LIEF::Binary::relocations_t get_abstract_relocations() override;
787 LIEF::Binary::functions_t get_abstract_exported_functions() const override;
788 LIEF::Binary::functions_t get_abstract_imported_functions() const override;
789 std::vector<std::string> get_abstract_imported_libraries() const override;
790
791 relocations_t& relocations_list() {
792 return this->relocations_;
793 }
794
795 const relocations_t& relocations_list() const {
796 return this->relocations_;
797 }
798
799 size_t pointer_size() const {
800 return this->is64_ ? sizeof(uint64_t) : sizeof(uint32_t);
801 }
802
803 bool is64_ = true;
804 Header header_;
805 commands_t commands_;
806 symbols_t symbols_;
807
808 // Same purpose as sections_cache_t
809 libraries_cache_t libraries_;
810
811 // The sections are owned by the SegmentCommand object.
812 // This attribute is a cache to speed-up the iteration
813 sections_cache_t sections_;
814
815 // Same purpose as sections_cache_t
816 segments_cache_t segments_;
817
818 fileset_binaries_t filesets_;
819
820 // Cached relocations from segment / sections
821 mutable relocations_t relocations_;
822 int32_t available_command_space_ = 0;
823
824 // This is used to improve performances of
825 // offset_to_virtual_address
826 std::map<uint64_t, SegmentCommand*> offset_seg_;
827
828 protected:
829 uint64_t fat_offset_ = 0;
830 uint64_t fileset_offset_ = 0;
831 uint64_t in_memory_base_addr_ = 0;
832 std::string fileset_name_;
833 std::vector<uint8_t> overlay_;
834};
835
836} // namespace MachO
837} // namespace LIEF
838#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:39
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:98
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:69
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:43
@ AUTO
Try to guess if it's relative or not.
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:60
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:78
Definition Abstract/Header.hpp:29
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
Class which represents a MachO binary.
Definition MachO/Binary.hpp:73
Symbol * add_local_symbol(uint64_t address, const std::string &name)
Add a symbol in LC_SYMTAB command of the current binary.
UUIDCommand * uuid()
Return the MachO::UUIDCommand if present, a nullptr otherwise.
bool has_build_version() const
true if the binary has the BuildVersion command.
std::vector< uint8_t > raw()
Reconstruct the binary object and return its content as bytes.
bool is_pie() const override
Check if the binary is position independent.
Definition MachO/Binary.hpp:506
bool has_function_starts() const
true if the binary has a MachO::FunctionStarts command.
uint64_t entrypoint() const override
The binary entrypoint.
bool has_segment_split_info() const
true if the binary has segment split info.
it_imported_symbols imported_symbols()
Return binary's imported symbols (iterator over LIEF::MachO::Symbol)
Definition MachO/Binary.hpp:243
Section * section_from_offset(uint64_t offset)
Return the MachO::Section that encompasses the provided offset. If a section can't be found,...
bool has_source_version() const
true if the binary has a MachO::SourceVersion command.
const SegmentCommand * get_segment(const std::string &name) const
Return the segment from the given name.
it_commands commands()
Return an iterator over the MachO LoadCommand present in the binary.
Definition MachO/Binary.hpp:188
range_t off_ranges() const
Return the range of offsets.
result< uint64_t > offset_to_virtual_address(uint64_t offset, uint64_t slide=0) const override
Convert the given offset into a virtual address.
result< uint64_t > virtual_address_to_offset(uint64_t virtual_address) const
Convert a virtual address to an offset in the file.
bool has_dynamic_symbol_command() const
true if the binary has a MachO::DynamicSymbolCommand command.
TwoLevelHints * two_level_hints()
Return the MachO::DyldChainedFixups if present, a nullptr otherwise.
bool is_valid_addr(uint64_t address) const
Check if the given address is encompassed in the binary's virtual addresses range.
LoadCommand * add(const DylibCommand &library)
Insert the given DylibCommand.
DylinkerCommand * dylinker()
Return the MachO::DylinkerCommand if present, a nullptr otherwise.
bool has_dylinker() const
true if the binary has a MachO::DylinkerCommand.
Section * get_section(const std::string &segname, const std::string &secname)
Return the section from the segment with the name given in the first parameter and with the section's...
bool has_dyld_chained_fixups() const
true if the binary has the command LC_DYLD_CHAINED_FIXUPS.
void write(const std::string &filename) override
Reconstruct the binary object and write the result in the given filename
bool has_dyld_info() const
true if the binary has a MachO::DyldInfo command.
EncryptionInfo * encryption_info()
Return the MachO::DyldEnvironment if present, a nullptr otherwise.
DataInCode * data_in_code()
Return the MachO::DataInCode if present, a nullptr otherwise.
std::vector< DylibCommand * > libraries_cache_t
Internal container for storing Mach-O DylibCommand.
Definition MachO/Binary.hpp:135
std::string loader() const
Return the binary's loader (e.g. /usr/lib/dyld) or an empty string if the binary does not use a loade...
bool has_symbol(const std::string &name) const
Check if a symbol with the given name exists.
Definition MachO/Binary.hpp:215
BuildVersion * build_version()
Return the MachO::BuildVersion if present, a nullptr otherwise.
bool has_symbol_command() const
true if the binary has a MachO::SymbolCommand command.
size_t segment_index(const SegmentCommand &segment) const
Return the index of the given SegmentCommand.
bool has_rpath() const
true if the binary has a MachO::RPathCommand command.
ok_error_t shift_linkedit(size_t width)
Shift the position on the __LINKEDIT data by width
std::vector< SegmentCommand * > segments_cache_t
Internal container for storing Mach-O SegmentCommand.
Definition MachO/Binary.hpp:126
it_relocations relocations()
Return an iterator over the MachO::Relocation.
bool extend(const LoadCommand &command, uint64_t size)
Extend the size of the given LoadCommand.
VersionMin * version_min()
Return the MachO::VersionMin command if present, a nullptr otherwise.
bool remove_signature()
Remove the LC_SIGNATURE command.
bool has_nx() const override
Check if the binary uses NX protection.
Definition MachO/Binary.hpp:511
bool can_remove(const Symbol &sym) const
Check if the given symbol can be safely removed.
void remove_section(const std::string &segname, const std::string &secname, bool clear=false)
Remove the section from the segment with the name given in the first parameter and with the section's...
uint64_t imagebase() const override
Return the binary's imagebase. 0 if not relevant.
bool disable_pie()
Remove the PIE flag.
bool can_remove_symbol(const std::string &name) const
Check if the MachO::Symbol with the given name can be safely removed.
ThreadCommand * thread_command()
Return the MachO::ThreadCommand command if present, a nullptr otherwise.
Section * section_from_virtual_address(uint64_t virtual_address)
Return the MachO::Section that encompasses the provided virtual address. If a section can't be found,...
bool has_segment(const std::string &name) const
Check if a segment with the given name exists.
Definition MachO/Binary.hpp:396
bool has_uuid() const
true if the binary has a MachO::UUIDCommand command.
bool remove_symbol(const std::string &name)
Remove the symbol with the given name.
DyldExportsTrie * dyld_exports_trie()
Return the MachO::DyldChainedFixups if present, a nullptr otherwise.
std::vector< std::unique_ptr< LoadCommand > > commands_t
Internal container for storing Mach-O LoadCommand.
Definition MachO/Binary.hpp:87
void accept(LIEF::Visitor &visitor) const override
Method so that the visitor can visit us.
bool has_dyld_exports_trie() const
true if the binary has the command LC_DYLD_CHAINED_FIXUPS.
bool remove(LoadCommand::TYPE type)
Remove all LoadCommand with the given type (MachO::LoadCommand::TYPE)
it_symbols symbols()
Return binary's symbols .
Definition MachO/Binary.hpp:207
bool has_linker_opt_hint() const
true if the binary has the command LC_LINKER_OPTIMIZATION_HINT.
ok_error_t shift(size_t value)
Shift the content located right after the Load commands table. This operation can be used to add a ne...
std::vector< std::unique_ptr< Binary > > fileset_binaries_t
Internal container for storing Mach-O Fileset Binary.
Definition MachO/Binary.hpp:144
LIEF::Binary::functions_t ctor_functions() const override
Return the list of the MachO's constructors.
it_segments segments()
Return an iterator over the SegmentCommand.
Definition MachO/Binary.hpp:265
Section * get_section(const std::string &name)
Return the section from the given name of a nullptr if the section can't be found.
it_fileset_binaries filesets()
Return an iterator over the MachO::Binary associated with the LoadCommand::TYPE::FILESET_ENTRY comman...
Definition MachO/Binary.hpp:198
bool has_nx_stack() const
Return True if the heap is flagged as non-executable. False otherwise.
Definition MachO/Binary.hpp:516
it_libraries libraries()
Return binary imported libraries (MachO::DylibCommand)
Definition MachO/Binary.hpp:256
bool has_data_in_code() const
true if the binary has a MachO::DataInCode command.
const Section * get_section(const std::string &name) const
Return the section from the given name or a nullptr if the section can't be found.
static bool is_exported(const Symbol &symbol)
Check if the given symbol is exported.
Section * add_section(const Section &section)
Add a new MachO::Section in the __TEXT segment.
it_exported_symbols exported_symbols()
Return binary's exported symbols (iterator over LIEF::MachO::Symbol)
Definition MachO/Binary.hpp:228
SegmentCommand * segment_from_virtual_address(uint64_t virtual_address)
Return the binary's SegmentCommand which encompasses the given virtual address or a nullptr if not fo...
DyldChainedFixups * dyld_chained_fixups()
Return the MachO::DyldChainedFixups if present, a nullptr otherwise.
RPathCommand * rpath()
Return the MachO::RPathCommand command if present, a nullptr otherwise.
SegmentCommand * get_segment(const std::string &name)
Return the segment from the given name.
span< const uint8_t > get_content_from_virtual_address(uint64_t virtual_address, uint64_t size, Binary::VA_TYPES addr_type=Binary::VA_TYPES::AUTO) const override
Return the content located at virtual address.
bool extend_segment(const SegmentCommand &segment, size_t size)
Extend the content of the given SegmentCommand.
SymbolCommand * symbol_command()
Return the MachO::SymbolCommand if present, a nullptr otherwise.
LinkerOptHint * linker_opt_hint()
Return the MachO::LinkerOptHint if present, a nullptr otherwise.
uint64_t fat_offset() const
Return binary's fat offset. 0 if not relevant.
Definition MachO/Binary.hpp:453
it_rpaths rpaths()
Iterator over all the MachO::RPathCommand commands.
bool has_nx_heap() const
Return True if the stack is flagged as non-executable. False otherwise.
Definition MachO/Binary.hpp:521
LoadCommand * add(const LoadCommand &command)
Insert a new LoadCommand.
bool remove(const Symbol &sym)
Remove the given symbol.
bool has_version_min() const
true if the binary has a MachO::VersionMin command.
DyldEnvironment * dyld_environment()
Return the MachO::DyldEnvironment if present, a nullptr otherwise.
bool remove(const LoadCommand &command)
Remove the given LoadCommand.
SourceVersion * source_version()
Return the MachO::SourceVersion command if present, a nullptr otherwise.
static bool is_imported(const Symbol &symbol)
Check if the given symbol is an imported one.
LoadCommand * add(const LoadCommand &command, size_t index)
Insert a new LoadCommand at the specified index
std::vector< std::unique_ptr< Symbol > > symbols_t
Internal container for storing Mach-O Symbol.
Definition MachO/Binary.hpp:96
void patch_address(uint64_t address, uint64_t patch_value, size_t size=sizeof(uint64_t), LIEF::Binary::VA_TYPES addr_type=LIEF::Binary::VA_TYPES::AUTO) override
Patch the address with the given value.
CodeSignature * code_signature()
Return the MachO::CodeSignature if present, a nullptr otherwise.
LoadCommand * add_library(const std::string &name)
Insert a new shared library through a LC_LOAD_DYLIB command.
Section * add_section(const SegmentCommand &segment, const Section &section)
Add a section in the given MachO::SegmentCommand.
void remove_section(const std::string &name, bool clear=false) override
Remove the section with the name provided in the first parameter.
const Symbol * get_symbol(const std::string &name) const
Return Symbol from the given name. If the symbol does not exists, it returns a null pointer.
bool remove_command(size_t index)
Remove the Load Command at the provided index
uint64_t memory_base_address() const
If this Mach-O binary has been parsed from memory, it returns the in-memory base address of this bina...
Definition MachO/Binary.hpp:755
bool has_sub_framework() const
true if the binary has a sub framework command.
LIEF::Binary::functions_t functions() const
Return all the functions found in this MachO.
const LoadCommand * get(LoadCommand::TYPE type) const
Return the LoadCommand associated with the given LoadCommand::TYPE or a nullptr if the command can't ...
FunctionStarts * function_starts()
Return the MachO::FunctionStarts command if present, a nullptr otherwise.
SegmentSplitInfo * segment_split_info()
Return the MachO::SegmentSplitInfo if present, a nullptr otherwise.
LoadCommand * add(const SegmentCommand &segment)
Add a new LC_SEGMENT command from the given SegmentCommand.
bool has_entrypoint() const
true if the binary has an entrypoint.
Definition MachO/Binary.hpp:528
bool has_two_level_hints() const
true if the binary has the command LC_TWO_LEVEL_HINTS.
DyldInfo * dyld_info()
Return the MachO::Dyld command if present, a nullptr otherwise.
void patch_address(uint64_t address, const std::vector< uint8_t > &patch_value, LIEF::Binary::VA_TYPES addr_type=LIEF::Binary::VA_TYPES::AUTO) override
Patch the content at virtual address address with patch_value.
MainCommand * main_command()
Return the MachO::MainCommand if present, a nullptr otherwise.
bool has_encryption_info() const
true if the binary has Encryption Info.
bool has(LoadCommand::TYPE type) const
Check if the current binary has the given MachO::LoadCommand::TYPE.
uint64_t virtual_size() const
Size of the binary in memory when mapped by the loader (dyld)
range_t va_ranges() const
Return the range of virtual addresses.
DynamicSymbolCommand * dynamic_symbol_command()
Return the MachO::SymbolCommand if present, a nullptr otherwise.
bool has_code_signature_dir() const
true if the binary is signed with the command DYLIB_CODE_SIGN_DRS
CodeSignatureDir * code_signature_dir()
Return the MachO::CodeSignatureDir if present, a nullptr otherwise.
std::set< Relocation *, KeyCmp > relocations_t
Internal container that store all the relocations found in a Mach-O. The relocations are actually own...
Definition MachO/Binary.hpp:159
bool has_code_signature() const
true if the binary is signed with LC_CODE_SIGNATURE command
std::vector< Section * > sections_cache_t
Internal container for caching Mach-O Section.
Definition MachO/Binary.hpp:117
bool has_section(const std::string &name) const
Check if a section with the given name exists.
Definition MachO/Binary.hpp:376
bool has_main_command() const
true if the binary has a MachO::MainCommand command.
Header & header()
Return a reference to the MachO::Header.
Definition MachO/Binary.hpp:178
ExportInfo * add_exported_function(uint64_t address, const std::string &name)
Add a symbol in the export trie of the current binary.
bool has_dyld_environment() const
true if the binary has Dyld envrionment variables.
const std::string & fileset_name() const
Name associated with the LC_FILESET_ENTRY binary.
Definition MachO/Binary.hpp:738
bool has_filesets() const
true if the binary has a LoadCommand::TYPE::FILESET_ENTRY command
SegmentCommand * segment_from_offset(uint64_t offset)
Return the binary's SegmentCommand that encompasses the provided offset.
bool unexport(const std::string &name)
Remove the given MachO::Symbol with the given name from the export table.
LIEF::Binary::functions_t unwind_functions() const
Return the functions found in the __unwind_info section.
bool has_thread_command() const
true if the binary has a MachO::ThreadCommand command.
void write(std::ostream &os) override
Reconstruct the binary object and write the result in the given os stream.
bool unexport(const Symbol &sym)
Remove the given symbol from the export table.
SubFramework * sub_framework()
Return the MachO::SubFramework if present, a nullptr otherwise.
it_sections sections()
Return an iterator over the MachO::Section.
Definition MachO/Binary.hpp:273
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 that represents the LC_ENCRYPTION_INFO / LC_ENCRYPTION_INFO_64 commands.
Definition EncryptionInfo.hpp:36
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 that represents the Mach-O header.
Definition MachO/Header.hpp:41
Class which represents the LC_LINKER_OPTIMIZATION_HINT command.
Definition LinkerOptHint.hpp:37
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:36
Class that represent the LC_MAIN command. This kind of command can be used to determine the entrypoin...
Definition MainCommand.hpp:34
The main interface to parse a Mach-O binary.
Definition MachO/Parser.hpp:42
Class that represents the LC_RPATH command.
Definition RPathCommand.hpp:36
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
Class that represents a Mach-O section.
Definition MachO/Section.hpp:44
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:48
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 a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
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 represents the UUID command.
Definition UUIDCommand.hpp:35
Class that wraps the LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, ... commands.
Definition VersionMin.hpp:33
Definition Visitor.hpp:219
Iterator which return a ref on container's values given predicates.
Definition iterators.hpp:265
Iterator which returns reference on container's values.
Definition iterators.hpp:48
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
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:72
Definition MachO/Binary.hpp:152
Definition MachO/Binary.hpp:81