Skip to content

Commit

Permalink
Add AI generated comments for classes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1 committed Jan 3, 2025
1 parent 4c829d4 commit cba0a73
Show file tree
Hide file tree
Showing 17 changed files with 1,371 additions and 250 deletions.
268 changes: 195 additions & 73 deletions elfio/elf_types.hpp

Large diffs are not rendered by default.

183 changes: 164 additions & 19 deletions elfio/elfio.hpp

Large diffs are not rendered by default.

95 changes: 63 additions & 32 deletions elfio/elfio_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,86 @@ THE SOFTWARE.
namespace ELFIO {

//------------------------------------------------------------------------------
// Template class for accessing array sections
template <class S, typename T> class array_section_accessor_template
{
public:
//------------------------------------------------------------------------------
// Constructor
explicit array_section_accessor_template( const elfio& elf_file,
S* section )
: elf_file( elf_file ), array_section( section )
{
}
S* section );

//------------------------------------------------------------------------------
Elf_Xword get_entries_num() const
{
Elf_Xword entry_size = sizeof( T );
return array_section->get_size() / entry_size;
}
// Returns the number of entries in the array section
Elf_Xword get_entries_num() const;

//------------------------------------------------------------------------------
bool get_entry( Elf_Xword index, Elf64_Addr& address ) const
{
if ( index >= get_entries_num() ) { // Is index valid
return false;
}

const endianness_convertor& convertor = elf_file.get_convertor();

const T temp = *reinterpret_cast<const T*>( array_section->get_data() +
index * sizeof( T ) );
address = convertor( temp );

return true;
}
// Retrieves an entry from the array section
bool get_entry( Elf_Xword index, Elf64_Addr& address ) const;

//------------------------------------------------------------------------------
void add_entry( Elf64_Addr address )
{
const endianness_convertor& convertor = elf_file.get_convertor();

T temp = convertor( (T)address );
array_section->append_data( reinterpret_cast<char*>( &temp ),
sizeof( temp ) );
}
// Adds an entry to the array section
void add_entry( Elf64_Addr address );

private:
//------------------------------------------------------------------------------
// Reference to the ELF file
const elfio& elf_file;
S* array_section;
//------------------------------------------------------------------------------
// Pointer to the array section
S* array_section;
};

//------------------------------------------------------------------------------
// Constructor
template <class S, typename T>
array_section_accessor_template<S, T>::array_section_accessor_template(
const elfio& elf_file, S* section )
: elf_file( elf_file ), array_section( section )
{
}

//------------------------------------------------------------------------------
// Returns the number of entries in the array section
template <class S, typename T>
Elf_Xword array_section_accessor_template<S, T>::get_entries_num() const
{
Elf_Xword entry_size = sizeof( T );
return array_section->get_size() / entry_size;
}

//------------------------------------------------------------------------------
// Retrieves an entry from the array section
template <class S, typename T>
bool array_section_accessor_template<S, T>::get_entry(
Elf_Xword index, Elf64_Addr& address ) const
{
if ( index >= get_entries_num() ) { // Is index valid
return false;
}

const endianness_convertor& convertor = elf_file.get_convertor();

const T temp = *reinterpret_cast<const T*>( array_section->get_data() +
index * sizeof( T ) );
address = convertor( temp );

return true;
}

//------------------------------------------------------------------------------
// Adds an entry to the array section
template <class S, typename T>
void array_section_accessor_template<S, T>::add_entry( Elf64_Addr address )
{
const endianness_convertor& convertor = elf_file.get_convertor();

T temp = convertor( (T)address );
array_section->append_data( reinterpret_cast<char*>( &temp ),
sizeof( temp ) );
}

// Type aliases for array section accessors
template <typename T = Elf32_Word>
using array_section_accessor = array_section_accessor_template<section, T>;
template <typename T = Elf32_Word>
Expand Down
17 changes: 17 additions & 0 deletions elfio/elfio_dump.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ static const struct note_tag_t
static const ELFIO::Elf_Xword MAX_DATA_ENTRIES = 64;

//------------------------------------------------------------------------------
// Class representing the ELF dump functionality
class dump
{
#define DUMP_DEC_FORMAT( width ) \
Expand All @@ -685,6 +686,7 @@ class dump

public:
//------------------------------------------------------------------------------
// Dumps the ELF header information
static void header( std::ostream& out, const elfio& reader )
{
if ( !reader.get_header_size() ) {
Expand Down Expand Up @@ -713,6 +715,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the section headers information
static void section_headers( std::ostream& out, const elfio& reader )
{
Elf_Half n = reader.sections.size();
Expand Down Expand Up @@ -748,6 +751,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps a single section header information
static void section_header( std::ostream& out,
Elf_Half no,
const section* sec,
Expand Down Expand Up @@ -792,6 +796,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the segment headers information
static void segment_headers( std::ostream& out, const elfio& reader )
{
Elf_Half n = reader.segments.size();
Expand Down Expand Up @@ -836,6 +841,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps a single segment header information
static void segment_header( std::ostream& out,
Elf_Half no,
const segment* seg,
Expand Down Expand Up @@ -874,6 +880,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the symbol tables information
static void symbol_tables( std::ostream& out, const elfio& reader )
{
for ( const auto& sec : reader.sections ) { // For all sections
Expand Down Expand Up @@ -918,6 +925,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps a single symbol table entry information
static void symbol_table( std::ostream& out,
Elf_Xword no,
const std::string& name,
Expand Down Expand Up @@ -954,6 +962,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the notes information
static void notes( std::ostream& out, const elfio& reader )
{
for ( const auto& sec : reader.sections ) { // For all sections
Expand Down Expand Up @@ -1018,6 +1027,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps a single note information
static void note( std::ostream& out,
int no,
Elf_Word type,
Expand Down Expand Up @@ -1065,6 +1075,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the module information
static void modinfo( std::ostream& out, const elfio& reader )
{
for ( const auto& sec : reader.sections ) { // For all sections
Expand All @@ -1088,6 +1099,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the dynamic tags information
static void dynamic_tags( std::ostream& out, const elfio& reader )
{
for ( const auto& sec : reader.sections ) { // For all sections
Expand Down Expand Up @@ -1118,6 +1130,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps a single dynamic tag information
static void dynamic_tag( std::ostream& out,
Elf_Xword no,
Elf_Xword tag,
Expand All @@ -1137,6 +1150,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the section data
static void section_data( std::ostream& out, const section* sec )
{
std::ios_base::fmtflags original_flags = out.flags();
Expand Down Expand Up @@ -1169,6 +1183,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps all sections data
static void section_datas( std::ostream& out, const elfio& reader )
{
Elf_Half n = reader.sections.size();
Expand All @@ -1191,6 +1206,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps the segment data
static void
segment_data( std::ostream& out, Elf_Half no, const segment* seg )
{
Expand Down Expand Up @@ -1224,6 +1240,7 @@ class dump
}

//------------------------------------------------------------------------------
// Dumps all segments data
static void segment_datas( std::ostream& out, const elfio& reader )
{
Elf_Half n = reader.segments.size();
Expand Down
19 changes: 15 additions & 4 deletions elfio/elfio_dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ THE SOFTWARE.
namespace ELFIO {

//------------------------------------------------------------------------------
// Template class for accessing dynamic sections
template <class S> class dynamic_section_accessor_template
{
public:
//------------------------------------------------------------------------------
// Constructor
explicit dynamic_section_accessor_template( const elfio& elf_file,
S* section )
: elf_file( elf_file ), dynamic_section( section ), entries_num( 0 )
{
}

//------------------------------------------------------------------------------
// Returns the number of entries in the dynamic section
Elf_Xword get_entries_num() const
{
size_t needed_entry_size = -1;
Expand Down Expand Up @@ -70,6 +73,7 @@ template <class S> class dynamic_section_accessor_template
}

//------------------------------------------------------------------------------
// Retrieves an entry from the dynamic section
bool get_entry( Elf_Xword index,
Elf_Xword& tag,
Elf_Xword& value,
Expand Down Expand Up @@ -106,6 +110,7 @@ template <class S> class dynamic_section_accessor_template
}

//------------------------------------------------------------------------------
// Adds an entry to the dynamic section
void add_entry( Elf_Xword tag, Elf_Xword value )
{
if ( elf_file.get_class() == ELFCLASS32 ) {
Expand All @@ -117,6 +122,7 @@ template <class S> class dynamic_section_accessor_template
}

//------------------------------------------------------------------------------
// Adds an entry with a string value to the dynamic section
void add_entry( Elf_Xword tag, const std::string& str )
{
string_section_accessor strsec(
Expand All @@ -125,15 +131,16 @@ template <class S> class dynamic_section_accessor_template
add_entry( tag, value );
}

//------------------------------------------------------------------------------
private:
//------------------------------------------------------------------------------
// Returns the index of the string table
Elf_Half get_string_table_index() const
{
return (Elf_Half)dynamic_section->get_link();
}

//------------------------------------------------------------------------------
// Retrieves a generic entry from the dynamic section
template <class T>
void generic_get_entry_dyn( Elf_Xword index,
Elf_Xword& tag,
Expand Down Expand Up @@ -200,6 +207,7 @@ template <class S> class dynamic_section_accessor_template
}

//------------------------------------------------------------------------------
// Adds a generic entry to the dynamic section
template <class T>
void generic_add_entry_dyn( Elf_Xword tag, Elf_Xword value )
{
Expand Down Expand Up @@ -258,13 +266,16 @@ template <class S> class dynamic_section_accessor_template
sizeof( entry ) );
}

//------------------------------------------------------------------------------
private:
const elfio& elf_file;
S* dynamic_section;
// Reference to the ELF file
const elfio& elf_file;
// Pointer to the dynamic section
S* dynamic_section;
// Number of entries in the dynamic section
mutable Elf_Xword entries_num;
};

// Type aliases for dynamic section accessors
using dynamic_section_accessor = dynamic_section_accessor_template<section>;
using const_dynamic_section_accessor =
dynamic_section_accessor_template<const section>;
Expand Down
Loading

0 comments on commit cba0a73

Please sign in to comment.