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 <ostream>
#include <sstream>
#include <elfio.hpp>
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
{
@ -36,64 +70,65 @@ class dump
static void
header( std::ostream& out, elfio& reader )
{
out << "ELF Header\n" << std::endl;
out << " Class: "
<< str_elf_class( reader.get_class() )
<< " (" << (int)reader.get_class() << ")"
<< std::endl;
out << " Encoding: "
<< ( ( ELFDATA2LSB == reader.get_encoding() ) ? "Little endian" : "" )
<< ( ( ELFDATA2MSB == reader.get_encoding() ) ? "Big endian" : "" )
<< ( ( ( ELFDATA2LSB != reader.get_encoding() ) &&
( ELFDATA2MSB != reader.get_encoding() ) ) ? "Unknown" : "" )
<< 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;
out << "ELF Header" << std::endl << std::endl
<< " Class: " << str_class( reader.get_class() ) << std::endl
<< " Encoding: " << str_endian( reader.get_encoding() ) << std::endl
<< " ELFVersion: " << str_version( reader.get_elf_version() )
<< std::endl
<< " Type: " << std::hex << reader.get_type() << std::endl
<< " Machine: " << std::hex << reader.get_machine() << std::endl
<< " Version: " << std::hex << reader.get_version() << std::endl
<< " Entry: " << std::hex << reader.get_entry() << std::endl
<< " Flags: " << std::hex << reader.get_flags() << std::endl;
}
struct convert
{
Elf_Word type;
const char* str;
};
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 )
private:
template< typename T, typename K >
std::string
static
find_value_in_table( const T& table, const K& key )
{
std::string res = "UNKNOWN";
for ( unsigned int i = 0; i < sizeof( converts )/sizeof( convert ); ++i ) {
if ( converts[i].type == type ) {
res = converts[i].str;
for ( unsigned int i = 0; i < sizeof( table )/sizeof( table[0] ); ++i ) {
if ( table[i].key == key ) {
res = table[i].str;
break;
}
}
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