ElfReader: read 64-bit ELF binaries

ElfReader now detects whether a 32- or 64-bit ELF is opened, and uses
the corresponding elf.h data structures.  Internally maps 32-bit ELF
structures onto 64-bit structures to use common processing code.

Change-Id: Ib42a4b21701aeadac7568e369a80c08f2807694e
This commit is contained in:
Horst Schirmeier
2018-07-14 16:11:44 +02:00
parent 3d292cb217
commit 9bd58cb294
5 changed files with 580 additions and 386 deletions

View File

@ -13,8 +13,6 @@ set(SRCS
Disassembler.cc
DwarfReader.cc
DwarfReader.hpp
elfinfo/elfinfo.cc
elfinfo/elfinfo.h
gzstream/gzstream.C
gzstream/gzstream.h
Logger.cc

View File

@ -26,7 +26,7 @@ std::ostream& operator<< (std::ostream &out, const ElfSymbol &symbol) {
ElfReader::ElfReader() : m_log("FAIL*Elfinfo", false) {
ElfReader::ElfReader() : m_log("ElfReader", false) {
// try to open elf file from environment variable
char * elfpath = getenv("FAIL_ELF_PATH");
if (elfpath == NULL) {
@ -42,7 +42,7 @@ ElfReader::ElfReader(const char* path) : m_log("FAIL*Elfinfo", false) {
void ElfReader::setup(const char* path) {
// Try to open the ELF file
FILE * fp = fopen(path, "r");
FILE *fp = fopen(path, "rb");
if (!fp) {
m_log << "Error: Could not open " << path << std::endl;
return;
@ -51,70 +51,61 @@ void ElfReader::setup(const char* path) {
m_filename = std::string(path);
// Evaluate headers
Elf32_Ehdr ehdr;
Elf32_Shdr sec_hdr;
int num_hdrs,i;
fseek(fp,(off_t)0,SEEK_SET);
read_ELF_file_header(fp, &ehdr);
num_hdrs=ehdr.e_shnum;
m_log << "Evaluating ELF File: " << path << std::endl;
Elf64_Ehdr ehdr;
Elf64_Shdr sec_hdr;
int num_hdrs;
if (!read_ELF_file_header(fp, &ehdr)) {
m_log << "Error: " << path << " is not an ELF file" << std::endl;
return;
}
num_hdrs = ehdr.e_shnum;
m_log << "Evaluating "
<< (m_elfclass == ELFCLASS32 ? "32-bit" : "64-bit")
<< " ELF file: " << path << std::endl;
// Parse symbol table and generate internal map
for (i=0;i<num_hdrs;i++)
{
if (read_ELF_section_header(i,&sec_hdr,fp)==-1)
{
m_log << "Wrong Section to read" << std::endl;
}
else
{
if ((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
{
process_symboltable(i,fp);
}
else
{
continue;
}
for (int i = 0; i < num_hdrs; ++i) {
if (!read_ELF_section_header(fp, &ehdr, i, &sec_hdr)) {
m_log << "Wrong section to read" << std::endl;
continue;
} else if (sec_hdr.sh_type != SHT_SYMTAB && sec_hdr.sh_type != SHT_DYNSYM) {
continue;
}
process_symboltable(fp, &ehdr, i);
}
// Parse section information
if (read_ELF_section_header(ehdr.e_shstrndx,&sec_hdr,fp)==-1)
{
if (!read_ELF_section_header(fp, &ehdr, ehdr.e_shstrndx, &sec_hdr)) {
m_log << "Error: reading section string table sect_num = " << ehdr.e_shstrndx << std::endl;
return;
}
char* buff=(char*)malloc(sec_hdr.sh_size);
if (!buff)
{
m_log << "Malloc failed to allocate buffer for shstrtab" << std::endl;
exit(0);
char *buff = (char*)malloc(sec_hdr.sh_size);
if (!buff) {
m_log << "Error: malloc failed to allocate buffer for shstrtab" << std::endl;
return;
}
// seek to the offset in the file,
fseek(fp,(off_t)sec_hdr.sh_offset,SEEK_SET);
fread(buff,sec_hdr.sh_size,1,fp);
m_log << "Total number of sections: " << num_hdrs << std::endl;
for (i=0;i<num_hdrs;i++)
{
if (read_ELF_section_header(i,&sec_hdr,fp)==-1)
{
m_log << "Wrong Section to read\n" << std::endl;
}
else
{
process_section(&sec_hdr, buff);
}
fseek(fp, (off_t)sec_hdr.sh_offset, SEEK_SET);
if (fread(buff, sec_hdr.sh_size, 1, fp) != 1) {
printf("Couldn't read complete shstrtab\n");
exit(0);
}
m_log << "Total number of sections: " << num_hdrs << std::endl;
for (int i = 0; i < num_hdrs; ++i) {
if (!read_ELF_section_header(fp, &ehdr, i, &sec_hdr)) {
printf("Error: wrong Section to read\n");
} else {
process_section(&sec_hdr, buff);
}
}
free(buff);
fclose(fp);
// printDemangled();
// printSections();
}
int ElfReader::process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff) {
int ElfReader::process_section(Elf64_Shdr *sect_hdr, char *sect_name_buff) {
// Add section name, start address and size to list
int idx=sect_hdr->sh_name;
// m_sections_map.push_back( sect_hdr->sh_addr, sect_hdr->sh_size, sect_name_buff+idx );
@ -123,54 +114,169 @@ int ElfReader::process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff) {
return 0;
}
int ElfReader::process_symboltable(int sect_num, FILE* fp) {
static void Elf32to64_Sym(Elf32_Sym const *src, Elf64_Sym *dest)
{
dest->st_name = src->st_name;
dest->st_value = src->st_value;
dest->st_size = src->st_size;
dest->st_info = src->st_info;
dest->st_other = src->st_other;
dest->st_shndx = src->st_shndx;
}
Elf32_Shdr sect_hdr;
Elf32_Sym mysym;
char *name_buf=NULL;
int num_sym,link,i,idx;
bool ElfReader::process_symboltable(FILE *fp, Elf64_Ehdr const *ehdr, int sect_num) {
Elf64_Shdr sect_hdr;
Elf32_Sym mysym32;
Elf64_Sym mysym;
char *name_buf = 0;
int num_sym, link, idx;
off_t sym_data_offset;
int sym_data_size;
if (read_ELF_section_header(sect_num,&sect_hdr,fp)==-1)
{
return -1;
if (!read_ELF_section_header(fp, ehdr, sect_num, &sect_hdr)) {
return false;
}
// we have to check to which strtab it is linked
link=sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym);
link = sect_hdr.sh_link;
sym_data_offset = sect_hdr.sh_offset;
sym_data_size = sect_hdr.sh_size;
num_sym = sym_data_size /
(m_elfclass == ELFCLASS32 ? sizeof(Elf32_Sym) : sizeof(Elf64_Sym));
// read the coresponding strtab
if (read_ELF_section_header(link,&sect_hdr,fp)==-1)
{
return -1;
// read the corresponding strtab
if (!read_ELF_section_header(fp, ehdr, link, &sect_hdr)) {
return false;
}
// get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size);
if (!name_buf)
return -1;
name_buf = (char *) malloc(sect_hdr.sh_size);
if (!name_buf) {
return false;
}
// get the offset of strtab in file and seek to it
fseek(fp,sect_hdr.sh_offset,SEEK_SET);
fseek(fp, sect_hdr.sh_offset, SEEK_SET);
// read all data from the section to the buffer.
fread(name_buf,sect_hdr.sh_size,1,fp);
if (fread(name_buf, sect_hdr.sh_size, 1, fp) != 1) {
return false;
}
// so we have the namebuf now seek to symtab data
fseek(fp,sym_data_offset,SEEK_SET);
fseek(fp, sym_data_offset, SEEK_SET);
for (i=0;i<num_sym;i++)
{
for (int i = 0; i < num_sym; ++i) {
int type = ELFCLASSNONE;
if (m_elfclass == ELFCLASS32) {
if (fread(&mysym32, sizeof(mysym32), 1, fp) != 1) {
return false;
}
Elf32to64_Sym(&mysym32, &mysym);
type = ELF32_ST_TYPE(mysym32.st_info);
} else if (m_elfclass == ELFCLASS64) {
if (fread(&mysym, sizeof(mysym), 1, fp) != 1) {
return false;
}
type = ELF64_ST_TYPE(mysym.st_info);
}
idx = mysym.st_name;
fread(&mysym,sizeof(Elf32_Sym),1,fp);
idx=mysym.st_name;
int type = ELF32_ST_TYPE(mysym.st_info);
if ((type != STT_SECTION) && (type != STT_FILE)) {
m_symboltable.push_back( ElfSymbol(name_buf+idx, mysym.st_value, mysym.st_size, ElfSymbol::SYMBOL,
type) );
if (type != STT_SECTION && type != STT_FILE) {
m_symboltable.push_back(
ElfSymbol(name_buf + idx, mysym.st_value, mysym.st_size, ElfSymbol::SYMBOL, type));
}
}
free (name_buf);
return 0;
free(name_buf);
return true;
}
static void Elf32to64_Ehdr(Elf32_Ehdr const *src, Elf64_Ehdr *dest)
{
memcpy(dest->e_ident, src->e_ident, sizeof(dest->e_ident));
dest->e_type = src->e_type;
dest->e_machine = src->e_machine;
dest->e_version = src->e_version;
dest->e_entry = src->e_entry;
dest->e_phoff = src->e_phoff;
dest->e_shoff = src->e_shoff;
dest->e_flags = src->e_flags;
dest->e_ehsize = src->e_ehsize;
dest->e_phentsize = src->e_phentsize;
dest->e_phnum = src->e_phnum;
dest->e_shentsize = src->e_shentsize;
dest->e_shnum = src->e_shnum;
dest->e_shstrndx = src->e_shstrndx;
}
bool ElfReader::read_ELF_file_header(FILE *fp, Elf64_Ehdr *filehdr)
{
Elf32_Ehdr filehdr32;
size_t ret;
rewind(fp);
ret = fread(&filehdr32, sizeof(filehdr32), 1, fp);
if (ret != 1 ||
strncmp((const char *)filehdr32.e_ident, "\177ELF", 4) != 0) {
return false;
}
m_elfclass = filehdr32.e_ident[EI_CLASS];
if (m_elfclass == ELFCLASS32) {
Elf32to64_Ehdr(&filehdr32, filehdr);
} else if (m_elfclass == ELFCLASS64) {
rewind(fp);
ret = fread(filehdr, sizeof(*filehdr), 1, fp);
if (ret != 1) {
return false;
}
} else {
return false;
}
return true;
}
static void Elf32to64_Shdr(Elf32_Shdr const *src, Elf64_Shdr *dest)
{
dest->sh_name = src->sh_name;
dest->sh_type = src->sh_type;
dest->sh_flags = src->sh_flags;
dest->sh_addr = src->sh_addr;
dest->sh_offset = src->sh_offset;
dest->sh_size = src->sh_size;
dest->sh_link = src->sh_link;
dest->sh_info = src->sh_info;
dest->sh_addralign = src->sh_addralign;
dest->sh_entsize = src->sh_entsize;
}
bool ElfReader::read_ELF_section_header(FILE *fp, Elf64_Ehdr const *filehdr, int section, Elf64_Shdr *sect_hdr)
{
int numsect = filehdr->e_shnum;
off_t sect_hdr_off;
Elf32_Shdr sect_hdr32;
if (numsect < section || section < 0) {
return false;
}
sect_hdr_off = filehdr->e_shoff;
sect_hdr_off += filehdr->e_shentsize * section;
fseek(fp, (off_t) sect_hdr_off, SEEK_SET);
if (m_elfclass == ELFCLASS32) {
int ret = fread(&sect_hdr32, sizeof(sect_hdr32), 1, fp);
if (ret != 1) {
return false;
}
Elf32to64_Shdr(&sect_hdr32, sect_hdr);
} else if (m_elfclass == ELFCLASS64) {
int ret = fread(sect_hdr, sizeof(*sect_hdr), 1, fp);
if (ret != 1) {
return false;
}
} else {
return false;
}
return true;
}
const ElfSymbol& ElfReader::getSymbol(guest_address_t address) {

View File

@ -3,11 +3,11 @@
#include <string>
#include <ostream>
#include "sal/SALConfig.hpp" // for ADDR_INV
#include "Logger.hpp"
#include "elfinfo/elfinfo.h"
#include <vector>
#include <map>
#include <elf.h>
#include "sal/SALConfig.hpp" // for ADDR_INV
#include "Logger.hpp"
#include "Demangler.hpp"
namespace fail {
@ -28,19 +28,19 @@ class ElfSymbol {
ElfSymbol(const std::string & name = ELF::NOTFOUND, guest_address_t addr = ADDR_INV,
size_t size = -1, int type = UNDEF, int symbol_type = 0)
: name(name), address(addr), size(size), m_type(type), m_symbol_type(symbol_type) {};
: name(name), address(addr), size(size), m_type(type), m_symbol_type(symbol_type) {}
const std::string& getName() const { return name; };
std::string getDemangledName() const { return Demangler::demangle(name); };
guest_address_t getAddress() const { return address; };
size_t getSize() const { return size; };
guest_address_t getStart() const { return getAddress(); }; // alias
guest_address_t getEnd() const { return address + size; };
int getSymbolType() const { return m_symbol_type; };
const std::string& getName() const { return name; }
std::string getDemangledName() const { return Demangler::demangle(name); }
guest_address_t getAddress() const { return address; }
size_t getSize() const { return size; }
guest_address_t getStart() const { return getAddress(); } // alias
guest_address_t getEnd() const { return address + size; }
int getSymbolType() const { return m_symbol_type; }
bool isSection() const { return m_type == SECTION; };
bool isSymbol() const { return m_type == SYMBOL; };
bool isValid() const { return name != ELF::NOTFOUND; };
bool isSection() const { return m_type == SECTION; }
bool isSymbol() const { return m_type == SYMBOL; }
bool isValid() const { return name != ELF::NOTFOUND; }
bool operator==(const std::string& rhs) const {
if (rhs == name) {
@ -157,10 +157,16 @@ public:
private:
Logger m_log;
std::string m_filename;
int m_elfclass;
void setup(const char*);
int process_symboltable(int sect_num, FILE* fp);
int process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff);
bool process_symboltable(FILE *fp, Elf64_Ehdr const *ehdr, int sect_num);
int process_section(Elf64_Shdr *sect_hdr, char *sect_name_buff);
// Returns true if it finds a valid ELF header. Stores ELFCLASS32 or 64 in m_elfclass.
bool read_ELF_file_header(FILE *fp, Elf64_Ehdr *ehdr);
// Returns true if it finds a valid ELF section header.
bool read_ELF_section_header(FILE *fp, Elf64_Ehdr const *filehdr, int sect_num, Elf64_Shdr *sect_hdr);
container_t m_symboltable;
container_t m_sectiontable;

View File

@ -28,318 +28,400 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <string.h>
#include <elf.h>
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr)
static void Elf32to64_Ehdr(Elf32_Ehdr const *src, Elf64_Ehdr *dest)
{
//rewind(fp);
fread(filehdr,sizeof(Elf32_Ehdr),1,fp);
memcpy(dest->e_ident, src->e_ident, sizeof(dest->e_ident));
dest->e_type = src->e_type;
dest->e_machine = src->e_machine;
dest->e_version = src->e_version;
dest->e_entry = src->e_entry;
dest->e_phoff = src->e_phoff;
dest->e_shoff = src->e_shoff;
dest->e_flags = src->e_flags;
dest->e_ehsize = src->e_ehsize;
dest->e_phentsize = src->e_phentsize;
dest->e_phnum = src->e_phnum;
dest->e_shentsize = src->e_shentsize;
dest->e_shnum = src->e_shnum;
dest->e_shstrndx = src->e_shstrndx;
}
int is_ELF(Elf32_Ehdr *hdr)
bool read_ELF_file_header(FILE *fp, Elf64_Ehdr *filehdr, int *elfclass)
{
if(strncmp(reinterpret_cast<const char*>(hdr->e_ident),"\177ELF",4)==0)
return 1;
else
return -1;
Elf32_Ehdr filehdr32;
size_t ret;
rewind(fp);
ret = fread(&filehdr32, sizeof(filehdr32), 1, fp);
if (ret != 1 ||
strncmp((const char *)filehdr32.e_ident, "\177ELF", 4) != 0) {
return false;
}
*elfclass = filehdr32.e_ident[EI_CLASS];
if (*elfclass == ELFCLASS32) {
Elf32to64_Ehdr(&filehdr32, filehdr);
} else if (*elfclass == ELFCLASS64) {
rewind(fp);
ret = fread(filehdr, sizeof(*filehdr), 1, fp);
if (ret != 1) {
return false;
}
} else {
return false;
}
return true;
}
int read_ELF_section_header(int sect_num,Elf32_Shdr *sect_hdr,FILE *fp)
static void Elf32to64_Shdr(Elf32_Shdr const *src, Elf64_Shdr *dest)
{
int numsect;
Elf32_Ehdr elfhdr;
off_t sect_hdr_off;
fseek(fp,(off_t)0,SEEK_SET);
read_ELF_file_header(fp,&elfhdr);
numsect=elfhdr.e_shnum;
if ((numsect<sect_num)||(sect_num<0))
return -1;
sect_hdr_off=elfhdr.e_shoff;
//rewind(fp);
sect_hdr_off+=elfhdr.e_shentsize*sect_num;
fseek(fp,(off_t)sect_hdr_off,SEEK_SET);
fread(sect_hdr,sizeof(Elf32_Shdr),1,fp);
return 1;
dest->sh_name = src->sh_name;
dest->sh_type = src->sh_type;
dest->sh_flags = src->sh_flags;
dest->sh_addr = src->sh_addr;
dest->sh_offset = src->sh_offset;
dest->sh_size = src->sh_size;
dest->sh_link = src->sh_link;
dest->sh_info = src->sh_info;
dest->sh_addralign = src->sh_addralign;
dest->sh_entsize = src->sh_entsize;
}
void process_sect_hdr(Elf32_Shdr *sect_hdr,char *sect_name_buff)
bool read_ELF_section_header(FILE *fp, Elf64_Ehdr const *filehdr, int elfclass, int section, Elf64_Shdr *sect_hdr)
{
int idx;
idx=sect_hdr->sh_name;
printf(" %-20s (0x%x++0x%x) ",sect_name_buff+idx, sect_hdr->sh_addr, sect_hdr->sh_size);
if(sect_hdr->sh_entsize)
printf(" %c ",'*');
else
printf(" %c ",' ');
switch(sect_hdr->sh_type)
{
case 0: printf("NULL \t");
break;
case 1: printf("PROGBITS \t");
break;
case 2: printf("SYMTAB \t");
break;
case 3: printf("STRTAB \t");
break;
case 4: printf("RELOC ADN \t");
break;
case 5: printf("HASH \t");
break;
case 6: printf("SYMTAB \t");
break;
case 7: printf("NOTE \t");
break;
case 8: printf("NOBIT \t");
break;
case 9: printf("RELOC \t");
break;
case 10: printf("*reserved* \t");
break;
case 11: printf("DYNSYM \t");
break;
case 14: printf("INITARR \t");
break;
case 15: printf("FINIARR \t");
break;
case 16: printf("PREINIT \t");
break;
case 17: printf("SECTGRP \t");
break;
case 18: printf("EXT-SHNDX \t");
break;
case 19: printf("NUM-DEF-TYP \t");
break;
case 0x60000000: printf("OS SPECIFIC \t");
break;
case 0x6ffffff5: printf("GNU_ATTR \t");
break;
case 0x6ffffff6: printf("GNU_HASH \t");
break;
case 0x6ffffff7: printf("PRELNKLIBLST \t");
break;
case 0x6ffffff8: printf("CKSUM \t");
break;
case 0x6ffffffa: printf("SUN SPECIFIC \t");
break;
case 0x6ffffffb: printf("SUNCOMDAT \t");
break;
case 0x6ffffffc: printf("SUNSYMINFO \t");
break;
case 0x6ffffffd: printf("GNUVERDEF \t");
break;
case 0x6ffffffe: printf("GNUVERNEED \t");
break;
case 0x6fffffff: printf("GNUVERSYM \t");
break;
case 0x70000000: printf("LOPROC \t");
break;
case 0x7fffffff: printf("HIPROC \t");
break;
case 0x80000000: printf("APP beg \t");
break;
case 0x8fffffff: printf("APP end \t");
break;
}
printf("linked to %3d sect\n",sect_hdr->sh_link);
int numsect = filehdr->e_shnum;
off_t sect_hdr_off;
Elf32_Shdr sect_hdr32;
if (numsect < section || section < 0) {
return false;
}
sect_hdr_off = filehdr->e_shoff;
sect_hdr_off += filehdr->e_shentsize * section;
fseek(fp, (off_t) sect_hdr_off, SEEK_SET);
if (elfclass == ELFCLASS32) {
int ret = fread(&sect_hdr32, sizeof(sect_hdr32), 1, fp);
if (ret != 1) {
return false;
}
Elf32to64_Shdr(&sect_hdr32, sect_hdr);
} else if (elfclass == ELFCLASS64) {
int ret = fread(sect_hdr, sizeof(*sect_hdr), 1, fp);
if (ret != 1) {
return false;
}
} else {
return false;
}
return true;
}
#ifdef TESTPROGRAM
void process_sect_hdr(Elf64_Shdr const *sect_hdr, char const *sect_name_buff)
{
int idx;
idx = sect_hdr->sh_name;
printf(" %-20s (0x%lx++0x%lx) ", sect_name_buff + idx, sect_hdr->sh_addr, sect_hdr->sh_size);
if (sect_hdr->sh_entsize) {
printf(" %c ",'*');
} else {
printf(" %c ",' ');
}
switch (sect_hdr->sh_type) {
case 0: printf("NULL \t");
break;
case 1: printf("PROGBITS \t");
break;
case 2: printf("SYMTAB \t");
break;
case 3: printf("STRTAB \t");
break;
case 4: printf("RELOC ADN \t");
break;
case 5: printf("HASH \t");
break;
case 6: printf("SYMTAB \t");
break;
case 7: printf("NOTE \t");
break;
case 8: printf("NOBIT \t");
break;
case 9: printf("RELOC \t");
break;
case 10: printf("*reserved* \t");
break;
case 11: printf("DYNSYM \t");
break;
case 14: printf("INITARR \t");
break;
case 15: printf("FINIARR \t");
break;
case 16: printf("PREINIT \t");
break;
case 17: printf("SECTGRP \t");
break;
case 18: printf("EXT-SHNDX \t");
break;
case 19: printf("NUM-DEF-TYP \t");
break;
case 0x60000000: printf("OS SPECIFIC \t");
break;
case 0x6ffffff5: printf("GNU_ATTR \t");
break;
case 0x6ffffff6: printf("GNU_HASH \t");
break;
case 0x6ffffff7: printf("PRELNKLIBLST \t");
break;
case 0x6ffffff8: printf("CKSUM \t");
break;
case 0x6ffffffa: printf("SUN SPECIFIC \t");
break;
case 0x6ffffffb: printf("SUNCOMDAT \t");
break;
case 0x6ffffffc: printf("SUNSYMINFO \t");
break;
case 0x6ffffffd: printf("GNUVERDEF \t");
break;
case 0x6ffffffe: printf("GNUVERNEED \t");
break;
case 0x6fffffff: printf("GNUVERSYM \t");
break;
case 0x70000000: printf("LOPROC \t");
break;
case 0x7fffffff: printf("HIPROC \t");
break;
case 0x80000000: printf("APP beg \t");
break;
case 0x8fffffff: printf("APP end \t");
break;
}
printf("linked to %3d sect\n", sect_hdr->sh_link);
}
void display_sections(FILE *fp)
{
Elf32_Ehdr ehdr;
Elf32_Shdr sec_hdr;
int num_hdrs,i;
char *buff=NULL;
fseek(fp,(off_t)0,SEEK_SET);
read_ELF_file_header(fp, &ehdr);
if(is_ELF(&ehdr)==-1)
{
printf("Not an ELF file\n");
exit(0);
}
num_hdrs=ehdr.e_shnum;
if(read_ELF_section_header(ehdr.e_shstrndx,&sec_hdr,fp)==-1)
{
printf("Error: reading section string table sect_num= %d\n",ehdr.e_shstrndx);
exit(0);
}
//we read the shstrtab
// read content from the file
buff=(char*)malloc(sec_hdr.sh_size);
if (!buff)
{
printf("Malloc failed to allocate buffer for shstrtab\n");
exit(0);
}
//seek to the offset in the file,
fseek(fp,(off_t)sec_hdr.sh_offset,SEEK_SET);
fread(buff,sec_hdr.sh_size,1,fp);
printf("There are [%d] sections\n",num_hdrs);
for(i=0;i<num_hdrs;i++)
{
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
{
printf("Wrong Section to read\n");
}
else
{
printf("[section %3d] ",i);
process_sect_hdr(&sec_hdr,buff);
Elf64_Ehdr ehdr;
Elf64_Shdr sec_hdr;
int num_hdrs, elfclass;
char *buff = NULL;
}
if (!read_ELF_file_header(fp, &ehdr, &elfclass)) {
printf("Not an ELF file\n");
exit(0);
}
num_hdrs = ehdr.e_shnum;
if (!read_ELF_section_header(fp, &ehdr, elfclass, ehdr.e_shstrndx, &sec_hdr)) {
printf("Error: reading section string table sect_num= %d\n", ehdr.e_shstrndx);
exit(0);
}
//we read the shstrtab
// read content from the file
buff = (char *)malloc(sec_hdr.sh_size);
if (!buff) {
printf("Malloc failed to allocate buffer for shstrtab\n");
exit(0);
}
//seek to the offset in the file,
fseek(fp, (off_t)sec_hdr.sh_offset, SEEK_SET);
if (fread(buff, sec_hdr.sh_size, 1, fp) != 1) {
printf("Couldn't read complete shstrtab\n");
exit(0);
}
printf("There are [%d] sections\n",num_hdrs);
for (int i = 0; i < num_hdrs; ++i) {
if (!read_ELF_section_header(fp, &ehdr, elfclass, i, &sec_hdr)) {
printf("Wrong Section to read\n");
} else {
printf("[section %3d] ", i);
process_sect_hdr(&sec_hdr, buff);
}
if(buff)
free(buff);
}
free(buff);
}
int process_symtab(int sect_num,FILE *fp)
static void Elf32to64_Sym(Elf32_Sym const *src, Elf64_Sym *dest)
{
Elf32_Shdr sect_hdr;
Elf32_Sym mysym;
char *name_buf=NULL;
int num_sym,link,i,idx;
off_t sym_data_offset;
int sym_data_size;
if(read_ELF_section_header(sect_num,&sect_hdr,fp)==-1)
{
return -1;
}
//we have to check to which strtab it is linked
link=sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym);
dest->st_name = src->st_name;
dest->st_value = src->st_value;
dest->st_size = src->st_size;
dest->st_info = src->st_info;
dest->st_other = src->st_other;
dest->st_shndx = src->st_shndx;
}
//read the coresponding strtab
if(read_ELF_section_header(link,&sect_hdr,fp)==-1)
{
return -1;
}
//get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size);
if(!name_buf)
return -1;
//get the offset of strtab in file and seek to it
fseek(fp,sect_hdr.sh_offset,SEEK_SET);
//read all data from the section to the buffer.
fread(name_buf,sect_hdr.sh_size,1,fp);
//so we have the namebuf now seek to symtab data
fseek(fp,sym_data_offset,SEEK_SET);
printf("[%d] symbols\n",num_sym);
for(i=0;i<num_sym;i++)
{
int process_symtab(FILE *fp, Elf64_Ehdr *ehdr, int elfclass, int sect_num)
{
Elf64_Shdr sect_hdr;
Elf32_Sym mysym32;
Elf64_Sym mysym;
char *name_buf = NULL;
int num_sym, link, idx;
off_t sym_data_offset;
int sym_data_size;
//memcpy((char *)&mysym,(char *)sym_buf,sizeof(Elf32_Sym));
fread(&mysym,sizeof(Elf32_Sym),1,fp);
//dump_sym(&mysym);
idx=mysym.st_name;
//printf("symbol %d index in strtab %d\n",i,idx);
printf("[%3d] %s ",i,name_buf+idx);
switch(ELF32_ST_BIND(mysym.st_info))
{
case 0: printf(" LOCAL ");
break;
case 1: printf(" GLOBAL ");
break;
case 2: printf(" WEAK ");
break;
case 3: printf(" NUM ");
break;
case 10: printf(" OS-specific-start ");
break;
case 12: printf(" OS-specific-end ");
break;
case 13: printf(" CPU-specific-start ");
break;
case 15: printf("CPU-specific-end ");
break;
}
if (!read_ELF_section_header(fp, ehdr, elfclass, sect_num, &sect_hdr)) {
return -1;
}
//we have to check to which strtab it is linked
link = sect_hdr.sh_link;
sym_data_offset = sect_hdr.sh_offset;
sym_data_size = sect_hdr.sh_size;
num_sym = sym_data_size /
(elfclass == ELFCLASS32 ? sizeof(Elf32_Sym) : sizeof(Elf64_Sym));
//read the coresponding strtab
if (!read_ELF_section_header(fp, ehdr, elfclass, link, &sect_hdr)) {
return -1;
}
//get the size of strtab in file and allocate a buffer
name_buf = (char *) malloc(sect_hdr.sh_size);
if (!name_buf) {
return -1;
}
//get the offset of strtab in file and seek to it
fseek(fp, sect_hdr.sh_offset, SEEK_SET);
//read all data from the section to the buffer.
if (fread(name_buf, sect_hdr.sh_size, 1, fp) != 1) {
return -1;
}
//so we have the namebuf now seek to symtab data
fseek(fp, sym_data_offset, SEEK_SET);
printf("[%d] symbols\n", num_sym);
for (int i = 0; i < num_sym; ++i) {
if (elfclass == ELFCLASS32) {
if (fread(&mysym32, sizeof(mysym32), 1, fp) != 1) {
return -1;
}
Elf32to64_Sym(&mysym32, &mysym);
} else if (elfclass == ELFCLASS64) {
if (fread(&mysym, sizeof(mysym), 1, fp) != 1) {
return -1;
}
}
idx = mysym.st_name;
//printf("symbol %d index in strtab %d\n",i,idx);
printf("[%3d] %s ", i, name_buf + idx);
int binding = elfclass == ELFCLASS32 ?
ELF32_ST_BIND(mysym.st_info) :
ELF64_ST_BIND(mysym.st_info);
switch (binding) {
case 0: printf(" LOCAL ");
break;
case 1: printf(" GLOBAL ");
break;
case 2: printf(" WEAK ");
break;
case 3: printf(" NUM ");
break;
case 10: printf(" OS-specific-start ");
break;
case 12: printf(" OS-specific-end ");
break;
case 13: printf(" CPU-specific-start ");
break;
case 15: printf("CPU-specific-end ");
break;
}
switch(ELF32_ST_TYPE(mysym.st_info))
{
case 0: printf(" NOTYPE ");
break;
case 1: printf(" OBJECT ");
break;
case 2: printf(" FUNC ");
break;
case 3: printf(" SECTION ");
break;
case 4: printf(" FILE ");
break;
case 5: printf(" COMMON ");
break;
case 6: printf(" TLS ");
break;
case 7: printf(" NUM ");
break;
case 10: printf(" OS-specific-start ");
break;
break;
case 12: printf(" OS-specific-end ");
break;
case 13: printf(" CPU-specific-start ");
break;
break;
case 15: printf(" CPU-specific-end ");
break;
}
switch(mysym.st_shndx)
{
case SHN_UNDEF: printf(" UNDEF ");
break;
case SHN_LOPROC: printf(" CPU-specific-start ");
break;
case SHN_HIPROC: printf(" CPU-specific-end ");
break;
case SHN_LOOS: printf(" OS-specific-start ");
break;
case SHN_HIOS: printf(" OS-specific-end ");
break;
case SHN_ABS: printf(" ABSOLUTE ");
break;
case SHN_COMMON: printf(" COMMON ");
break;
case SHN_XINDEX: printf(" XINDEX ");
break;
int type = elfclass == ELFCLASS32 ?
ELF32_ST_TYPE(mysym.st_info) :
ELF64_ST_TYPE(mysym.st_info);
switch (type) {
case 0: printf(" NOTYPE ");
break;
case 1: printf(" OBJECT ");
break;
case 2: printf(" FUNC ");
break;
case 3: printf(" SECTION ");
break;
case 4: printf(" FILE ");
break;
case 5: printf(" COMMON ");
break;
case 6: printf(" TLS ");
break;
case 7: printf(" NUM ");
break;
case 10: printf(" OS-specific-start ");
break;
break;
case 12: printf(" OS-specific-end ");
break;
case 13: printf(" CPU-specific-start ");
break;
break;
case 15: printf(" CPU-specific-end ");
break;
}
switch (mysym.st_shndx) {
case SHN_UNDEF: printf(" UNDEF ");
break;
case SHN_LOPROC: printf(" CPU-specific-start ");
break;
case SHN_HIPROC: printf(" CPU-specific-end ");
break;
case SHN_LOOS: printf(" OS-specific-start ");
break;
case SHN_HIOS: printf(" OS-specific-end ");
break;
case SHN_ABS: printf(" ABSOLUTE ");
break;
case SHN_COMMON: printf(" COMMON ");
break;
case SHN_XINDEX: printf(" XINDEX ");
break;
}
printf("\n");
}
printf("\n");
}
if(name_buf)
free(name_buf);
return 0;
}
free(name_buf);
return 0;
}
void dump_symbols(FILE *fp)
{
Elf32_Ehdr ehdr;
Elf32_Shdr sec_hdr;
int num_hdrs,i;
fseek(fp,(off_t)0,SEEK_SET);
read_ELF_file_header(fp, &ehdr);
num_hdrs=ehdr.e_shnum;
for(i=0;i<num_hdrs;i++)
{
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
{
printf("Wrong Section to read\n");
}
else
{
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
{
printf("\n[section %3d] contains ",i);
process_symtab(i,fp);
Elf64_Ehdr ehdr;
Elf64_Shdr sec_hdr;
int num_hdrs, elfclass;
}
else
continue;
}
}
if (!read_ELF_file_header(fp, &ehdr, &elfclass)) {
printf("Not an ELF file\n");
exit(0);
}
num_hdrs = ehdr.e_shnum;
for (int i = 0; i < num_hdrs; ++i) {
if (!read_ELF_section_header(fp, &ehdr, elfclass, i, &sec_hdr)) {
printf("Wrong Section to read\n");
} else if (sec_hdr.sh_type != SHT_SYMTAB && sec_hdr.sh_type != SHT_DYNSYM) {
continue;
}
printf("\n[section %3d] contains ", i);
process_symtab(fp, &ehdr, elfclass, i);
}
}
int main(int argc, char const * const *argv)
{
FILE *fp;
if (--argc != 1) {
printf("usage: %s elf-binary\n", argv[0]);
return 1;
}
fp = fopen(argv[1], "rb");
if (!fp) {
perror("cannot open file");
return 1;
}
dump_symbols(fp);
return 0;
}
#endif

View File

@ -15,8 +15,10 @@
#include <stdio.h>
// ELFinfo
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr);
int read_ELF_section_header(int sect_num,Elf32_Shdr *sect_hdr,FILE *fp);
// Returns true if it finds a valid ELF header. Stores ELFCLASS32 or 64 in elfclass.
bool read_ELF_file_header(FILE *fp, Elf64_Ehdr *filehdr, int *elfclass);
// Returns true if it finds a valid ELF section header.
bool read_ELF_section_header(FILE *fp, Elf64_Ehdr const *filehdr, int elfclass, int sect_num, Elf64_Shdr *sect_hdr);
void display_sections(FILE *fp);
#endif // ELFINFO_H