Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
loader.h File Reference
#include <sys/elf64.h>
Include dependency graph for loader.h:

Go to the source code of this file.

Functions

bool Loader_CheckHeader (const Elf64_Ehdr *ehdr)
 
bool Loader_Load (Thread *thr, VNode *vn, void *buf, uint64_t len)
 

Function Documentation

◆ Loader_CheckHeader()

bool Loader_CheckHeader ( const Elf64_Ehdr ehdr)

Loader_CheckHeader –

Check that the program has a valid ELF header.

Definition at line 34 of file loader.c.

35{
36 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
37 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
38 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
39 ehdr->e_ident[EI_MAG3] != ELFMAG3)
40 return false;
41
42 if (ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
43 return false;
44 }
45
46 if (ehdr->e_machine != EM_AMD64) {
47 return false;
48 }
49
50 return true;
51}
unsigned char e_ident[EI_NIDENT]
Definition: elf64.h:65
Elf64_Half e_machine
Definition: elf64.h:67
#define EI_MAG2
Definition: elf_common.h:67
#define ELFMAG0
Definition: elf_common.h:79
#define ELFMAG3
Definition: elf_common.h:82
#define EI_MAG1
Definition: elf_common.h:66
#define EI_CLASS
Definition: elf_common.h:69
#define ELFMAG1
Definition: elf_common.h:80
#define EM_AMD64
Definition: elf_common.h:184
#define ELFMAG2
Definition: elf_common.h:81
#define ELFCLASS64
Definition: elf_common.h:93
#define EI_MAG0
Definition: elf_common.h:65
#define EI_MAG3
Definition: elf_common.h:68
Here is the caller graph for this function:

◆ Loader_Load()

bool Loader_Load ( Thread thr,
VNode vn,
void *  buf,
uint64_t  len 
)

Loader_Load –

Load the ELF binary into the process belonging to the thread.

Definition at line 131 of file loader.c.

132{
133 int i;
134 const Elf64_Ehdr *ehdr;
135 const Elf64_Phdr *phdr;
136 AS *as = thr->space;
137
138 ehdr = (const Elf64_Ehdr *)(buf);
139 phdr = (const Elf64_Phdr *)(buf + ehdr->e_phoff);
140
141 if (!Loader_CheckHeader(ehdr)) {
142 Log(loader, "Not a valid executable!\n");
143 return false;
144 }
145
146 Log(loader, "%8s %16s %8s %8s\n", "Offset", "VAddr", "FileSize", "MemSize");
147 for (i = 0; i < ehdr->e_phnum; i++)
148 {
149 ASSERT(phdr[i].p_type != PT_DYNAMIC);
150 if (phdr[i].p_type == PT_LOAD) {
151 uint64_t va = phdr[i].p_vaddr;
152 uint64_t memsz = phdr[i].p_memsz;
153 Log(loader, "%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
154 phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz);
155
156 // Make sure it is page aligned
157 va = va & ~(uint64_t)PGMASK;
158 memsz += phdr[i].p_vaddr - va;
159
160 Log(loader, "AllocMap %016llx %08llx\n", va, memsz);
161 if (!PMap_AllocMap(as, va, memsz, PTE_W)) {
162 // XXX: Cleanup!
163 ASSERT(false);
164 return false;
165 }
166 }
167 }
168
170
171 /* XXXFILLMEIN: Load the ELF segments. */
172
173 /* Save the process entry point (i.e., _start) */
174 thr->proc->entrypoint = ehdr->e_entry;
175
176 return true;
177}
#define PTE_W
Definition: amd64.h:36
#define PGMASK
Definition: amd64.h:22
Elf64_Xword p_memsz
Definition: elf64.h:110
Elf64_Addr p_vaddr
Definition: elf64.h:107
Elf64_Addr e_entry
Definition: elf64.h:69
Elf64_Off e_phoff
Definition: elf64.h:70
Elf64_Half e_phnum
Definition: elf64.h:75
#define PT_DYNAMIC
Definition: elf_common.h:319
#define PT_LOAD
Definition: elf_common.h:318
static char buf[4096]
Definition: ethdump.c:10
#define Log(_module, _format,...)
Definition: kassert.h:32
#define ASSERT(_x)
Definition: kassert.h:8
bool Loader_CheckHeader(const Elf64_Ehdr *ehdr)
Definition: loader.c:34
#define MEM_USERSPACE_STKLEN
Definition: pmap.h:37
#define MEM_USERSPACE_STKBASE
Definition: pmap.h:36
bool PMap_AllocMap(AS *as, uint64_t virt, uint64_t len, uint64_t flags)
Definition: pmap.c:423
Definition: pmap.h:52
uintptr_t entrypoint
Definition: thread.h:69
struct Process * proc
Definition: thread.h:39
AS * space
Definition: thread.h:33
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:
Here is the caller graph for this function: