14 /* We load ELF binaries. The following definitions are taken
15 from the ELF specification, [ELF], more-or-less verbatim. */
18 typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
19 typedef uint16_t Elf32_Half;
27 This appears at the very beginning of an ELF binary. */
30 unsigned char e_ident[16];
39 Elf32_Half e_phentsize;
41 Elf32_Half e_shentsize;
43 Elf32_Half e_shstrndx;
47 There are e_phnum of these, starting at file offset e_phoff. */
60 /* Values for p_type. */
61 #define PT_NULL 0 /* Ignore. */
62 #define PT_LOAD 1 /* Loadable segment. */
63 #define PT_DYNAMIC 2 /* Dynamic linking info. */
64 #define PT_INTERP 3 /* Name of dynamic loader. */
65 #define PT_NOTE 4 /* Auxiliary info. */
66 #define PT_SHLIB 5 /* Reserved. */
67 #define PT_PHDR 6 /* Program header table. */
68 #define PT_STACK 0x6474e551 /* Stack segment. */
70 /* Flags for p_flags. */
71 #define PF_X 1 /* Executable. */
72 #define PF_W 2 /* Writable. */
73 #define PF_R 4 /* Readable. */
75 static bool install_page (struct thread *, void *upage, void *kpage);
76 static bool load_segment (struct thread *, struct file *,
77 const struct Elf32_Phdr *);
78 static bool setup_stack (struct thread *);
80 /* Aborts loading an executable, with an error message. */
81 #define LOAD_ERROR(MSG) \
83 printk ("addrspace_load: %s: ", filename); \
90 addrspace_load (struct thread *t, const char *filename,
91 void (**start) (void))
93 struct Elf32_Ehdr ehdr;
95 bool file_open = false;
100 /* Allocate page directory. */
101 t->pagedir = pagedir_create ();
102 if (t->pagedir == NULL)
103 LOAD_ERROR (("page directory allocation failed"));
105 /* Open executable file. */
106 file_open = filesys_open (filename, &file);
108 LOAD_ERROR (("open failed"));
110 /* Read and verify executable header. */
111 if (file_read (&file, &ehdr, sizeof ehdr) != sizeof ehdr)
112 LOAD_ERROR (("error reading executable header"));
113 if (memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) != 0)
114 LOAD_ERROR (("file is not ELF"));
115 if (ehdr.e_type != 2)
116 LOAD_ERROR (("ELF file is not an executable"));
117 if (ehdr.e_machine != 3)
118 LOAD_ERROR (("ELF executable is not x86"));
119 if (ehdr.e_version != 1)
120 LOAD_ERROR (("ELF executable hasunknown version %d",
121 (int) ehdr.e_version));
122 if (ehdr.e_phentsize != sizeof (struct Elf32_Phdr))
123 LOAD_ERROR (("bad ELF program header size"));
124 if (ehdr.e_phnum > 1024)
125 LOAD_ERROR (("too many ELF program headers"));
127 /* Read program headers. */
128 file_ofs = ehdr.e_phoff;
129 for (i = 0; i < ehdr.e_phnum; i++)
131 struct Elf32_Phdr phdr;
133 file_seek (&file, file_ofs);
134 if (file_read (&file, &phdr, sizeof phdr) != sizeof phdr)
135 LOAD_ERROR (("error reading program header"));
136 file_ofs += sizeof phdr;
143 /* Ignore this segment. */
148 /* Reject the executable. */
149 LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
152 printk ("unknown ELF segment type %08x\n", phdr.p_type);
155 if (!load_segment (t, &file, &phdr))
162 if (!setup_stack (t))
166 *start = (void (*) (void)) ehdr.e_entry;
174 addrspace_destroy (t);
179 addrspace_destroy (struct thread *t)
181 if (t->pagedir != NULL)
183 pagedir_destroy (t->pagedir);
189 addrspace_activate (struct thread *t)
193 pagedir_activate (t->pagedir);
194 tss_set_esp0 ((uint8_t *) t + PGSIZE);
197 /* addrspace_load() helpers. */
200 install_page (struct thread *t, void *upage, void *kpage)
202 /* Verify that there's not already a page at that virtual
203 address, then map our page there. */
204 if (pagedir_get_page (t->pagedir, upage) == NULL
205 && pagedir_set_page (t->pagedir, upage, kpage, true))
215 load_segment (struct thread *t, struct file *file,
216 const struct Elf32_Phdr *phdr)
223 ASSERT (file != NULL);
224 ASSERT (phdr != NULL);
225 ASSERT (phdr->p_type == PT_LOAD);
227 /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
228 if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE)
230 printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
231 phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
235 /* p_memsz must be at least as big as p_filesz. */
236 if (phdr->p_memsz < phdr->p_filesz)
238 printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
239 phdr->p_memsz, phdr->p_filesz);
243 /* Validate virtual memory region to be mapped. */
244 start = pg_round_down ((void *) phdr->p_vaddr);
245 end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
246 if (start >= PHYS_BASE || end >= PHYS_BASE || end < start)
248 printk ("bad virtual region %08lx...%08lx\n",
249 (unsigned long) start, (unsigned long) end);
253 filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
254 file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
255 for (upage = start; upage < (uint8_t *) end; upage += PGSIZE)
257 size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
258 size_t zero_bytes = PGSIZE - read_bytes;
259 uint8_t *kpage = palloc_get (0);
263 if (file_read (file, kpage, read_bytes) != (int) read_bytes)
265 memset (kpage + read_bytes, 0, zero_bytes);
266 filesz_left -= read_bytes;
268 if (!install_page (t, upage, kpage))
276 setup_stack (struct thread *t)
278 uint8_t *kpage = palloc_get (PAL_ZERO);
281 printk ("failed to allocate process stack\n");
285 return install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);