Luna/kernel/src/binfmt/ELF.h

69 lines
2.2 KiB
C
Raw Normal View History

2022-12-23 12:09:21 +00:00
#pragma once
#include "binfmt/BinaryFormat.h"
#include "fs/VFS.h"
#include "memory/AddressSpace.h"
#include <luna/SharedPtr.h>
2022-12-23 12:09:21 +00:00
#include <luna/Types.h>
#define ELFMAG "\177ELF"
#define SELFMAG 4
#define EI_CLASS 4 /* File class byte index */
#define ELFCLASS64 2 /* 64-bit objects */
#define EI_DATA 5 /* Data encoding byte index */
#define ELFDATA2LSB 1 /* 2's complement, little endian */
#define ET_EXEC 2 /* Executable file */
#define PT_LOAD 1 /* Loadable program segment */
#ifdef ARCH_X86_64
#define EM_MACH 62 /* AMD x86-64 architecture */
#else
#error "Unknown architecture."
#endif
typedef struct
{
u8 e_ident[16]; /* Magic number and other info */
u16 e_type; /* Object file type */
u16 e_machine; /* Architecture */
u32 e_version; /* Object file version */
u64 e_entry; /* Entry point virtual address */
u64 e_phoff; /* Program header table file offset */
u64 e_shoff; /* Section header table file offset */
u32 e_flags; /* Processor-specific flags */
u16 e_ehsize; /* ELF header size in bytes */
u16 e_phentsize; /* Program header table entry size */
u16 e_phnum; /* Program header table entry count */
u16 e_shentsize; /* Section header table entry size */
u16 e_shnum; /* Section header table entry count */
u16 e_shstrndx; /* Section header string table index */
} Elf64_Ehdr;
typedef struct
{
u32 p_type; /* Segment type */
u32 p_flags; /* Segment flags */
u64 p_offset; /* Segment file offset */
u64 p_vaddr; /* Segment virtual address */
u64 p_paddr; /* Segment physical address */
u64 p_filesz; /* Segment size in file */
u64 p_memsz; /* Segment size in memory */
u64 p_align; /* Segment alignment */
} Elf64_Phdr;
class ELFLoader : public BinaryFormatLoader
2022-12-23 12:09:21 +00:00
{
public:
Result<bool> sniff() override;
Result<u64> load(AddressSpace* space) override;
2022-12-23 12:09:21 +00:00
Result<Vector<String>> cmdline(Vector<String> args) override;
StringView format() const override
{
return "elf";
}
ELFLoader(SharedPtr<VFS::Inode> inode);
static Result<SharedPtr<BinaryFormatLoader>> create(SharedPtr<VFS::Inode> inode, void*);
2023-01-02 12:07:29 +00:00
};