2012-11-27 09:45:28 +00:00
|
|
|
/*
|
2021-01-19 07:43:01 +00:00
|
|
|
Copyright (C) 2001-present by Serge Lamikhov-Center
|
2012-11-27 09:45:28 +00: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 {
|
|
|
|
|
2016-07-10 08:22:53 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-11-27 09:45:28 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2022-01-29 08:48:04 +00:00
|
|
|
template <class S, Elf_Xword ( S::*F_get_size )() const>
|
|
|
|
class note_section_accessor_template
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2021-02-05 08:40:18 +00:00
|
|
|
note_section_accessor_template( const elfio& elf_file, S* section )
|
2022-01-29 15:03:37 +00:00
|
|
|
: elf_file( elf_file ), notes( section )
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
|
|
|
process_section();
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
Elf_Word get_notes_num() const
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
|
|
|
return (Elf_Word)note_start_positions.size();
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
bool get_note( Elf_Word index,
|
|
|
|
Elf_Word& type,
|
|
|
|
std::string& name,
|
|
|
|
void*& desc,
|
|
|
|
Elf_Word& descSize ) const
|
2012-11-27 09:45:28 +00:00
|
|
|
{
|
2022-01-29 15:03:37 +00:00
|
|
|
if ( index >= ( notes->*F_get_size )() ) {
|
2012-11-27 09:45:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:03:37 +00:00
|
|
|
const char* pData = notes->get_data() + note_start_positions[index];
|
|
|
|
int align = sizeof( Elf_Word );
|
2012-11-27 09:45:28 +00:00
|
|
|
|
|
|
|
const endianess_convertor& convertor = elf_file.get_convertor();
|
2020-10-30 16:06:26 +00:00
|
|
|
type = convertor( *(const Elf_Word*)( pData + 2 * (size_t)align ) );
|
2016-09-20 15:21:48 +00:00
|
|
|
Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) );
|
2020-08-21 14:56:08 +00:00
|
|
|
descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) );
|
2020-08-07 17:27:13 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
Elf_Xword max_name_size =
|
2022-01-29 15:03:37 +00:00
|
|
|
( notes->*F_get_size )() - note_start_positions[index];
|
2020-08-21 14:56:08 +00:00
|
|
|
if ( namesz < 1 || namesz > max_name_size ||
|
|
|
|
(Elf_Xword)namesz + descSize > max_name_size ) {
|
2015-02-24 22:57:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-30 16:06:26 +00:00
|
|
|
name.assign( pData + 3 * (size_t)align, namesz - 1 );
|
2012-11-27 09:45:28 +00:00
|
|
|
if ( 0 == descSize ) {
|
2021-08-26 09:52:23 +00:00
|
|
|
desc = nullptr;
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-10-30 16:06:26 +00:00
|
|
|
desc = const_cast<char*>( pData + 3 * (size_t)align +
|
|
|
|
( ( namesz + align - 1 ) / align ) *
|
|
|
|
(size_t)align );
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-11-27 09:45:28 +00:00
|
|
|
void add_note( Elf_Word type,
|
|
|
|
const std::string& name,
|
|
|
|
const void* desc,
|
|
|
|
Elf_Word descSize )
|
|
|
|
{
|
|
|
|
const endianess_convertor& convertor = elf_file.get_convertor();
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
int align = sizeof( Elf_Word );
|
|
|
|
Elf_Word nameLen = (Elf_Word)name.size() + 1;
|
|
|
|
Elf_Word nameLenConv = convertor( nameLen );
|
2016-07-10 08:22:53 +00:00
|
|
|
std::string buffer( reinterpret_cast<char*>( &nameLenConv ), align );
|
2020-08-21 14:56:08 +00:00
|
|
|
Elf_Word descSizeConv = convertor( descSize );
|
2020-10-30 16:06:26 +00:00
|
|
|
|
2016-07-10 08:22:53 +00:00
|
|
|
buffer.append( reinterpret_cast<char*>( &descSizeConv ), align );
|
2012-11-27 09:45:28 +00:00
|
|
|
type = convertor( type );
|
2016-07-10 08:22:53 +00:00
|
|
|
buffer.append( reinterpret_cast<char*>( &type ), align );
|
2012-11-27 09:45:28 +00:00
|
|
|
buffer.append( name );
|
|
|
|
buffer.append( 1, '\x00' );
|
|
|
|
const char pad[] = { '\0', '\0', '\0', '\0' };
|
2016-07-10 08:22:53 +00:00
|
|
|
if ( nameLen % align != 0 ) {
|
2020-10-30 16:06:26 +00:00
|
|
|
buffer.append( pad, (size_t)align - nameLen % align );
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
2021-08-26 09:52:23 +00:00
|
|
|
if ( desc != nullptr && descSize != 0 ) {
|
2012-11-27 09:45:28 +00:00
|
|
|
buffer.append( reinterpret_cast<const char*>( desc ), descSize );
|
2016-07-10 08:22:53 +00:00
|
|
|
if ( descSize % align != 0 ) {
|
2020-10-30 16:06:26 +00:00
|
|
|
buffer.append( pad, (size_t)align - descSize % align );
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:03:37 +00:00
|
|
|
note_start_positions.emplace_back( ( notes->*F_get_size )() );
|
|
|
|
notes->append_data( buffer );
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-11-27 09:45:28 +00:00
|
|
|
void process_section()
|
|
|
|
{
|
|
|
|
const endianess_convertor& convertor = elf_file.get_convertor();
|
2022-01-29 15:03:37 +00:00
|
|
|
const char* data = notes->get_data();
|
|
|
|
Elf_Xword size = ( notes->*F_get_size )();
|
2020-08-21 14:56:08 +00:00
|
|
|
Elf_Xword current = 0;
|
2012-11-27 09:45:28 +00:00
|
|
|
|
|
|
|
note_start_positions.clear();
|
|
|
|
|
|
|
|
// Is it empty?
|
2021-08-26 09:52:23 +00:00
|
|
|
if ( nullptr == data || 0 == size ) {
|
2012-11-27 09:45:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-07 17:27:13 +00:00
|
|
|
Elf_Word align = sizeof( Elf_Word );
|
2020-08-21 14:56:08 +00:00
|
|
|
while ( current + (Elf_Xword)3 * align <= size ) {
|
2021-08-16 19:32:48 +00:00
|
|
|
note_start_positions.emplace_back( current );
|
2020-08-21 14:56:08 +00:00
|
|
|
Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) );
|
2012-11-27 09:45:28 +00:00
|
|
|
Elf_Word descsz = convertor(
|
2020-08-21 14:56:08 +00:00
|
|
|
*(const Elf_Word*)( data + current + sizeof( namesz ) ) );
|
2012-11-27 09:45:28 +00:00
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
current += (Elf_Xword)3 * sizeof( Elf_Word ) +
|
2020-08-07 17:27:13 +00:00
|
|
|
( ( namesz + align - 1 ) / align ) * (Elf_Xword)align +
|
|
|
|
( ( descsz + align - 1 ) / align ) * (Elf_Xword)align;
|
2012-11-27 09:45:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:56:08 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-11-27 09:45:28 +00:00
|
|
|
private:
|
|
|
|
const elfio& elf_file;
|
2022-01-29 15:03:37 +00:00
|
|
|
S* notes;
|
2012-11-27 09:45:28 +00:00
|
|
|
std::vector<Elf_Xword> note_start_positions;
|
|
|
|
};
|
|
|
|
|
2022-01-29 08:48:04 +00:00
|
|
|
using note_section_accessor =
|
|
|
|
note_section_accessor_template<section, §ion::get_size>;
|
2020-08-21 14:56:08 +00:00
|
|
|
using const_note_section_accessor =
|
2022-01-29 08:48:04 +00:00
|
|
|
note_section_accessor_template<const section, §ion::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>;
|
2017-09-24 11:13:06 +00:00
|
|
|
|
2012-11-27 09:45:28 +00:00
|
|
|
} // namespace ELFIO
|
|
|
|
|
|
|
|
#endif // ELFIO_NOTE_HPP
|