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_MODINFO_HPP 24 #define ELFIO_MODINFO_HPP 25 26 #include <string> 27 #include <vector> 28 29 namespace ELFIO { 30 31 //------------------------------------------------------------------------------ 32 template <class S> class modinfo_section_accessor_template 33 { 34 public: 35 //------------------------------------------------------------------------------ modinfo_section_accessor_template(S * section)36 modinfo_section_accessor_template( S* section ) 37 : modinfo_section( section ) 38 { 39 process_section(); 40 } 41 42 //------------------------------------------------------------------------------ get_attribute_num() const43 Elf_Word get_attribute_num() const { return (Elf_Word)content.size(); } 44 45 //------------------------------------------------------------------------------ 46 bool get_attribute(Elf_Word no,std::string & field,std::string & value) const47 get_attribute( Elf_Word no, std::string& field, std::string& value ) const 48 { 49 if ( no < content.size() ) { 50 field = content[no].first; 51 value = content[no].second; 52 return true; 53 } 54 55 return false; 56 } 57 58 //------------------------------------------------------------------------------ get_attribute(const std::string field_name,std::string & value) const59 bool get_attribute( const std::string field_name, std::string& value ) const 60 { 61 for ( auto i = content.begin(); i != content.end(); i++ ) { 62 if ( field_name == i->first ) { 63 value = i->second; 64 return true; 65 } 66 } 67 68 return false; 69 } 70 71 //------------------------------------------------------------------------------ add_attribute(const std::string field,const std::string value)72 Elf_Word add_attribute( const std::string field, const std::string value ) 73 { 74 Elf_Word current_position = 0; 75 76 if ( modinfo_section ) { 77 // Strings are addeded to the end of the current section data 78 current_position = (Elf_Word)modinfo_section->get_size(); 79 80 std::string attribute = field + "=" + value; 81 82 modinfo_section->append_data( attribute + '\0' ); 83 content.push_back( 84 std::pair<std::string, std::string>( field, value ) ); 85 } 86 87 return current_position; 88 } 89 90 //------------------------------------------------------------------------------ 91 private: process_section()92 void process_section() 93 { 94 const char* pdata = modinfo_section->get_data(); 95 if ( pdata ) { 96 ELFIO::Elf_Xword i = 0; 97 while ( i < modinfo_section->get_size() ) { 98 while ( i < modinfo_section->get_size() && !pdata[i] ) 99 i++; 100 if ( i < modinfo_section->get_size() ) { 101 std::string info = pdata + i; 102 size_t loc = info.find( '=' ); 103 std::pair<std::string, std::string> attribute( 104 info.substr( 0, loc ), info.substr( loc + 1 ) ); 105 106 content.push_back( attribute ); 107 108 i += info.length(); 109 } 110 } 111 } 112 } 113 114 //------------------------------------------------------------------------------ 115 private: 116 S* modinfo_section; 117 std::vector<std::pair<std::string, std::string>> content; 118 }; 119 120 using modinfo_section_accessor = modinfo_section_accessor_template<section>; 121 using const_modinfo_section_accessor = 122 modinfo_section_accessor_template<const section>; 123 124 } // namespace ELFIO 125 126 #endif // ELFIO_MODINFO_HPP 127