EnvironmentLoader/source/elfio/elfio_note.hpp

185 lines
7.1 KiB
C++
Raw Normal View History

2021-12-25 20:14:56 +01:00
/*
2023-01-05 16:26:11 +01:00
Copyright (C) 2001-present by Serge Lamikhov-Center
2021-12-25 20:14:56 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef ELFIO_NOTE_HPP
#define ELFIO_NOTE_HPP
namespace ELFIO {
//------------------------------------------------------------------------------
// There are discrepancies in documentations. SCO documentation
// (http://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section)
// requires 8 byte entries alignment for 64-bit ELF file,
// but Oracle's definition uses the same structure
// for 32-bit and 64-bit formats.
// (https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-18048.html)
//
// It looks like EM_X86_64 Linux implementation is similar to Oracle's
// definition. Therefore, the same alignment works for both formats
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
2023-01-05 16:26:11 +01:00
template <class S, Elf_Xword ( S::*F_get_size )() const>
2021-12-25 20:14:56 +01:00
class note_section_accessor_template
{
public:
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
explicit note_section_accessor_template( const elfio& elf_file, S* section )
: elf_file( elf_file ), notes( section )
2021-12-25 20:14:56 +01:00
{
process_section();
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
Elf_Word get_notes_num() const
2021-12-25 20:14:56 +01:00
{
return (Elf_Word)note_start_positions.size();
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
bool get_note( Elf_Word index,
Elf_Word& type,
std::string& name,
char*& desc,
Elf_Word& descSize ) const
2021-12-25 20:14:56 +01:00
{
2023-01-05 16:26:11 +01:00
if ( index >= ( notes->*F_get_size )() ) {
2021-12-25 20:14:56 +01:00
return false;
}
2023-01-05 16:26:11 +01:00
const char* pData = notes->get_data() + note_start_positions[index];
int align = sizeof( Elf_Word );
2021-12-25 20:14:56 +01:00
const endianess_convertor& convertor = elf_file.get_convertor();
2023-01-05 16:26:11 +01:00
type = convertor( *(const Elf_Word*)( pData + 2 * (size_t)align ) );
2021-12-25 20:14:56 +01:00
Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) );
descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) );
2023-01-05 16:26:11 +01:00
Elf_Xword max_name_size =
( notes->*F_get_size )() - note_start_positions[index];
if ( namesz < 1 || namesz > max_name_size ||
(Elf_Xword)namesz + descSize > max_name_size ) {
2021-12-25 20:14:56 +01:00
return false;
}
2023-01-05 16:26:11 +01:00
name.assign( pData + 3 * (size_t)align, namesz - 1 );
2021-12-25 20:14:56 +01:00
if ( 0 == descSize ) {
2023-01-05 16:26:11 +01:00
desc = nullptr;
2021-12-25 20:14:56 +01:00
}
else {
2023-01-05 16:26:11 +01:00
desc = const_cast<char*>( pData + 3 * (size_t)align +
( ( namesz + align - 1 ) / align ) *
(size_t)align );
2021-12-25 20:14:56 +01:00
}
return true;
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
2021-12-25 20:14:56 +01:00
void add_note( Elf_Word type,
const std::string& name,
2023-01-05 16:26:11 +01:00
const char* desc,
2021-12-25 20:14:56 +01:00
Elf_Word descSize )
{
const endianess_convertor& convertor = elf_file.get_convertor();
2023-01-05 16:26:11 +01:00
int align = sizeof( Elf_Word );
Elf_Word nameLen = (Elf_Word)name.size() + 1;
Elf_Word nameLenConv = convertor( nameLen );
2021-12-25 20:14:56 +01:00
std::string buffer( reinterpret_cast<char*>( &nameLenConv ), align );
2023-01-05 16:26:11 +01:00
Elf_Word descSizeConv = convertor( descSize );
2021-12-25 20:14:56 +01:00
buffer.append( reinterpret_cast<char*>( &descSizeConv ), align );
type = convertor( type );
buffer.append( reinterpret_cast<char*>( &type ), align );
buffer.append( name );
buffer.append( 1, '\x00' );
const char pad[] = { '\0', '\0', '\0', '\0' };
if ( nameLen % align != 0 ) {
2023-01-05 16:26:11 +01:00
buffer.append( pad, (size_t)align - nameLen % align );
2021-12-25 20:14:56 +01:00
}
2023-01-05 16:26:11 +01:00
if ( desc != nullptr && descSize != 0 ) {
buffer.append( desc, descSize );
2021-12-25 20:14:56 +01:00
if ( descSize % align != 0 ) {
2023-01-05 16:26:11 +01:00
buffer.append( pad, (size_t)align - descSize % align );
2021-12-25 20:14:56 +01:00
}
}
2023-01-05 16:26:11 +01:00
note_start_positions.emplace_back( ( notes->*F_get_size )() );
notes->append_data( buffer );
2021-12-25 20:14:56 +01:00
}
private:
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
2021-12-25 20:14:56 +01:00
void process_section()
{
const endianess_convertor& convertor = elf_file.get_convertor();
2023-01-05 16:26:11 +01:00
const char* data = notes->get_data();
Elf_Xword size = ( notes->*F_get_size )();
Elf_Xword current = 0;
2021-12-25 20:14:56 +01:00
note_start_positions.clear();
// Is it empty?
2023-01-05 16:26:11 +01:00
if ( nullptr == data || 0 == size ) {
2021-12-25 20:14:56 +01:00
return;
}
2023-01-05 16:26:11 +01:00
Elf_Word align = sizeof( Elf_Word );
while ( current + (Elf_Xword)3 * align <= size ) {
Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) );
2021-12-25 20:14:56 +01:00
Elf_Word descsz = convertor(
2023-01-05 16:26:11 +01:00
*(const Elf_Word*)( data + current + sizeof( namesz ) ) );
Elf_Word advance =
(Elf_Xword)3 * sizeof( Elf_Word ) +
( ( namesz + align - 1 ) / align ) * (Elf_Xword)align +
( ( descsz + align - 1 ) / align ) * (Elf_Xword)align;
if ( namesz < size && descsz < size && current + advance <= size ) {
note_start_positions.emplace_back( current );
}
else {
break;
}
2021-12-25 20:14:56 +01:00
2023-01-05 16:26:11 +01:00
current += advance;
2021-12-25 20:14:56 +01:00
}
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
2021-12-25 20:14:56 +01:00
private:
const elfio& elf_file;
2023-01-05 16:26:11 +01:00
S* notes;
2021-12-25 20:14:56 +01:00
std::vector<Elf_Xword> note_start_positions;
};
2023-01-05 16:26:11 +01:00
using note_section_accessor =
note_section_accessor_template<section, &section::get_size>;
using const_note_section_accessor =
note_section_accessor_template<const section, &section::get_size>;
using note_segment_accessor =
note_section_accessor_template<segment, &segment::get_file_size>;
using const_note_segment_accessor =
note_section_accessor_template<const segment, &segment::get_file_size>;
2021-12-25 20:14:56 +01:00
} // namespace ELFIO
#endif // ELFIO_NOTE_HPP