summaryrefslogtreecommitdiff
path: root/load.c
blob: 1aa4efd1d26f5656f973f11c72c97e86f230a261 (plain)
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
#include "load.h"
#include "qdme.h"
#include "elf.h"
#include <stdlib.h>

int LoadBinary(const char * const path) {
	FILE *fp = fopen(path, "r");
	if (fp == NULL) {
		perror("LoadBinary");
		return -1;
	}

	ElfHeader_t e = ReadElf(fp);
	if (!ValidateElf(e)) {
		return -1;
	}

	pc = e.e_entry;

	const size_t n = e.e_phnum;
	ProgramHeader_t *p = (ProgramHeader_t *)calloc(n, e.e_phentsize);
	if (p == NULL) {
		perror("calloc");
		return -1;
	}
	ReadProgramHeader(n, p, fp);

	for (size_t i = 0; i < n; i++) {
		memsize += p[i].memsz;
	}

	if ((memory = (uint8_t *)calloc(memsize, sizeof(uint8_t))) == NULL) {
		perror("calloc");
		free((void *)p);
		fclose(fp);
		return -1;
	}

	for (size_t i = 0; i < n; i++) {
		if (LoadProgramHeader(p[i], fp) == -1) {
			free((void *)p);
			fclose(fp);
			return -1;
		}
	}

	free((void *)p);
	fclose(fp);
	return 0;
}

/* LoadProgramHeader: Load the program segment described by the given program
 * header from file fp. Returns 0 on success and -1 on failure. */
int LoadProgramHeader(ProgramHeader_t p, FILE *fp) {
	uint8_t *memloc = memory + p.vaddr;
	size_t s = p.filesz;
	if (fseek(fp, p.offset, SEEK_SET) != 0) {
		perror("fseek");
		return -1;
	}
	fread(memloc, sizeof(uint8_t), s, fp);
	if (ferror(fp)) {
		return -1;
	}
	return 0;
}