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, typename T> class array_section_accessor_template 32 { 33 public: 34 //------------------------------------------------------------------------------ array_section_accessor_template(const elfio & elf_file,S * section)35 explicit array_section_accessor_template( const elfio& elf_file, 36 S* section ) 37 : elf_file( elf_file ), array_section( section ) 38 { 39 } 40 41 //------------------------------------------------------------------------------ get_entries_num() const42 Elf_Xword get_entries_num() const 43 { 44 Elf_Xword entry_size = sizeof( T ); 45 return array_section->get_size() / entry_size; 46 } 47 48 //------------------------------------------------------------------------------ get_entry(Elf_Xword index,Elf64_Addr & address) const49 bool get_entry( Elf_Xword index, Elf64_Addr& address ) const 50 { 51 if ( index >= get_entries_num() ) { // Is index valid 52 return false; 53 } 54 55 const endianess_convertor& convertor = elf_file.get_convertor(); 56 57 const T temp = *reinterpret_cast<const T*>( array_section->get_data() + 58 index * sizeof( T ) ); 59 address = convertor( temp ); 60 61 return true; 62 } 63 64 //------------------------------------------------------------------------------ add_entry(Elf64_Addr address)65 void add_entry( Elf64_Addr address ) 66 { 67 const endianess_convertor& convertor = elf_file.get_convertor(); 68 69 T temp = convertor( (T)address ); 70 array_section->append_data( reinterpret_cast<char*>( &temp ), 71 sizeof( temp ) ); 72 } 73 74 private: 75 //------------------------------------------------------------------------------ 76 const elfio& elf_file; 77 S* array_section; 78 }; 79 80 template <typename T = Elf32_Word> 81 using array_section_accessor = array_section_accessor_template<section, T>; 82 template <typename T = Elf32_Word> 83 using const_array_section_accessor = 84 array_section_accessor_template<const section, T>; 85 86 } // namespace ELFIO 87 88 #endif // ELFIO_ARRAY_HPP 89