diff options
| author | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2024-07-06 22:49:17 -0400 |
|---|---|---|
| committer | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2024-07-06 22:49:17 -0400 |
| commit | 14e3f70e7b62d2c5c3b934d4eba214d72e198830 (patch) | |
| tree | c9da7a92f57882bed3a624eb50eceaa04e21056e /load.c | |
| parent | aa317d9a7aec97d478bd5f5af3305642a4f258c5 (diff) | |
Restructured the Project
Diffstat (limited to 'load.c')
| -rw-r--r-- | load.c | 67 |
1 files changed, 67 insertions, 0 deletions
@@ -0,0 +1,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; +} + |
