Finished OOP conversion.

This commit is contained in:
2024-02-14 23:39:35 -08:00
parent c3eb95e8f3
commit 669b6d66a3
20 changed files with 769 additions and 820 deletions

View File

@@ -1,101 +0,0 @@
#pragma once
#include <ehs/Serializer.h>
/// Architecture's memory bit-depth.
#define ELFH_ARCH_32 0x01
#define ELFH_ARCH_64 0x02
/// Architecture's endianness.
#define ELFH_END_LE 0x01
#define ELFH_END_BE 0x02
/// OS' ABI Types.
#define ELFH_ABI_SYSTEMV 0x00
/// ELF Binary Types.
#define ELFH_TYPE_EXEC 0x02
#define ELFH_TYPE_DYN 0x03
/// Machine architectures.
#define ELFH_MARCH_AMD_X86_64 0x003E
/// Program types.
#define PRGH_TYPE_LOAD 0x00000001
/// Program flags.
#define PRGH_FLAG_EXEC 0x00000001
#define PRGH_FLAG_WRITE 0x00000002
#define PRGH_FLAG_READ 0x00000004
/// Section types.
#define SECH_TYPE_NULL 0x00
#define SECH_TYPE_PROGBITS 0x01
#define SECH_TYPE_STRTAB 0x03
/// Section flags.
#define SECH_FLAG_ALLOC 0x02
#define SECH_FLAG_EXEC 0x04
#define SECH_FLAG_STRINGS 0x20
/// True header sizes.
#define ELFH_SIZE 64
#define PRGH_SIZE 56
#define SECH_SIZE 64
/// ELF Header
struct ElfHeader
{
ehs::UInt_8 architecture;
ehs::UInt_8 endianness;
ehs::UInt_8 elfV1;
ehs::UInt_8 targetABI;
ehs::UInt_8 abiV;
ehs::UInt_16 elfType;
ehs::UInt_16 machineArch;
ehs::UInt_32 elfV2;
ehs::UInt_64 entryAddress;
ehs::UInt_64 programsOffset;
ehs::UInt_64 sectionsOffset;
ehs::UInt_32 flags;
ehs::UInt_16 headerSize;
ehs::UInt_16 pHeaderSize;
ehs::UInt_16 pEntries;
ehs::UInt_16 sSize;
ehs::UInt_16 sEntries;
ehs::UInt_16 strTabIndex;
};
/// ELF Program Header
struct ElfPrgHeader
{
ehs::UInt_32 type;
ehs::UInt_32 flags;
ehs::UInt_64 offset;
ehs::UInt_64 vAddr;
ehs::UInt_64 pAddr;
ehs::UInt_64 fileSize;
ehs::UInt_64 memSize;
ehs::UInt_64 align;
};
/// ELF Section Header
struct ElfSecHeader
{
ehs::UInt_32 index;
ehs::UInt_32 type;
ehs::UInt_64 flags;
ehs::UInt_64 vAddr;
ehs::UInt_64 offset;
ehs::UInt_64 size;
ehs::UInt_32 associatedIndex;
ehs::UInt_32 info;
ehs::UInt_64 align;
ehs::UInt_64 entrySize;
};
void WriteHeader(const ElfHeader& header, ehs::Serializer<ehs::UInt_64>& data);
void WriteHeader(const ElfPrgHeader& header, ehs::Serializer<ehs::UInt_64>& data);
void WriteHeader(const ElfSecHeader& header, ehs::Serializer<ehs::UInt_64>& data);

View File

@@ -23,60 +23,36 @@
/// Machine architectures.
#define ELFH_MARCH_AMD_X86_64 0x003E
/// Program types.
#define PRGH_TYPE_LOAD 0x00000001
/// Program flags.
#define PRGH_FLAG_EXEC 0x00000001
#define PRGH_FLAG_WRITE 0x00000002
#define PRGH_FLAG_READ 0x00000004
/// Section types.
#define SECH_TYPE_NULL 0x00
#define SECH_TYPE_PROGBITS 0x01
#define SECH_TYPE_STRTAB 0x03
/// Section flags.
#define SECH_FLAG_ALLOC 0x02
#define SECH_FLAG_EXEC 0x04
#define SECH_FLAG_STRINGS 0x20
/// True header sizes.
#define ELFH_SIZE 64
#define PRGH_SIZE 56
#define SECH_SIZE 64
class ELF64
{
private:
ehs::UInt_8 subArch;
ehs::UInt_8 endianness;
ehs::UInt_8 elfV1;
ehs::UInt_8 targetABI;
ehs::UInt_8 abiV;
ehs::UInt_8 abi;
ehs::UInt_16 type;
ehs::UInt_16 arch;
ehs::UInt_32 elfV2;
ehs::UInt_64 entryAddress;
ehs::UInt_64 programsOffset;
ehs::UInt_64 sectionsOffset;
ehs::UInt_32 flags;
ehs::UInt_16 headerSize;
ehs::UInt_16 pHeaderSize;
ehs::UInt_16 pEntries;
ehs::UInt_16 sSize;
ehs::UInt_16 sEntries;
ehs::UInt_16 strTabIndex;
ehs::Vector<ELF64_Program> programs;
ehs::Vector<ELF64_Section> sections;
ehs::UInt_16 entryPoint;
ehs::Vector<ELF64_Program, ehs::UInt_16> programs;
ehs::Vector<ELF64_Section, ehs::UInt_16> sections;
public:
ELF64();
ELF64(ehs::UInt_8& subArch, ehs::UInt_8& end, ehs::UInt_8& abi, ehs::UInt_16& type, ehs::UInt_16& arch);
ELF64(ehs::UInt_8 subArch, ehs::UInt_8 end, ehs::UInt_8 abi, ehs::UInt_16 type, ehs::UInt_16 arch);
ELF64(ehs::Serializer<ehs::UInt_64>& data);
ELF64(ELF64&& header) noexcept;
ELF64(const ELF64& header);
ELF64& operator=(ELF64&& header) noexcept;
ELF64& operator=(const ELF64& header);
ehs::UInt_8 GetSubArchitecture() const;
ehs::UInt_8 GetEndianness() const;
@@ -87,9 +63,26 @@ public:
ehs::UInt_16 GetArchitecture() const;
void SetEntryPoint(ehs::UInt_64 newEntryPoint);
void SetEntryPoint(ehs::UInt_16 programIndex);
ehs::UInt_64 GetEntryPoint() const;
ELF64_Program* GetEntryPoint() const;
void AddProgram(ELF64_Program newProgram);
bool HasSection(ehs::UInt_64 hashId);
bool HasSection(ehs::Str_8& id);
void AddSection(ELF64_Section newSection);
ehs::Serializer<ehs::UInt_64> Serialize() const;
private:
void InitializeSections();
ehs::UInt_64 RetrieveSectionsOffset() const;
ehs::UInt_16 FindShStrTabIndex() const;
ELF64_Section* FindShStrTab() const;
};

View File

@@ -1,5 +1,55 @@
#pragma once
#include <ehs/Serializer.h>
/// Program types.
#define PRGH_TYPE_LOAD 0x00000001
/// Program flags.
#define PRGH_FLAG_EXEC 0x00000001
#define PRGH_FLAG_WRITE 0x00000002
#define PRGH_FLAG_READ 0x00000004
#define PRGH_SIZE 56
class ELF64;
class ELF64_Program
{
private:
friend class ELF64;
ELF64* owner;
ehs::UInt_16 sectionIndex;
ehs::UInt_32 type;
ehs::UInt_32 flags;
ehs::UInt_64 offset;
ehs::UInt_64 vAddr;
ehs::UInt_64 pAddr;
ehs::UInt_64 fileSize;
ehs::UInt_64 adjustedMemSize;
ehs::UInt_64 align;
public:
ELF64_Program();
ELF64_Program(ehs::UInt_16 sectionIndex, ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 address, ehs::UInt_64 align);
ELF64_Program(ehs::Serializer<ehs::UInt_64>& data);
ELF64_Program(ELF64_Program&& program) noexcept;
ELF64_Program(const ELF64_Program& program);
ELF64_Program& operator=(ELF64_Program&& program) noexcept;
ELF64_Program& operator=(const ELF64_Program& program);
ehs::UInt_32 GetType() const;
ehs::UInt_32 GetFlags() const;
ehs::UInt_64 GetAlign() const;
void Serialize(ehs::Serializer<ehs::UInt_64>& inData) const;
};

View File

@@ -1,27 +1,68 @@
#pragma once
#include <ehs/Serializer.h>
#include <ehs/Array.h>
#include <ehs/Str.h>
/// Section types.
#define SECH_TYPE_NULL 0x0000
#define SECH_TYPE_PROGBITS 0x0001
#define SECH_TYPE_STRTAB 0x0003
/// Section flags.
#define SECH_FLAG_ALLOC 0x02
#define SECH_FLAG_EXEC 0x04
#define SECH_FLAG_STRINGS 0x20
#define SECH_SIZE 64
class ELF64_Section
{
private:
friend class ELF64;
ehs::Str_8 name;
ehs::UInt_32 nameIndex;
ehs::UInt_64 hashId;
ehs::Str_8 id;
ehs::UInt_32 programIndex;
ehs::UInt_32 nameOffset;
ehs::UInt_32 type;
ehs::UInt_64 flags;
ehs::UInt_64 vAddr;
ehs::UInt_64 segmentOffset;
ehs::UInt_64 segmentSize;
ehs::UInt_32 associatedIndex;
ehs::UInt_32 info;
ehs::UInt_64 align;
ehs::UInt_64 entrySize;
ehs::Serializer<ehs::UInt_64> data;
public:
ELF64_Section();
ELF64_Section();
ELF64_Section(ehs::Str_8 id, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 align);
ELF64_Section(ELF64_Section&& sect) noexcept;
ELF64_Section(const ELF64_Section& sect);
ELF64_Section& operator=(ELF64_Section&& sect) noexcept;
ELF64_Section& operator=(const ELF64_Section& sect);
ehs::UInt_64 GetHashId() const;
ehs::Str_8 GetId() const;
ehs::UInt_32 GetType() const;
ehs::UInt_64 GetFlags() const;
ehs::UInt_64 GetVirtualAddress() const;
ehs::UInt_64 GetAlignment() const;
void SetData(ehs::Serializer<ehs::UInt_64> newData);
ehs::Serializer<ehs::UInt_64>& GetData();
void Serialize(ehs::Serializer<ehs::UInt_64>& inData) const;
};