-
Notifications
You must be signed in to change notification settings - Fork 1
/
elf.c
151 lines (133 loc) · 4.77 KB
/
elf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "elf.h"
#include "error.h"
#include "vm.h"
#include "stdint.h"
#include "stdio.h"
#include "string.h"
int load_elf(pte_t *pt, void *elf, size_t len, uintptr_t *entry_va)
{
uint8_t *u8 = (uint8_t *)elf;
if (len < sizeof(Elf64_Ehdr))
{
puts("Loader: ELF file is too small (Elf64_Ehdr).\n");
return -EOOB;
}
Elf64_Ehdr *elf_header = (Elf64_Ehdr *)u8;
if (memcmp(elf_header->e_ident, ELFMAG, SELFMAG))
{
printf("Loader: Not an ELF file (0x%08x).\n", *(uint64_t *)elf_header->e_ident);
return -EBADPKT;
}
if (elf_header->e_type != ET_EXEC)
{
puts("Loader: Not an executable ELF file.\n");
return -EBADPKT;
}
uintptr_t entry = elf_header->e_entry;
if (entry_va) *entry_va = entry;
printf("Loader: ELF Entry is at 0x%016lx.\n", entry);
if (!elf_header->e_phnum)
{
puts("Loader: No program header.\n");
return -EBADPKT;
}
if (elf_header->e_phentsize != sizeof(Elf64_Phdr))
{
puts("Loader: elf_header->e_phentsize != sizeof(Elf64_Phdr)\n");
return -ENOIMPL;
}
size_t phsize = elf_header->e_phnum * sizeof(Elf64_Phdr);
if (!(elf_header->e_phoff <= elf_header->e_phoff + phsize && elf_header->e_phoff + phsize <= len))
{
puts("Loader: ELF file is too small (Elf64_Phdr).\n");
return -EOOB;
}
Elf64_Phdr *phdr = (Elf64_Phdr *)(u8 + elf_header->e_phoff);
puts("+-----------------------------------------------------------------------------------------------------+\n"
"| PROGRAM HEADERS |\n"
"+-----------------------------------------------------------------------------------------------------+\n"
" TYPE OFFSET VIRT ADDR PHY ADDR FILE SIZE MEM SIZE \n");
for (int i = 0; i < elf_header->e_phnum; ++i)
{
printf("%02d: ", i);
if (phdr[i].p_type == PT_LOAD)
{
printf("LOAD");
}
else if (phdr[i].p_type == PT_TLS)
{
printf("TLS ");
}
else
{
printf("????");
}
printf(" 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
phdr[i].p_offset, phdr[i].p_vaddr,
phdr[i].p_paddr, phdr[i].p_filesz, phdr[i].p_memsz);
}
for (int i = 0; i < elf_header->e_phnum; ++i)
{
if (phdr[i].p_type == PT_LOAD)
{
printf("Loader: Loading %02d... ", i);
if (phdr[i].p_filesz && !(phdr[i].p_offset <= phdr[i].p_offset + phdr[i].p_filesz
&& phdr[i].p_offset + phdr[i].p_filesz <= len))
{
puts("ELF file is too small.\n");
return -EOOB;
}
if (!(USER_BOTTOM <= phdr[i].p_vaddr
&& phdr[i].p_vaddr <= phdr[i].p_vaddr + phdr[i].p_memsz
&& phdr[i].p_vaddr + phdr[i].p_memsz <= USER_TOP))
{
puts("virtual address out of bound.\n");
return -EOOB;
}
if (phdr[i].p_filesz > phdr[i].p_memsz)
{
puts("Bad ELF file (p_filesz > p_memsz).\n");
return -EBADPKT;
}
uintptr_t flags = VM_U | VM_A;
if (phdr[i].p_flags & PF_R) flags |= VM_R;
if (phdr[i].p_flags & PF_W) flags |= VM_W | VM_D;
if (phdr[i].p_flags & PF_X) flags |= VM_X;
int ret = map_and_copy(pt, phdr[i].p_vaddr, flags,
u8 + phdr[i].p_offset, phdr[i].p_filesz);
if (ret < 0)
{
puts("out of memory.\n");
return ret;
}
if (phdr[i].p_filesz < phdr[i].p_memsz)
{
puts("init .bss... ");
size_t file_end = PGALIGN_FLOOR(phdr[i].p_vaddr + phdr[i].p_filesz),
mem_end = PGALIGN(phdr[i].p_vaddr + phdr[i].p_memsz);
for (size_t pg = file_end; pg < mem_end; pg += PGSIZE)
{
if (!map_page(pt, pg, flags))
{
puts("out of memory.\n");
return -EOOM;
}
}
}
puts("done.\n");
}
}
puts("Loader: done.\n");
return 0;
}
int parse_elf_phdr(void *elf, size_t len,
uintptr_t *phdr_offset, uintptr_t *phent, uintptr_t *phnum)
{
// Assuming ELF data is checked by load_elf.
uint8_t *u8 = (uint8_t *)elf;
Elf64_Ehdr *elf_header = (Elf64_Ehdr *)u8;
*phdr_offset = elf_header->e_phoff;
*phent = elf_header->e_phentsize;
*phnum = elf_header->e_phnum;
return 0;
}