Moved ELF structures to C header

This commit is contained in:
2021-04-12 01:21:59 -05:00
parent e235e9bad2
commit 74fe6e1a58
3 changed files with 119 additions and 186 deletions

119
include/elf.h Normal file
View File

@@ -0,0 +1,119 @@
#pragma once
#include "types/physaddr.h"
#include <stdint.h>
const uint32_t elf_magic_number = 0x464c457f;
enum elf_endianness_t
{
ELF_LITTLE_ENDIAN = 1,
ELF_BIG_ENDIAN = 2
};
enum elf_isa_t
{
ELF_ISA_NA = 0x00,
ELF_ISA_x86 = 0x03,
ELF_ISA_MIPS = 0x08,
ELF_ISA_PPC = 0x14,
ELF_ISA_PPC64 = 0x15,
ELF_ISA_ARM = 0x28,
ELF_ISA_x86_64 = 0x3E,
ELF_ISA_AARCH64 = 0xB7
};
enum elf_segment_type_t
{
ELF_UNUSED = 0,
ELF_LOAD = 1,
ELF_DYNAMIC = 2
};
struct elf_file_header_t
{
uint32_t magic;
char size;
char endianness;
char version;
char abi;
char abi_version;
char reserved[7];
uint16_t type;
uint16_t machine;
uint32_t _version;
void *entry;
#if defined __i386__ || defined __arm__
uint32_t phoffset;
uint32_t shoffset;
#elif defined __x86_64__ || defined __aarch64__
uint64_t phoffset;
uint64_t shoffset;
#endif
uint32_t flags;
uint16_t header_size;
uint16_t phsize;
uint16_t phcount;
uint16_t shsize;
uint16_t shcount;
uint16_t shstrndx;
};
struct elf_program_header_t
{
uint32_t type;
#if defined __i386__ || defined __arm__
uint32_t offset;
void *vaddr;
physaddr_t paddr;
uint32_t filesize;
uint32_t memsize;
uint32_t flags;
uint32_t align;
#elif defined __x86_64__ || defined __aarch64__
uint32_t flags;
uint64_t offset;
void *vaddr;
physaddr_t paddr;
uint64_t filesize;
uint64_t memsize;
uint64_t align;
#endif
};
struct elf_section_header_t
{
uint32_t name;
uint32_t type;
#if defined __i386__ || defined __arm__
uint32_t flags;
void *addr;
uint32_t offset;
uint32_t size;
uint32_t link;
uint32_t info;
uint32_t align;
uint32_t entry_size;
#elif defined __x86_64__ || defined __aarch64__
uint64_t flags;
void *addr;
uint64_t offset;
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t align;
uint64_t entry_size;
#endif
};
#if defined __i386__
static const elf_isa_t HOST_ISA = ELF_ISA_x86;
#elif defined __x86_64__
static const elf_isa_t HOST_ISA = ELF_ISA_x86_64;
#elif defined __arm__
static const elf_isa_t HOST_ISA = ELF_ISA_ARM;
#elif defined __aarch64__
static const elf_isa_t HOST_ISA = ELF_ISA_AARCH64;
#endif
int load_elf_executable(void *file);

View File

@@ -1,44 +0,0 @@
#include "elf.hpp"
#include "util.hpp"
kernelns::ELF::ELF()
{
this->m_fileLocation = (void*) NULL;
}
kernelns::ELF::ELF(void* location)
{
this->m_fileLocation = location;
}
void* kernelns::ELF::entry()
{
Header* fileHeader = (Header*) m_fileLocation;
return fileHeader->entry;
}
int kernelns::ELF::validate()
{
Header* fileHeader = (Header*) m_fileLocation;
if(fileHeader->magic != 0x464c457f)
return -1;
else if((ISA) fileHeader->machine != HOST_ISA)
return -1;
else if((Endianness) fileHeader->endianness != Little)
return -1;
return 0;
}
int kernelns::ELF::load()
{
Header* fileHeader = (Header*) m_fileLocation;
ProgramHeader* programHeader = (ProgramHeader*) ((size_t) m_fileLocation + fileHeader->phoffset);
int count = (int) fileHeader->phcount;
for(int i = 0; i < count; i++)
{
if((SegmentType) programHeader->type != Load)
continue;
memcpy(programHeader->vaddr, m_fileLocation + programHeader->offset, programHeader->filesize);
}
return 0;
}

View File

@@ -1,142 +0,0 @@
#ifndef ELF_H
#define ELF_H
#include "systypes.hpp"
namespace kernelns
{
class ELF
{
public:
enum Endianness
{
Little = 1,
Big = 2
};
enum ISA
{
NA = 0x00,
x86 = 0x03,
MIPS = 0x08,
PPC = 0x14,
PPC64 = 0x15,
ARM = 0x28,
x86_64 = 0x3E,
aarch64 = 0xB7
};
enum SegmentType
{
Unused = 0,
Load = 1,
Dynamic = 2
};
struct Header
{
uint32_t magic;
char size;
char endianness;
char version;
char abi;
char abiVersion;
char reserved[7];
uint16_t type;
uint16_t machine;
uint32_t _version;
void* entry;
#if defined __i386__ || defined __arm__
uint32_t phoffset;
uint32_t shoffset;
#elif defined __x86_64__ || defined __aarch64__
uint64_t phoffset;
uint64_t shoffset;
#endif
uint32_t flags;
uint16_t headerSize;
uint16_t phsize;
uint16_t phcount;
uint16_t shsize;
uint16_t shcount;
uint16_t shstrndx;
};
struct ProgramHeader
{
uint32_t type;
#if defined __i386__ || defined __arm__
uint32_t offset;
void* vaddr;
physaddr_t paddr;
uint32_t filesize;
uint32_t memsize;
uint32_t flags;
uint32_t align;
#elif defined __x86_64__ || defined __aarch64__
uint32_t flags;
uint64_t offset;
void* vaddr;
physaddr_t paddr;
uint64_t filesize;
uint64_t memsize;
uint64_t align;
#endif
};
struct SectionHeader
{
uint32_t name;
uint32_t type;
#if defined __i386__ || defined __arm__
uint32_t flags;
void* addr;
uint32_t offset;
uint32_t size;
uint32_t link;
uint32_t info;
uint32_t align;
uint32_t entrysize;
#elif defined __x86_64__ || defined __aarch64__
uint64_t flags;
void* addr;
uint64_t offset;
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t align;
uint64_t entrysize;
#endif
};
#if defined __i386__
static const ISA HOST_ISA = x86;
#elif defined __x86_64__
static const ISA HOST_ISA = x86_64;
#elif defined __arm__
static const ISA HOST_ISA = ARM;
#elif defined __aarch64__
static const ISA HOST_ISA = aarch64;
#endif
ELF();
ELF(void* location);
void* entry();
int validate();
int load();
private:
void* m_fileLocation;
};
}
#endif