[163] | 1 | /* |
---|
| 2 | This file is part of Libelfpp. |
---|
| 3 | |
---|
| 4 | Libelfpp is free software: you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU Lesser General Public License as |
---|
| 6 | published by the Free Software Foundation, either version 3 of the |
---|
| 7 | License, or (at your option) any later version. |
---|
| 8 | |
---|
| 9 | Libelfpp is distributed in the hope that it will be useful, but |
---|
| 10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 12 | General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU Lesser General Public |
---|
| 15 | License along with Libelfpp. If not, see |
---|
| 16 | <http://www.gnu.org/licenses/>. |
---|
| 17 | |
---|
| 18 | Copyright (c) Alexandre Becoulet <alexandre.becoulet@free.fr> |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #ifndef ELFPP_OBJECT_HH_ |
---|
| 22 | #define ELFPP_OBJECT_HH_ |
---|
| 23 | |
---|
| 24 | #include <string> |
---|
| 25 | #include <vector> |
---|
| 26 | |
---|
| 27 | #include <dpp/linked_list> |
---|
| 28 | |
---|
| 29 | #include "elfpp.hh" |
---|
| 30 | #include "elfpp_bits.hh" |
---|
| 31 | |
---|
| 32 | namespace elfpp |
---|
| 33 | { |
---|
| 34 | class section; |
---|
| 35 | class symbol; |
---|
| 36 | class elf_access; |
---|
| 37 | template <typename bits_t, int width> class elfn_access; |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | @short ELF object file class |
---|
| 41 | @header elfpp/object |
---|
| 42 | */ |
---|
| 43 | class object |
---|
| 44 | { |
---|
| 45 | template <typename, int> friend class elfn_access; |
---|
| 46 | friend class section; |
---|
| 47 | public: |
---|
| 48 | |
---|
| 49 | /** Create an empty elf object */ |
---|
| 50 | object(e_machine_e machine, |
---|
| 51 | ei_class_e word_width = ELFCLASSNONE, |
---|
| 52 | ei_data_e byte_order = ELFDATANONE); |
---|
| 53 | |
---|
| 54 | /** Create a new elf object and load sections from file. */ |
---|
| 55 | object(const std::string &filename); |
---|
| 56 | |
---|
| 57 | /** Create a new elf object */ |
---|
| 58 | object(); |
---|
| 59 | |
---|
| 60 | /** Copy all header fields of obj and set the access_ variable*/ |
---|
[238] | 61 | void copy_info(object& obj, size_t word_width = 0); |
---|
[163] | 62 | |
---|
| 63 | ~object(); |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | Parse symbol and relocation tables sections and create |
---|
| 67 | associated object representation. Parsed table sections are |
---|
| 68 | discarded and will be regenerated when writing elf file. |
---|
| 69 | */ |
---|
| 70 | void parse_symbol_table(); |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | @this creates symbols for all area not covered by a symbol in |
---|
| 74 | allocatable sections. |
---|
| 75 | */ |
---|
| 76 | void create_orphan_symbols(); |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | @this loads symbol data from section content to symbol content |
---|
| 80 | storage. |
---|
| 81 | */ |
---|
| 82 | void load_symbol_data(); |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | @this update all relocations to be relative to mangled |
---|
| 86 | symbols. Sections will be updated from symbol content when |
---|
| 87 | writing elf file. |
---|
| 88 | */ |
---|
| 89 | void set_relative_relocs(); |
---|
| 90 | |
---|
| 91 | /** Add a new section to object. */ |
---|
| 92 | void add_section(section &sec); |
---|
| 93 | |
---|
| 94 | /** Remove a section from object. */ |
---|
| 95 | void remove_section(section &sec); |
---|
| 96 | |
---|
| 97 | /** Get first section with given name. */ |
---|
| 98 | section & get_section(const std::string &name); |
---|
| 99 | |
---|
| 100 | /** Return section table container. */ |
---|
| 101 | inline dpp::linked_list<section> & get_section_table(); |
---|
| 102 | |
---|
| 103 | /** Add a new segment to object. */ |
---|
| 104 | void add_segment(segment &seg); |
---|
| 105 | |
---|
| 106 | /** Return section table container. */ |
---|
| 107 | inline dpp::linked_list<segment> & get_segment_table(); |
---|
| 108 | |
---|
| 109 | /** Add a new symbol to object. Symbol must be stored per |
---|
| 110 | section. Only symbol without associated section must be added |
---|
| 111 | to object. */ |
---|
| 112 | void add_symbol(symbol &sym); |
---|
| 113 | |
---|
| 114 | /** Remove a section independent symbol from object. */ |
---|
| 115 | void remove_symbol(symbol &sym); |
---|
| 116 | |
---|
| 117 | /** Get section independent symbol by name. */ |
---|
| 118 | inline symbol & get_symbol(const std::string &name); |
---|
| 119 | |
---|
| 120 | /** Get section independent symbols table. */ |
---|
| 121 | inline const sym_tab_map_t & get_symbol_table() const; |
---|
| 122 | |
---|
| 123 | /** Write elf object to file. Symbol and relocation table may be |
---|
| 124 | generated from object representation if available. Sections |
---|
| 125 | content are updated with symbols content. */ |
---|
| 126 | void write(const std::string &filename); |
---|
| 127 | |
---|
| 128 | e_machine_e get_machine(); |
---|
| 129 | ei_class_e get_word_width(); |
---|
| 130 | ei_data_e get_byteorder(); |
---|
| 131 | |
---|
| 132 | private: |
---|
| 133 | |
---|
| 134 | void set_relative_relocs(symbol *sym); |
---|
| 135 | |
---|
| 136 | static ei_class_e get_word_width(e_machine_e machine); |
---|
| 137 | static ei_data_e get_byte_order(e_machine_e machine); |
---|
| 138 | |
---|
| 139 | dpp::linked_list<section> section_tab_; |
---|
| 140 | dpp::linked_list<segment> segment_tab_; |
---|
| 141 | |
---|
| 142 | ei_class_e word_width_; |
---|
| 143 | ei_data_e byteorder_; |
---|
| 144 | ei_osabi_e os_abi_; |
---|
| 145 | uint8_t abi_ver_; |
---|
| 146 | e_type_e type_; |
---|
| 147 | e_machine_e machine_; |
---|
| 148 | |
---|
| 149 | elf_access *access_; |
---|
| 150 | |
---|
| 151 | bool rel_with_addend_; |
---|
| 152 | bool generate_symtab_; |
---|
| 153 | std::vector<section *> secidx_; |
---|
| 154 | std::vector<symbol *> symidx_; |
---|
| 155 | uint64_t entry_; |
---|
| 156 | unsigned int flags_; |
---|
| 157 | sym_tab_map_t sym_tab_; |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | #endif |
---|
| 163 | |
---|