From e95bf38d46b16e0220cdb839b74039d7fee4e9be Mon Sep 17 00:00:00 2001 From: Karutoh Date: Tue, 8 Apr 2025 22:51:50 -0700 Subject: [PATCH] First commit. --- CMakeLists.txt | 11 +- README.md | 20 ++- include/ELF64.h | 95 -------------- include/arctyx/Compiler.h | 1 + include/arctyx/ELF.h | 88 +++++++++++++ include/arctyx/ELF_Architecture.h | 16 +++ .../{ELF64_Program.h => arctyx/ELF_Program.h} | 20 +-- .../{ELF64_Section.h => arctyx/ELF_Section.h} | 16 +-- include/{ELF64_Sym.h => arctyx/ELF_Sym.h} | 6 +- include/arctyx/Linker.h | 1 + src/Compiler.cpp | 1 + src/{ELF64.cpp => ELF.cpp} | 116 +++++++++--------- src/{ELF64_Program.cpp => ELF_Program.cpp} | 24 ++-- src/{ELF64_Section.cpp => ELF_Section.cpp} | 38 +++--- src/{ELF64_Sym.cpp => ELF_Sym.cpp} | 8 +- src/Linker.cpp | 1 + src/main.cpp | 24 ++-- 17 files changed, 262 insertions(+), 224 deletions(-) delete mode 100644 include/ELF64.h create mode 100644 include/arctyx/Compiler.h create mode 100644 include/arctyx/ELF.h create mode 100644 include/arctyx/ELF_Architecture.h rename include/{ELF64_Program.h => arctyx/ELF_Program.h} (54%) rename include/{ELF64_Section.h => arctyx/ELF_Section.h} (74%) rename include/{ELF64_Sym.h => arctyx/ELF_Sym.h} (96%) create mode 100644 include/arctyx/Linker.h create mode 100644 src/Compiler.cpp rename src/{ELF64.cpp => ELF.cpp} (68%) rename src/{ELF64_Program.cpp => ELF_Program.cpp} (73%) rename src/{ELF64_Section.cpp => ELF_Section.cpp} (71%) rename src/{ELF64_Sym.cpp => ELF_Sym.cpp} (55%) create mode 100644 src/Linker.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 01ca0a4..665b155 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,10 +27,13 @@ endif () set(CMAKE_CXX_STANDARD 20) add_executable(Arctyx - src/ELF64_Sym.cpp include/ELF64_Sym.h - src/ELF64_Section.cpp include/ELF64_Section.h - src/ELF64_Program.cpp include/ELF64_Program.h - src/ELF64.cpp include/ELF64.h + include/arctyx/ELF_Architecture.h + src/ELF_Sym.cpp include/arctyx/ELF_Sym.h + src/ELF_Section.cpp include/arctyx/ELF_Section.h + src/ELF_Program.cpp include/arctyx/ELF_Program.h + src/ELF.cpp include/arctyx/ELF.h + src/Linker.cpp include/arctyx/Linker.h + src/Compiler.cpp include/arctyx/Compiler.h src/main.cpp ) diff --git a/README.md b/README.md index cf1969f..5e824e9 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,30 @@ # Work In Progress -Does not currently compile anything, it simply creates an ELF binary with the given machine code. +Does not currently compile anything, it simply creates an ELF binary with provided machine code. + +# Features +- Compiler, and Linker Plugins +- Cross-Platform +- Compiler +- Cross-Compiler +- Linker +- Object-Oriented Programing +- Manual Heap Memory Management +- Fast Reflection +- Object Serializing # Usage `./Compiler /file/path/to/machine/code /output/file/path` # Supported Architectures -- AMD64 +- x86 +- x86_64 +- AARCH +- AARCH64 +- RISCV # Supported OS' - Linux +- Windows # ELF Structure [Format Example](https://kevinboone.me/elfdemo.html) diff --git a/include/ELF64.h b/include/ELF64.h deleted file mode 100644 index a6d8b0f..0000000 --- a/include/ELF64.h +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include "ELF64_Section.h" -#include "ELF64_Program.h" - -#include - -/// 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 - -/// True header sizes. -#define ELFH_SIZE 64 - -class ELF64 -{ -private: - ehs::UInt_8 endianness; - ehs::UInt_8 abi; - ehs::UInt_16 type; - ehs::UInt_16 arch; - ehs::UInt_16 entryPoint; - ehs::Vector programs; - ehs::Vector sections; - -public: - ELF64(); - - ELF64(ehs::UInt_8 end, ehs::UInt_8 abi, ehs::UInt_16 type, ehs::UInt_16 arch); - - ELF64(ehs::Serializer& data); - - ELF64(ELF64&& header) noexcept; - - ELF64(const ELF64& header); - - ELF64& operator=(ELF64&& header) noexcept; - - ELF64& operator=(const ELF64& header); - - ehs::UInt_8 GetEndianness() const; - - ehs::UInt_8 GetABI() const; - - ehs::UInt_16 GetType() const; - - ehs::UInt_16 GetArchitecture() const; - - void SetEntryPoint(ehs::UInt_16 programIndex); - - 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); - - ELF64_Section* GetSection(ehs::UInt_64 sectionHashId) const; - - ELF64_Section* GetSection(const ehs::Str_8& sectionId) const; - - ehs::UInt_64 GetLastSegmentOffset() const; - - ehs::Vec2_u64 SegmentRange(ehs::UInt_64 sectionHashId); - - ehs::Vec2_u64 SegmentRange(const ehs::Str_8& sectionId); - - ehs::Serializer Serialize() const; - -private: - void InitializeSections(); - - ehs::UInt_64 RetrieveSectionsOffset() const; - - ehs::UInt_16 FindShStrTabIndex() const; - - ELF64_Section* FindShStrTab() const; -}; \ No newline at end of file diff --git a/include/arctyx/Compiler.h b/include/arctyx/Compiler.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/include/arctyx/Compiler.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/include/arctyx/ELF.h b/include/arctyx/ELF.h new file mode 100644 index 0000000..7cce62f --- /dev/null +++ b/include/arctyx/ELF.h @@ -0,0 +1,88 @@ +#pragma once + +#include "ELF_Architecture.h" +#include "ELF_Section.h" +#include "ELF_Program.h" + +#include + +/// OS' ABI Types. +#define ELFH_ABI_SYSTEMV 0x00 + +/// ELF Binary Types. +#define ELFH_TYPE_EXEC 0x02 +#define ELFH_TYPE_DYN 0x03 + +/// True header sizes. +#define ELFH_SIZE 64 + +class ELF +{ +private: + ehs::UInt_8 bitDepth; + ehs::UInt_8 endianness; + ehs::UInt_8 abi; + ehs::UInt_16 type; + ehs::UInt_16 arch; + ehs::UInt_16 entryPoint; + ehs::Vector programs; + ehs::Vector sections; + +public: + ELF(); + + ELF(const ehs::UInt_8 &bitDepth, const ehs::UInt_8 &end, const ehs::UInt_8 &abi, const ehs::UInt_16 &type, const ehs::UInt_16 &arch); + + ELF(ehs::Serializer& data); + + ELF(ELF&& header) noexcept; + + ELF(const ELF& header); + + ELF& operator=(ELF&& header) noexcept; + + ELF& operator=(const ELF& header); + + ehs::UInt_8 GetBitDepth() 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_16 programIndex); + + ELF_Program* GetEntryPoint() const; + + void AddProgram(ELF_Program newProgram); + + bool HasSection(ehs::UInt_64 hashId); + + bool HasSection(ehs::Str_8& id); + + void AddSection(ELF_Section newSection); + + ELF_Section* GetSection(ehs::UInt_64 sectionHashId) const; + + ELF_Section* GetSection(const ehs::Str_8& sectionId) const; + + ehs::UInt_64 GetLastSegmentOffset() const; + + ehs::Vec2_u64 SegmentRange(ehs::UInt_64 sectionHashId); + + ehs::Vec2_u64 SegmentRange(const ehs::Str_8& sectionId); + + ehs::Serializer Serialize() const; + +private: + void InitializeSections(); + + ehs::UInt_64 RetrieveSectionsOffset() const; + + ehs::UInt_16 FindShStrTabIndex() const; + + ELF_Section* FindShStrTab() const; +}; diff --git a/include/arctyx/ELF_Architecture.h b/include/arctyx/ELF_Architecture.h new file mode 100644 index 0000000..11984b2 --- /dev/null +++ b/include/arctyx/ELF_Architecture.h @@ -0,0 +1,16 @@ +#pragma once + +// Machine architecture's. +#define ELF_ARCH_X86 0x0003 +#define ELF_ARCH_AARCH 0x0028 +#define ELF_ARCH_X86_64 0x003E +#define ELF_ARCH_AARCH64 0x00B7 +#define ELF_ARCH_RISCV 0x00F3 + +/// Architecture's memory bit-depth. +#define ELF_BIT_DEPTH_32 0x01 +#define ELF_BIT_DEPTH_64 0x02 + +/// Architecture's endianness. +#define ELF_END_LE 0x01 +#define ELF_END_BE 0x02 \ No newline at end of file diff --git a/include/ELF64_Program.h b/include/arctyx/ELF_Program.h similarity index 54% rename from include/ELF64_Program.h rename to include/arctyx/ELF_Program.h index dc8af33..480674f 100644 --- a/include/ELF64_Program.h +++ b/include/arctyx/ELF_Program.h @@ -12,12 +12,12 @@ #define PRGH_SIZE 56 -class ELF64; +class ELF; -class ELF64_Program +class ELF_Program { private: - friend class ELF64; + friend class ELF; ehs::UInt_32 type; ehs::UInt_32 flags; @@ -29,19 +29,19 @@ private: ehs::UInt_64 align; public: - ELF64_Program(); + ELF_Program(); - ELF64_Program(ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 offset, ehs::UInt_64 address, ehs::UInt_64 size, ehs::UInt_64 adjustedMemSize, ehs::UInt_64 align); + ELF_Program(ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 offset, ehs::UInt_64 address, ehs::UInt_64 size, ehs::UInt_64 adjustedMemSize, ehs::UInt_64 align); - ELF64_Program(ehs::Serializer& data); + ELF_Program(ehs::Serializer& data); - ELF64_Program(ELF64_Program&& program) noexcept; + ELF_Program(ELF_Program&& program) noexcept; - ELF64_Program(const ELF64_Program& program); + ELF_Program(const ELF_Program& program); - ELF64_Program& operator=(ELF64_Program&& program) noexcept; + ELF_Program& operator=(ELF_Program&& program) noexcept; - ELF64_Program& operator=(const ELF64_Program& program); + ELF_Program& operator=(const ELF_Program& program); ehs::UInt_32 GetType() const; diff --git a/include/ELF64_Section.h b/include/arctyx/ELF_Section.h similarity index 74% rename from include/ELF64_Section.h rename to include/arctyx/ELF_Section.h index e2af4dd..076d7c5 100644 --- a/include/ELF64_Section.h +++ b/include/arctyx/ELF_Section.h @@ -17,10 +17,10 @@ #define SECH_SIZE 64 -class ELF64_Section +class ELF_Section { private: - friend class ELF64; + friend class ELF; ehs::UInt_64 hashId; ehs::Str_8 id; @@ -37,17 +37,17 @@ private: ehs::Serializer data; public: - ELF64_Section(); + ELF_Section(); - ELF64_Section(ehs::Str_8 id, ehs::UInt_64 fileOffset, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 vAddr, ehs::UInt_64 align); + ELF_Section(ehs::Str_8 id, ehs::UInt_64 fileOffset, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 vAddr, ehs::UInt_64 align); - ELF64_Section(ELF64_Section&& sect) noexcept; + ELF_Section(ELF_Section&& sect) noexcept; - ELF64_Section(const ELF64_Section& sect); + ELF_Section(const ELF_Section& sect); - ELF64_Section& operator=(ELF64_Section&& sect) noexcept; + ELF_Section& operator=(ELF_Section&& sect) noexcept; - ELF64_Section& operator=(const ELF64_Section& sect); + ELF_Section& operator=(const ELF_Section& sect); ehs::UInt_64 GetHashId() const; diff --git a/include/ELF64_Sym.h b/include/arctyx/ELF_Sym.h similarity index 96% rename from include/ELF64_Sym.h rename to include/arctyx/ELF_Sym.h index 2da1a10..41be2fe 100644 --- a/include/ELF64_Sym.h +++ b/include/arctyx/ELF_Sym.h @@ -74,7 +74,7 @@ #define SYMH_SIZE 24 -class ELF64_Sym +class ELF_Sym { private: ehs::UInt_32 nameOffset; @@ -85,9 +85,9 @@ private: ehs::UInt_64 size; public: - ELF64_Sym(); + ELF_Sym(); - ELF64_Sym(ehs::UInt_32 nameOffset, ehs::UInt_8 type, ehs::UInt_8 visibility, ehs::UInt_16 section, ehs::UInt_64 value, ehs::UInt_64 size); + ELF_Sym(ehs::UInt_32 nameOffset, ehs::UInt_8 type, ehs::UInt_8 visibility, ehs::UInt_16 section, ehs::UInt_64 value, ehs::UInt_64 size); void Serialize(ehs::Serializer& inData) const; }; \ No newline at end of file diff --git a/include/arctyx/Linker.h b/include/arctyx/Linker.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/include/arctyx/Linker.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/src/Compiler.cpp b/src/Compiler.cpp new file mode 100644 index 0000000..c09f83e --- /dev/null +++ b/src/Compiler.cpp @@ -0,0 +1 @@ +#include "arctyx/Compiler.h" \ No newline at end of file diff --git a/src/ELF64.cpp b/src/ELF.cpp similarity index 68% rename from src/ELF64.cpp rename to src/ELF.cpp index d49566a..886f4e4 100644 --- a/src/ELF64.cpp +++ b/src/ELF.cpp @@ -1,62 +1,62 @@ -#include "ELF64.h" +#include "arctyx/ELF.h" -constexpr ehs::Char_8 Elf_Magic[] = "\x7F" "ELF"; -constexpr ehs::UInt_8 Elf_Magic_Size = sizeof(Elf_Magic) - 1; - -ELF64::ELF64() - : endianness(ELFH_END_LE), abi(ELFH_ABI_SYSTEMV), type(ELFH_TYPE_EXEC), - arch(ELFH_MARCH_AMD_X86_64), entryPoint(0) +ELF::ELF() + : bitDepth(ELF_BIT_DEPTH_64), endianness(ELF_END_LE), abi(ELFH_ABI_SYSTEMV), type(ELFH_TYPE_EXEC), + arch(ELF_ARCH_X86_64), entryPoint(0) { InitializeSections(); } -ELF64::ELF64(ehs::UInt_8 end, ehs::UInt_8 abi, ehs::UInt_16 type, ehs::UInt_16 arch) - : endianness(end), abi(abi), type(type), arch(arch), entryPoint(0) +ELF::ELF(const ehs::UInt_8 &bitDepth, const ehs::UInt_8 &end, const ehs::UInt_8 &abi, const ehs::UInt_16 &type, const ehs::UInt_16 &arch) + : bitDepth(bitDepth), endianness(end), abi(abi), type(type), arch(arch), entryPoint(0) { InitializeSections(); } -ELF64::ELF64(ehs::Serializer& data) +ELF::ELF(ehs::Serializer& data) { } -ELF64::ELF64(ELF64&& header) noexcept - : endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch), - entryPoint(header.entryPoint), programs((ehs::Vector&&)header.programs), - sections((ehs::Vector&&)header.sections) +ELF::ELF(ELF&& header) noexcept + : bitDepth(header.bitDepth), endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch), + entryPoint(header.entryPoint), programs((ehs::Vector&&)header.programs), + sections((ehs::Vector&&)header.sections) { - header.endianness = ELFH_END_LE; + header.bitDepth = ELF_BIT_DEPTH_64; + header.endianness = ELF_END_LE; header.abi = ELFH_ABI_SYSTEMV; header.type = ELFH_TYPE_EXEC; - header.arch = ELFH_MARCH_AMD_X86_64; + header.arch = ELF_ARCH_X86_64; header.entryPoint = 0; header.InitializeSections(); } -ELF64::ELF64(const ELF64& header) - : endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch), +ELF::ELF(const ELF& header) + : bitDepth(header.bitDepth), endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch), entryPoint(header.entryPoint), programs(header.programs), sections(header.sections) { } -ELF64& ELF64::operator=(ELF64&& header) noexcept +ELF& ELF::operator=(ELF&& header) noexcept { if (this == &header) return *this; + bitDepth = header.bitDepth; endianness = header.endianness; abi = header.abi; type = header.type; arch = header.arch; entryPoint = header.entryPoint; - programs = (ehs::Vector&&)header.programs; - sections = (ehs::Vector&&)header.sections; + programs = (ehs::Vector&&)header.programs; + sections = (ehs::Vector&&)header.sections; - header.endianness = ELFH_END_LE; + header.bitDepth = ELF_BIT_DEPTH_64; + header.endianness = ELF_END_LE; header.abi = ELFH_ABI_SYSTEMV; header.type = ELFH_TYPE_EXEC; - header.arch = ELFH_MARCH_AMD_X86_64; + header.arch = ELF_ARCH_X86_64; header.entryPoint = 0; header.InitializeSections(); @@ -64,11 +64,12 @@ ELF64& ELF64::operator=(ELF64&& header) noexcept return *this; } -ELF64& ELF64::operator=(const ELF64& header) +ELF& ELF::operator=(const ELF& header) { if (this == &header) return *this; + bitDepth = header.bitDepth; endianness = header.endianness; abi = header.abi; type = header.type; @@ -80,27 +81,32 @@ ELF64& ELF64::operator=(const ELF64& header) return *this; } -ehs::UInt_8 ELF64::GetEndianness() const +ehs::UInt_8 ELF::GetBitDepth() const +{ + return bitDepth; +} + +ehs::UInt_8 ELF::GetEndianness() const { return endianness; } -ehs::UInt_8 ELF64::GetABI() const +ehs::UInt_8 ELF::GetABI() const { return abi; } -ehs::UInt_16 ELF64::GetType() const +ehs::UInt_16 ELF::GetType() const { return type; } -ehs::UInt_16 ELF64::GetArchitecture() const +ehs::UInt_16 ELF::GetArchitecture() const { return arch; } -void ELF64::SetEntryPoint(ehs::UInt_16 programIndex) +void ELF::SetEntryPoint(ehs::UInt_16 programIndex) { if (programIndex > programs.End()) { @@ -115,12 +121,12 @@ void ELF64::SetEntryPoint(ehs::UInt_16 programIndex) EHS_LOG_SUCCESS(); } -ELF64_Program* ELF64::GetEntryPoint() const +ELF_Program* ELF::GetEntryPoint() const { return &programs[entryPoint]; } -void ELF64::AddProgram(ELF64_Program newProgram) +void ELF::AddProgram(ELF_Program newProgram) { /* for (ehs::UInt_16 i = 0; i < programs.Size(); i++) @@ -135,10 +141,10 @@ void ELF64::AddProgram(ELF64_Program newProgram) } */ - programs.Push((ELF64_Program&&)newProgram); + programs.Push((ELF_Program&&)newProgram); } -bool ELF64::HasSection(ehs::UInt_64 hashId) +bool ELF::HasSection(ehs::UInt_64 hashId) { for (ehs::UInt_16 i = 0; i < sections.Size(); i++) if (sections[i].GetHashId() == hashId) @@ -147,12 +153,12 @@ bool ELF64::HasSection(ehs::UInt_64 hashId) return false; } -bool ELF64::HasSection(ehs::Str_8& id) +bool ELF::HasSection(ehs::Str_8& id) { return HasSection(id.Hash_64()); } -void ELF64::AddSection(ELF64_Section newSection) +void ELF::AddSection(ELF_Section newSection) { if (HasSection(newSection.GetHashId())) { @@ -162,7 +168,7 @@ void ELF64::AddSection(ELF64_Section newSection) ehs::Str_8 newId = newSection.GetId(); - ELF64_Section* shStrTab = FindShStrTab(); + ELF_Section* shStrTab = FindShStrTab(); ehs::Serializer& data = shStrTab->GetData(); newSection.nameOffset = data.GetOffset(); @@ -172,12 +178,12 @@ void ELF64::AddSection(ELF64_Section newSection) data.SetOffset(data.GetOffset() + newId.Size()); data.Write('\0'); - sections.Push((ELF64_Section&&)newSection); + sections.Push((ELF_Section&&)newSection); EHS_LOG_SUCCESS(); } -ELF64_Section* ELF64::GetSection(ehs::UInt_64 sectionHashId) const +ELF_Section* ELF::GetSection(ehs::UInt_64 sectionHashId) const { for (ehs::UInt_16 i = 0; i < sections.Size(); i++) if (sections[i].GetHashId() == sectionHashId) @@ -186,12 +192,12 @@ ELF64_Section* ELF64::GetSection(ehs::UInt_64 sectionHashId) const return nullptr; } -ELF64_Section* ELF64::GetSection(const ehs::Str_8& sectionId) const +ELF_Section* ELF::GetSection(const ehs::Str_8& sectionId) const { return GetSection(sectionId.Hash_64()); } -ehs::UInt_64 ELF64::GetLastSegmentOffset() const +ehs::UInt_64 ELF::GetLastSegmentOffset() const { ehs::UInt_64 offset = ELFH_SIZE + PRGH_SIZE * programs.Size(); @@ -206,7 +212,7 @@ ehs::UInt_64 ELF64::GetLastSegmentOffset() const return offset; } -ehs::Vec2_u64 ELF64::SegmentRange(ehs::UInt_64 sectionHashId) +ehs::Vec2_u64 ELF::SegmentRange(ehs::UInt_64 sectionHashId) { ehs::UInt_64 offset = ELFH_SIZE + PRGH_SIZE * programs.Size(); if (sectionHashId) @@ -237,19 +243,19 @@ ehs::Vec2_u64 ELF64::SegmentRange(ehs::UInt_64 sectionHashId) return {}; } -ehs::Vec2_u64 ELF64::SegmentRange(const ehs::Str_8& sectionId) +ehs::Vec2_u64 ELF::SegmentRange(const ehs::Str_8& sectionId) { return SegmentRange(sectionId.Hash_64()); } -ehs::Serializer ELF64::Serialize() const +ehs::Serializer ELF::Serialize() const { ehs::Serializer result(ehs::Endianness::LE); - result.Resize(Elf_Magic_Size); - ehs::Util::Copy(&result[0], Elf_Magic, Elf_Magic_Size); - result.SetOffset(Elf_Magic_Size); - result.Write(ELFH_ARCH_64); + // ELF magic number. + result.Write(0x7F454C46); + + result.Write(ELF_BIT_DEPTH_64); result.Write(endianness); result.Write(1); result.Write(abi); @@ -296,15 +302,15 @@ ehs::Serializer ELF64::Serialize() const return result; } -void ELF64::InitializeSections() +void ELF::InitializeSections() { ehs::Str_8 nullName = ".NULL"; - ELF64_Section null(nullName, 0, SECH_TYPE_NULL, 0, 0, 0); + ELF_Section null(nullName, 0, SECH_TYPE_NULL, 0, 0, 0); ehs::Str_8 shStrTabName = ".shstrtab"; - ELF64_Section shStrTab(shStrTabName, 0, SECH_TYPE_STRTAB, SECH_FLAG_STRINGS, 0, 1); + ELF_Section shStrTab(shStrTabName, 0, SECH_TYPE_STRTAB, SECH_FLAG_STRINGS, 0, 1); ehs::Serializer& data = shStrTab.GetData(); @@ -320,11 +326,11 @@ void ELF64::InitializeSections() data.SetOffset(data.GetOffset() + shStrTabName.Size()); data.Write('\0'); - sections.Push((ELF64_Section&&)null); - sections.Push((ELF64_Section&&)shStrTab); + sections.Push((ELF_Section&&)null); + sections.Push((ELF_Section&&)shStrTab); } -ehs::UInt_64 ELF64::RetrieveSectionsOffset() const +ehs::UInt_64 ELF::RetrieveSectionsOffset() const { ehs::UInt_64 result = ELFH_SIZE + PRGH_SIZE * programs.Size(); @@ -339,7 +345,7 @@ ehs::UInt_64 ELF64::RetrieveSectionsOffset() const return result; } -ehs::UInt_16 ELF64::FindShStrTabIndex() const +ehs::UInt_16 ELF::FindShStrTabIndex() const { for (ehs::UInt_16 i = 0; i < sections.Size(); i++) { @@ -356,7 +362,7 @@ ehs::UInt_16 ELF64::FindShStrTabIndex() const return 0; } -ELF64_Section* ELF64::FindShStrTab() const +ELF_Section* ELF::FindShStrTab() const { for (ehs::UInt_16 i = 0; i < sections.Size(); i++) { diff --git a/src/ELF64_Program.cpp b/src/ELF_Program.cpp similarity index 73% rename from src/ELF64_Program.cpp rename to src/ELF_Program.cpp index 5dd844d..1b1901e 100644 --- a/src/ELF64_Program.cpp +++ b/src/ELF_Program.cpp @@ -1,24 +1,24 @@ -#include "ELF64_Program.h" +#include "arctyx/ELF_Program.h" -ELF64_Program::ELF64_Program() +ELF_Program::ELF_Program() : type(PRGH_TYPE_LOAD), flags(PRGH_FLAG_EXEC | PRGH_FLAG_READ), offset(0), vAddr(0), pAddr(0), fileSize(0), adjustedMemSize(0), align(0x200000) { } -ELF64_Program::ELF64_Program(ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 offset, ehs::UInt_64 address, ehs::UInt_64 size, ehs::UInt_64 adjustedMemSize, ehs::UInt_64 align) +ELF_Program::ELF_Program(ehs::UInt_32 type, ehs::UInt_32 flags, ehs::UInt_64 offset, ehs::UInt_64 address, ehs::UInt_64 size, ehs::UInt_64 adjustedMemSize, ehs::UInt_64 align) : type(type), flags(flags), offset(offset), vAddr(address), pAddr(address), fileSize(size), adjustedMemSize(adjustedMemSize), align(align) { } -ELF64_Program::ELF64_Program(ehs::Serializer& data) +ELF_Program::ELF_Program(ehs::Serializer& data) : type(PRGH_TYPE_LOAD), flags(PRGH_FLAG_EXEC | PRGH_FLAG_READ), offset(0), vAddr(0), pAddr(0), fileSize(0), adjustedMemSize(0), align(0x200000) { } -ELF64_Program::ELF64_Program(ELF64_Program&& program) noexcept +ELF_Program::ELF_Program(ELF_Program&& program) noexcept : type(program.type), flags(program.flags), offset(program.offset), vAddr(program.vAddr), pAddr(program.pAddr), fileSize(program.fileSize), adjustedMemSize(program.adjustedMemSize), align(program.align) { @@ -32,13 +32,13 @@ ELF64_Program::ELF64_Program(ELF64_Program&& program) noexcept program.align = 0x200000; } -ELF64_Program::ELF64_Program(const ELF64_Program& program) +ELF_Program::ELF_Program(const ELF_Program& program) : type(program.type), flags(program.flags), offset(program.offset), vAddr(program.vAddr), pAddr(program.pAddr), fileSize(program.fileSize), adjustedMemSize(program.adjustedMemSize), align(program.align) { } -ELF64_Program& ELF64_Program::operator=(ELF64_Program&& program) noexcept +ELF_Program& ELF_Program::operator=(ELF_Program&& program) noexcept { if (this == &program) return *this; @@ -64,7 +64,7 @@ ELF64_Program& ELF64_Program::operator=(ELF64_Program&& program) noexcept return *this; } -ELF64_Program& ELF64_Program::operator=(const ELF64_Program& program) +ELF_Program& ELF_Program::operator=(const ELF_Program& program) { if (this == &program) return *this; @@ -81,22 +81,22 @@ ELF64_Program& ELF64_Program::operator=(const ELF64_Program& program) return *this; } -ehs::UInt_32 ELF64_Program::GetType() const +ehs::UInt_32 ELF_Program::GetType() const { return type; } -ehs::UInt_32 ELF64_Program::GetFlags() const +ehs::UInt_32 ELF_Program::GetFlags() const { return flags; } -ehs::UInt_64 ELF64_Program::GetAlign() const +ehs::UInt_64 ELF_Program::GetAlign() const { return align; } -void ELF64_Program::Serialize(ehs::Serializer& inData) const +void ELF_Program::Serialize(ehs::Serializer& inData) const { inData.Write(type); inData.Write(flags); diff --git a/src/ELF64_Section.cpp b/src/ELF_Section.cpp similarity index 71% rename from src/ELF64_Section.cpp rename to src/ELF_Section.cpp index 6eaea14..9bd6a21 100644 --- a/src/ELF64_Section.cpp +++ b/src/ELF_Section.cpp @@ -1,19 +1,19 @@ -#include "ELF64_Section.h" +#include "arctyx/ELF_Section.h" -ELF64_Section::ELF64_Section() +ELF_Section::ELF_Section() : hashId(0), nameOffset(0), fileOffset(0), type(0), flags(0), vAddr(0), segmentOffset(0), link(0), info(0), align(0), entries(0), data(ehs::Endianness::LE) { } -ELF64_Section::ELF64_Section(ehs::Str_8 id, ehs::UInt_64 fileOffset, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 vAddr, ehs::UInt_64 align) +ELF_Section::ELF_Section(ehs::Str_8 id, ehs::UInt_64 fileOffset, ehs::UInt_32 type, ehs::UInt_64 flags, ehs::UInt_64 vAddr, ehs::UInt_64 align) : hashId(id.Hash_64()), id((ehs::Str_8&&)id), nameOffset(0), fileOffset(fileOffset), type(type), flags(flags), vAddr(vAddr), segmentOffset(0), link(0), info(0), align(align), entries(0), data(ehs::Endianness::LE) { } -ELF64_Section::ELF64_Section(ELF64_Section&& sect) noexcept +ELF_Section::ELF_Section(ELF_Section&& sect) noexcept : hashId(sect.hashId), id((ehs::Str_8&&)sect.id), nameOffset(sect.nameOffset), fileOffset(sect.fileOffset), type(sect.type), flags(sect.flags), vAddr(sect.vAddr), segmentOffset(sect.segmentOffset), link(sect.link), info(sect.info), align(sect.align), entries(sect.entries), @@ -32,14 +32,14 @@ ELF64_Section::ELF64_Section(ELF64_Section&& sect) noexcept sect.entries = 0; } -ELF64_Section::ELF64_Section(const ELF64_Section& sect) +ELF_Section::ELF_Section(const ELF_Section& sect) : hashId(sect.hashId), id(sect.id), nameOffset(0), fileOffset(sect.fileOffset), type(sect.type), flags(sect.flags), vAddr(sect.vAddr), segmentOffset(sect.segmentOffset), link(sect.link), info(sect.info), align(sect.align), entries(sect.entries), data(sect.data) { } -ELF64_Section& ELF64_Section::operator=(ELF64_Section&& sect) noexcept +ELF_Section& ELF_Section::operator=(ELF_Section&& sect) noexcept { if (this == §) return *this; @@ -74,7 +74,7 @@ ELF64_Section& ELF64_Section::operator=(ELF64_Section&& sect) noexcept return *this; } -ELF64_Section& ELF64_Section::operator=(const ELF64_Section& sect) +ELF_Section& ELF_Section::operator=(const ELF_Section& sect) { if (this == §) return *this; @@ -96,62 +96,62 @@ ELF64_Section& ELF64_Section::operator=(const ELF64_Section& sect) return *this; } -ehs::UInt_64 ELF64_Section::GetHashId() const +ehs::UInt_64 ELF_Section::GetHashId() const { return hashId; } -ehs::Str_8 ELF64_Section::GetId() const +ehs::Str_8 ELF_Section::GetId() const { return id; } -ehs::UInt_32 ELF64_Section::GetType() const +ehs::UInt_32 ELF_Section::GetType() const { return type; } -ehs::UInt_64 ELF64_Section::GetFlags() const +ehs::UInt_64 ELF_Section::GetFlags() const { return flags; } -ehs::UInt_64 ELF64_Section::GetVirtualAddress() const +ehs::UInt_64 ELF_Section::GetVirtualAddress() const { return vAddr; } -void ELF64_Section::SetLink(ehs::UInt_32 newLink) +void ELF_Section::SetLink(ehs::UInt_32 newLink) { link = newLink; } -void ELF64_Section::SetInfo(ehs::UInt_32 newInfo) +void ELF_Section::SetInfo(ehs::UInt_32 newInfo) { info = newInfo; } -ehs::UInt_64 ELF64_Section::GetAlignment() const +ehs::UInt_64 ELF_Section::GetAlignment() const { return align; } -void ELF64_Section::SetEntries(ehs::UInt_64 newEntries) +void ELF_Section::SetEntries(ehs::UInt_64 newEntries) { entries = newEntries; } -void ELF64_Section::SetData(ehs::Serializer newData) +void ELF_Section::SetData(ehs::Serializer newData) { data = (ehs::Serializer&&)newData; } -ehs::Serializer& ELF64_Section::GetData() +ehs::Serializer& ELF_Section::GetData() { return data; } -void ELF64_Section::Serialize(ehs::Serializer& inData) const +void ELF_Section::Serialize(ehs::Serializer& inData) const { inData.Write(nameOffset); inData.Write(type); diff --git a/src/ELF64_Sym.cpp b/src/ELF_Sym.cpp similarity index 55% rename from src/ELF64_Sym.cpp rename to src/ELF_Sym.cpp index 9120d5b..5dd90f0 100644 --- a/src/ELF64_Sym.cpp +++ b/src/ELF_Sym.cpp @@ -1,17 +1,17 @@ -#include "ELF64_Sym.h" +#include "arctyx/ELF_Sym.h" -ELF64_Sym::ELF64_Sym() +ELF_Sym::ELF_Sym() : nameOffset(0), type(0), visibility(0), section(0), value(0), size(0) { } -ELF64_Sym::ELF64_Sym(ehs::UInt_32 nameOffset, ehs::UInt_8 type, ehs::UInt_8 visibility, ehs::UInt_16 section, ehs::UInt_64 value, ehs::UInt_64 size) +ELF_Sym::ELF_Sym(ehs::UInt_32 nameOffset, ehs::UInt_8 type, ehs::UInt_8 visibility, ehs::UInt_16 section, ehs::UInt_64 value, ehs::UInt_64 size) : nameOffset(nameOffset), type(type), visibility(visibility), section(section), value(value), size(size) { } -void ELF64_Sym::Serialize(ehs::Serializer& inData) const +void ELF_Sym::Serialize(ehs::Serializer& inData) const { inData.Write(nameOffset); inData.Write(type); diff --git a/src/Linker.cpp b/src/Linker.cpp new file mode 100644 index 0000000..56fabb2 --- /dev/null +++ b/src/Linker.cpp @@ -0,0 +1 @@ +#include "arctyx/Linker.h" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 30aa9cd..70d7407 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ -#include "ELF64.h" -#include "ELF64_Sym.h" +#include "arctyx/ELF.h" +#include "arctyx/ELF_Sym.h" #include #include @@ -7,7 +7,7 @@ int main() { - ehs::Initialize("Compiler", "Alpha", {1, 0, 0}); + ehs::Initialize("Arctyx", "Alpha", {1, 0, 0}); ehs::Vector args = ehs::Console::GetArgs_8(); if (args.Size() <= 1) @@ -22,15 +22,15 @@ int main() ehs::Serializer code = codeFile.ReadSerializer_64(ehs::Endianness::LE, codeFile.Size()); codeFile.Release(); - ELF64 executable(ELFH_END_LE, ELFH_ABI_SYSTEMV, ELFH_TYPE_EXEC, ELFH_MARCH_AMD_X86_64); + ELF executable(ELF_BIT_DEPTH_64, ELF_END_LE, ELFH_ABI_SYSTEMV, ELFH_TYPE_EXEC, ELF_ARCH_X86_64); ehs::UInt_64 lSgmtOffset = executable.GetLastSegmentOffset(); - ELF64_Section text(".text", 0x1000, SECH_TYPE_PROGBITS, SECH_FLAG_ALLOC | SECH_FLAG_EXEC, 0x401000, 16); + ELF_Section text(".text", 0x1000, SECH_TYPE_PROGBITS, SECH_FLAG_ALLOC | SECH_FLAG_EXEC, 0x401000, 16); text.SetData(code); - executable.AddSection((ELF64_Section&&)text); + executable.AddSection((ELF_Section&&)text); - ELF64_Section strTab(".strtab", 0, SECH_TYPE_STRTAB, 0, 0, 1); + ELF_Section strTab(".strtab", 0, SECH_TYPE_STRTAB, 0, 0, 1); ehs::Serializer& strTabData = strTab.GetData(); constexpr ehs::Char_8 symName[] = "\0_start\0"; @@ -38,21 +38,21 @@ int main() ehs::Util::Copy(&strTabData[0], symName, sizeof(symName)); strTabData.SetOffset(sizeof(symName)); - executable.AddSection((ELF64_Section&&)strTab); + executable.AddSection((ELF_Section&&)strTab); - ELF64_Section symTab(".symtab", 0, SECH_TYPE_SYMTAB, 0, 0, 8); + ELF_Section symTab(".symtab", 0, SECH_TYPE_SYMTAB, 0, 0, 8); symTab.SetLink(3); symTab.SetInfo(2); symTab.SetEntries(SYMH_SIZE); ehs::Serializer& symTabData = symTab.GetData(); - ELF64_Sym nullSym; + ELF_Sym nullSym; nullSym.Serialize(symTabData); - ELF64_Sym _start(1, SYM_INFO(SYM_BIND_GLOBAL, SYM_TYPE_NOTYPE), SYM_VIS_DEFAULT, 2, 0x401000, 0); + ELF_Sym _start(1, SYM_INFO(SYM_BIND_GLOBAL, SYM_TYPE_NOTYPE), SYM_VIS_DEFAULT, 2, 0x401000, 0); _start.Serialize(symTabData); - executable.AddSection((ELF64_Section&&)symTab); + executable.AddSection((ELF_Section&&)symTab); executable.AddProgram({PRGH_TYPE_LOAD, PRGH_FLAG_READ, 0, 0x400000, lSgmtOffset, 0, 0x1000}); executable.AddProgram({PRGH_TYPE_LOAD, PRGH_FLAG_EXEC | PRGH_FLAG_READ, 0x1000, 0x401000, code.Size(), 0, 0x1000});