#ifndef ELFPCP_FILE_H
#define ELFCPP_FILE_H
#include <string>
#include <cstring>
namespace elfcpp
{
template<int size, bool big_endian, typename File>
class Elf_file
{
private:
typedef Elf_file<size, big_endian, File> This;
public:
static const int ehdr_size = Elf_sizes<size>::ehdr_size;
static const int phdr_size = Elf_sizes<size>::phdr_size;
static const int shdr_size = Elf_sizes<size>::shdr_size;
static const int sym_size = Elf_sizes<size>::sym_size;
static const int rel_size = Elf_sizes<size>::rel_size;
static const int rela_size = Elf_sizes<size>::rela_size;
typedef Ehdr<size, big_endian> Ef_ehdr;
typedef Phdr<size, big_endian> Ef_phdr;
typedef Shdr<size, big_endian> Ef_shdr;
typedef Sym<size, big_endian> Ef_sym;
Elf_file(File* file, const Ef_ehdr& ehdr)
{ this->construct(file, ehdr); }
inline
Elf_file(File* file);
off_t
shoff() const
{ return this->shoff_; }
unsigned int
shnum()
{
this->initialize_shnum();
return this->shnum_;
}
unsigned int
shstrndx()
{
this->initialize_shnum();
return this->shstrndx_;
}
int
large_shndx_offset()
{
this->initialize_shnum();
return this->large_shndx_offset_;
}
typename File::Location
section_header(unsigned int shndx)
{
return typename File::Location(this->section_header_offset(shndx),
shdr_size);
}
std::string
section_name(unsigned int shndx);
typename File::Location
section_contents(unsigned int shndx);
typename Elf_types<size>::Elf_WXword
section_size(unsigned int shndx);
typename Elf_types<size>::Elf_WXword
section_flags(unsigned int shndx);
typename Elf_types<size>::Elf_Addr
section_addr(unsigned int shndx);
Elf_Word
section_type(unsigned int shndx);
Elf_Word
section_link(unsigned int shndx);
Elf_Word
section_info(unsigned int shndx);
typename Elf_types<size>::Elf_WXword
section_addralign(unsigned int shndx);
private:
void
construct(File* file, const Ef_ehdr& ehdr);
void
initialize_shnum();
off_t
section_header_offset(unsigned int shndx);
File* file_;
off_t shoff_;
unsigned int shnum_;
unsigned int shstrndx_;
int large_shndx_offset_;
};
template<int size, bool big_endian, typename File>
void
Elf_file<size, big_endian, File>::construct(File* file, const Ef_ehdr& ehdr)
{
this->file_ = file;
this->shoff_ = ehdr.get_e_shoff();
this->shnum_ = ehdr.get_e_shnum();
this->shstrndx_ = ehdr.get_e_shstrndx();
this->large_shndx_offset_ = 0;
if (ehdr.get_e_ehsize() != This::ehdr_size)
file->error(_("bad e_ehsize (%d != %d)"),
ehdr.get_e_ehsize(), This::ehdr_size);
if (ehdr.get_e_shentsize() != This::shdr_size)
file->error(_("bad e_shentsize (%d != %d)"),
ehdr.get_e_shentsize(), This::shdr_size);
}
template<int size, bool big_endian, typename File>
inline
Elf_file<size, big_endian, File>::Elf_file(File* file)
{
typename File::View v(file->view(file_header_offset, This::ehdr_size));
this->construct(file, Ef_ehdr(v.data()));
}
template<int size, bool big_endian, typename File>
void
Elf_file<size, big_endian, File>::initialize_shnum()
{
if ((this->shnum_ == 0 || this->shstrndx_ == SHN_XINDEX)
&& this->shoff_ != 0)
{
typename File::View v(this->file_->view(this->shoff_, This::shdr_size));
Ef_shdr shdr(v.data());
if (this->shnum_ == 0)
this->shnum_ = shdr.get_sh_size();
if (this->shstrndx_ == SHN_XINDEX)
{
this->shstrndx_ = shdr.get_sh_link();
if (this->shstrndx_ >= this->shnum_)
{
if (this->shstrndx_ >= elfcpp::SHN_LORESERVE + 0x100)
{
this->large_shndx_offset_ = - 0x100;
this->shstrndx_ -= 0x100;
}
if (this->shstrndx_ >= this->shnum_)
this->file_->error(_("bad shstrndx: %u >= %u"),
this->shstrndx_, this->shnum_);
}
}
}
}
template<int size, bool big_endian, typename File>
off_t
Elf_file<size, big_endian, File>::section_header_offset(unsigned int shndx)
{
if (shndx >= this->shnum())
this->file_->error(_("section_header_offset: bad shndx %u >= %u"),
shndx, this->shnum());
return this->shoff_ + This::shdr_size * shndx;
}
template<int size, bool big_endian, typename File>
std::string
Elf_file<size, big_endian, File>::section_name(unsigned int shndx)
{
File* const file = this->file_;
unsigned int sh_name;
{
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
sh_name = shdr.get_sh_name();
}
off_t shstr_off;
off_t shstr_size;
{
const unsigned int shstrndx = this->shstrndx_;
typename File::View v(file->view(this->section_header_offset(shstrndx),
This::shdr_size));
Ef_shdr shstr_shdr(v.data());
shstr_off = shstr_shdr.get_sh_offset();
shstr_size = shstr_shdr.get_sh_size();
}
if (sh_name >= shstr_size)
file->error(_("bad section name offset for section %u: %u"),
shndx, sh_name);
typename File::View v(file->view(shstr_off, shstr_size));
const unsigned char* datau = v.data();
const char* data = reinterpret_cast<const char*>(datau);
const void* p = ::memchr(data + sh_name, '\0', shstr_size - sh_name);
if (p == NULL)
file->error(_("missing null terminator for name of section %u"),
shndx);
size_t len = static_cast<const char*>(p) - (data + sh_name);
return std::string(data + sh_name, len);
}
template<int size, bool big_endian, typename File>
typename File::Location
Elf_file<size, big_endian, File>::section_contents(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_contents: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return typename File::Location(shdr.get_sh_offset(), shdr.get_sh_size());
}
template<int size, bool big_endian, typename File>
typename Elf_types<size>::Elf_WXword
Elf_file<size, big_endian, File>::section_size(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_size: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_size();
}
template<int size, bool big_endian, typename File>
typename Elf_types<size>::Elf_WXword
Elf_file<size, big_endian, File>::section_flags(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_flags: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_flags();
}
template<int size, bool big_endian, typename File>
typename Elf_types<size>::Elf_Addr
Elf_file<size, big_endian, File>::section_addr(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_flags: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_addr();
}
template<int size, bool big_endian, typename File>
Elf_Word
Elf_file<size, big_endian, File>::section_type(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_type: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_type();
}
template<int size, bool big_endian, typename File>
Elf_Word
Elf_file<size, big_endian, File>::section_link(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_link: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_link();
}
template<int size, bool big_endian, typename File>
Elf_Word
Elf_file<size, big_endian, File>::section_info(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_info: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_info();
}
template<int size, bool big_endian, typename File>
typename Elf_types<size>::Elf_WXword
Elf_file<size, big_endian, File>::section_addralign(unsigned int shndx)
{
File* const file = this->file_;
if (shndx >= this->shnum())
file->error(_("section_addralign: bad shndx %u >= %u"),
shndx, this->shnum());
typename File::View v(file->view(this->section_header_offset(shndx),
This::shdr_size));
Ef_shdr shdr(v.data());
return shdr.get_sh_addralign();
}
}
#endif