|
| 1 | +/* |
| 2 | +Copyright (C) 2001-present by Serge Lamikhov-Center |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#ifndef ELFIO_ARRAY_HPP |
| 24 | +#define ELFIO_ARRAY_HPP |
| 25 | + |
| 26 | +#include <algorithm> |
| 27 | + |
| 28 | +namespace ELFIO { |
| 29 | + |
| 30 | +//------------------------------------------------------------------------------ |
| 31 | +template <class S> class array_section_accessor_template |
| 32 | +{ |
| 33 | + public: |
| 34 | + //------------------------------------------------------------------------------ |
| 35 | + array_section_accessor_template( const elfio& elf_file, S* section ) |
| 36 | + : elf_file( elf_file ), array_section( section ) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + //------------------------------------------------------------------------------ |
| 41 | + Elf_Xword get_entries_num() const |
| 42 | + { |
| 43 | + Elf_Xword entry_size = elf_file.get_class() == ELFCLASS32 |
| 44 | + ? sizeof( Elf32_Addr ) |
| 45 | + : sizeof( Elf64_Addr ); |
| 46 | + return array_section->get_size() / entry_size; |
| 47 | + } |
| 48 | + |
| 49 | + //------------------------------------------------------------------------------ |
| 50 | + bool get_entry( Elf_Xword index, Elf64_Addr& address ) const |
| 51 | + { |
| 52 | + if ( index >= get_entries_num() ) { // Is index valid |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if ( elf_file.get_class() == ELFCLASS32 ) { |
| 57 | + generic_get_entry_arr<Elf32_Addr>( index, address ); |
| 58 | + } |
| 59 | + else { |
| 60 | + generic_get_entry_arr<Elf64_Addr>( index, address ); |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + //------------------------------------------------------------------------------ |
| 67 | + void add_entry( Elf64_Addr address ) |
| 68 | + { |
| 69 | + if ( elf_file.get_class() == ELFCLASS32 ) { |
| 70 | + generic_add_entry_arr<Elf32_Addr>( address ); |
| 71 | + } |
| 72 | + else { |
| 73 | + generic_add_entry_arr<Elf64_Addr>( address ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private: |
| 78 | + //------------------------------------------------------------------------------ |
| 79 | + template <class T> |
| 80 | + void generic_get_entry_arr( Elf_Xword index, Elf64_Addr& address ) const |
| 81 | + { |
| 82 | + const endianess_convertor& convertor = elf_file.get_convertor(); |
| 83 | + |
| 84 | + const T temp = *reinterpret_cast<const T*>( array_section->get_data() + |
| 85 | + index * sizeof( T ) ); |
| 86 | + address = convertor( temp ); |
| 87 | + } |
| 88 | + |
| 89 | + //------------------------------------------------------------------------------ |
| 90 | + template <class T> void generic_add_entry_arr( Elf64_Addr address ) |
| 91 | + { |
| 92 | + const endianess_convertor& convertor = elf_file.get_convertor(); |
| 93 | + |
| 94 | + T temp = convertor( (T)address ); |
| 95 | + array_section->append_data( reinterpret_cast<char*>( &temp ), |
| 96 | + sizeof( temp ) ); |
| 97 | + } |
| 98 | + |
| 99 | + //------------------------------------------------------------------------------ |
| 100 | + private: |
| 101 | + const elfio& elf_file; |
| 102 | + S* array_section; |
| 103 | +}; |
| 104 | + |
| 105 | +using array_section_accessor = array_section_accessor_template<section>; |
| 106 | +using const_array_section_accessor = |
| 107 | + array_section_accessor_template<const section>; |
| 108 | + |
| 109 | +} // namespace ELFIO |
| 110 | + |
| 111 | +#endif // ELFIO_ARRAY_HPP |
0 commit comments