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 Disassembler.cc
DwarfReader.cc DwarfReader.cc
DwarfReader.hpp DwarfReader.hpp
elfinfo/elfinfo.cc
elfinfo/elfinfo.h
gzstream/gzstream.C gzstream/gzstream.C
gzstream/gzstream.h gzstream/gzstream.h
Logger.cc 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 // try to open elf file from environment variable
char * elfpath = getenv("FAIL_ELF_PATH"); char * elfpath = getenv("FAIL_ELF_PATH");
if (elfpath == NULL) { if (elfpath == NULL) {
@ -42,7 +42,7 @@ ElfReader::ElfReader(const char* path) : m_log("FAIL*Elfinfo", false) {
void ElfReader::setup(const char* path) { void ElfReader::setup(const char* path) {
// Try to open the ELF file // Try to open the ELF file
FILE * fp = fopen(path, "r"); FILE *fp = fopen(path, "rb");
if (!fp) { if (!fp) {
m_log << "Error: Could not open " << path << std::endl; m_log << "Error: Could not open " << path << std::endl;
return; return;
@ -51,70 +51,61 @@ void ElfReader::setup(const char* path) {
m_filename = std::string(path); m_filename = std::string(path);
// Evaluate headers // Evaluate headers
Elf32_Ehdr ehdr; Elf64_Ehdr ehdr;
Elf32_Shdr sec_hdr; Elf64_Shdr sec_hdr;
int num_hdrs,i; int num_hdrs;
fseek(fp,(off_t)0,SEEK_SET); if (!read_ELF_file_header(fp, &ehdr)) {
read_ELF_file_header(fp, &ehdr); m_log << "Error: " << path << " is not an ELF file" << std::endl;
num_hdrs=ehdr.e_shnum; return;
m_log << "Evaluating ELF File: " << path << std::endl; }
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 // Parse symbol table and generate internal map
for (i=0;i<num_hdrs;i++) for (int i = 0; i < num_hdrs; ++i) {
{ if (!read_ELF_section_header(fp, &ehdr, i, &sec_hdr)) {
if (read_ELF_section_header(i,&sec_hdr,fp)==-1) m_log << "Wrong section to read" << std::endl;
{ continue;
m_log << "Wrong Section to read" << std::endl; } else if (sec_hdr.sh_type != SHT_SYMTAB && sec_hdr.sh_type != SHT_DYNSYM) {
} continue;
else
{
if ((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
{
process_symboltable(i,fp);
}
else
{
continue;
}
} }
process_symboltable(fp, &ehdr, i);
} }
// Parse section information // 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; m_log << "Error: reading section string table sect_num = " << ehdr.e_shstrndx << std::endl;
return;
} }
char* buff=(char*)malloc(sec_hdr.sh_size); char *buff = (char*)malloc(sec_hdr.sh_size);
if (!buff) if (!buff) {
{ m_log << "Error: malloc failed to allocate buffer for shstrtab" << std::endl;
m_log << "Malloc failed to allocate buffer for shstrtab" << std::endl; return;
exit(0);
} }
// seek to the offset in the file, // seek to the offset in the file,
fseek(fp,(off_t)sec_hdr.sh_offset,SEEK_SET); fseek(fp, (off_t)sec_hdr.sh_offset, SEEK_SET);
fread(buff,sec_hdr.sh_size,1,fp); if (fread(buff, sec_hdr.sh_size, 1, fp) != 1) {
m_log << "Total number of sections: " << num_hdrs << std::endl; printf("Couldn't read complete shstrtab\n");
exit(0);
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);
}
} }
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); free(buff);
fclose(fp); 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 // Add section name, start address and size to list
int idx=sect_hdr->sh_name; int idx=sect_hdr->sh_name;
// m_sections_map.push_back( sect_hdr->sh_addr, sect_hdr->sh_size, sect_name_buff+idx ); // 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; 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; bool ElfReader::process_symboltable(FILE *fp, Elf64_Ehdr const *ehdr, int sect_num) {
Elf32_Sym mysym;
char *name_buf=NULL; Elf64_Shdr sect_hdr;
int num_sym,link,i,idx; Elf32_Sym mysym32;
Elf64_Sym mysym;
char *name_buf = 0;
int num_sym, link, idx;
off_t sym_data_offset; off_t sym_data_offset;
int sym_data_size; int sym_data_size;
if (read_ELF_section_header(sect_num,&sect_hdr,fp)==-1)
{ if (!read_ELF_section_header(fp, ehdr, sect_num, &sect_hdr)) {
return -1; return false;
} }
// we have to check to which strtab it is linked // we have to check to which strtab it is linked
link=sect_hdr.sh_link; link = sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset; sym_data_offset = sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size; sym_data_size = sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym); num_sym = sym_data_size /
(m_elfclass == ELFCLASS32 ? sizeof(Elf32_Sym) : sizeof(Elf64_Sym));
// read the coresponding strtab // read the corresponding strtab
if (read_ELF_section_header(link,&sect_hdr,fp)==-1) if (!read_ELF_section_header(fp, ehdr, link, &sect_hdr)) {
{ return false;
return -1;
} }
// get the size of strtab in file and allocate a buffer // get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size); name_buf = (char *) malloc(sect_hdr.sh_size);
if (!name_buf) if (!name_buf) {
return -1; return false;
}
// get the offset of strtab in file and seek to it // 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. // 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 // 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); if (type != STT_SECTION && type != STT_FILE) {
idx=mysym.st_name; m_symboltable.push_back(
ElfSymbol(name_buf + idx, mysym.st_value, mysym.st_size, ElfSymbol::SYMBOL, type));
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) );
} }
} }
free (name_buf); free(name_buf);
return 0; 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) { const ElfSymbol& ElfReader::getSymbol(guest_address_t address) {

View File

@ -3,11 +3,11 @@
#include <string> #include <string>
#include <ostream> #include <ostream>
#include "sal/SALConfig.hpp" // for ADDR_INV
#include "Logger.hpp"
#include "elfinfo/elfinfo.h"
#include <vector> #include <vector>
#include <map> #include <map>
#include <elf.h>
#include "sal/SALConfig.hpp" // for ADDR_INV
#include "Logger.hpp"
#include "Demangler.hpp" #include "Demangler.hpp"
namespace fail { namespace fail {
@ -28,19 +28,19 @@ class ElfSymbol {
ElfSymbol(const std::string & name = ELF::NOTFOUND, guest_address_t addr = ADDR_INV, ElfSymbol(const std::string & name = ELF::NOTFOUND, guest_address_t addr = ADDR_INV,
size_t size = -1, int type = UNDEF, int symbol_type = 0) 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; }; const std::string& getName() const { return name; }
std::string getDemangledName() const { return Demangler::demangle(name); }; std::string getDemangledName() const { return Demangler::demangle(name); }
guest_address_t getAddress() const { return address; }; guest_address_t getAddress() const { return address; }
size_t getSize() const { return size; }; size_t getSize() const { return size; }
guest_address_t getStart() const { return getAddress(); }; // alias guest_address_t getStart() const { return getAddress(); } // alias
guest_address_t getEnd() const { return address + size; }; guest_address_t getEnd() const { return address + size; }
int getSymbolType() const { return m_symbol_type; }; int getSymbolType() const { return m_symbol_type; }
bool isSection() const { return m_type == SECTION; }; bool isSection() const { return m_type == SECTION; }
bool isSymbol() const { return m_type == SYMBOL; }; bool isSymbol() const { return m_type == SYMBOL; }
bool isValid() const { return name != ELF::NOTFOUND; }; bool isValid() const { return name != ELF::NOTFOUND; }
bool operator==(const std::string& rhs) const { bool operator==(const std::string& rhs) const {
if (rhs == name) { if (rhs == name) {
@ -157,10 +157,16 @@ public:
private: private:
Logger m_log; Logger m_log;
std::string m_filename; std::string m_filename;
int m_elfclass;
void setup(const char*); void setup(const char*);
int process_symboltable(int sect_num, FILE* fp); bool process_symboltable(FILE *fp, Elf64_Ehdr const *ehdr, int sect_num);
int process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff); 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_symboltable;
container_t m_sectiontable; 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 <string.h>
#include <elf.h> #include <elf.h>
static void Elf32to64_Ehdr(Elf32_Ehdr const *src, Elf64_Ehdr *dest)
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr)
{ {
//rewind(fp); memcpy(dest->e_ident, src->e_ident, sizeof(dest->e_ident));
fread(filehdr,sizeof(Elf32_Ehdr),1,fp); 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) Elf32_Ehdr filehdr32;
return 1; size_t ret;
else
return -1;
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; dest->sh_name = src->sh_name;
Elf32_Ehdr elfhdr; dest->sh_type = src->sh_type;
off_t sect_hdr_off; dest->sh_flags = src->sh_flags;
fseek(fp,(off_t)0,SEEK_SET); dest->sh_addr = src->sh_addr;
read_ELF_file_header(fp,&elfhdr); dest->sh_offset = src->sh_offset;
numsect=elfhdr.e_shnum; dest->sh_size = src->sh_size;
if ((numsect<sect_num)||(sect_num<0)) dest->sh_link = src->sh_link;
return -1; dest->sh_info = src->sh_info;
sect_hdr_off=elfhdr.e_shoff; dest->sh_addralign = src->sh_addralign;
//rewind(fp); dest->sh_entsize = src->sh_entsize;
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;
} }
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; int numsect = filehdr->e_shnum;
idx=sect_hdr->sh_name; off_t sect_hdr_off;
printf(" %-20s (0x%x++0x%x) ",sect_name_buff+idx, sect_hdr->sh_addr, sect_hdr->sh_size); Elf32_Shdr sect_hdr32;
if(sect_hdr->sh_entsize)
printf(" %c ",'*'); if (numsect < section || section < 0) {
else return false;
printf(" %c ",' '); }
switch(sect_hdr->sh_type) sect_hdr_off = filehdr->e_shoff;
{ sect_hdr_off += filehdr->e_shentsize * section;
case 0: printf("NULL \t"); fseek(fp, (off_t) sect_hdr_off, SEEK_SET);
break; if (elfclass == ELFCLASS32) {
case 1: printf("PROGBITS \t"); int ret = fread(&sect_hdr32, sizeof(sect_hdr32), 1, fp);
break; if (ret != 1) {
case 2: printf("SYMTAB \t"); return false;
break; }
case 3: printf("STRTAB \t");
break; Elf32to64_Shdr(&sect_hdr32, sect_hdr);
case 4: printf("RELOC ADN \t"); } else if (elfclass == ELFCLASS64) {
break; int ret = fread(sect_hdr, sizeof(*sect_hdr), 1, fp);
case 5: printf("HASH \t"); if (ret != 1) {
break; return false;
case 6: printf("SYMTAB \t"); }
break; } else {
case 7: printf("NOTE \t"); return false;
break; }
case 8: printf("NOBIT \t");
break; return true;
case 9: printf("RELOC \t"); }
break;
case 10: printf("*reserved* \t"); #ifdef TESTPROGRAM
break; void process_sect_hdr(Elf64_Shdr const *sect_hdr, char const *sect_name_buff)
case 11: printf("DYNSYM \t"); {
break; int idx;
case 14: printf("INITARR \t"); idx = sect_hdr->sh_name;
break; printf(" %-20s (0x%lx++0x%lx) ", sect_name_buff + idx, sect_hdr->sh_addr, sect_hdr->sh_size);
case 15: printf("FINIARR \t"); if (sect_hdr->sh_entsize) {
break; printf(" %c ",'*');
case 16: printf("PREINIT \t"); } else {
break; printf(" %c ",' ');
case 17: printf("SECTGRP \t"); }
break; switch (sect_hdr->sh_type) {
case 18: printf("EXT-SHNDX \t"); case 0: printf("NULL \t");
break; break;
case 19: printf("NUM-DEF-TYP \t"); case 1: printf("PROGBITS \t");
break; break;
case 0x60000000: printf("OS SPECIFIC \t"); case 2: printf("SYMTAB \t");
break; break;
case 0x6ffffff5: printf("GNU_ATTR \t"); case 3: printf("STRTAB \t");
break; break;
case 0x6ffffff6: printf("GNU_HASH \t"); case 4: printf("RELOC ADN \t");
break; break;
case 0x6ffffff7: printf("PRELNKLIBLST \t"); case 5: printf("HASH \t");
break; break;
case 0x6ffffff8: printf("CKSUM \t"); case 6: printf("SYMTAB \t");
break; break;
case 0x6ffffffa: printf("SUN SPECIFIC \t"); case 7: printf("NOTE \t");
break; break;
case 0x6ffffffb: printf("SUNCOMDAT \t"); case 8: printf("NOBIT \t");
break; break;
case 0x6ffffffc: printf("SUNSYMINFO \t"); case 9: printf("RELOC \t");
break; break;
case 0x6ffffffd: printf("GNUVERDEF \t"); case 10: printf("*reserved* \t");
break; break;
case 0x6ffffffe: printf("GNUVERNEED \t"); case 11: printf("DYNSYM \t");
break; break;
case 0x6fffffff: printf("GNUVERSYM \t"); case 14: printf("INITARR \t");
break; break;
case 0x70000000: printf("LOPROC \t"); case 15: printf("FINIARR \t");
break; break;
case 0x7fffffff: printf("HIPROC \t"); case 16: printf("PREINIT \t");
break; break;
case 0x80000000: printf("APP beg \t"); case 17: printf("SECTGRP \t");
break; break;
case 0x8fffffff: printf("APP end \t"); case 18: printf("EXT-SHNDX \t");
break; break;
} case 19: printf("NUM-DEF-TYP \t");
printf("linked to %3d sect\n",sect_hdr->sh_link); 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) void display_sections(FILE *fp)
{ {
Elf32_Ehdr ehdr; Elf64_Ehdr ehdr;
Elf32_Shdr sec_hdr; Elf64_Shdr sec_hdr;
int num_hdrs,i; int num_hdrs, elfclass;
char *buff=NULL; 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);
} 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; dest->st_name = src->st_name;
Elf32_Sym mysym; dest->st_value = src->st_value;
char *name_buf=NULL; dest->st_size = src->st_size;
int num_sym,link,i,idx; dest->st_info = src->st_info;
off_t sym_data_offset; dest->st_other = src->st_other;
int sym_data_size; dest->st_shndx = src->st_shndx;
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);
//read the coresponding strtab int process_symtab(FILE *fp, Elf64_Ehdr *ehdr, int elfclass, int sect_num)
if(read_ELF_section_header(link,&sect_hdr,fp)==-1) {
{ Elf64_Shdr sect_hdr;
return -1; Elf32_Sym mysym32;
} Elf64_Sym mysym;
//get the size of strtab in file and allocate a buffer char *name_buf = NULL;
name_buf=(char*)malloc(sect_hdr.sh_size); int num_sym, link, idx;
if(!name_buf) off_t sym_data_offset;
return -1; int sym_data_size;
//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++)
{
//memcpy((char *)&mysym,(char *)sym_buf,sizeof(Elf32_Sym)); if (!read_ELF_section_header(fp, ehdr, elfclass, sect_num, &sect_hdr)) {
fread(&mysym,sizeof(Elf32_Sym),1,fp); return -1;
//dump_sym(&mysym); }
idx=mysym.st_name; //we have to check to which strtab it is linked
//printf("symbol %d index in strtab %d\n",i,idx); link = sect_hdr.sh_link;
printf("[%3d] %s ",i,name_buf+idx); sym_data_offset = sect_hdr.sh_offset;
switch(ELF32_ST_BIND(mysym.st_info)) sym_data_size = sect_hdr.sh_size;
{ num_sym = sym_data_size /
case 0: printf(" LOCAL "); (elfclass == ELFCLASS32 ? sizeof(Elf32_Sym) : sizeof(Elf64_Sym));
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;
}
//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)) int type = elfclass == ELFCLASS32 ?
{ ELF32_ST_TYPE(mysym.st_info) :
case 0: printf(" NOTYPE "); ELF64_ST_TYPE(mysym.st_info);
break; switch (type) {
case 1: printf(" OBJECT "); case 0: printf(" NOTYPE ");
break; break;
case 2: printf(" FUNC "); case 1: printf(" OBJECT ");
break; break;
case 3: printf(" SECTION "); case 2: printf(" FUNC ");
break; break;
case 4: printf(" FILE "); case 3: printf(" SECTION ");
break; break;
case 5: printf(" COMMON "); case 4: printf(" FILE ");
break; break;
case 6: printf(" TLS "); case 5: printf(" COMMON ");
break; break;
case 7: printf(" NUM "); case 6: printf(" TLS ");
break; break;
case 10: printf(" OS-specific-start "); case 7: printf(" NUM ");
break; break;
break; case 10: printf(" OS-specific-start ");
case 12: printf(" OS-specific-end "); break;
break; break;
case 13: printf(" CPU-specific-start "); case 12: printf(" OS-specific-end ");
break; break;
break; case 13: printf(" CPU-specific-start ");
case 15: printf(" CPU-specific-end "); break;
break; break;
} case 15: printf(" CPU-specific-end ");
switch(mysym.st_shndx) break;
{ }
case SHN_UNDEF: printf(" UNDEF "); switch (mysym.st_shndx) {
break; case SHN_UNDEF: printf(" UNDEF ");
case SHN_LOPROC: printf(" CPU-specific-start "); break;
break; case SHN_LOPROC: printf(" CPU-specific-start ");
case SHN_HIPROC: printf(" CPU-specific-end "); break;
break; case SHN_HIPROC: printf(" CPU-specific-end ");
case SHN_LOOS: printf(" OS-specific-start "); break;
break; case SHN_LOOS: printf(" OS-specific-start ");
case SHN_HIOS: printf(" OS-specific-end "); break;
break; case SHN_HIOS: printf(" OS-specific-end ");
case SHN_ABS: printf(" ABSOLUTE "); break;
break; case SHN_ABS: printf(" ABSOLUTE ");
case SHN_COMMON: printf(" COMMON "); break;
break; case SHN_COMMON: printf(" COMMON ");
case SHN_XINDEX: printf(" XINDEX "); break;
break; case SHN_XINDEX: printf(" XINDEX ");
break;
}
printf("\n");
} }
printf("\n"); free(name_buf);
return 0;
}
if(name_buf)
free(name_buf);
return 0;
} }
void dump_symbols(FILE *fp) void dump_symbols(FILE *fp)
{ {
Elf32_Ehdr ehdr; Elf64_Ehdr ehdr;
Elf32_Shdr sec_hdr; Elf64_Shdr sec_hdr;
int num_hdrs,i; int num_hdrs, elfclass;
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);
} if (!read_ELF_file_header(fp, &ehdr, &elfclass)) {
else printf("Not an ELF file\n");
continue; 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> #include <stdio.h>
// ELFinfo // ELFinfo
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr); // Returns true if it finds a valid ELF header. Stores ELFCLASS32 or 64 in elfclass.
int read_ELF_section_header(int sect_num,Elf32_Shdr *sect_hdr,FILE *fp); 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); void display_sections(FILE *fp);
#endif // ELFINFO_H #endif // ELFINFO_H