diff options
author | Thomas <doodrabbit@hotmail.com> | 2021-12-17 20:57:03 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-17 20:57:03 -0500 |
commit | af67eaffa7ab1a347a6f0e59ed7f1e107749d15a (patch) | |
tree | b9f90f7b047b3dc5a411dbf65117bf07b237a37d /.github/calcrom/ElfFile.h | |
parent | 3ab18655ca1311019212b3a2a9dbe32e5fbee55d (diff) | |
parent | 44cd7753b5dde323d1e8274b2dc8a5599729e83f (diff) |
Real-match math_util.c
Diffstat (limited to '.github/calcrom/ElfFile.h')
-rw-r--r-- | .github/calcrom/ElfFile.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/.github/calcrom/ElfFile.h b/.github/calcrom/ElfFile.h new file mode 100644 index 00000000..85e01800 --- /dev/null +++ b/.github/calcrom/ElfFile.h @@ -0,0 +1,60 @@ +#ifndef CALCROM_ELFFILE_H +#define CALCROM_ELFFILE_H + +#include <vector> +#include <fstream> +#include <filesystem> +#include <elf.h> + +using namespace std; +using namespace std::filesystem; + +class Elf32File { + ifstream handle; + Elf32_Ehdr ehdr; + vector<Elf32_Shdr> shdr; + vector<Elf32_Sym> symtab; + vector<Elf32_Phdr> phdr; + vector<char> shstrtab; + vector<char> strtab; + +private: + void ReadElfHeaderAndVerify(); + void ReadSectionHeaders(); + void ReadProgramHeaders(); + void ReadShstrtab(); + void ReadStrtab(); + void ReadSymtab(); +public: + typedef unsigned int elfload; + static const elfload sections = 1; + static const elfload programs = 2; + static const elfload symbols = 4; + + Elf32File() = default; + Elf32File(path const& filename, elfload load = sections); + ~Elf32File(); + void open(path const& filename, elfload load = sections); + void close(); + bool is_open() const; + template <typename _V> + vector<_V> ReadSectionData(const Elf32_Shdr &_shdr) { + vector<_V> ret; + ret.resize((_shdr.sh_size + sizeof(_V) - 1) / sizeof(_V)); + handle.seekg(_shdr.sh_offset); + handle.read((char *)ret.data(), _shdr.sh_size); + return ret; + } + template <typename _V> + size_t GetSectionElementCount(const Elf32_Shdr &_shdr) { + return (_shdr.sh_size + sizeof(_V) - 1) / sizeof(_V); + } + vector<Elf32_Shdr>& GetSectionHeaders(); + vector<Elf32_Phdr>& GetProgramHeaders(); + string GetSectionName(const Elf32_Shdr §ion) const; + string GetSymbolName(const Elf32_Sym &symbol) const; + Elf32_Sym &operator[](const string &name); + Elf32_Sym &at(const string &name); +}; + +#endif //CALCROM_ELFFILE_H |