451 {
456 bool success = false;
457 int i;
458
459
462 goto done;
464
465
468 printf (
"load: %s: open failed\n", file_name);
469 goto done;
470 }
471
473
475
476
478 ||
memcmp (ehdr.e_ident,
"\177ELF\2\1\1", 7)
479 || ehdr.e_type != 2
480 || ehdr.e_machine != 0x3E
481 || ehdr.e_version != 1
482 || ehdr.e_phentsize != sizeof (
struct Phdr)
483 || ehdr.e_phnum > 1024) {
484 printf (
"load: %s: error loading executable\n", file_name);
485 goto done;
486 }
487
488
489 file_ofs = ehdr.e_phoff;
490 for (i = 0; i < ehdr.e_phnum; i++) {
492
494 goto done;
496
498 goto done;
499 file_ofs += sizeof phdr;
500 switch (phdr.p_type) {
505 default:
506
507 break;
511 goto done;
514 bool writable = (phdr.p_flags &
PF_W) != 0;
516 uint64_t mem_page = phdr.p_vaddr & ~PGMASK;
519 if (phdr.p_filesz > 0) {
520
521
522 read_bytes = page_offset + phdr.p_filesz;
524 - read_bytes);
525 } else {
526
527
528 read_bytes = 0;
530 }
532 read_bytes, zero_bytes, writable))
533 goto done;
534 }
535 else
536 goto done;
537 break;
538 }
539 }
540
541
543 goto done;
544
545
546 if_->
rip = ehdr.e_entry;
547
548
549
550
551
552
553
554 success = true;
555
556done:
557
558
559 return success;
560}
void file_seek(struct file *file, off_t new_pos)
Definition: file.c:149
off_t file_length(struct file *file)
Definition: file.c:141
void file_deny_write(struct file *file)
Definition: file.c:119
off_t file_read(struct file *file, void *buffer, off_t size)
Definition: file.c:73
struct file * filesys_open(const char *name)
Definition: filesys.c:81
int32_t off_t
Definition: off_t.h:9
#define PT_STACK
Definition: process.c:400
#define PF_W
Definition: process.c:403
#define PT_DYNAMIC
Definition: process.c:395
static bool load_segment(struct file *file, off_t ofs, uint8_t *upage, uint32_t read_bytes, uint32_t zero_bytes, bool writable)
Definition: process.c:630
bool setup_stack(struct intr_frame *if_)
Definition: process.c:673
#define PT_PHDR
Definition: process.c:399
static bool validate_segment(const struct Phdr *, struct file *)
Definition: process.c:566
#define ELF
Definition: process.c:437
#define PT_NOTE
Definition: process.c:397
#define PT_LOAD
Definition: process.c:394
#define PT_NULL
Definition: process.c:393
#define Phdr
Definition: process.c:438
#define PT_INTERP
Definition: process.c:396
#define PT_SHLIB
Definition: process.c:398
#define ROUND_UP(X, STEP)
Definition: round.h:6
unsigned int uint32_t
Definition: stdint.h:26
unsigned long long int uint64_t
Definition: stdint.h:29
int printf(const char *,...) PRINTF_FORMAT(1
int memcmp(const void *, const void *, size_t)
Definition: string.c:48
uintptr_t rip
Definition: interrupt.h:55
struct file * running
Definition: thread.h:130
#define PGMASK
Definition: vaddr.h:21