miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
elf.h
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2026 Christian Spoo
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef _MINIOS_FS_ELF_H_
24#define _MINIOS_FS_ELF_H_
25
26#include <miniOS/types.h>
27
28/* ELF magic */
29#define ELFMAG "\177ELF"
30
31/* e_type values */
32#define ET_EXEC 2
33#define ET_DYN 3 /* position-independent executable (PIE) */
34
35/* e_machine values */
36#define EM_X86_64 62
37
38/* p_type values */
39#define PT_LOAD 1
40#define PT_DYNAMIC 2
41
42/* Dynamic section tag values */
43#define DT_NULL 0
44#define DT_RELA 7
45#define DT_RELASZ 8
46#define DT_RELAENT 9
47
48/* Relocation type */
49#define R_X86_64_RELATIVE 8
50#define ELF64_R_TYPE(i) ((uint32_t)(i))
51
52/* Load base for PIE binaries — kernel maps ET_DYN at this VA */
53#define ELF_PIE_BASE 0x400000ULL
54
55/* p_flags bit masks */
56#define PF_X 1 /* Execute */
57#define PF_W 2 /* Write */
58#define PF_R 4 /* Read */
59
60/* ELF64 executable header — only fields needed for ET_EXEC loading */
61typedef struct {
62 uint8_t e_ident[16]; /* Magic number and other info */
63 uint16_t e_type; /* Object file type (ET_EXEC = 2) */
64 uint16_t e_machine; /* Architecture (EM_X86_64 = 62) */
65 uint32_t e_version; /* Object file version */
66 uint64_t e_entry; /* Entry point virtual address */
67 uint64_t e_phoff; /* Program header table file offset */
68 uint64_t e_shoff; /* Section header table file offset */
69 uint32_t e_flags; /* Processor-specific flags */
70 uint16_t e_ehsize; /* ELF header size in bytes */
71 uint16_t e_phentsize; /* Program header table entry size */
72 uint16_t e_phnum; /* Program header table entry count */
73 uint16_t e_shentsize; /* Section header table entry size */
74 uint16_t e_shnum; /* Section header table entry count */
75 uint16_t e_shstrndx; /* Section header string table index */
76} __attribute__((packed)) Elf64_Ehdr;
77
78/* ELF64 program header — only fields needed for PT_LOAD mapping */
79typedef struct {
80 uint32_t p_type; /* Segment type (PT_LOAD = 1) */
81 uint32_t p_flags; /* Segment flags (PF_X / PF_W / PF_R) */
82 uint64_t p_offset; /* Segment file offset */
83 uint64_t p_vaddr; /* Segment virtual address */
84 uint64_t p_paddr; /* Segment physical address */
85 uint64_t p_filesz; /* Segment size in file */
86 uint64_t p_memsz; /* Segment size in memory */
87 uint64_t p_align; /* Segment alignment */
88} __attribute__((packed)) Elf64_Phdr;
89
90/* ELF64 dynamic section entry */
91typedef struct {
93 union { uint64_t d_val; uint64_t d_ptr; } d_un;
94} Elf64_Dyn;
95
96/* ELF64 relocation with addend */
102
103/* Load a static ELF binary from an open VFS fd.
104 * Maps all PT_LOAD segments into the current address space (PAGE_USER).
105 * On success: *entry_out = e_entry. If image_end_out is non-NULL, receives the
106 * first page-aligned address after the loaded PT_LOAD image for heap/brk init.
107 * phdr_va_out (if non-NULL) receives the virtual address of the phdr table
108 * (needed for AT_PHDR in the exec auxv so glibc can find PT_TLS).
109 * phnum_out (if non-NULL) receives e_phnum.
110 * On failure: returns -1 (bad magic, wrong type/machine, or OOM). */
111int elf_load(int fd, uint64_t *entry_out, uint64_t *image_end_out,
112 uint64_t *phdr_va_out, uint16_t *phnum_out);
113
114#endif /* _MINIOS_FS_ELF_H_ */
int elf_load(int fd, uint64_t *entry_out, uint64_t *image_end_out, uint64_t *phdr_va_out, uint16_t *phnum_out)
Definition elf.c:51
Definition elf.h:91
uint64_t d_ptr
Definition elf.h:93
uint64_t d_val
Definition elf.h:93
int64_t d_tag
Definition elf.h:92
Definition elf.h:97
uint64_t r_offset
Definition elf.h:98
uint64_t r_info
Definition elf.h:99
int64_t r_addend
Definition elf.h:100
uint16_t e_shnum
Definition elf.h:74
uint32_t e_flags
Definition elf.h:69
uint64_t p_offset
Definition elf.h:82
uint64_t p_filesz
Definition elf.h:85
uint64_t e_entry
Definition elf.h:66
uint16_t e_shstrndx
Definition elf.h:75
uint16_t e_machine
Definition elf.h:64
uint64_t e_shoff
Definition elf.h:68
uint64_t p_vaddr
Definition elf.h:83
uint64_t p_align
Definition elf.h:87
uint16_t e_phnum
Definition elf.h:72
uint16_t e_ehsize
Definition elf.h:70
uint32_t e_version
Definition elf.h:65
uint32_t p_type
Definition elf.h:80
uint16_t e_type
Definition elf.h:63
uint64_t p_memsz
Definition elf.h:86
uint64_t p_paddr
Definition elf.h:84
uint16_t e_phentsize
Definition elf.h:71
uint64_t e_phoff
Definition elf.h:67
uint16_t e_shentsize
Definition elf.h:73
uint32_t p_flags
Definition elf.h:81
uint8_t e_ident[16]
Definition elf.h:62
int fd
Definition syscall_fs.c:0
unsigned short uint16_t
Definition types.h:31
unsigned int uint32_t
Definition types.h:34
signed long int int64_t
Definition types.h:36
unsigned long int uint64_t
Definition types.h:37
unsigned char uint8_t
Definition types.h:28
typedef __attribute__