First commit.

This commit is contained in:
2025-04-08 21:49:27 -07:00
commit 6ae60f21d1
13 changed files with 1363 additions and 0 deletions

374
src/ELF64.cpp Normal file
View File

@@ -0,0 +1,374 @@
#include "ELF64.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)
{
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)
{
InitializeSections();
}
ELF64::ELF64(ehs::Serializer<ehs::UInt_64>& data)
{
}
ELF64::ELF64(ELF64&& header) noexcept
: endianness(header.endianness), abi(header.abi), type(header.type), arch(header.arch),
entryPoint(header.entryPoint), programs((ehs::Vector<ELF64_Program, ehs::UInt_16>&&)header.programs),
sections((ehs::Vector<ELF64_Section, ehs::UInt_16>&&)header.sections)
{
header.endianness = ELFH_END_LE;
header.abi = ELFH_ABI_SYSTEMV;
header.type = ELFH_TYPE_EXEC;
header.arch = ELFH_MARCH_AMD_X86_64;
header.entryPoint = 0;
header.InitializeSections();
}
ELF64::ELF64(const ELF64& header)
: 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
{
if (this == &header)
return *this;
endianness = header.endianness;
abi = header.abi;
type = header.type;
arch = header.arch;
entryPoint = header.entryPoint;
programs = (ehs::Vector<ELF64_Program, ehs::UInt_16>&&)header.programs;
sections = (ehs::Vector<ELF64_Section, ehs::UInt_16>&&)header.sections;
header.endianness = ELFH_END_LE;
header.abi = ELFH_ABI_SYSTEMV;
header.type = ELFH_TYPE_EXEC;
header.arch = ELFH_MARCH_AMD_X86_64;
header.entryPoint = 0;
header.InitializeSections();
return *this;
}
ELF64& ELF64::operator=(const ELF64& header)
{
if (this == &header)
return *this;
endianness = header.endianness;
abi = header.abi;
type = header.type;
arch = header.arch;
entryPoint = header.entryPoint;
programs = header.programs;
sections = header.sections;
return *this;
}
ehs::UInt_8 ELF64::GetEndianness() const
{
return endianness;
}
ehs::UInt_8 ELF64::GetABI() const
{
return abi;
}
ehs::UInt_16 ELF64::GetType() const
{
return type;
}
ehs::UInt_16 ELF64::GetArchitecture() const
{
return arch;
}
void ELF64::SetEntryPoint(ehs::UInt_16 programIndex)
{
if (programIndex > programs.End())
{
EHS_LOG(ehs::LogType::WARN, 0, "The given index, \"" + ehs::Str_8::FromNum(programIndex) +
"\", is outside the bounds of, \"" + ehs::Str_8::FromNum(programs.End()) + "\".");
return;
}
entryPoint = programIndex;
EHS_LOG_SUCCESS();
}
ELF64_Program* ELF64::GetEntryPoint() const
{
return &programs[entryPoint];
}
void ELF64::AddProgram(ELF64_Program newProgram)
{
/*
for (ehs::UInt_16 i = 0; i < programs.Size(); i++)
{
if (programs[i].fileSize < newProgram.fileSize)
continue;
EHS_LOG("Warning", 0, "The new program file size, \"" + ehs::Str_8::FromNum(newProgram.fileSize) +
"\" must be greater than, \"" + ehs::Str_8::FromNum(programs[i].fileSize) + "\".");
return;
}
*/
programs.Push((ELF64_Program&&)newProgram);
}
bool ELF64::HasSection(ehs::UInt_64 hashId)
{
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
if (sections[i].GetHashId() == hashId)
return true;
return false;
}
bool ELF64::HasSection(ehs::Str_8& id)
{
return HasSection(id.Hash_64());
}
void ELF64::AddSection(ELF64_Section newSection)
{
if (HasSection(newSection.GetHashId()))
{
EHS_LOG(ehs::LogType::WARN, 0, "The section, \"" + newSection.GetId() + "\", already exists.");
return;
}
ehs::Str_8 newId = newSection.GetId();
ELF64_Section* shStrTab = FindShStrTab();
ehs::Serializer<ehs::UInt_64>& data = shStrTab->GetData();
newSection.nameOffset = data.GetOffset();
data.Resize(data.Size() + newId.Size() + 1);
ehs::Util::Copy(&data[data.GetOffset()], &newId[0], newId.Size());
data.SetOffset(data.GetOffset() + newId.Size());
data.Write('\0');
sections.Push((ELF64_Section&&)newSection);
EHS_LOG_SUCCESS();
}
ELF64_Section* ELF64::GetSection(ehs::UInt_64 sectionHashId) const
{
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
if (sections[i].GetHashId() == sectionHashId)
return &sections[i];
return nullptr;
}
ELF64_Section* ELF64::GetSection(const ehs::Str_8& sectionId) const
{
return GetSection(sectionId.Hash_64());
}
ehs::UInt_64 ELF64::GetLastSegmentOffset() const
{
ehs::UInt_64 offset = ELFH_SIZE + PRGH_SIZE * programs.Size();
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
{
if (sections[i].fileOffset)
offset = sections[i].fileOffset;
offset += sections[i].GetData().Size();
}
return offset;
}
ehs::Vec2_u64 ELF64::SegmentRange(ehs::UInt_64 sectionHashId)
{
ehs::UInt_64 offset = ELFH_SIZE + PRGH_SIZE * programs.Size();
if (sectionHashId)
{
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
{
if (sections[i].GetHashId() == sectionHashId)
return {offset, sections[i].GetData().Size()};
else
{
if (sections[i].fileOffset)
offset = sections[i].fileOffset;
offset += sections[i].GetData().Size();
}
}
}
else
{
ehs::UInt_64 scale = 0;
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
scale += sections[i].GetData().Size();
return {offset, scale};
}
return {};
}
ehs::Vec2_u64 ELF64::SegmentRange(const ehs::Str_8& sectionId)
{
return SegmentRange(sectionId.Hash_64());
}
ehs::Serializer<ehs::UInt_64> ELF64::Serialize() const
{
ehs::Serializer<ehs::UInt_64> 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<ehs::UInt_8>(ELFH_ARCH_64);
result.Write(endianness);
result.Write<ehs::UInt_8>(1);
result.Write(abi);
result.Write<ehs::UInt_8>(0);
result.Resize(result.Size() + 7);
ehs::Util::Zero(&result[result.GetOffset()], 7);
result.SetOffset(result.GetOffset() + 7);
result.Write(type);
result.Write(arch);
result.Write<ehs::UInt_32>(1);
result.Write(programs[entryPoint].pAddr);
result.Write<ehs::UInt_64>(ELFH_SIZE);
result.Write(RetrieveSectionsOffset());
result.Write<ehs::UInt_32>(0);
result.Write<ehs::UInt_16>(ELFH_SIZE);
result.Write<ehs::UInt_16>(PRGH_SIZE);
result.Write(programs.Size());
result.Write<ehs::UInt_16>(SECH_SIZE);
result.Write(sections.Size());
result.Write(FindShStrTabIndex());
for (ehs::UInt_16 i = 0; i < programs.Size(); i++)
programs[i].Serialize(result);
for (ehs::UInt_16 i = 1; i < sections.Size(); i++)
{
if (sections[i].fileOffset)
{
ehs::UInt_64 padding = sections[i].fileOffset - result.Size();
result.Resize(result.Size() + padding);
ehs::Util::Zero(&result[result.GetOffset()], padding);
result.SetOffset(result.GetOffset() + padding);
}
sections[i].segmentOffset = result.GetOffset();
result.WriteSer(sections[i].GetData());
}
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
sections[i].Serialize(result);
return result;
}
void ELF64::InitializeSections()
{
ehs::Str_8 nullName = ".NULL";
ELF64_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);
ehs::Serializer<ehs::UInt_64>& data = shStrTab.GetData();
data.Resize(nullName.Size() + shStrTabName.Size() + 2);
null.nameOffset = data.GetOffset();
ehs::Util::Copy(&data[data.GetOffset()], &nullName[0], nullName.Size());
data.SetOffset(data.GetOffset() + nullName.Size());
data.Write('\0');
shStrTab.nameOffset = data.GetOffset();
ehs::Util::Copy(&data[data.GetOffset()], &shStrTabName[0], shStrTabName.Size());
data.SetOffset(data.GetOffset() + shStrTabName.Size());
data.Write('\0');
sections.Push((ELF64_Section&&)null);
sections.Push((ELF64_Section&&)shStrTab);
}
ehs::UInt_64 ELF64::RetrieveSectionsOffset() const
{
ehs::UInt_64 result = ELFH_SIZE + PRGH_SIZE * programs.Size();
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
{
if (sections[i].fileOffset)
result = sections[i].fileOffset;
result += sections[i].GetData().Size();
}
return result;
}
ehs::UInt_16 ELF64::FindShStrTabIndex() const
{
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
{
if (sections[i].GetHashId() == 7974729948679187860ull)
{
EHS_LOG_SUCCESS();
return i;
}
}
EHS_LOG(ehs::LogType::ERR, 0, "Could not find the \".shstrtab\" index.");
return 0;
}
ELF64_Section* ELF64::FindShStrTab() const
{
for (ehs::UInt_16 i = 0; i < sections.Size(); i++)
{
if (sections[i].GetHashId() == 7974729948679187860ull)
{
EHS_LOG_SUCCESS();
return &sections[i];
}
}
EHS_LOG(ehs::LogType::ERR, 0, "Could not find the \".shstrtab\".");
return nullptr;
}

109
src/ELF64_Program.cpp Normal file
View File

@@ -0,0 +1,109 @@
#include "ELF64_Program.h"
ELF64_Program::ELF64_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)
: type(type), flags(flags), offset(offset), vAddr(address), pAddr(address), fileSize(size),
adjustedMemSize(adjustedMemSize), align(align)
{
}
ELF64_Program::ELF64_Program(ehs::Serializer<ehs::UInt_64>& 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
: type(program.type), flags(program.flags), offset(program.offset), vAddr(program.vAddr), pAddr(program.pAddr),
fileSize(program.fileSize), adjustedMemSize(program.adjustedMemSize), align(program.align)
{
program.type = PRGH_TYPE_LOAD;
program.flags = PRGH_FLAG_EXEC | PRGH_FLAG_READ;
program.offset = 0;
program.vAddr = 0;
program.pAddr = 0;
program.fileSize = 0;
program.adjustedMemSize = 0;
program.align = 0x200000;
}
ELF64_Program::ELF64_Program(const ELF64_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
{
if (this == &program)
return *this;
type = program.type;
flags = program.flags;
offset = program.offset;
vAddr = program.vAddr;
pAddr = program.pAddr;
fileSize = program.fileSize;
adjustedMemSize = program.adjustedMemSize;
align = program.align;
program.type = PRGH_TYPE_LOAD;
program.flags = PRGH_FLAG_EXEC | PRGH_FLAG_READ;
program.offset = 0;
program.vAddr = 0;
program.pAddr = 0;
program.fileSize = 0;
program.adjustedMemSize = 0;
program.align = 0x200000;
return *this;
}
ELF64_Program& ELF64_Program::operator=(const ELF64_Program& program)
{
if (this == &program)
return *this;
type = program.type;
flags = program.flags;
offset = program.offset;
vAddr = program.vAddr;
pAddr = program.pAddr;
fileSize = program.fileSize;
adjustedMemSize = program.adjustedMemSize;
align = program.align;
return *this;
}
ehs::UInt_32 ELF64_Program::GetType() const
{
return type;
}
ehs::UInt_32 ELF64_Program::GetFlags() const
{
return flags;
}
ehs::UInt_64 ELF64_Program::GetAlign() const
{
return align;
}
void ELF64_Program::Serialize(ehs::Serializer<ehs::UInt_64>& inData) const
{
inData.Write(type);
inData.Write(flags);
inData.Write(offset);
inData.Write(vAddr);
inData.Write(pAddr);
inData.Write(fileSize);
inData.Write(fileSize + adjustedMemSize);
inData.Write(align);
}

166
src/ELF64_Section.cpp Normal file
View File

@@ -0,0 +1,166 @@
#include "ELF64_Section.h"
ELF64_Section::ELF64_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)
: 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
: 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),
data((ehs::Serializer<ehs::UInt_64>&&)sect.data)
{
sect.hashId = 0;
sect.nameOffset = 0;
sect.fileOffset = 0;
sect.type = 0;
sect.flags = 0;
sect.vAddr = 0;
sect.segmentOffset = 0;
sect.link = 0;
sect.info = 0;
sect.align = 0;
sect.entries = 0;
}
ELF64_Section::ELF64_Section(const ELF64_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
{
if (this == &sect)
return *this;
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;
data = (ehs::Serializer<ehs::UInt_64>&&)sect.data;
sect.hashId = 0;
sect.nameOffset = 0;
sect.fileOffset = 0;
sect.type = 0;
sect.flags = 0;
sect.vAddr = 0;
sect.segmentOffset = 0;
sect.link = 0;
sect.info = 0;
sect.align = 0;
sect.entries = 0;
sect.data = {ehs::Endianness::LE};
return *this;
}
ELF64_Section& ELF64_Section::operator=(const ELF64_Section& sect)
{
if (this == &sect)
return *this;
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;
return *this;
}
ehs::UInt_64 ELF64_Section::GetHashId() const
{
return hashId;
}
ehs::Str_8 ELF64_Section::GetId() const
{
return id;
}
ehs::UInt_32 ELF64_Section::GetType() const
{
return type;
}
ehs::UInt_64 ELF64_Section::GetFlags() const
{
return flags;
}
ehs::UInt_64 ELF64_Section::GetVirtualAddress() const
{
return vAddr;
}
void ELF64_Section::SetLink(ehs::UInt_32 newLink)
{
link = newLink;
}
void ELF64_Section::SetInfo(ehs::UInt_32 newInfo)
{
info = newInfo;
}
ehs::UInt_64 ELF64_Section::GetAlignment() const
{
return align;
}
void ELF64_Section::SetEntries(ehs::UInt_64 newEntries)
{
entries = newEntries;
}
void ELF64_Section::SetData(ehs::Serializer<ehs::UInt_64> newData)
{
data = (ehs::Serializer<ehs::UInt_64>&&)newData;
}
ehs::Serializer<ehs::UInt_64>& ELF64_Section::GetData()
{
return data;
}
void ELF64_Section::Serialize(ehs::Serializer<ehs::UInt_64>& inData) const
{
inData.Write(nameOffset);
inData.Write(type);
inData.Write(flags);
inData.Write(vAddr);
inData.Write(segmentOffset);
inData.Write(data.Size());
inData.Write(link);
inData.Write(info);
inData.Write(align);
inData.Write(entries);
}

23
src/ELF64_Sym.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "ELF64_Sym.h"
ELF64_Sym::ELF64_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)
: nameOffset(nameOffset), type(type), visibility(visibility), section(section), value(value), size(size)
{
}
void ELF64_Sym::Serialize(ehs::Serializer<ehs::UInt_64>& inData) const
{
inData.Write(nameOffset);
inData.Write(type);
inData.Write(visibility);
inData.Write(section);
inData.Write(value);
inData.Write(size);
}

68
src/main.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include "ELF64.h"
#include "ELF64_Sym.h"
#include <ehs/EHS.h>
#include <ehs/io/Console.h>
#include <ehs/io/File.h>
int main()
{
ehs::Initialize("Compiler", "Alpha", {1, 0, 0});
ehs::Vector<ehs::Str_8> args = ehs::Console::GetArgs_8();
if (args.Size() <= 1)
{
ehs::Console::Write_8(ehs::GetAppName_8() + " " + ehs::GetAppVersionId_8() + " v" + ehs::Str_8::FromNum(ehs::GetVersion().major) + "." +
ehs::Str_8::FromNum(ehs::GetVersion().minor) + "." + ehs::Str_8::FromNum(ehs::GetVersion().patch));
return 0;
}
ehs::File codeFile(args[1].RemoveAll("\""), ehs::Mode::READ, ehs::Disposition::OPEN);
ehs::Serializer<ehs::UInt_64> 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);
ehs::UInt_64 lSgmtOffset = executable.GetLastSegmentOffset();
ELF64_Section text(".text", 0x1000, SECH_TYPE_PROGBITS, SECH_FLAG_ALLOC | SECH_FLAG_EXEC, 0x401000, 16);
text.SetData(code);
executable.AddSection((ELF64_Section&&)text);
ELF64_Section strTab(".strtab", 0, SECH_TYPE_STRTAB, 0, 0, 1);
ehs::Serializer<ehs::UInt_64>& strTabData = strTab.GetData();
constexpr ehs::Char_8 symName[] = "\0_start\0";
strTabData.Resize(sizeof(symName));
ehs::Util::Copy(&strTabData[0], symName, sizeof(symName));
strTabData.SetOffset(sizeof(symName));
executable.AddSection((ELF64_Section&&)strTab);
ELF64_Section symTab(".symtab", 0, SECH_TYPE_SYMTAB, 0, 0, 8);
symTab.SetLink(3);
symTab.SetInfo(2);
symTab.SetEntries(SYMH_SIZE);
ehs::Serializer<ehs::UInt_64>& symTabData = symTab.GetData();
ELF64_Sym nullSym;
nullSym.Serialize(symTabData);
ELF64_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.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});
executable.SetEntryPoint(1);
ehs::File file(args[2].RemoveAll("\""), ehs::Mode::WRITE, ehs::Disposition::CREATE_PERSISTENT);
file.WriteSerializer_64(executable.Serialize());
ehs::Uninitialize();
return 0;
}