Headers dump is implemented by using tables

This commit is contained in:
Serge Lamikhov-Center 2012-08-18 08:50:03 +03:00
parent e04476791a
commit cb56843e26

View File

@ -25,9 +25,43 @@ THE SOFTWARE.
#include <string> #include <string>
#include <ostream> #include <ostream>
#include <sstream>
#include <elfio.hpp> #include <elfio.hpp>
namespace ELFIO { namespace ELFIO {
static struct class_table_t {
const char key;
const char* str;
} class_table [] =
{
{ ELFCLASS32, "ELF32" },
{ ELFCLASS64, "ELF64" },
};
static struct endian_table_t {
const char key;
const char* str;
} endian_table [] =
{
{ ELFDATANONE, "None" },
{ ELFDATA2LSB, "Little endian" },
{ ELFDATA2MSB, "Big endian" },
};
static struct version_table_t {
const char key;
const char* str;
} version_table [] =
{
{ EV_NONE, "None" },
{ EV_CURRENT, "Current" },
};
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
class dump class dump
{ {
@ -36,64 +70,65 @@ class dump
static void static void
header( std::ostream& out, elfio& reader ) header( std::ostream& out, elfio& reader )
{ {
out << "ELF Header\n" << std::endl; out << "ELF Header" << std::endl << std::endl
out << " Class: " << " Class: " << str_class( reader.get_class() ) << std::endl
<< str_elf_class( reader.get_class() ) << " Encoding: " << str_endian( reader.get_encoding() ) << std::endl
<< " (" << (int)reader.get_class() << ")" << " ELFVersion: " << str_version( reader.get_elf_version() )
<< std::endl; << std::endl
out << " Encoding: " << " Type: " << std::hex << reader.get_type() << std::endl
<< ( ( ELFDATA2LSB == reader.get_encoding() ) ? "Little endian" : "" ) << " Machine: " << std::hex << reader.get_machine() << std::endl
<< ( ( ELFDATA2MSB == reader.get_encoding() ) ? "Big endian" : "" ) << " Version: " << std::hex << reader.get_version() << std::endl
<< ( ( ( ELFDATA2LSB != reader.get_encoding() ) && << " Entry: " << std::hex << reader.get_entry() << std::endl
( ELFDATA2MSB != reader.get_encoding() ) ) ? "Unknown" : "" ) << " Flags: " << std::hex << reader.get_flags() << std::endl;
<< std::endl;
out << " ELFVersion: "
<< ( ( EV_CURRENT == reader.get_elf_version() ) ? "Current" : "Unknown" )
<< "(" << (int)reader.get_elf_version() << ")"
<< std::endl;
out << " Type: " << std::hex << reader.get_type() << std::endl;
out << " Machine: " << std::hex << reader.get_machine() << std::endl;
out << " Version: " << std::hex << reader.get_version() << std::endl;
out << " Entry: " << std::hex << reader.get_entry() << std::endl;
out << " Flags: " << std::hex << reader.get_flags() << std::endl;
} }
private:
struct convert template< typename T, typename K >
{ std::string
Elf_Word type; static
const char* str; find_value_in_table( const T& table, const K& key )
};
convert converts[] =
{
{ ELFCLASS32, "ELF32" },
{ ELFCLASS64, "ELF64" },
};
//------------------------------------------------------------------------------
template<class T> static std::string
str_section_type( Elf_Word type )
{
}
static std::string
str_elf_class( Elf_Word type )
{ {
std::string res = "UNKNOWN"; std::string res = "UNKNOWN";
for ( unsigned int i = 0; i < sizeof( converts )/sizeof( convert ); ++i ) { for ( unsigned int i = 0; i < sizeof( table )/sizeof( table[0] ); ++i ) {
if ( converts[i].type == type ) { if ( table[i].key == key ) {
res = converts[i].str; res = table[i].str;
break; break;
} }
} }
return res; return res;
} }
};
} // namespace ELFIO
template< typename T, typename K >
static
std::string
format_assoc( const T& table, const K& key )
{
std::string str = find_value_in_table( table, key );
std::ostringstream oss;
oss << str << " (" << key << ")";
return oss.str();
}
#define STR_FUNC_TABLE( name ) \
static \
std::string \
str_##name( const char key ) \
{ \
return format_assoc( name##_table, (int)key ); \
}
STR_FUNC_TABLE( class );
STR_FUNC_TABLE( endian );
STR_FUNC_TABLE( version );
#undef STR_FUNC_TABLE
};
}; // namespace ELFIO
#endif // ELFIO_DUMP_HPP #endif // ELFIO_DUMP_HPP