src/kernel/elf.c (view raw)
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
#include <stdint.h>
#include "cedos/elf.h"
#include "cedos/core.h"
#include "cedos/fat.h"
#include "cedos/sched/process.h"
#include "assert.h"
#ifdef DEBUG
#define PRINT_DBG(...) printk("[" __FILE__ "] " __VA_ARGS__)
#else
#define PRINT_DBG(...) {}
#endif
typedef struct {
uint8_t e_ident[16];
uint16_t type;
uint16_t machine;
uint32_t version;
uint32_t entry;
uint32_t proghead_offset;
uint32_t secthead_offset;
uint32_t flags;
uint16_t header_size;
uint16_t ph_entry_size;
uint16_t ph_num;
uint16_t sh_entry_size;
uint16_t sh_num;
uint16_t sh_strndx;
} ELF_HEADER;
typedef struct {
uint32_t type;
uint32_t offset;
VIRT_ADDR virt_addr;
PHYS_ADDR phys_addr;
uint32_t filesize;
uint32_t memsize;
uint32_t flags;
uint32_t align;
} PROG_HEADER;
typedef enum {
SHF_WRITE = 0x01,
SHF_ALLOC = 0x02,
SHF_EXECINSTR = 0x04
} SH_FLAGS;
typedef struct {
uint32_t name;
uint32_t type;
union {
SH_FLAGS flags;
uint32_t value;
};
VIRT_ADDR addr;
uint32_t offset;
uint32_t size;
uint32_t link;
uint32_t info;
uint32_t addr_align;
uint32_t entsize;
} SECT_HEADER;
void elf_infodump(VIRT_ADDR elf_pointer, uint32_t size) {
ELF_HEADER *header = (ELF_HEADER*)elf_pointer;
PRINT_DBG("Reading ELF file from location %p of length %i\n", elf_pointer, size);
PRINT_DBG("\n");
PRINT_DBG("ELF header size: %i\n", sizeof(ELF_HEADER));
assert(sizeof(ELF_HEADER) == 52);
PRINT_DBG("\n");
PRINT_DBG("ELF header hexdump:\n");
memdump(elf_pointer, 52);
PRINT_DBG("\n");
PRINT_DBG("ELF header magic number:\n");
PRINT_DBG("%s\n", &(header->e_ident));
PRINT_DBG("\n");
PRINT_DBG("ELF header infodump:\n");
PRINT_DBG("- machine: %i\n", header->machine);
PRINT_DBG("- version: %i\n", header->version);
PRINT_DBG("- entry: %p\n", header->entry);
PRINT_DBG("\n");
PRINT_DBG("- program headers:\n");
PRINT_DBG(" - offset: %i\n", header->proghead_offset);
PRINT_DBG(" - entries: %i\n", header->ph_num);
PRINT_DBG(" - entry size: %i\n", header->ph_entry_size);
PRINT_DBG("\n");
PRINT_DBG("- section headers:\n");
PRINT_DBG(" - offset: %i\n", header->secthead_offset);
PRINT_DBG(" - entries: %i\n", header->sh_num);
PRINT_DBG(" - entry size: %i\n", header->sh_entry_size);
PRINT_DBG("\n");
PRINT_DBG("Enumerating sections:\n");
int sh_offset = header->secthead_offset;
SECT_HEADER *sect_headers = (SECT_HEADER*)(elf_pointer + sh_offset);
int num_sections = header->sh_num;
int section_size = header->sh_entry_size;
SECT_HEADER sect_names_sh = sect_headers[header->sh_strndx];
VIRT_ADDR sect_names_addr = elf_pointer + sect_names_sh.offset;
assert(sizeof(SECT_HEADER) == section_size);
for (int i = 0; i < num_sections; i++) {
SECT_HEADER sh = sect_headers[i];
char *name = (char*)(sect_names_addr + sh.name);
PRINT_DBG("Section: %s\n", name);
PRINT_DBG("- type: %i\n", sh.type);
PRINT_DBG("- offset: %i\n", sh.offset);
PRINT_DBG("- size: %i\n", sh.size);
PRINT_DBG("- addr: %i\n", sh.addr);
PRINT_DBG("- addr_align: %i\n", sh.addr_align);
PRINT_DBG("\n");
}
}
PROCESS_ID elf_exec(const char *fname, char *args) {
PRINT_DBG("Loading ELF executable \"%s\".\n", fname);
VIRT_ADDR elf_addr = (VIRT_ADDR*)(0xA0000000);
// TODO: needs to change when we have other file systems
int fd = FAT_openat(0, fname, 0);
assert(fd != -1);
int size = FAT_read(fd, elf_addr, 0);
assert(size != 0);
ELF_HEADER *header = (ELF_HEADER*)(elf_addr);
// magic number correct
assert(((uint32_t*)(elf_addr))[0] == 0x464C457F);
// header size correct
assert(sizeof(ELF_HEADER) == 52);
// get section table
int sh_offset = header->secthead_offset;
SECT_HEADER *sect_headers = (SECT_HEADER*)(elf_addr + sh_offset);
int num_sections = header->sh_num;
int section_size = header->sh_entry_size;
SECT_HEADER sect_names_sh = sect_headers[header->sh_strndx];
VIRT_ADDR sect_names_addr = elf_addr + sect_names_sh.offset;
assert(sizeof(SECT_HEADER) == section_size);
// go through all sections and copy/allocate memory as necessary
PRINT_DBG("Enumerating %i sections:\n", num_sections);
for (int i = 0; i < num_sections; i++) {
SECT_HEADER sh = sect_headers[i];
char *name = (char*)(sect_names_addr + sh.name);
if ((sh.flags & SHF_ALLOC) && (sh.flags & SHF_EXECINSTR)) {
VIRT_ADDR lma = elf_addr + sh.offset;
VIRT_ADDR vma = sh.addr;
uint32_t size = sh.size;
PRINT_DBG("%p\n", sh.flags);
PRINT_DBG("Copying code section %s to its destination ", name);
PRINT_DBG("(LMA: %p, VMA: %p)\n", lma, vma);
memcpy(vma, lma, size);
} else if (sh.flags & SHF_ALLOC) {
VIRT_ADDR lma = elf_addr + sh.offset;
VIRT_ADDR vma = sh.addr;
PRINT_DBG("Allocating space for section %s ", name);
PRINT_DBG("(LMA: %p, VMA: %p)\n", lma, vma);
/* TODO: alloc */
} else {
PRINT_DBG("Skipping section %s\n", name);
}
}
PRINT_DBG("\n");
PRINT_DBG("Entry point: %p\n", header->entry);
// enter the process
PROCESS_MAIN *entry = header->entry;
entry(args);
return 0;
}