This commit is contained in:
Arron David Nelson 2024-02-14 18:42:04 -08:00
parent 81b3d6a08d
commit c3eb95e8f3
7 changed files with 139 additions and 0 deletions

View File

@ -28,6 +28,9 @@ set(CMAKE_CXX_STANDARD 20)
add_executable(Compiler
src/ELF.cpp include/ELF.h
src/ELF64_Section.cpp include/ELF64_Section.h
src/ELF64_Program.cpp include/ELF64_Program.h
src/ELF64.cpp include/ELF64.h
src/main.cpp
)

95
include/ELF64.h Normal file
View File

@ -0,0 +1,95 @@
#pragma once
#include "ELF64_Section.h"
#include "ELF64_Program.h"
#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
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_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;
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::Serializer<ehs::UInt_64>& data);
ehs::UInt_8 GetSubArchitecture() const;
ehs::UInt_8 GetEndianness() const;
ehs::UInt_8 GetABI() const;
ehs::UInt_16 GetType() const;
ehs::UInt_16 GetArchitecture() const;
void SetEntryPoint(ehs::UInt_64 newEntryPoint);
ehs::UInt_64 GetEntryPoint() const;
ehs::Serializer<ehs::UInt_64> Serialize() const;
};

5
include/ELF64_Program.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
class ELF64_Program
{
};

27
include/ELF64_Section.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <ehs/Array.h>
#include <ehs/Str.h>
class ELF64_Section
{
private:
friend class ELF64;
ehs::Str_8 name;
ehs::UInt_32 nameIndex;
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;
public:
ELF64_Section();
ELF64_Section();
};

3
src/ELF64.cpp Normal file
View File

@ -0,0 +1,3 @@
//
// Created by karutoh on 2/14/24.
//

3
src/ELF64_Program.cpp Normal file
View File

@ -0,0 +1,3 @@
//
// Created by karutoh on 2/14/24.
//

3
src/ELF64_Section.cpp Normal file
View File

@ -0,0 +1,3 @@
//
// Created by karutoh on 2/14/24.
//