550W Document
550W, a high-end OS
elf.h
浏览该文件的文档.
1 #pragma once
2 
3 #include <asm/pgtable.h>
4 #include <common/types.h>
5 #include <lib/string.h>
6 
7 /* 64-bit ELF base types. */
8 typedef uint64_t Elf64_Addr;
9 typedef uint16_t Elf64_Half;
10 typedef int16_t Elf64_SHalf;
11 typedef uint64_t Elf64_Off;
12 typedef int32_t Elf64_Sword;
13 typedef uint32_t Elf64_Word;
14 typedef uint64_t Elf64_Xword;
15 typedef int64_t Elf64_Sxword;
16 typedef uint16_t Elf64_Section;
18 
19 #define EI_MAG0 0 /* e_ident[] indexes */
20 #define EI_MAG1 1
21 #define EI_MAG2 2
22 #define EI_MAG3 3
23 #define EI_CLASS 4
24 #define EI_DATA 5
25 #define EI_VERSION 6
26 #define EI_OSABI 7
27 #define EI_PAD 8
28 
29 #define ELFMAG0 0x7f /* EI_MAG */
30 #define ELFMAG1 'E'
31 #define ELFMAG2 'L'
32 #define ELFMAG3 'F'
33 #define ELFMAG "\177ELF"
34 #define SELFMAG 4
35 
36 #define ELFCLASSNONE 0 /* EI_CLASS */
37 #define ELFCLASS32 1
38 #define ELFCLASS64 2
39 #define ELFCLASSNUM 3
40 
41 #define ELFDATANONE 0 /* e_ident[EI_DATA] */
42 #define ELFDATA2LSB 1
43 #define ELFDATA2MSB 2
44 
45 #define EV_NONE 0 /* e_version, EI_VERSION */
46 #define EV_CURRENT 1
47 #define EV_NUM 2
48 
49 #define ELFOSABI_NONE 0
50 #define ELFOSABI_LINUX 3
51 
52 #ifndef ELF_OSABI
53 #define ELF_OSABI ELFOSABI_NONE
54 #endif
55 
56 #define EI_NIDENT 16
57 typedef struct elf64_hdr {
58  unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
62  Elf64_Addr e_entry; /* Entry point virtual address */
63  Elf64_Off e_phoff; /* Program header table file offset */
64  Elf64_Off e_shoff; /* Section header table file offset */
73 
74 /* clang-format off */
75 /* These constants define the permissions on sections in the
76  program header, p_flags. */
77 #define PF_X (1 << 0) /* Segment is executable */
78 #define PF_W (1 << 1) /* Segment is writable */
79 #define PF_R (1 << 2) /* Segment is readable */
80 #define PF_MASKOS 0x0ff00000 /* OS-specific */
81 #define PF_MASKPROC 0xf0000000 /* Processor-specific */
82 
83 /* Legal values for p_type (segment type). */
84 
85 #define PT_NULL 0 /* Program header table entry unused */
86 #define PT_LOAD 1 /* Loadable program segment */
87 #define PT_DYNAMIC 2 /* Dynamic linking information */
88 #define PT_INTERP 3 /* Program interpreter */
89 #define PT_NOTE 4 /* Auxiliary information */
90 #define PT_SHLIB 5 /* Reserved */
91 #define PT_PHDR 6 /* Entry for header table itself */
92 #define PT_TLS 7 /* Thread-local storage segment */
93 #define PT_NUM 8 /* Number of defined types */
94 #define PT_LOOS 0x60000000 /* Start of OS-specific */
95 #define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
96 #define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */
97 #define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
98 #define PT_LOSUNW 0x6ffffffa
99 #define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */
100 #define PT_SUNWSTACK 0x6ffffffb /* Stack segment */
101 #define PT_HISUNW 0x6fffffff
102 #define PT_HIOS 0x6fffffff /* End of OS-specific */
103 #define PT_LOPROC 0x70000000 /* Start of processor-specific */
104 #define PT_HIPROC 0x7fffffff /* End of processor-specific */
105 
106 /* clang-format on */
107 
108 typedef struct elf64_phdr {
111  Elf64_Off p_offset; /* Segment file offset */
112  Elf64_Addr p_vaddr; /* Segment virtual address */
113  Elf64_Addr p_paddr; /* Segment physical address */
114  Elf64_Xword p_filesz; /* Segment size in file */
115  Elf64_Xword p_memsz; /* Segment size in memory */
116  Elf64_Xword p_align; /* Segment alignment, file & memory */
118 
119 typedef struct ELF_info {
120  uint64_t text_begin;
121  uint64_t phoff;
122  uint64_t phent;
123  uint64_t phnum;
124  uint64_t entry;
125  uint64_t edata;
127 
128 #define MIN(a, b) ((a) < (b) ? (a) : (b))
129 
130 static inline int is_elf_format(unsigned char *binary) {
131  Elf64_Ehdr *ehdr = (Elf64_Ehdr *)binary;
132 
133  if (ehdr->e_ident[0] == EI_MAG0 && ehdr->e_ident[1] == EI_MAG1 && ehdr->e_ident[2] == EI_MAG2 && ehdr->e_ident[3] == EI_MAG3) {
134  return 0;
135  }
136 
137  return 1;
138 }
139 
140 /* prepare_page_for_kva should return a kernel virtual address */
141 uintptr_t load_elf(unsigned char elf_binary[], unsigned length, uintptr_t pgdir, uintptr_t (*prepare_page_for_va)(uintptr_t va, uintptr_t pgdir));
#define EI_MAG2
Definition: elf.h:21
#define EI_MAG1
Definition: elf.h:20
uintptr_t load_elf(unsigned char elf_binary[], unsigned length, uintptr_t pgdir, uintptr_t(*prepare_page_for_va)(uintptr_t va, uintptr_t pgdir))
Definition: elf.c:3
int32_t Elf64_Sword
Definition: elf.h:12
uint64_t Elf64_Xword
Definition: elf.h:14
int64_t Elf64_Sxword
Definition: elf.h:15
uint64_t Elf64_Off
Definition: elf.h:11
int16_t Elf64_SHalf
Definition: elf.h:10
uint32_t Elf64_Word
Definition: elf.h:13
#define EI_MAG0
Definition: elf.h:19
uint16_t Elf64_Section
Definition: elf.h:16
Elf64_Half Elf64_Versym
Definition: elf.h:17
struct elf64_phdr Elf64_Phdr
#define EI_MAG3
Definition: elf.h:22
struct ELF_info ELF_info_t
uint16_t Elf64_Half
Definition: elf.h:9
#define EI_NIDENT
Definition: elf.h:56
uint64_t Elf64_Addr
Definition: elf.h:8
struct elf64_hdr Elf64_Ehdr
Definition: elf.h:119
uint64_t entry
Definition: elf.h:124
uint64_t phnum
Definition: elf.h:123
uint64_t edata
Definition: elf.h:125
uint64_t phent
Definition: elf.h:122
uint64_t phoff
Definition: elf.h:121
uint64_t text_begin
Definition: elf.h:120
Definition: elf.h:57
Elf64_Half e_phentsize
Definition: elf.h:67
Elf64_Half e_shentsize
Definition: elf.h:69
Elf64_Word e_version
Definition: elf.h:61
Elf64_Half e_shnum
Definition: elf.h:70
Elf64_Half e_type
Definition: elf.h:59
Elf64_Half e_phnum
Definition: elf.h:68
Elf64_Addr e_entry
Definition: elf.h:62
Elf64_Half e_shstrndx
Definition: elf.h:71
Elf64_Half e_ehsize
Definition: elf.h:66
Elf64_Half e_machine
Definition: elf.h:60
Elf64_Off e_shoff
Definition: elf.h:64
unsigned char e_ident[EI_NIDENT]
Definition: elf.h:58
Elf64_Off e_phoff
Definition: elf.h:63
Elf64_Word e_flags
Definition: elf.h:65
Definition: elf.h:108
Elf64_Off p_offset
Definition: elf.h:111
Elf64_Addr p_vaddr
Definition: elf.h:112
Elf64_Xword p_memsz
Definition: elf.h:115
Elf64_Word p_flags
Definition: elf.h:110
Elf64_Xword p_filesz
Definition: elf.h:114
Elf64_Word p_type
Definition: elf.h:109
Elf64_Xword p_align
Definition: elf.h:116
Elf64_Addr p_paddr
Definition: elf.h:113
uint64 uintptr_t
Definition: types.h:47