130 lines
3.5 KiB
C
130 lines
3.5 KiB
C
/* ELF64 Object representation and parsing header. */
|
|
|
|
#include "cdef.h"
|
|
#include "proc.h"
|
|
|
|
typedef uint64 Elf64_Addr;
|
|
typedef uint64 Elf64_Off;
|
|
typedef uint16 Elf64_Half;
|
|
typedef uint32 Elf64_Word;
|
|
typedef uint32 Elf64_Sword;
|
|
typedef uint64 Elf64_Xword;
|
|
typedef uint64 Elf64_Sxword;
|
|
// This is custom
|
|
typedef uint8 uchar;
|
|
|
|
|
|
/* HEADER */
|
|
|
|
typedef struct
|
|
{
|
|
uchar e_ident[16];
|
|
Elf64_Half e_type;
|
|
Elf64_Half e_machine;
|
|
Elf64_Word e_version;
|
|
Elf64_Addr e_entry;
|
|
Elf64_Off e_phoff;
|
|
Elf64_Off e_shoff;
|
|
Elf64_Word e_flags;
|
|
Elf64_Half e_ehsize;
|
|
Elf64_Half e_phentsize;
|
|
Elf64_Half e_phnum;
|
|
Elf64_Half e_shentsize;
|
|
Elf64_Half e_shnum;
|
|
Elf64_Half e_shstrndx;
|
|
} elf64_hdr;
|
|
|
|
#define EI_MAG0 0 // should be \x7f
|
|
#define EI_MAG1 1 // E
|
|
#define EI_MAG2 2 // L
|
|
#define EI_MAG3 3 // F
|
|
|
|
#define ELFMAG0 '\x7f'
|
|
#define ELFMAG1 'E'
|
|
#define ELFMAG2 'L'
|
|
#define ELFMAG3 'F'
|
|
|
|
#define EI_CLASS 4
|
|
#define EI_DATA 5
|
|
#define EI_VERSION 6
|
|
#define EI_OSABI 7
|
|
#define EI_ABIVERSION 8
|
|
#define EI_PAD 9
|
|
#define EI_NIDENT 16
|
|
|
|
#define ELFCLASS32 1
|
|
#define ELFCLASS64 2
|
|
|
|
#define ELFDATA2LSB 1
|
|
#define ELFDATA2MSB 2
|
|
|
|
#define ELFOSABI_SYSV 0
|
|
#define ELFOSABI_HPUX 1
|
|
#define ELFOSABI_STANDALONE 255
|
|
|
|
#define ET_NONE 0
|
|
#define ET_REL 1
|
|
#define ET_EXEC 2
|
|
#define ET_DYN 3
|
|
#define ET_CORE 4
|
|
|
|
#define ET_LOOS 0xFE00
|
|
#define ET_HIOS 0XFEFF
|
|
#define ET_LOPROC 0XFF00
|
|
#define ET_HIPROC 0XFFFF
|
|
|
|
#define EV_CURRENT 1
|
|
|
|
typedef struct
|
|
{
|
|
Elf64_Word p_type; /* Segment type */
|
|
Elf64_Word p_flags; /* Segment flags */
|
|
Elf64_Off p_offset; /* Segment file offset */
|
|
Elf64_Addr p_vaddr; /* Segment virtual address */
|
|
Elf64_Addr p_paddr; /* Segment physical address */
|
|
Elf64_Xword p_filesz; /* Segment size in file */
|
|
Elf64_Xword p_memsz; /* Segment size in memory */
|
|
Elf64_Xword p_align; /* Segment alignment */
|
|
} elf64_phdr;
|
|
|
|
/* Special value for e_phnum. This indicates that the real number of
|
|
program headers is too large to fit into e_phnum. Instead the real
|
|
value is in the field sh_info of section 0. */
|
|
|
|
#define PN_XNUM 0xffff
|
|
|
|
/* Legal values for p_type (segment type). */
|
|
|
|
#define PT_NULL 0 /* Program header table entry unused */
|
|
#define PT_LOAD 1 /* Loadable program segment */
|
|
#define PT_DYNAMIC 2 /* Dynamic linking information */
|
|
#define PT_INTERP 3 /* Program interpreter */
|
|
#define PT_NOTE 4 /* Auxiliary information */
|
|
#define PT_SHLIB 5 /* Reserved */
|
|
#define PT_PHDR 6 /* Entry for header table itself */
|
|
#define PT_TLS 7 /* Thread-local storage segment */
|
|
#define PT_NUM 8 /* Number of defined types */
|
|
#define PT_LOOS 0x60000000 /* Start of OS-specific */
|
|
#define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
|
|
#define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */
|
|
#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
|
|
#define PT_LOSUNW 0x6ffffffa
|
|
#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */
|
|
#define PT_SUNWSTACK 0x6ffffffb /* Stack segment */
|
|
#define PT_HISUNW 0x6fffffff
|
|
#define PT_HIOS 0x6fffffff /* End of OS-specific */
|
|
#define PT_LOPROC 0x70000000 /* Start of processor-specific */
|
|
#define PT_HIPROC 0x7fffffff /* End of processor-specific */
|
|
|
|
/* Legal values for p_flags (segment flags). */
|
|
|
|
#define PF_X (1 << 0) /* Segment is executable */
|
|
#define PF_W (1 << 1) /* Segment is writable */
|
|
#define PF_R (1 << 2) /* Segment is readable */
|
|
#define PF_MASKOS 0x0ff00000 /* OS-specific */
|
|
#define PF_MASKPROC 0xf0000000 /* Processor-specific */
|
|
|
|
|
|
int32 elf_load_file(struct pcb *proc, void *file, void **entry);
|
|
|