started elf loader

This commit is contained in:
georgemoralis 2023-03-31 08:59:19 +03:00
parent be996892a9
commit 730f9ecdbb
2 changed files with 80 additions and 0 deletions

View File

@ -19,12 +19,48 @@ static self_segment_header* load_self_segments(FsFile& f, u16 num)
return segs; return segs;
} }
static elf_header* load_elf_header(FsFile& f)
{
auto* ehdr = new elf_header;
f.Read(ehdr, sizeof(elf_header));
return ehdr;
}
static elf_program_header* load_program_header(FsFile& f, u64 offset, u16 num)
{
auto* phdr = new elf_program_header[num];
f.Seek(offset,fsSeekMode::fsSeekSet);
f.Read(phdr, sizeof(elf_program_header) * num);
return phdr;
}
static elf_section_header* load_section_header(FsFile& f, u64 offset, u16 num)
{
if (num == 0)//just in case we don't have section headers
{
return nullptr;
}
auto* shdr = new elf_section_header[num];
f.Seek(offset,fsSeekMode::fsSeekSet);
f.Read(shdr, sizeof(elf_section_header) * num);
return shdr;
}
void Elf::Open(const std::string& file_name) void Elf::Open(const std::string& file_name)
{ {
m_f = new FsFile; m_f = new FsFile;
m_f->Open(file_name, fsOpenMode::fsRead); m_f->Open(file_name, fsOpenMode::fsRead);
m_self = load_self(*m_f); m_self = load_self(*m_f);
if (!isSelfFile()) if (!isSelfFile())
{ {
delete m_self; delete m_self;

View File

@ -32,6 +32,50 @@ struct self_segment_header
u64 memory_size; u64 memory_size;
}; };
struct elf_header
{
u08 e_ident[16]; /* ELF identification */
u16 e_type; /* Object file type */
u16 e_machine; /* Machine type */
u32 e_version; /* Object file version */
u64 e_entry; /* Entry point address */
u64 e_phoff; /* Program header offset */
u64 e_shoff; /* Section header offset */
u32 e_flags; /* Processor-specific flags */
u16 e_ehsize; /* ELF header size */
u16 e_phentsize; /* Size of program header entry */
u16 e_phnum; /* Number of program header entries */
u16 e_shentsize; /* Size of section header entry */
u16 e_shnum; /* Number of section header entries */
u16 e_shstrndx; /* Section name string table index */
};
struct elf_program_header
{
u32 p_type; /* Type of segment */
u32 p_flags; /* Segment attributes */
u64 p_offset; /* Offset in file */
u64 p_vaddr; /* Virtual address in memory */
u64 p_paddr; /* Reserved */
u64 p_filesz; /* Size of segment in file */
u64 p_memsz; /* Size of segment in memory */
u64 p_align; /* Alignment of segment */
};
struct elf_section_header
{
u32 sh_name; /* Section name */
u32 sh_type; /* Section type */
u64 sh_flags; /* Section attributes */
u64 sh_addr; /* Virtual address in memory */
u64 sh_offset; /* Offset in file */
u64 sh_size; /* Size of section */
u32 sh_link; /* Link to other section */
u32 sh_info; /* Miscellaneous information */
u64 sh_addralign; /* Address alignment boundary */
u64 sh_entsize; /* Size of entries, if section has table */
};
class Elf class Elf
{ {
public: public: