-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
375 lines (309 loc) · 9.92 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "minilib.c"
#define panic(...) do { dprintf(2, __VA_ARGS__); exit(1); } while (0)
#define floor_size(addr, size) ((addr) - ((addr) % (size)))
#define round_size(addr, size) (((addr) + (size)) - ((addr) % (size)))
#define ELFCLASS64 2
#define ELFDATA2LSB 1
#define EM_X86_64 62
#define SHF_WRITE (1 << 0)
#define SHF_ALLOC (1 << 1)
#define SHF_TLS (1 << 10)
#define PT_LOAD 1
#define PT_TLS 7
#define ELF64_R_SYM(i) ((i) >> 32)
#define ELF64_R_TYPE(i) ((i) & (0xFFFFFFFFL))
enum {
SHT_NULL = 0,
SHT_PROGBITS = 1,
SHT_SYMTAB = 2,
SHT_STRTAB = 3,
SHT_RELA = 4,
SHT_HASH = 5,
SHT_DYNAMIC = 6,
SHT_NOTE = 7,
SHT_NOBITS = 8,
SHT_REL = 9,
};
#pragma pack(1)
typedef struct {
u8 magic[4];
u8 class;
u8 endian;
u8 hdr_version;
u8 target_abi;
u8 pad[8];
} ELF_Pre_Header;
typedef struct {
u8 ident[16];
u16 type;
u16 machine;
u32 version;
u64 entrypoint;
u64 program_hdr_offset;
u64 section_hdr_offset;
u32 flags;
u16 eh_size;
u16 program_hdr_entry_size;
u16 program_hdr_num;
u16 section_hdr_entry_size;
u16 section_hdr_num;
u16 section_hdr_str_idx;
} ELF64_Header;
typedef struct {
u32 type;
u32 flags;
u64 offset;
u64 virtual_addr;
u64 physical_addr;
u64 file_size;
u64 mem_size;
u64 align;
} ELF64_Program_Header;
typedef struct {
u32 name;
u32 type;
u64 flags;
u64 addr;
u64 offset;
u64 size;
u32 link;
u32 info;
u64 addr_align;
u64 entry_size;
} ELF64_Section_Header;
typedef struct {
} ELF64_Rela;
#pragma pack()
typedef struct {
u8 *data;
u64 size;
} Segment;
typedef struct {
u8 *data;
u64 size;
} Slice;
Slice to_slice(u8 *data, u64 size) {
Slice s;
s.data = data;
s.size = size;
return s;
}
Slice slice_idx(Slice s, u64 idx) {
if (idx > s.size) {
panic("Invalid idx %d:%d!\n", idx, s.size);
}
Slice out;
out.data = s.data + idx;
out.size = s.size - idx;
return out;
}
Slice load_file(char *filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
panic("Failed to open %s\n", filename);
}
u64 length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
u64 aligned_length = round_size(length, 0x1000);
u8 *data = mmap(NULL, aligned_length, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
return to_slice(data, length);
}
void hexdump(Slice s) {
int display_width = 32;
printf("[");
int tail = s.size % display_width;
int trunc_width = s.size - tail;
for (size_t i = 0; i < trunc_width; i += display_width) {
int j = 0;
for (; j < (display_width - 2); j += 2) {
printf("%02x%02x ", s.data[i+j], s.data[i+j+1]);
}
if (i + display_width == s.size) {
printf("%02x%02x ", s.data[i+j], s.data[i+j+1]);
} else {
printf("%02x%02x\n", s.data[i+j], s.data[i+j+1]);
}
}
if (tail) {
int j = trunc_width;
for (; j < (s.size - 2); j += 2) {
printf("%02x%02x ", s.data[j], s.data[j+1]);
}
int rem = s.size - j;
if (rem == 2) {
printf("%02x%02x", s.data[j], s.data[j+1]);
} else if (rem == 1) {
printf("%02x", s.data[j]);
}
}
printf("]\n");
}
/*```
A quick ELF briefer:
----------------------------------
| ELF Header |--+
---------------------------------- |
| Program Header | |
---------------------------------- |
+->| Sections: .text, .strtab, etc. | |
| ---------------------------------- |
+--| Section Headers |<-+
----------------------------------
Finding the Section Header table: ("What sections does this ELF have?")
File start + ELF_Header.shoff -> Section_Header table
Finding the String Table for Section Header Names: ("How do I know what section 1 is called?")
Section_Header table[ELF_Header.shstrndx] -> Section Header for Name Table
Finding Data for Section Headers: ("How do I get the data in the .text section?")
File start + Section_Header.offset -> Section Data
```*/
u64 load_elf(Slice s) {
if (s.size < sizeof(ELF64_Header)) {
panic("Invalid elf file!\n");
}
// Checking a small chunk of the ELF header (Calling it a Pre_Header here)
// to know how to interpret the rest of the header
// The full header could be ELF32 or ELF64 or garbage,
// I won't know until I've scanned the Pre_Header fields
u8 magic[4] = { 0x7F, 'E', 'L', 'F' };
ELF_Pre_Header *pre_hdr = (ELF_Pre_Header *)s.data;
if (!memeq(pre_hdr->magic, magic, sizeof(magic))) {
panic("File is not an ELF!\n");
}
if (pre_hdr->class != ELFCLASS64 || pre_hdr->endian != ELFDATA2LSB) {
panic("TODO: Only supports 64 bit, little endian ELF\n");
}
ELF64_Header *elf_hdr = (ELF64_Header *)s.data;
if (elf_hdr->machine != EM_X86_64) {
panic("TODO: Only supports x86_64\n");
}
if (elf_hdr->version != 1) {
panic("Invalid ELF version\n");
}
// Ensure that the ELF file actually has enough space to fit the full claimed program header table
if (elf_hdr->program_hdr_offset + (elf_hdr->program_hdr_num * sizeof(ELF64_Program_Header)) > s.size) {
panic("Invalid elf file!\n");
}
ELF64_Program_Header *program_hdr_table = (ELF64_Program_Header *)(s.data + elf_hdr->program_hdr_offset);
Segment segment_table[20] = {0};
u64 seg_count = 0;
// Load segments into memory
for (int i = 0; i < elf_hdr->program_hdr_num; i += 1) {
ELF64_Program_Header *p_hdr = &program_hdr_table[i];
switch (p_hdr->type) {
case PT_LOAD: {
bool exec_perm = (p_hdr->flags & 0x1) == 0x1;
bool write_perm = (p_hdr->flags & 0x2) == 0x2;
bool read_perm = (p_hdr->flags & 0x4) == 0x4;
printf("LOAD 0x%02x | %s%s%s | vaddr: 0x%08x paddr: 0x%08x memsz: 0x%08x | align: %d\n",
i, (exec_perm) ? "X" : " ", (write_perm) ? "W" : " ", (read_perm) ? "R" : " ",
p_hdr->virtual_addr, p_hdr->physical_addr, p_hdr->mem_size, p_hdr->align);
u64 aligned_start_addr = floor_size(p_hdr->virtual_addr, 0x1000);
u64 aligned_end_addr = round_size(p_hdr->virtual_addr + p_hdr->mem_size, 0x1000);
u64 region_size = floor_size(aligned_end_addr - aligned_start_addr, 0x1000);
printf("allocating 0x%08x\n", region_size);
u8 *aligned_seg = mmap((void *)aligned_start_addr, region_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
if ((u64)aligned_seg != aligned_start_addr) {
panic("WTF? 0x%08x != 0x%08x\n", aligned_seg, aligned_start_addr);
}
u8 *seg = (u8 *)p_hdr->virtual_addr;
// load file data into memory
if (p_hdr->file_size > 0) {
u8 *segment_ptr = s.data + p_hdr->offset;
printf("%d | 0x%08x <- 0x%08x | %d B\n", i, seg, segment_ptr, p_hdr->file_size);
memcpy(seg, segment_ptr, p_hdr->file_size);
}
// clear out leftover space
u64 rem_size = aligned_end_addr - (u64)(seg + p_hdr->file_size);
if (rem_size > 0) {
printf("%d | 0x%08x <- ZERO | %d B\n", i, seg + p_hdr->file_size, rem_size);
memset(seg + p_hdr->file_size, 0, rem_size);
}
Segment sg = {.data = seg, .size = region_size};
segment_table[seg_count++] = sg;
} break;
case PT_TLS: {
bool exec_perm = (p_hdr->flags & 0x1) == 0x1;
bool write_perm = (p_hdr->flags & 0x2) == 0x2;
bool read_perm = (p_hdr->flags & 0x4) == 0x4;
printf("TLS 0x%02x | %s%s%s | vaddr: 0x%08x paddr: 0x%08x memsz: 0x%08x | align: %d\n",
i, (exec_perm) ? "X" : " ", (write_perm) ? "W" : " ", (read_perm) ? "R" : " ",
p_hdr->virtual_addr, p_hdr->physical_addr, p_hdr->mem_size, p_hdr->align);
int seg_idx = -1;
for (int i = 0; i < seg_count; i++) {
u64 vaddr = floor_size(p_hdr->virtual_addr, p_hdr->align);
if ((u64)segment_table[i].data == vaddr) {
seg_idx = i;
break;
}
}
if (seg_idx == -1) {
panic("Failed to get existing segment for TLS!\n");
}
u64 region_size = round_size(p_hdr->mem_size, 0x1000);
u8 *seg = mmap(0, region_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (seg == NULL) {
panic("Failed to allocate TLS segment!\n");
}
memcpy(seg, segment_table[seg_idx].data, p_hdr->mem_size);
printf("Setting FS to 0x%08x\n", seg);
arch_prctl(ARCH_SET_FS, seg);
} break;
}
}
// Ensure that the ELF file actually has enough space to fit the full claimed section header table
if (elf_hdr->section_hdr_offset + (elf_hdr->section_hdr_num * sizeof(ELF64_Section_Header)) > s.size) {
panic("Invalid elf file!\n");
}
ELF64_Section_Header *section_hdr_table = (ELF64_Section_Header *)(s.data + elf_hdr->section_hdr_offset);
// Get section header name table header
if (elf_hdr->section_hdr_str_idx >= elf_hdr->section_hdr_num) {
panic("Invalid section header name table index\n");
}
ELF64_Section_Header *section_strtable_hdr = §ion_hdr_table[elf_hdr->section_hdr_str_idx];
if (section_strtable_hdr->type != SHT_STRTAB) {
panic("Section header name table is invalid\n");
}
// Get section header name table
if ((section_strtable_hdr->offset + section_strtable_hdr->size) > s.size) {
panic("Section header name table is too large for file?\n");
}
Slice strtable = to_slice(s.data + section_strtable_hdr->offset, section_strtable_hdr->size);
for (int i = 0; i < elf_hdr->section_hdr_num; i += 1) {
ELF64_Section_Header *s_hdr = §ion_hdr_table[i];
Slice sect_name = slice_idx(strtable, s_hdr->name);
printf("%d | %s\n", i, (char *)sect_name.data);
if (s_hdr->type == SHT_PROGBITS) {
Slice sect = slice_idx(s, s_hdr->offset);
//hexdump(sect);
}
if (s_hdr->type == SHT_RELA) {
Slice sect = slice_idx(s, s_hdr->offset);
/*
for (int j = 0; j <
switch (s_hdr->info) {
case
}
*/
}
}
return elf_hdr->entrypoint;
}
static struct builtin_tls {
char c;
void *space[16];
} builtin_tls[1];
int main(int argc, char **argv) {
// Step 1 is setting a bootstrap FS so clang doesn't act dumb
syscall(SYS_arch_prctl, ARCH_SET_FS, (void *)builtin_tls);
if (argc < 2) {
panic("Please provide the linker a program to link\n");
}
Slice s = load_file(argv[1]);
printf("Loaded %s and got %d B\n", argv[1], s.size);
u64 entrypoint = load_elf(s);
printf("entrypoint at 0x%08x\n", entrypoint);
//dbgbreak();
typedef void (*extern_main)(int argc, char **argv);
((extern_main)(entrypoint))(argc - 1, argv + 1);
return 0;
}