forked from SanseroGames/LetsGo-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelfloader.go
198 lines (181 loc) · 5.94 KB
/
elfloader.go
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"debug/elf"
"unsafe"
)
type auxVecEntry struct {
Type uint32
Value uint32
}
const (
AT_NULL = 0 /* end of vector */
AT_IGNORE = 1 /* entry should be ignored */
AT_EXECFD = 2 /* file descriptor of program */
AT_PHDR = 3 /* program headers for program */
AT_PHENT = 4 /* size of program header entry */
AT_PHNUM = 5 /* number of program headers */
AT_PAGESZ = 6 /* system page size */
AT_BASE = 7 /* base address of interpreter */
AT_FLAGS = 8 /* flags */
AT_ENTRY = 9 /* entry point of program */
AT_NOTELF = 10 /* program is not ELF */
AT_UID = 11 /* real uid */
AT_EUID = 12 /* effective uid */
AT_GID = 13 /* real gid */
AT_EGID = 14 /* effective gid */
AT_PLATFORM = 15 /* string identifying CPU for optimizations */
AT_HWCAP = 16 /* arch dependent hints at CPU capabilities */
AT_CLKTCK = 17 /* frequency at which times() increments */
/* values 18 through 22 are reserved */
AT_SECURE = 23 /* secure mode boolean */
AT_BASE_PLATFORM = 24 /* string identifying real platform, may
* differ from AT_PLATFORM. */
AT_RANDOM = 25 /* address of 16 random bytes */
AT_HWCAP2 = 26 /* extension of AT_HWCAP */
AT_EXECFN = 31 /* filename of program */
)
func LoadAuxVector(buf []auxVecEntry, elfHdr *elf.Header32, loadAddr uintptr) int {
//NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
start := 0
buf[start].Type = AT_PAGESZ
buf[start].Value = PAGE_SIZE
start++
buf[start].Type = AT_UID
buf[start].Value = 0
start++
buf[start].Type = AT_GID
buf[start].Value = 0
start++
buf[start].Type = AT_EUID
buf[start].Value = 0
start++
buf[start].Type = AT_EGID
buf[start].Value = 0
start++
buf[start].Type = AT_ENTRY
buf[start].Value = elfHdr.Entry
start++
buf[start].Type = AT_PHDR
buf[start].Value = uint32(loadAddr) + elfHdr.Phoff
start++
buf[start].Type = AT_PHENT
buf[start].Value = uint32(elfHdr.Phentsize)
start++
buf[start].Type = AT_PHNUM
buf[start].Value = uint32(elfHdr.Phnum)
start++
buf[start].Type = AT_RANDOM
buf[start].Value = uint32(loadAddr) // Currently just pointing somewhere. don't care about security
start++
buf[start].Type = AT_NULL
buf[start].Value = 0
start++
//NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
//NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
//NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
//NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
//NEW_AUX_ENT(AT_BASE, interp_load_addr);
//if (bprm->interp_flags & BINPRM_FLAGS_PRESERVE_ARGV0)
//flags |= AT_FLAGS_PRESERVE_ARGV0;
//NEW_AUX_ENT(AT_FLAGS, flags);
//NEW_AUX_ENT(AT_ENTRY, e_entry);
//NEW_AUX_ENT(AT_UID, from_kuid_munged(cred->user_ns, cred->uid));
//NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid));
//NEW_AUX_ENT(AT_GID, from_kgid_munged(cred->user_ns, cred->gid));
//NEW_AUX_ENT(AT_EGID, from_kgid_munged(cred->user_ns, cred->egid));
//NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
//NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
//#ifdef ELF_HWCAP2
//NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
//#endif
//NEW_AUX_ENT(AT_EXECFN, bprm->exec);
//if (k_platform) {
// NEW_AUX_ENT(AT_PLATFORM,
// (elf_addr_t)(unsigned long)u_platform);
//}
//if (k_base_platform) {
// NEW_AUX_ENT(AT_BASE_PLATFORM,
// (elf_addr_t)(unsigned long)u_base_platform);
//}
//if (bprm->have_execfd) {
// NEW_AUX_ENT(AT_EXECFD, bprm->execfd);
//}
return start
}
func LoadElfFile(multibootModule string, space *MemSpace) (*elf.Header32, uintptr, uintptr, *MultibootModule) {
var module *MultibootModule
for i, n := range loadedModules {
if n.Cmdline() == multibootModule {
module = &loadedModules[i]
break
}
}
if module == nil || module.Cmdline() != multibootModule {
kerrorln("[ELF] Unknown module: ", multibootModule)
return nil, 0, 0, nil
}
moduleLen := int(module.End - module.Start)
// catch weird things...
if moduleLen < 4 {
return nil, 0, 0, nil
}
elfData := unsafe.Slice((*byte)(unsafe.Pointer(uintptr(module.Start))), moduleLen)
// Test if really elf file
if elfData[0] != 0x7f || elfData[1] != 'E' || elfData[2] != 'L' || elfData[3] != 'F' {
kerrorln("[ELF] '", multibootModule, "' is not a ELF file")
return nil, 0, 0, nil
}
elfHeader := (*elf.Header32)(unsafe.Pointer(uintptr(module.Start)))
progHeaders := unsafe.Slice((*elf.Prog32)(unsafe.Add(unsafe.Pointer(uintptr(module.Start)), elfHeader.Phoff)), elfHeader.Phnum)
baseAddr := uintptr(0xffffffff)
topAddr := uint32(0)
for _, n := range progHeaders {
if n.Type == uint32(elf.PT_LOAD) {
if uintptr(n.Vaddr) < baseAddr {
baseAddr = uintptr(n.Vaddr)
}
localTop := uint32(0)
contents := unsafe.Slice((*byte)(unsafe.Add(unsafe.Pointer(uintptr(module.Start)), n.Off)), n.Filesz)
offset := n.Vaddr & (PAGE_SIZE - 1)
start := uint32(0)
if offset != 0 {
p := AllocPage()
Memclr(p, PAGE_SIZE)
target := unsafe.Slice((*byte)(unsafe.Pointer(p)), PAGE_SIZE)
end := int(PAGE_SIZE - offset)
if end > len(contents) {
end = len(contents)
}
copy(target[offset:PAGE_SIZE], contents[0:end])
space.MapPage(p, uintptr(n.Vaddr&^(PAGE_SIZE-1)), PAGE_RW|PAGE_PERM_USER)
localTop = n.Vaddr&^(PAGE_SIZE-1) + PAGE_SIZE
start = PAGE_SIZE - offset
}
for i := start; i < n.Filesz; i += PAGE_SIZE {
p := AllocPage()
Memclr(p, PAGE_SIZE)
target := unsafe.Slice((*byte)(unsafe.Pointer(p)), PAGE_SIZE)
end := int(i + PAGE_SIZE)
if end > len(contents) {
end = len(contents)
}
copy(target, contents[i:end])
// Currently don't care about write protecton of code
space.MapPage(p, uintptr(n.Vaddr+i), PAGE_RW|PAGE_PERM_USER)
localTop = n.Vaddr + i + PAGE_SIZE
}
if n.Filesz < n.Memsz {
for i := localTop; i < n.Vaddr+n.Memsz; i += PAGE_SIZE {
p := AllocPage()
Memclr(p, PAGE_SIZE)
space.MapPage(p, uintptr(i), PAGE_RW|PAGE_PERM_USER)
localTop = i + PAGE_SIZE
}
}
if localTop > topAddr {
topAddr = localTop
}
}
}
return elfHeader, baseAddr, uintptr(topAddr), module
}