• 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_NOTE_HPP
24 #define ELFIO_NOTE_HPP
25 
26 namespace ELFIO {
27 
28 //------------------------------------------------------------------------------
29 // There are discrepancies in documentations. SCO documentation
30 // (http://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section)
31 // requires 8 byte entries alignment for 64-bit ELF file,
32 // but Oracle's definition uses the same structure
33 // for 32-bit and 64-bit formats.
34 // (https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-18048.html)
35 //
36 // It looks like EM_X86_64 Linux implementation is similar to Oracle's
37 // definition. Therefore, the same alignment works for both formats
38 //------------------------------------------------------------------------------
39 
40 //------------------------------------------------------------------------------
41 template <class S, Elf_Xword ( S::*F_get_size )() const>
42 class note_section_accessor_template
43 {
44   public:
45     //------------------------------------------------------------------------------
note_section_accessor_template(const elfio & elf_file,S * section)46     explicit note_section_accessor_template( const elfio& elf_file, S* section )
47         : elf_file( elf_file ), notes( section )
48     {
49         process_section();
50     }
51 
52     //------------------------------------------------------------------------------
get_notes_num() const53     Elf_Word get_notes_num() const
54     {
55         return (Elf_Word)note_start_positions.size();
56     }
57 
58     //------------------------------------------------------------------------------
get_note(Elf_Word index,Elf_Word & type,std::string & name,char * & desc,Elf_Word & descSize) const59     bool get_note( Elf_Word     index,
60                    Elf_Word&    type,
61                    std::string& name,
62                    char*&       desc,
63                    Elf_Word&    descSize ) const
64     {
65         if ( index >= ( notes->*F_get_size )() ) {
66             return false;
67         }
68 
69         const char* pData = notes->get_data() + note_start_positions[index];
70         int         align = sizeof( Elf_Word );
71 
72         const endianess_convertor& convertor = elf_file.get_convertor();
73         type = convertor( *(const Elf_Word*)( pData + 2 * (size_t)align ) );
74         Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) );
75         descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) );
76 
77         Elf_Xword max_name_size =
78             ( notes->*F_get_size )() - note_start_positions[index];
79         if ( namesz < 1 || namesz > max_name_size ||
80              (Elf_Xword)namesz + descSize > max_name_size ) {
81             return false;
82         }
83         name.assign( pData + 3 * (size_t)align, namesz - 1 );
84         if ( 0 == descSize ) {
85             desc = nullptr;
86         }
87         else {
88             desc = const_cast<char*>( pData + 3 * (size_t)align +
89                                       ( ( namesz + align - 1 ) / align ) *
90                                           (size_t)align );
91         }
92 
93         return true;
94     }
95 
96     //------------------------------------------------------------------------------
add_note(Elf_Word type,const std::string & name,const char * desc,Elf_Word descSize)97     void add_note( Elf_Word           type,
98                    const std::string& name,
99                    const char*        desc,
100                    Elf_Word           descSize )
101     {
102         const endianess_convertor& convertor = elf_file.get_convertor();
103 
104         int         align       = sizeof( Elf_Word );
105         Elf_Word    nameLen     = (Elf_Word)name.size() + 1;
106         Elf_Word    nameLenConv = convertor( nameLen );
107         std::string buffer( reinterpret_cast<char*>( &nameLenConv ), align );
108         Elf_Word    descSizeConv = convertor( descSize );
109 
110         buffer.append( reinterpret_cast<char*>( &descSizeConv ), align );
111         type = convertor( type );
112         buffer.append( reinterpret_cast<char*>( &type ), align );
113         buffer.append( name );
114         buffer.append( 1, '\x00' );
115         const char pad[] = { '\0', '\0', '\0', '\0' };
116         if ( nameLen % align != 0 ) {
117             buffer.append( pad, (size_t)align - nameLen % align );
118         }
119         if ( desc != nullptr && descSize != 0 ) {
120             buffer.append( desc, descSize );
121             if ( descSize % align != 0 ) {
122                 buffer.append( pad, (size_t)align - descSize % align );
123             }
124         }
125 
126         note_start_positions.emplace_back( ( notes->*F_get_size )() );
127         notes->append_data( buffer );
128     }
129 
130   private:
131     //------------------------------------------------------------------------------
process_section()132     void process_section()
133     {
134         const endianess_convertor& convertor = elf_file.get_convertor();
135         const char*                data      = notes->get_data();
136         Elf_Xword                  size      = ( notes->*F_get_size )();
137         Elf_Xword                  current   = 0;
138 
139         note_start_positions.clear();
140 
141         // Is it empty?
142         if ( nullptr == data || 0 == size ) {
143             return;
144         }
145 
146         Elf_Word align = sizeof( Elf_Word );
147         while ( current + (Elf_Xword)3 * align <= size ) {
148             Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) );
149             Elf_Word descsz = convertor(
150                 *(const Elf_Word*)( data + current + sizeof( namesz ) ) );
151             Elf_Word advance =
152                 (Elf_Xword)3 * sizeof( Elf_Word ) +
153                 ( ( namesz + align - 1 ) / align ) * (Elf_Xword)align +
154                 ( ( descsz + align - 1 ) / align ) * (Elf_Xword)align;
155             if ( namesz < size && descsz < size && current + advance <= size ) {
156                 note_start_positions.emplace_back( current );
157             }
158             else {
159                 break;
160             }
161 
162             current += advance;
163         }
164     }
165 
166     //------------------------------------------------------------------------------
167   private:
168     const elfio&           elf_file;
169     S*                     notes;
170     std::vector<Elf_Xword> note_start_positions;
171 };
172 
173 using note_section_accessor =
174     note_section_accessor_template<section, &section::get_size>;
175 using const_note_section_accessor =
176     note_section_accessor_template<const section, &section::get_size>;
177 using note_segment_accessor =
178     note_section_accessor_template<segment, &segment::get_file_size>;
179 using const_note_segment_accessor =
180     note_section_accessor_template<const segment, &segment::get_file_size>;
181 
182 } // namespace ELFIO
183 
184 #endif // ELFIO_NOTE_HPP
185