1 #include <lunaix/exebi/elf32.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/spike.h>
6 #include <klibc/string.h>
9 elf32_read(struct v_file* elf, void* data, size_t off, size_t len)
11 // it is wise to do cached read
12 return pcache_read(elf->inode, data, len, off);
16 elf32_do_open(struct elf32* elf, struct v_file* elf_file)
20 elf->elf_file = elf_file;
22 if ((status = elf32_read_ehdr(elf)) < 0) {
27 if ((status = elf32_read_phdr(elf)) < 0) {
36 elf32_open(struct elf32* elf, const char* path)
38 struct v_dnode* elfdn;
39 struct v_file* elffile;
42 if ((error = vfs_walk_proc(path, &elfdn, NULL, 0))) {
46 if ((error = vfs_open(elfdn, &elffile))) {
50 return elf32_do_open(elf, elffile);
54 elf32_openat(struct elf32* elf, void* elf_vfile)
56 // so the ref count kept in sync
57 vfs_ref_file(elf_vfile);
58 return elf32_do_open(elf, elf_vfile);
62 elf32_close(struct elf32* elf)
69 vfs_close((struct v_file*)elf->elf_file);
72 memset(elf, 0, sizeof(*elf));
78 elf32_static_linked(const struct elf32* elf)
80 for (size_t i = 0; i < elf->eheader.e_phnum; i++) {
81 struct elf32_phdr* phdre = &elf->pheaders[i];
82 if (phdre->p_type == PT_INTERP) {
90 elf32_loadable_memsz(const struct elf32* elf)
92 // XXX: Hmmmm, I am not sure if we need this. This is designed to be handy
93 // if we decided to map the heap region before transfer to loader. As
94 // currently, we push *everything* to user-space loader, thus we modify the
95 // brk syscall to do the initial heap mapping.
98 for (size_t i = 0; i < elf->eheader.e_phnum; i++) {
99 struct elf32_phdr* phdre = &elf->pheaders[i];
100 if (phdre->p_type == PT_LOAD) {
101 sz += phdre->p_memsz;
109 elf32_find_loader(const struct elf32* elf, char* path_out, size_t len)
111 int retval = NO_LOADER;
113 assert_msg(len >= sizeof(DEFAULT_LOADER), "path_out: too small");
115 struct v_file* elfile = (struct v_file*)elf->elf_file;
117 for (size_t i = 0; i < elf->eheader.e_phnum; i++) {
118 struct elf32_phdr* phdre = &elf->pheaders[i];
119 if (phdre->p_type == PT_INTERP) {
120 if (len >= phdre->p_filesz) {
125 elf32_read(elfile, path_out, phdre->p_offset, phdre->p_filesz);
139 elf32_read_ehdr(struct elf32* elf)
141 struct v_file* elfile = (struct v_file*)elf->elf_file;
143 return elf32_read(elfile, (void*)&elf->eheader, 0, SIZE_EHDR);
147 elf32_read_phdr(struct elf32* elf)
151 struct v_file* elfile = (struct v_file*)elf->elf_file;
153 size_t entries = elf->eheader.e_phnum;
154 size_t tbl_sz = entries * SIZE_PHDR;
156 struct elf32_phdr* phdrs = valloc(tbl_sz);
162 status = elf32_read(elfile, phdrs, elf->eheader.e_phoff, tbl_sz);
169 elf->pheaders = phdrs;
174 elf32_check_exec(const struct elf32* elf, int type)
176 const struct elf32_ehdr* ehdr = &elf->eheader;
178 return (ehdr->e_entry) && ehdr->e_type == type;
182 elf32_check_arch(const struct elf32* elf)
184 const struct elf32_ehdr* ehdr = &elf->eheader;
186 return *(u32_t*)(ehdr->e_ident) == ELFMAGIC &&
187 ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
188 ehdr->e_ident[EI_DATA] == ELFDATA2LSB && ehdr->e_machine == EM_386;