LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
MachO/Binary.hpp
Go to the documentation of this file.
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
25#include "LIEF/visibility.h"
26
28
29#include "LIEF/iterators.hpp"
30#include "LIEF/MachO/Header.hpp"
31#include "LIEF/errors.hpp"
32
33namespace LIEF {
34
35namespace objc {
36class Metadata;
37}
38
40namespace MachO {
41
42class BinaryParser;
43class BuildVersion;
44class Builder;
45class CodeSignature;
47class DataInCode;
49class DyldEnvironment;
50class DyldExportsTrie;
51class DyldInfo;
52class DylibCommand;
53class DylinkerCommand;
55class EncryptionInfo;
56class ExportInfo;
57class FunctionStarts;
58class Header;
59class LinkerOptHint;
60class MainCommand;
61class Parser;
62class RPathCommand;
63class Relocation;
64class Section;
65class SegmentCommand;
67class SourceVersion;
68class SubFramework;
69class Symbol;
70class SymbolCommand;
71class ThreadCommand;
72class TwoLevelHints;
73class UUIDCommand;
74class VersionMin;
75
78
79 friend class Parser;
80 friend class BinaryParser;
81 friend class Builder;
82 friend class DyldInfo;
83
84 public:
85 struct range_t {
86 uint64_t start = 0;
87 uint64_t end = 0;
88 };
89
91 using commands_t = std::vector<std::unique_ptr<LoadCommand>>;
92
95
98
100 using symbols_t = std::vector<std::unique_ptr<Symbol>>;
101
104
107
110
113
116
119
121 using sections_cache_t = std::vector<Section*>;
122
125
128
130 using segments_cache_t = std::vector<SegmentCommand*>;
131
134
137
139 using libraries_cache_t = std::vector<DylibCommand*>;
140
143
146
148 using fileset_binaries_t = std::vector<std::unique_ptr<Binary>>;
149
152
155
156 struct KeyCmp {
157 bool operator() (const Relocation* lhs, const Relocation* rhs) const;
158 };
159
163 using relocations_t = std::set<Relocation*, KeyCmp>;
164
167
170
173
176
177 public:
178 Binary(const Binary&) = delete;
179 Binary& operator=(const Binary&) = delete;
180
183 return header_;
184 }
185
186 const Header& header() const {
187 return header_;
188 }
189
193 return commands_;
194 }
195
197 return commands_;
198 }
199
203 return filesets_;
204 }
205
207 return filesets_;
208 }
209
212 return symbols_;
213 }
215 return symbols_;
216 }
217
219 bool has_symbol(const std::string& name) const {
220 return get_symbol(name) != nullptr;
221 }
222
225 const Symbol* get_symbol(const std::string& name) const;
226 Symbol* get_symbol(const std::string& name);
227
229 static bool is_exported(const Symbol& symbol);
230
233 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
234 return is_exported(*symbol); }
235 };
236 }
238 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
239 return is_exported(*symbol);
240 }};
241 }
242
244 static bool is_imported(const Symbol& symbol);
245
248 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
249 return is_imported(*symbol);
250 }};
251 }
252
254 return {symbols_, [] (const std::unique_ptr<Symbol>& symbol) {
255 return is_imported(*symbol);
256 }};
257 }
258
261 return libraries_;
262 }
263
265 return libraries_;
266 }
267
270 return segments_;
271 }
273 return segments_;
274 }
275
278 return sections_;
279 }
281 return sections_;
282 }
283
287
291 void write(const std::string& filename) override;
292
296 void write(std::ostream& os) override;
297
299 std::vector<uint8_t> raw();
300
302 bool has(LoadCommand::TYPE type) const;
303
308
310 LoadCommand* add(const LoadCommand& command);
311
313 LoadCommand* add(const LoadCommand& command, size_t index);
314
316 LoadCommand* add(const DylibCommand& library);
317
320
322 LoadCommand* add_library(const std::string& name);
323
325 Section* add_section(const Section& section);
326
331 Section* add_section(const SegmentCommand& segment, const Section& section);
332
337 void remove_section(const std::string& name, bool clear = false) override;
338
346 void remove_section(const std::string& segname, const std::string& secname, bool clear = false);
347
349 bool remove(const LoadCommand& command);
350
353
355 bool remove_command(size_t index);
356
359
361 bool extend(const LoadCommand& command, uint64_t size);
362
364 bool extend_segment(const SegmentCommand& segment, size_t size);
365
368
370 uint64_t imagebase() const override;
371
373 uint64_t virtual_size() const;
374
377 std::string loader() const;
378
380 bool has_section(const std::string& name) const {
381 return get_section(name) != nullptr;
382 }
383
386 Section* get_section(const std::string& name);
387
390 const Section* get_section(const std::string& name) const;
391
395 Section* get_section(const std::string& segname, const std::string& secname);
396
397 const Section* get_section(const std::string& segname, const std::string& secname) const;
398
400 bool has_segment(const std::string& name) const {
401 return get_segment(name) != nullptr;
402 }
403
405 const SegmentCommand* get_segment(const std::string& name) const;
406
408 SegmentCommand* get_segment(const std::string& name);
409
411 bool remove_symbol(const std::string& name);
412
414 bool remove(const Symbol& sym);
415
417 bool can_remove(const Symbol& sym) const;
418
420 bool can_remove_symbol(const std::string& name) const;
421
423 bool unexport(const std::string& name);
424
426 bool unexport(const Symbol& sym);
427
430 Section* section_from_offset(uint64_t offset);
431 const Section* section_from_offset(uint64_t offset) const;
432
435 Section* section_from_virtual_address(uint64_t virtual_address);
436 const Section* section_from_virtual_address(uint64_t virtual_address) const;
437
439 result<uint64_t> virtual_address_to_offset(uint64_t virtual_address) const;
440
445 result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const override;
446
451 const SegmentCommand* segment_from_offset(uint64_t offset) const;
452
454 size_t segment_index(const SegmentCommand& segment) const;
455
457 uint64_t fat_offset() const {
458 return fat_offset_;
459 }
460
464 const SegmentCommand* segment_from_virtual_address(uint64_t virtual_address) const;
465
468
471
474 bool is_valid_addr(uint64_t address) const;
475
477 void accept(LIEF::Visitor& visitor) const override;
478
479 std::ostream& print(std::ostream& os) const override;
480
487 void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
489
497 void patch_address(uint64_t address, uint64_t patch_value,
498 size_t size = sizeof(uint64_t),
500
503 uint64_t virtual_address, uint64_t size,
504 Binary::VA_TYPES addr_type = Binary::VA_TYPES::AUTO) const override;
505
507 uint64_t entrypoint() const override;
508
510 bool is_pie() const override {
511 return header().has(Header::FLAGS::PIE);
512 }
513
515 bool has_nx() const override {
516 return !has_nx_stack();
517 }
518
520 bool has_nx_stack() const {
521 return !header().has(Header::FLAGS::ALLOW_STACK_EXECUTION);
522 }
523
525 bool has_nx_heap() const {
526 return !header().has(Header::FLAGS::NO_HEAP_EXECUTION);
527 }
528
532 bool has_entrypoint() const {
533 return has_main_command() || has_thread_command();
534 }
535
537 bool has_uuid() const;
538
541 const UUIDCommand* uuid() const;
542
544 bool has_main_command() const;
545
548 const MainCommand* main_command() const;
549
551 bool has_dylinker() const;
552
555 const DylinkerCommand* dylinker() const;
556
558 bool has_dyld_info() const;
559
562 const DyldInfo* dyld_info() const;
563
566
570
572 bool has_source_version() const;
573
577
579 bool has_version_min() const;
580
583 const VersionMin* version_min() const;
584
586 bool has_thread_command() const;
587
591
593 bool has_rpath() const;
594
597 const RPathCommand* rpath() const;
598
602
604 bool has_symbol_command() const;
605
609
612
616
618 bool has_code_signature() const;
619
623
626
630
632 bool has_data_in_code() const;
633
636 const DataInCode* data_in_code() const;
637
640
644
646 bool has_sub_framework() const;
647
650
654
658
661
665
667 bool has_build_version() const;
668
672
675
679
682
686
689
693
696
700
702 ExportInfo* add_exported_function(uint64_t address, const std::string& name);
703
705 Symbol* add_local_symbol(uint64_t address, const std::string& name);
706
708 std::unique_ptr<objc::Metadata> objc_metadata() const;
709
710 template<class T>
712
713 template<class T>
715
716 template<class T>
717 LIEF_LOCAL const T* command() const;
718
719 template<class T>
721
722 template<class CMD, class Func>
724
726 return get(type);
727 }
729 return get(type);
730 }
731
734
737
740
742 bool has_filesets() const;
743
745 const std::string& fileset_name() const {
746 return fileset_name_;
747 }
748
749 ~Binary() override;
750
753 ok_error_t shift(size_t value);
754
757
762 uint64_t memory_base_address() const {
763 return in_memory_base_addr_;
764 }
765
766 // Check if the binary is supporting ARM64 pointer authentication (arm64e)
768 static constexpr auto CPU_SUBTYPE_ARM64E = 2;
769 return header().cpu_type() == Header::CPU_TYPE::ARM64 &&
770 (header().cpu_subtype() & ~Header::CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM64E;
771 }
772
773 uint32_t page_size() const;
774
775 static bool classof(const LIEF::Binary* bin) {
776 return bin->format() == Binary::FORMATS::MACHO;
777 }
778
780 return overlay_;
781 }
782
783 private:
785 Binary();
786
787 void shift_command(size_t width, uint64_t from_offset);
788
791 size_t add_cached_segment(SegmentCommand& segment);
792 void refresh_seg_offset();
793
794 template<class T>
795 LIEF_LOCAL ok_error_t patch_relocation(Relocation& relocation, uint64_t from, uint64_t shift);
796
797 LIEF::Header get_abstract_header() const override;
798 LIEF::Binary::sections_t get_abstract_sections() override;
799 LIEF::Binary::symbols_t get_abstract_symbols() override;
800 LIEF::Binary::relocations_t get_abstract_relocations() override;
801 LIEF::Binary::functions_t get_abstract_exported_functions() const override;
802 LIEF::Binary::functions_t get_abstract_imported_functions() const override;
803 std::vector<std::string> get_abstract_imported_libraries() const override;
804
805 relocations_t& relocations_list() {
806 return this->relocations_;
807 }
808
809 const relocations_t& relocations_list() const {
810 return this->relocations_;
811 }
812
813 size_t pointer_size() const {
814 return this->is64_ ? sizeof(uint64_t) : sizeof(uint32_t);
815 }
816
817 bool is64_ = true;
818 Header header_;
819 commands_t commands_;
820 symbols_t symbols_;
821
822 // Same purpose as sections_cache_t
823 libraries_cache_t libraries_;
824
825 // The sections are owned by the SegmentCommand object.
826 // This attribute is a cache to speed-up the iteration
827 sections_cache_t sections_;
828
829 // Same purpose as sections_cache_t
830 segments_cache_t segments_;
831
832 fileset_binaries_t filesets_;
833
834 // Cached relocations from segment / sections
835 mutable relocations_t relocations_;
836 int32_t available_command_space_ = 0;
837
838 // This is used to improve performances of
839 // offset_to_virtual_address
840 std::map<uint64_t, SegmentCommand*> offset_seg_;
841
842 protected:
843 uint64_t fat_offset_ = 0;
844 uint64_t fileset_offset_ = 0;
845 uint64_t in_memory_base_addr_ = 0;
846 std::string fileset_name_;
847 std::vector<uint8_t> overlay_;
848};
849
850} // namespace MachO
851} // namespace LIEF
852#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:41
std::vector< Function > functions_t
Definition Abstract/Binary.hpp:59
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:71
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:45
@ AUTO
Try to guess if it's relative or not.
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:62
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:80
Definition Abstract/Header.hpp:29
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
Class which represents a MachO binary.
Definition MachO/Binary.hpp:77
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.
size_t count_commands() const
static bool classof(const LIEF::Binary *bin)
Definition MachO/Binary.hpp:775
bool is_pie() const override
Check if the binary is position independent.
Definition MachO/Binary.hpp:510
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:247
const ThreadCommand * thread_command() const
Section * section_from_offset(uint64_t offset)
Return the MachO::Section that encompasses the provided offset. If a section can't be found,...
const SegmentSplitInfo * segment_split_info() const
const Section * section_from_offset(uint64_t offset) const
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.
const UUIDCommand * uuid() const
it_commands commands()
Return an iterator over the MachO LoadCommand present in the binary.
Definition MachO/Binary.hpp:192
range_t off_ranges() const
Return the range of offsets.
const DataInCode * data_in_code() const
result< uint64_t > offset_to_virtual_address(uint64_t offset, uint64_t slide=0) const override
Convert the given offset into a virtual address.
const SegmentCommand * segment_from_virtual_address(uint64_t virtual_address) const
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.
const DyldInfo * dyld_info() const
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...
const VersionMin * version_min() const
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.
const TwoLevelHints * two_level_hints() const
std::vector< DylibCommand * > libraries_cache_t
Internal container for storing Mach-O DylibCommand.
Definition MachO/Binary.hpp:139
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:219
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.
const LoadCommand * operator[](LoadCommand::TYPE type) const
Definition MachO/Binary.hpp:728
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.
it_const_fileset_binaries filesets() const
Definition MachO/Binary.hpp:206
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:130
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:515
bool can_remove(const Symbol &sym) const
Check if the given symbol can be safely removed.
const SourceVersion * source_version() const
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.
std::ostream & print(std::ostream &os) const override
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:400
bool has_uuid() const
true if the binary has a MachO::UUIDCommand command.
const Section * section_from_virtual_address(uint64_t virtual_address) const
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.
const DyldEnvironment * dyld_environment() const
it_const_sections sections() const
Definition MachO/Binary.hpp:280
it_const_symbols symbols() const
Definition MachO/Binary.hpp:214
std::vector< std::unique_ptr< LoadCommand > > commands_t
Internal container for storing Mach-O LoadCommand.
Definition MachO/Binary.hpp:91
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:211
LoadCommand * operator[](LoadCommand::TYPE type)
Definition MachO/Binary.hpp:725
bool has_linker_opt_hint() const
true if the binary has the command LC_LINKER_OPTIMIZATION_HINT.
const DyldChainedFixups * dyld_chained_fixups() const
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:148
LIEF::Binary::functions_t ctor_functions() const override
Return the list of the MachO's constructors.
std::unique_ptr< objc::Metadata > objc_metadata() const
Return Objective-C metadata if present.
const FunctionStarts * function_starts() const
it_segments segments()
Return an iterator over the SegmentCommand.
Definition MachO/Binary.hpp:269
Binary(const Binary &)=delete
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:202
Binary & operator=(const Binary &)=delete
it_const_segments segments() const
Definition MachO/Binary.hpp:272
bool has_nx_stack() const
Return True if the heap is flagged as non-executable. False otherwise.
Definition MachO/Binary.hpp:520
it_libraries libraries()
Return binary imported libraries (MachO::DylibCommand)
Definition MachO/Binary.hpp:260
bool has_data_in_code() const
true if the binary has a MachO::DataInCode command.
const SubFramework * sub_framework() const
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.
const SegmentCommand * segment_from_offset(uint64_t offset) const
it_exported_symbols exported_symbols()
Return binary's exported symbols (iterator over LIEF::MachO::Symbol)
Definition MachO/Binary.hpp:232
const MainCommand * main_command() const
it_const_relocations relocations() const
span< const uint8_t > overlay() const
Definition MachO/Binary.hpp:779
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.
const RPathCommand * rpath() const
const CodeSignature * code_signature() const
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.
const CodeSignatureDir * code_signature_dir() const
bool extend_segment(const SegmentCommand &segment, size_t size)
Extend the content of the given SegmentCommand.
const DynamicSymbolCommand * dynamic_symbol_command() const
const T * command() const
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:457
const Header & header() const
Definition MachO/Binary.hpp:186
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:525
uint32_t page_size() const
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 has_command() const
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:100
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:762
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 ...
const LinkerOptHint * linker_opt_hint() const
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.
LoadCommand * get(LoadCommand::TYPE type)
bool has_entrypoint() const
true if the binary has an entrypoint.
Definition MachO/Binary.hpp:532
bool has_two_level_hints() const
true if the binary has the command LC_TWO_LEVEL_HINTS.
it_const_rpaths rpaths() const
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.
const EncryptionInfo * encryption_info() const
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.
const DyldExportsTrie * dyld_exports_trie() const
uint64_t virtual_size() const
Size of the binary in memory when mapped by the loader (dyld)
const SymbolCommand * symbol_command() const
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:163
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:121
const Section * get_section(const std::string &segname, const std::string &secname) const
bool has_section(const std::string &name) const
Check if a section with the given name exists.
Definition MachO/Binary.hpp:380
it_const_imported_symbols imported_symbols() const
Definition MachO/Binary.hpp:253
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:182
ExportInfo * add_exported_function(uint64_t address, const std::string &name)
Add a symbol in the export trie of the current binary.
bool support_arm64_ptr_auth() const
Definition MachO/Binary.hpp:767
it_const_commands commands() const
Definition MachO/Binary.hpp:196
bool has_dyld_environment() const
true if the binary has Dyld envrionment variables.
it_const_libraries libraries() const
Definition MachO/Binary.hpp:264
const std::string & fileset_name() const
Name associated with the LC_FILESET_ENTRY binary.
Definition MachO/Binary.hpp:745
Binary & for_commands(Func f)
bool has_filesets() const
true if the binary has a LoadCommand::TYPE::FILESET_ENTRY command
Symbol * get_symbol(const std::string &name)
SegmentCommand * segment_from_offset(uint64_t offset)
Return the binary's SegmentCommand that encompasses the provided offset.
const DylinkerCommand * dylinker() const
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.
it_const_exported_symbols exported_symbols() const
Definition MachO/Binary.hpp:237
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.
const BuildVersion * build_version() const
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:277
Definition BuildVersion.hpp:34
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:55
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 command which is used by the Mach-O linker/loader to init...
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:35
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
TYPE
Definition LoadCommand.hpp:42
Class that represent the LC_MAIN command. This kind of command can be used to determine the entrypoin...
Definition MainCommand.hpp:33
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:224
Iterator which return a ref on container's values given predicates.
Definition iterators.hpp:265
This class is the main interface to inspect Objective-C metadata.
Definition Metadata.hpp:35
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:108
tcb::span< ElementType, Extent > span
Definition span.hpp:22
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74
Definition MachO/Binary.hpp:156
Definition MachO/Binary.hpp:85
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42