-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathelf_loader.c
180 lines (161 loc) · 3.92 KB
/
elf_loader.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "elf_loader.h"
#include "libc/calls/internal.h"
static const unsigned align_mask = 4095;
static uintptr_t
pgtrunc(uintptr_t x)
{
return x & ~align_mask;
}
static uintptr_t
pground(uintptr_t x)
{
return pgtrunc(x + align_mask);
}
static unsigned
pflags(unsigned x)
{
unsigned r = 0;
if (x & PF_R)
r += PROT_READ;
if (x & PF_W)
r += PROT_WRITE;
if (x & PF_X)
r += PROT_EXEC;
return r;
}
static void
z_fini(void)
{
}
static char *
loadelf_anon(int fd, Elf64_Ehdr *ehdr, Elf64_Phdr *phdr)
{
uintptr_t minva = -1;
uintptr_t maxva = 0;
for (Elf64_Phdr *p = phdr; p < &phdr[ehdr->e_phnum]; p++) {
if (p->p_type != PT_LOAD)
continue;
if (p->p_vaddr < minva)
minva = p->p_vaddr;
if (p->p_vaddr + p->p_memsz > maxva)
maxva = p->p_vaddr + p->p_memsz;
}
minva = pgtrunc(minva);
maxva = pground(maxva);
uint8_t *base = __sys_mmap(
0, maxva - minva, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, 0);
assert(base != (void *)-1);
__sys_munmap(base, maxva - minva);
for (Elf64_Phdr *p = phdr; p < &phdr[ehdr->e_phnum]; p++) {
if (p->p_type != PT_LOAD)
continue;
uintptr_t off = p->p_vaddr & align_mask;
uint8_t * start = base;
start += pgtrunc(p->p_vaddr);
size_t sz = pground(p->p_memsz + off);
uint8_t *m = __sys_mmap(start, sz, PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, 0);
assert(m != (void *)-1);
ssize_t sr = lseek(fd, p->p_offset, SEEK_SET);
assert(sr >= 0);
ssize_t rr = read(fd, m + off, p->p_filesz);
assert(rr == (ssize_t)p->p_filesz);
sys_mprotect(m, sz, pflags(p->p_flags));
}
return base;
}
struct loaded {
char * base;
char * entry;
Elf64_Ehdr eh;
Elf64_Phdr ph[32];
};
static void
loadfd(struct loaded *l, int fd)
{
assert(fd >= 0);
size_t hsz = read(fd, &l->eh, sizeof(l->eh));
assert(hsz == sizeof(l->eh));
assert(l->eh.e_phnum < sizeof(l->ph) / sizeof(l->ph[0]));
int rs = lseek(fd, l->eh.e_phoff, SEEK_SET);
assert(rs >= 0);
int rsz = read(fd, l->ph, l->eh.e_phnum * sizeof(l->ph[0]));
assert(rsz == l->eh.e_phnum * sizeof(l->ph[0]));
l->entry = (uint8_t *)l->eh.e_entry;
}
static void
load(struct loaded *l, const char *file)
{
int fd = open(file, O_RDONLY);
loadfd(l, fd);
l->base = loadelf_anon(fd, &l->eh, l->ph);
l->entry += (uintptr_t)l->base;
close(fd);
}
void
elf_interp(char *buf, size_t bsz, const char *file)
{
int fd = open(file, O_RDONLY);
struct loaded l;
size_t sz;
loadfd(&l, fd);
for (unsigned i = 0; i < l.eh.e_phnum; i++)
switch (l.ph[i].p_type) {
case PT_INTERP:
assert(l.ph[i].p_filesz < bsz);
sz = read(fd, buf, l.ph[i].p_filesz);
assert(sz == l.ph[i].p_filesz);
break;
}
close(fd);
}
void
elf_exec(
const char *file, const char *iinterp, int argc, char *argv[], char **envp)
{
struct loaded prog;
load(&prog, file);
struct loaded interp;
load(&interp, iinterp);
char **envv = envp;
int envc = 0;
while (*envp++)
envc++;
Elf64_auxv_t *av = (Elf64_auxv_t *)envp;
char ** sp = argv - 1;
const char **nargv = (const char **)&sp[1];
int nargc = *(long *)sp;
#define AVSET(t, expr) \
do { \
if (av->a_type == (t)) \
av->a_un.a_val = (uintptr_t)(expr); \
} while (0)
while (av->a_type) {
AVSET(AT_PHDR, prog.base + prog.eh.e_phoff);
AVSET(AT_PHENT, prog.eh.e_phentsize);
AVSET(AT_PHNUM, prog.eh.e_phnum);
AVSET(AT_PAGESZ, 0x1000);
AVSET(AT_BASE, interp.base);
AVSET(AT_FLAGS, 0);
AVSET(AT_ENTRY, prog.entry);
AVSET(AT_EXECFN, argv[0]);
++av;
}
#undef AVSET
static struct ps_strings {
char **argv;
int argc;
char **envv;
int envc;
} pss;
pss = (struct ps_strings) { argv, argc, envv, envc };
asm volatile("mov\t%2,%%rsp\n\t"
"jmpq\t*%1"
: /* no outputs */
: "D"(IsFreebsd() ? sp : 0), //
"S"(interp.entry), //
"d"(sp), //
"b"(IsNetbsd() ? &pss : 0)
: "memory");
unreachable;
}