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> class note_section_accessor_template 42 { 43 public: 44 //------------------------------------------------------------------------------ note_section_accessor_template(const elfio & elf_file,S * section)45 note_section_accessor_template( const elfio& elf_file, S* section ) 46 : elf_file( elf_file ), note_section( section ) 47 { 48 process_section(); 49 } 50 51 //------------------------------------------------------------------------------ get_notes_num() const52 Elf_Word get_notes_num() const 53 { 54 return (Elf_Word)note_start_positions.size(); 55 } 56 57 //------------------------------------------------------------------------------ get_note(Elf_Word index,Elf_Word & type,std::string & name,void * & desc,Elf_Word & descSize) const58 bool get_note( Elf_Word index, 59 Elf_Word& type, 60 std::string& name, 61 void*& desc, 62 Elf_Word& descSize ) const 63 { 64 if ( index >= note_section->get_size() ) { 65 return false; 66 } 67 68 const char* pData = 69 note_section->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 note_section->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 = 0; 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 void * desc,Elf_Word descSize)97 void add_note( Elf_Word type, 98 const std::string& name, 99 const void* 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 != 0 && descSize != 0 ) { 120 buffer.append( reinterpret_cast<const char*>( desc ), descSize ); 121 if ( descSize % align != 0 ) { 122 buffer.append( pad, (size_t)align - descSize % align ); 123 } 124 } 125 126 note_start_positions.push_back( note_section->get_size() ); 127 note_section->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 = note_section->get_data(); 136 Elf_Xword size = note_section->get_size(); 137 Elf_Xword current = 0; 138 139 note_start_positions.clear(); 140 141 // Is it empty? 142 if ( 0 == data || 0 == size ) { 143 return; 144 } 145 146 Elf_Word align = sizeof( Elf_Word ); 147 while ( current + (Elf_Xword)3 * align <= size ) { 148 note_start_positions.push_back( current ); 149 Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) ); 150 Elf_Word descsz = convertor( 151 *(const Elf_Word*)( data + current + sizeof( namesz ) ) ); 152 153 current += (Elf_Xword)3 * sizeof( Elf_Word ) + 154 ( ( namesz + align - 1 ) / align ) * (Elf_Xword)align + 155 ( ( descsz + align - 1 ) / align ) * (Elf_Xword)align; 156 } 157 } 158 159 //------------------------------------------------------------------------------ 160 private: 161 const elfio& elf_file; 162 S* note_section; 163 std::vector<Elf_Xword> note_start_positions; 164 }; 165 166 using note_section_accessor = note_section_accessor_template<section>; 167 using const_note_section_accessor = 168 note_section_accessor_template<const section>; 169 170 } // namespace ELFIO 171 172 #endif // ELFIO_NOTE_HPP 173