3 /* We load ELF binaries. The following definitions are taken
4 from the ELF specification more-or-less literally. */
7 typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
8 typedef uint16_t Elf32_Half;
11 This appears at the very beginning of an ELF binary. */
14 unsigned char e_ident[16];
23 Elf32_Half e_phentsize;
25 Elf32_Half e_shentsize;
27 Elf32_Half e_shstrndx;
31 There are e_phnum of these, starting at file offset e_phoff. */
44 /* Values for p_type in struct Elf32_Phdr. */
45 #define PT_NULL 0 /* Ignore this program header. */
46 #define PT_LOAD 1 /* Loadable segment. */
47 #define PT_DYNAMIC 2 /* Dynamic linking info. */
48 #define PT_INTERP 3 /* Name of dynamic loader. */
49 #define PT_NOTE 4 /* Auxiliary info. */
50 #define PT_SHLIB 5 /* Reserved. */
51 #define PT_PHDR 6 /* Program header table. */
53 #define LOAD_ERROR(MSG) \
55 printk ("addrspace_load: %s: ", filename); \
62 addrspace_load (struct addrspace *as, const char *filename)
67 file = filesys_open (filename);
69 LOAD_ERROR (("open failed"));
71 /* Read and verify executable header. */
72 if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr)
73 LOAD_ERROR (("error reading executable header"));
74 if (memcmp (ehdr.e_ident, "\x7fELF\1\1\1", 7) != 0)
75 LOAD_ERROR (("not an ELF file"));
77 LOAD_ERROR (("not an executable"));
78 if (ehdr.e_machine != 3)
79 LOAD_ERROR (("not an x86 binary"));
80 if (ehdr.e_version != 1)
81 LOAD_ERROR (("unknown ELF version %d", (int) ehdr.e_version));
82 if (ehdr.e_phentsize != sizeof (struct Elf32_Phdr))
83 LOAD_ERROR (("bad program header size", (int) ehdr.e_phentsize));
84 if (ehdr.e_phnum > 1024)
85 LOAD_ERROR (("too many program headers"));
87 /* Read program headers. */
89 as->page_dir = create_page_dir ();
90 list_init (&as->vmas);