LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
ELF/Binary.hpp
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_ELF_BINARY_H
17#define LIEF_ELF_BINARY_H
18
19#include <vector>
20#include <memory>
21
22#include "LIEF/visibility.h"
23#include "LIEF/errors.hpp"
24#include "LIEF/iterators.hpp"
25
26#include "LIEF/Abstract/Binary.hpp"
27
28#include "LIEF/ELF/Note.hpp"
29#include "LIEF/ELF/DynamicEntry.hpp"
30#include "LIEF/ELF/Header.hpp"
31#include "LIEF/ELF/Section.hpp"
32#include "LIEF/ELF/Segment.hpp"
33#include "LIEF/ELF/Builder.hpp"
34
35namespace LIEF {
37namespace ELF {
38namespace DataHandler {
39class Handler;
40}
41
42class ExeLayout;
43class GnuHash;
44class Layout;
45class ObjectFileLayout;
46class Parser;
47class Relocation;
48class Section;
49class Segment;
50class Symbol;
51class SymbolVersion;
55class SysvHash;
56struct sizing_info_t;
57
59class LIEF_API Binary : public LIEF::Binary {
60 friend class Parser;
61 friend class Builder;
62 friend class ExeLayout;
63 friend class Layout;
64 friend class ObjectFileLayout;
65
66 public:
67 using string_list_t = std::vector<std::string>;
68
70 using notes_t = std::vector<std::unique_ptr<Note>>;
71
74
77
79 using symbols_version_requirement_t = std::vector<std::unique_ptr<SymbolVersionRequirement>>;
80
83
86
88 using symbols_version_definition_t = std::vector<std::unique_ptr<SymbolVersionDefinition>>;
89
92
95
97 using segments_t = std::vector<std::unique_ptr<Segment>>;
98
101
104
106 using dynamic_entries_t = std::vector<std::unique_ptr<DynamicEntry>>;
107
110
113
115 using symbols_version_t = std::vector<std::unique_ptr<SymbolVersion>>;
116
119
122
124 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
125
128
131
134
137
140
143
146
149
151 using symbols_t = std::vector<std::unique_ptr<Symbol>>;
152
155
158
161
164
167
170
173
176
179
182
184 using sections_t = std::vector<std::unique_ptr<Section>>;
185
188
191
192 public:
225
226 public:
227 Binary& operator=(const Binary& ) = delete;
228 Binary(const Binary& copy) = delete;
229
232 return type_;
233 }
234
237 return header_;
238 }
239
240 const Header& header() const {
241 return header_;
242 }
243
246 uint64_t last_offset_section() const;
247
250 uint64_t last_offset_segment() const;
251
253 uint64_t next_virtual_address() const;
254
257 return sections_;
258 }
259
260 it_const_sections sections() const {
261 return sections_;
262 }
263
265 uint64_t entrypoint() const override {
266 return header_.entrypoint();
267 }
268
271 return segments_;
272 }
273
274 it_const_segments segments() const {
275 return segments_;
276 }
277
280 return dynamic_entries_;
281 }
282
283 it_const_dynamic_entries dynamic_entries() const {
284 return dynamic_entries_;
285 }
286
289
291 Note& add(const Note& note);
292
294 void remove(const DynamicEntry& entry);
295
298
304 void remove(const Section& section, bool clear = false);
305
307 void remove(const Note& note);
308
310 void remove(Note::TYPE type);
311
313 void remove(const Segment& seg);
314
318 return dynamic_symbols_;
319 }
320
321 it_const_dynamic_symbols dynamic_symbols() const {
322 return dynamic_symbols_;
323 }
324
327 it_const_exported_symbols exported_symbols() const;
328
331 it_const_imported_symbols imported_symbols() const;
332
335 return symtab_symbols_;
336 }
337
338 it_const_symtab_symbols symtab_symbols() const {
339 return symtab_symbols_;
340 }
341
344 return symbol_version_table_;
345 }
346 it_const_symbols_version symbols_version() const {
347 return symbol_version_table_;
348 }
349
352 return symbol_version_definition_;
353 }
354
355 it_const_symbols_version_definition symbols_version_definition() const {
356 return symbol_version_definition_;
357 }
358
361 return symbol_version_requirements_;
362 }
363
364 it_const_symbols_version_requirement symbols_version_requirement() const {
365 return symbol_version_requirements_;
366 }
367
370 it_const_dynamic_relocations dynamic_relocations() const;
371
378
384
392 Relocation* add_object_relocation(const Relocation& relocation, const Section& section);
393
396 it_const_pltgot_relocations pltgot_relocations() const;
397
400 it_const_object_relocations object_relocations() const;
401
404 return relocations_;
405 }
406
407 it_const_relocations relocations() const {
408 return relocations_;
409 }
410
413 const Relocation* get_relocation(uint64_t address) const;
414 Relocation* get_relocation(uint64_t address);
415
418 const Relocation* get_relocation(const Symbol& symbol) const;
419 Relocation* get_relocation(const Symbol& symbol);
420
423 const Relocation* get_relocation(const std::string& symbol_name) const;
424 Relocation* get_relocation(const std::string& symbol_name);
425
429 bool use_gnu_hash() const {
430 return gnu_hash_ != nullptr && has(DynamicEntry::TAG::GNU_HASH);
431 }
432
435 const GnuHash* gnu_hash() const {
436 return use_gnu_hash() ? gnu_hash_.get() : nullptr;
437 }
438
442 bool use_sysv_hash() const {
443 return sysv_hash_ != nullptr && has(DynamicEntry::TAG::HASH);
444 }
445
448 const SysvHash* sysv_hash() const {
449 return use_sysv_hash() ? sysv_hash_.get() : nullptr;
450 }
451
453 bool has_section(const std::string& name) const;
454
456 bool has_section_with_offset(uint64_t offset) const;
457
459 bool has_section_with_va(uint64_t va) const;
460
463 Section* get_section(const std::string& name);
464 const Section* get_section(const std::string& name) const;
465
469 return get_section(".text");
470 }
471
475
479
483
488 uint64_t imagebase() const override;
489
491 uint64_t virtual_size() const;
492
495 bool has_interpreter() const;
496
501 const std::string& interpreter() const;
502
504 void interpreter(const std::string& interpreter);
505
508 return symtab_dyn_symbols();
509 }
510
511 it_const_symbols symbols() const {
512 return symtab_dyn_symbols();
513 }
514
516 Symbol& export_symbol(const Symbol& symbol);
517
519 Symbol& export_symbol(const std::string& symbol_name, uint64_t value = 0);
520
522 bool has_dynamic_symbol(const std::string& name) const;
523
526 const Symbol* get_dynamic_symbol(const std::string& name) const;
527
528 Symbol* get_dynamic_symbol(const std::string& name);
529
531 bool has_symtab_symbol(const std::string& name) const {
532 return get_symtab_symbol(name) != nullptr;
533 }
534
537 const Symbol* get_symtab_symbol(const std::string& name) const;
538
539 Symbol* get_symtab_symbol(const std::string& name);
540
544 string_list_t strings(size_t min_size = 5) const;
545
551 void remove_symbol(const std::string& name);
552
554 void remove_symtab_symbol(const std::string& name);
555 void remove_symtab_symbol(Symbol* symbol);
556
558 void remove_dynamic_symbol(const std::string& name);
559
566
568 result<uint64_t> get_function_address(const std::string& func_name) const override;
569
571 //
574 result<uint64_t> get_function_address(const std::string& func_name, bool demangled) const;
575
585 Section* add(const Section& section, bool loaded = true);
586
587 Section* extend(const Section& section, uint64_t size);
588
591
593 Symbol& add_dynamic_symbol(const Symbol& symbol, const SymbolVersion* version = nullptr);
594
596 Symbol& add_exported_function(uint64_t address, const std::string& name = "");
597
599 DynamicEntryLibrary& add_library(const std::string& library_name);
600
602 void remove_library(const std::string& library_name);
603
606 DynamicEntryLibrary* get_library(const std::string& library_name);
607
610 const DynamicEntryLibrary* get_library(const std::string& library_name) const;
611
613 bool has_library(const std::string& name) const;
614
622 Segment* add(const Segment& segment, uint64_t base = 0);
623
627 Segment* replace(const Segment& new_segment, const Segment& original_segment, uint64_t base = 0);
628
629 Segment* extend(const Segment& segment, uint64_t size);
630
631
637 void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
639
640
647 void patch_address(uint64_t address, uint64_t patch_value,
648 size_t size = sizeof(uint64_t),
650
655 void patch_pltgot(const Symbol& symbol, uint64_t address);
656
657
662 void patch_pltgot(const std::string& symbol_name, uint64_t address);
663
665 void strip();
666
671 void remove_section(const std::string& name, bool clear = false) override;
672
679 void write(const std::string& filename) override;
680
688 void write(const std::string& filename, Builder::config_t config);
689
696 void write(std::ostream& os) override;
697
702 void write(std::ostream& os, Builder::config_t config);
703
705 std::vector<uint8_t> raw();
706
708 result<uint64_t> virtual_address_to_offset(uint64_t virtual_address) const;
709
714 result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const override;
715
720 bool is_pie() const override;
721
723 bool has_nx() const override;
724
727 int64_t dynsym_idx(const std::string& name) const;
728
729 int64_t dynsym_idx(const Symbol& sym) const;
730
732 int64_t symtab_idx(const std::string& name) const;
733
734 int64_t symtab_idx(const Symbol& sym) const;
735
741 const Section* section_from_offset(uint64_t offset, bool skip_nobits = true) const;
742 Section* section_from_offset(uint64_t offset, bool skip_nobits = true);
743
749 const Section* section_from_virtual_address(uint64_t address, bool skip_nobits = true) const;
750 Section* section_from_virtual_address(uint64_t address, bool skip_nobits = true);
751
754 const Segment* segment_from_virtual_address(uint64_t address) const;
755 Segment* segment_from_virtual_address(uint64_t address);
756
757
758 const Segment* segment_from_virtual_address(Segment::TYPE type, uint64_t address) const;
759 Segment* segment_from_virtual_address(Segment::TYPE type, uint64_t address);
760
763 const Segment* segment_from_offset(uint64_t offset) const;
764 Segment* segment_from_offset(uint64_t offset);
765
770 return const_cast<DynamicEntry*>(static_cast<const Binary*>(this)->get(tag));
771 }
772
775 const Segment* get(Segment::TYPE type) const;
776 Segment* get(Segment::TYPE type);
777
780 const Note* get(Note::TYPE type) const;
781 Note* get(Note::TYPE type);
782
785 const Section* get(Section::TYPE type) const;
786 Section* get(Section::TYPE type);
787
789 bool has(DynamicEntry::TAG tag) const {
790 return get(tag) != nullptr;
791 }
792
794 bool has(Segment::TYPE type) const {
795 return get(type) != nullptr;
796 }
797
799 bool has(Note::TYPE type) const {
800 return get(type) != nullptr;
801 }
802
804 bool has(Section::TYPE type) const {
805 return get(type) != nullptr;
806 }
807
809 span<const uint8_t> get_content_from_virtual_address(uint64_t virtual_address, uint64_t size,
810 Binary::VA_TYPES addr_type = Binary::VA_TYPES::AUTO) const override;
811
813 void accept(LIEF::Visitor& visitor) const override;
814
816 void permute_dynamic_symbols(const std::vector<size_t>& permutation);
817
819 LIEF::Binary::functions_t ctor_functions() const override;
820
822 LIEF::Binary::functions_t dtor_functions() const;
823
825 LIEF::Binary::functions_t functions() const;
826
828 bool has_notes() const;
829
834 return notes_;
835 }
836
837 it_notes notes() {
838 return notes_;
839 }
840
843 uint64_t eof_offset() const;
844
846 bool has_overlay() const {
847 return !overlay_.empty();
848 }
849
851 span<const uint8_t> overlay() const {
852 return overlay_;
853 }
854
856 void overlay(std::vector<uint8_t> overlay) {
857 overlay_ = std::move(overlay);
858 }
859
869
870 static bool classof(const LIEF::Binary* bin) {
871 return bin->format() == Binary::FORMATS::ELF ||
872 bin->format() == Binary::FORMATS::OAT;
873 }
874
875 size_t hash(const std::string& name);
876
877 ~Binary() override;
878
879 std::ostream& print(std::ostream& os) const override;
880
881 Binary& operator+=(const DynamicEntry& entry) {
882 add(entry);
883 return *this;
884 }
885 Binary& operator+=(const Section& section) {
886 add(section);
887 return *this;
888 }
889
890 Binary& operator+=(const Segment& segment) {
891 add(segment);
892 return *this;
893 }
894
895 Binary& operator+=(const Note& note) {
896 add(note);
897 return *this;
898 }
899
900 Binary& operator-=(const DynamicEntry& entry) {
901 remove(entry);
902 return *this;
903 }
904
905 Binary& operator-=(DynamicEntry::TAG tag) {
906 remove(tag);
907 return *this;
908 }
909
910 Binary& operator-=(const Note& note) {
911 remove(note);
912 return *this;
913 }
914
915 Binary& operator-=(Note::TYPE type) {
916 remove(type);
917 return *this;
918 }
919
920 Segment* operator[](Segment::TYPE type) {
921 return get(type);
922 }
923
924 const Segment* operator[](Segment::TYPE type) const {
925 return get(type);
926 }
927
928 DynamicEntry* operator[](DynamicEntry::TAG tag) {
929 return get(tag);
930 }
931
932 const DynamicEntry* operator[](DynamicEntry::TAG tag) const {
933 return get(tag);
934 }
935
936 Note* operator[](Note::TYPE type) {
937 return get(type);
938 }
939
940 const Note* operator[](Note::TYPE type) const {
941 return get(type);
942 }
943
944 Section* operator[](Section::TYPE type) {
945 return get(type);
946 }
947
948 const Section* operator[](Section::TYPE type) const {
949 return get(type);
950 }
951
952 protected:
953 struct phdr_relocation_info_t {
954 uint64_t new_offset = 0;
955 size_t nb_segments = 0;
956 void clear() {
957 new_offset = 0;
958 nb_segments = 0;
959 }
960 };
961 Binary();
962
964 LIEF::Binary::sections_t get_abstract_sections() override;
965
966 LIEF::Header get_abstract_header() const override;
967
968 LIEF::Binary::functions_t get_abstract_exported_functions() const override;
969 LIEF::Binary::functions_t get_abstract_imported_functions() const override;
970 std::vector<std::string> get_abstract_imported_libraries() const override;
971 LIEF::Binary::symbols_t get_abstract_symbols() override;
972 LIEF::Binary::relocations_t get_abstract_relocations() override;
973
974 template<ELF::ARCH ARCH>
975 void patch_relocations(uint64_t from, uint64_t shift);
976
977 template<class T>
978 void patch_addend(Relocation& relocatio, uint64_t from, uint64_t shift);
979
980 void shift_sections(uint64_t from, uint64_t shift);
981 void shift_segments(uint64_t from, uint64_t shift);
982 void shift_dynamic_entries(uint64_t from, uint64_t shift);
983 void shift_symbols(uint64_t from, uint64_t shift);
984 void shift_relocations(uint64_t from, uint64_t shift);
985
986 template<class ELF_T>
987 void fix_got_entries(uint64_t from, uint64_t shift);
988
989 LIEF::Binary::functions_t eh_frame_functions() const;
990 LIEF::Binary::functions_t armexid_functions() const;
991
992 template<Header::FILE_TYPE OBJECT_TYPE, bool note = false>
993 Segment* add_segment(const Segment& segment, uint64_t base);
994
995 uint64_t relocate_phdr_table_auto();
996 uint64_t relocate_phdr_table_pie();
997 uint64_t relocate_phdr_table_v1();
998 uint64_t relocate_phdr_table_v2();
999 uint64_t relocate_phdr_table_v3();
1000
1001 template<Segment::TYPE PT>
1002 Segment* extend_segment(const Segment& segment, uint64_t size);
1003
1004 template<bool LOADED>
1005 Section* add_section(const Section& section);
1006 std::vector<Symbol*> symtab_dyn_symbols() const;
1007
1008 std::string shstrtab_name() const;
1009 Section* add_frame_section(const Section& sec);
1010
1011 LIEF::Binary::functions_t tor_functions(DynamicEntry::TAG tag) const;
1012
1013 Header::CLASS type_ = Header::CLASS::NONE;
1014 Header header_;
1015 sections_t sections_;
1016 segments_t segments_;
1017 dynamic_entries_t dynamic_entries_;
1018 symbols_t dynamic_symbols_;
1019 symbols_t symtab_symbols_;
1020 relocations_t relocations_;
1021 symbols_version_t symbol_version_table_;
1022 symbols_version_requirement_t symbol_version_requirements_;
1023 symbols_version_definition_t symbol_version_definition_;
1024 notes_t notes_;
1025 std::unique_ptr<GnuHash> gnu_hash_;
1026 std::unique_ptr<SysvHash> sysv_hash_;
1027 std::unique_ptr<DataHandler::Handler> datahandler_;
1028 phdr_relocation_info_t phdr_reloc_info_;
1029
1030 std::string interpreter_;
1031 std::vector<uint8_t> overlay_;
1032 std::unique_ptr<sizing_info_t> sizing_info_;
1033};
1034
1035}
1036}
1037#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
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Segment * add(const Segment &segment, uint64_t base=0)
Add a new segment in the binary.
bool use_gnu_hash() const
true if GNU hash is used
Definition ELF/Binary.hpp:429
const DynamicEntry * get(DynamicEntry::TAG tag) const
Return the first ELF::DynamicEntry associated with the given tag If the tag can't be found,...
Header::CLASS type() const
Return binary's class (ELF32 or ELF64)
Definition ELF/Binary.hpp:231
std::vector< std::unique_ptr< Symbol > > symbols_t
Internal container for storing ELF's Symbol.
Definition ELF/Binary.hpp:151
void write(const std::string &filename) override
Reconstruct the binary object and write it in filename
result< uint64_t > virtual_address_to_offset(uint64_t virtual_address) const
Convert a virtual address to a file offset.
uint64_t last_offset_section() const
Return the last offset used in binary according to sections table.
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.
PHDR_RELOC
Definition ELF/Binary.hpp:196
@ PIE_SHIFT
Definition ELF/Binary.hpp:207
@ BSS_END
Definition ELF/Binary.hpp:213
@ BINARY_END
Definition ELF/Binary.hpp:217
@ SEGMENT_GAP
Definition ELF/Binary.hpp:223
Header & header()
Return Elf header .
Definition ELF/Binary.hpp:236
const Section * section_from_offset(uint64_t offset, bool skip_nobits=true) const
Return the ELF::Section from the given offset. Return a nullptr if a section can't be found.
it_dynamic_entries dynamic_entries()
Return binary's dynamic entries.
Definition ELF/Binary.hpp:279
it_symbols_version_requirement symbols_version_requirement()
Return Symbol version requirement.
Definition ELF/Binary.hpp:360
it_pltgot_relocations pltgot_relocations()
Return plt.got relocations.
void strip()
Strip the binary by removing symtab symbols.
Note & add(const Note &note)
Add the given note and return the created entry.
Symbol & export_symbol(const std::string &symbol_name, uint64_t value=0)
Export the symbol with the given name and create it if it doesn't exist.
void remove(const Note &note)
Remove the given note.
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing ELF's Relocation.
Definition ELF/Binary.hpp:124
bool has_notes() const
true if the binary embeds notes
int64_t dynsym_idx(const std::string &name) const
Symbol index in the dynamic symbol table or -1 if the symbol does not exist.
const Relocation * get_relocation(const Symbol &symbol) const
Return relocation associated with the given Symbol It returns a nullptr if it is not found.
void remove_dynamic_symbol(const std::string &name)
Remove dynamic symbols with the given name.
const Section * section_from_virtual_address(uint64_t address, bool skip_nobits=true) const
Return the ELF::Section from the given address. Return a nullptr if a section can't be found.
const Relocation * get_relocation(const std::string &symbol_name) const
Return relocation associated with the given Symbol name It returns a nullptr if it is not found.
it_symtab_symbols symtab_symbols()
Return the debug symbols from the .symtab section.
Definition ELF/Binary.hpp:334
LIEF::Binary::functions_t ctor_functions() const override
List of binary constructors (typically, the functions located in the .init_array)
uint64_t imagebase() const override
Return program image base. For instance 0x40000
LIEF::Binary::functions_t functions() const
List of the functions found the in the binary.
bool has_library(const std::string &name) const
Check if the given library name exists in the current binary.
LIEF::Binary::functions_t dtor_functions() const
List of the binary destructors (typically, the functions located in the .fini_array)
std::vector< std::unique_ptr< SymbolVersion > > symbols_version_t
Internal container for storing ELF's SymbolVersion.
Definition ELF/Binary.hpp:115
const Symbol * get_dynamic_symbol(const std::string &name) const
Get the dynamic symbol from the given name. Return a nullptr if it can't be found.
DynamicEntryLibrary * get_library(const std::string &library_name)
Get the library object (DynamicEntryLibrary) from the given name If the library can't be found,...
bool is_pie() const override
Check if the binary has been compiled with -fpie -pie flags.
it_symbols_version symbols_version()
Return the symbol versions.
Definition ELF/Binary.hpp:343
DynamicEntryLibrary & add_library(const std::string &library_name)
Add a library as dependency.
Section * text_section()
Return the .text section. If the section can't be found, it returns a nullptr.
Definition ELF/Binary.hpp:468
it_object_relocations object_relocations()
Return relocations used in an object file (*.o)
Section * get_section(const std::string &name)
Return Section with the given name. If the section can't be found, it returns a nullptr.
it_imported_symbols imported_symbols()
Return symbols which are imported by the binary.
std::vector< std::unique_ptr< Segment > > segments_t
Internal container for storing ELF's Segment.
Definition ELF/Binary.hpp:97
it_sections sections()
Return an iterator over the binary's sections.
Definition ELF/Binary.hpp:256
it_const_notes notes() const
Return an iterator over the ELF's LIEF::ELF::Note.
Definition ELF/Binary.hpp:833
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.
it_symbols_version_definition symbols_version_definition()
Return symbols version definition.
Definition ELF/Binary.hpp:351
const Symbol * get_symtab_symbol(const std::string &name) const
Get the symtab symbol from the given name Return a nullptr if it can't be found.
it_dynamic_relocations dynamic_relocations()
Return dynamic relocations.
void overlay(std::vector< uint8_t > overlay)
Function to set the overlay.
Definition ELF/Binary.hpp:856
void remove_dynamic_symbol(Symbol *symbol)
Remove the given symbol from the dynamic symbols table.
span< const uint8_t > overlay() const
Overlay data (if any)
Definition ELF/Binary.hpp:851
bool has_overlay() const
True if data are present at the end of the binary.
Definition ELF/Binary.hpp:846
bool has(Section::TYPE type) const
Check if a ELF::Section associated with the given type exists.
Definition ELF/Binary.hpp:804
Symbol & add_dynamic_symbol(const Symbol &symbol, const SymbolVersion *version=nullptr)
Add a dynamic symbol with the associated SymbolVersion.
std::vector< std::unique_ptr< DynamicEntry > > dynamic_entries_t
Internal container for storing ELF's DynamicEntry.
Definition ELF/Binary.hpp:106
Symbol & add_symtab_symbol(const Symbol &symbol)
Add a symtab symbol.
void interpreter(const std::string &interpreter)
Change the interpreter.
Relocation * add_object_relocation(const Relocation &relocation, const Section &section)
Add relocation for object file (.o)
uint64_t virtual_size() const
Return the size of the mapped binary.
it_exported_symbols exported_symbols()
Return symbols which are exported by the binary.
std::vector< std::unique_ptr< Note > > notes_t
Internal container for storing notes.
Definition ELF/Binary.hpp:70
const Segment * get(Segment::TYPE type) const
Return the first ELF::Segment associated with the given type. If a segment can't be found,...
uint64_t entrypoint() const override
Return the binary's entrypoint.
Definition ELF/Binary.hpp:265
bool has(Note::TYPE type) const
Check if a ELF::Note associated with the given type exists.
Definition ELF/Binary.hpp:799
bool has_symtab_symbol(const std::string &name) const
Check if the symbol with the given name exists in the symtab symbol table.
Definition ELF/Binary.hpp:531
bool has_dynamic_symbol(const std::string &name) const
Check if the symbol with the given name exists in the dynamic symbols table.
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 Section * get(Section::TYPE type) const
Return the first ELF::Section associated with the given type If a section can't be found,...
Section * hash_section()
Return the hash section. If the section can't be found, it returns a nullptr.
uint64_t next_virtual_address() const
Return the next virtual address available.
const Segment * segment_from_virtual_address(uint64_t address) const
Return the ELF::Segment from the given address. Return a nullptr if a segment can't be found.
uint64_t eof_offset() const
Return the last offset used by the ELF binary according to both: the sections table and the segments ...
void remove(DynamicEntry::TAG tag)
Remove all dynamic entries with the given tag.
it_segments segments()
Return binary's segments.
Definition ELF/Binary.hpp:270
void remove_library(const std::string &library_name)
Remove the given library from the dependencies.
result< uint64_t > get_function_address(const std::string &func_name, bool demangled) const
Return the address of the given function name.
it_dynamic_symbols dynamic_symbols()
Return an iterator over the binary's dynamic symbols The dynamic symbols are those located in the ....
Definition ELF/Binary.hpp:317
Section * add(const Section &section, bool loaded=true)
Add a new section in the binary.
void remove_symtab_symbol(const std::string &name)
Remove symtabl symbols with the given name.
it_relocations relocations()
Return all relocations present in the binary.
Definition ELF/Binary.hpp:403
Section * dynamic_section()
Return the .dynamic section. If the section can't be found, it returns a nullptr.
const DynamicEntryLibrary * get_library(const std::string &library_name) const
Get the library object (DynamicEntryLibrary) from the given name If the library can't be found,...
it_symbols symbols()
Return an iterator on both static and dynamic symbols.
Definition ELF/Binary.hpp:507
uint64_t relocate_phdr_table(PHDR_RELOC type)
Force relocating the segments table in a specific way.
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing ELF's Section.
Definition ELF/Binary.hpp:184
Segment * replace(const Segment &new_segment, const Segment &original_segment, uint64_t base=0)
Replace the segment given in 2nd parameter with the segment given in the first one and return the upd...
void remove(const Section &section, bool clear=false)
Remove the given section. The clear parameter can be used to zeroize the original content beforehand.
bool has_section_with_offset(uint64_t offset) const
Check if a section that handles the given offset exists.
string_list_t strings(size_t min_size=5) const
Return list of the strings used by the ELF binary.
void remove_section(const std::string &name, bool clear=false) override
Remove a binary's section.
std::vector< uint8_t > raw()
Reconstruct the binary object and return its content as a byte vector.
const SysvHash * sysv_hash() const
Return the SysvHash object as a read-only object If the ELF binary does not use the legacy sysv hash ...
Definition ELF/Binary.hpp:448
std::vector< std::unique_ptr< SymbolVersionDefinition > > symbols_version_definition_t
Internal container for storing SymbolVersionDefinition.
Definition ELF/Binary.hpp:88
bool has_section(const std::string &name) const
Check if a section with the given name exists in the binary.
Symbol & add_exported_function(uint64_t address, const std::string &name="")
Create a symbol for the function at the given address and export it.
Section * symtab_symbols_section()
Return section which holds the symtab symbols. If the section can't be found, it returns a nullptr.
uint64_t last_offset_segment() const
Return the last offset used in binary according to segments table.
bool use_sysv_hash() const
true if SYSV hash is used
Definition ELF/Binary.hpp:442
void patch_pltgot(const Symbol &symbol, uint64_t address)
Patch the imported symbol with the address
void write(std::ostream &os, Builder::config_t config)
Reconstruct the binary object with the given config and write it in os stream.
bool has_interpreter() const
Check if the binary uses a loader (also named linker or interpreter)
const Relocation * get_relocation(uint64_t address) const
Return relocation associated with the given address. It returns a nullptr if it is not found.
void remove_symbol(const std::string &name)
Remove symbols with the given name in both:
const Segment * segment_from_offset(uint64_t offset) const
Return the ELF::Segment from the offset. Return a nullptr if a segment can't be found.
void write(std::ostream &os) override
Reconstruct the binary object and write it in os stream.
void remove(Note::TYPE type)
Remove all notes with the given type.
Relocation & add_dynamic_relocation(const Relocation &relocation)
Add a new dynamic relocation.
const std::string & interpreter() const
Return the ELF interpreter if any. (e.g. /lib64/ld-linux-x86-64.so.2) If the binary does not have an ...
int64_t symtab_idx(const std::string &name) const
Symbol index from the .symtab section or -1 if the symbol is not present.
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.
void write(const std::string &filename, Builder::config_t config)
Reconstruct the binary object with the given config and write it in filename
void permute_dynamic_symbols(const std::vector< size_t > &permutation)
Apply the given permutation on the dynamic symbols table.
bool has_section_with_va(uint64_t va) const
Check if a section that handles the given virtual address exists.
void patch_pltgot(const std::string &symbol_name, uint64_t address)
Patch the imported symbol's name with the address
void accept(LIEF::Visitor &visitor) const override
Method associated with the visitor pattern.
void remove(const Segment &seg)
Remove the given segment.
const Note * get(Note::TYPE type) const
Return the first ELF::Note associated with the given type If a note can't be found,...
Symbol & export_symbol(const Symbol &symbol)
Export the given symbol and create it if it doesn't exist.
Relocation & add_pltgot_relocation(const Relocation &relocation)
Add a .plt.got relocation. This kind of relocation is usually associated with a PLT stub that aims at...
std::vector< std::unique_ptr< SymbolVersionRequirement > > symbols_version_requirement_t
Internal container for storing SymbolVersionRequirement.
Definition ELF/Binary.hpp:79
result< uint64_t > get_function_address(const std::string &func_name) const override
Return the address of the given function name.
const GnuHash * gnu_hash() const
Return the GnuHash object in readonly If the ELF binary does not use the GNU hash table,...
Definition ELF/Binary.hpp:435
bool has_nx() const override
Check if the binary uses the NX protection (Non executable stack)
void remove(const DynamicEntry &entry)
Remove the given dynamic entry.
DynamicEntry & add(const DynamicEntry &entry)
Add the given dynamic entry and return the new entry.
bool has(Segment::TYPE type) const
Check if ELF::Segment associated with the given type exists.
Definition ELF/Binary.hpp:794
bool has(DynamicEntry::TAG tag) const
Check if an ELF::DynamicEntry associated with the given tag exists.
Definition ELF/Binary.hpp:789
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:51
Class which represents a DT_NEEDED entry in the dynamic table.
Definition DynamicEntryLibrary.hpp:29
Class which represents an entry in the dynamic table These entries are located in the ....
Definition DynamicEntry.hpp:37
TAG
Definition DynamicEntry.hpp:46
Class which provides a view over the GNU Hash implementation. Most of the fields are read-only since ...
Definition GnuHash.hpp:36
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:37
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS]
Definition ELF/Header.hpp:76
Class which represents an ELF note. This class can be instantiated using the static Note::create func...
Definition Note.hpp:36
TYPE
LIEF representation of the ELF NT_ values.
Definition Note.hpp:46
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
Class wich represents an ELF Section.
Definition ELF/Section.hpp:46
TYPE
Definition ELF/Section.hpp:60
Class which represents the ELF segments.
Definition Segment.hpp:44
TYPE
Definition Segment.hpp:64
Class which represents an entry defined in DT_VERDEF or .gnu.version_d
Definition SymbolVersionDefinition.hpp:39
Class which represents an entry in the DT_VERNEED or .gnu.version_r table.
Definition SymbolVersionRequirement.hpp:39
Class which represents an entry defined in the DT_VERSYM dynamic entry.
Definition SymbolVersion.hpp:32
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
Class which represents the SYSV hash for the symbols resolution.
Definition SysvHash.hpp:39
Definition Abstract/Header.hpp:29
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
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
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:72
Configuration options to tweak the building process.
Definition ELF/Builder.hpp:58