• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     explicit 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,
60                         std::string&       value ) const
61     {
62         for ( auto& i : content ) {
63             if ( field_name == i.first ) {
64                 value = i.second;
65                 return true;
66             }
67         }
68 
69         return false;
70     }
71 
72     //------------------------------------------------------------------------------
add_attribute(const std::string & field,const std::string & value)73     Elf_Word add_attribute( const std::string& field, const std::string& value )
74     {
75         Elf_Word current_position = 0;
76 
77         if ( modinfo_section ) {
78             // Strings are addeded to the end of the current section data
79             current_position = (Elf_Word)modinfo_section->get_size();
80 
81             std::string attribute = field + "=" + value;
82 
83             modinfo_section->append_data( attribute + '\0' );
84             content.emplace_back( 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                     content.emplace_back( info.substr( 0, loc ),
104                                           info.substr( loc + 1 ) );
105 
106                     i += info.length();
107                 }
108             }
109         }
110     }
111 
112     //------------------------------------------------------------------------------
113   private:
114     S*                                               modinfo_section;
115     std::vector<std::pair<std::string, std::string>> content;
116 };
117 
118 using modinfo_section_accessor = modinfo_section_accessor_template<section>;
119 using const_modinfo_section_accessor =
120     modinfo_section_accessor_template<const section>;
121 
122 } // namespace ELFIO
123 
124 #endif // ELFIO_MODINFO_HPP
125