Lines Matching refs:ehdr

24 static inline bool elf_is_elf_file(const struct elfhdr *ehdr)  in elf_is_elf_file()  argument
26 return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0; in elf_is_elf_file()
29 static uint64_t elf64_to_cpu(const struct elfhdr *ehdr, uint64_t value) in elf64_to_cpu() argument
31 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) in elf64_to_cpu()
33 else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) in elf64_to_cpu()
39 static uint32_t elf32_to_cpu(const struct elfhdr *ehdr, uint32_t value) in elf32_to_cpu() argument
41 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) in elf32_to_cpu()
43 else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) in elf32_to_cpu()
49 static uint16_t elf16_to_cpu(const struct elfhdr *ehdr, uint16_t value) in elf16_to_cpu() argument
51 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) in elf16_to_cpu()
53 else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) in elf16_to_cpu()
63 static bool elf_is_ehdr_sane(const struct elfhdr *ehdr, size_t buf_len) in elf_is_ehdr_sane() argument
65 if (ehdr->e_phnum > 0 && ehdr->e_phentsize != sizeof(struct elf_phdr)) { in elf_is_ehdr_sane()
68 } else if (ehdr->e_shnum > 0 && in elf_is_ehdr_sane()
69 ehdr->e_shentsize != sizeof(struct elf_shdr)) { in elf_is_ehdr_sane()
72 } else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT || in elf_is_ehdr_sane()
73 ehdr->e_version != EV_CURRENT) { in elf_is_ehdr_sane()
78 if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) { in elf_is_ehdr_sane()
85 phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum; in elf_is_ehdr_sane()
88 if (ehdr->e_phoff + phdr_size < ehdr->e_phoff) { in elf_is_ehdr_sane()
91 } else if (ehdr->e_phoff + phdr_size > buf_len) { in elf_is_ehdr_sane()
97 if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) { in elf_is_ehdr_sane()
104 shdr_size = sizeof(struct elf_shdr) * ehdr->e_shnum; in elf_is_ehdr_sane()
107 if (ehdr->e_shoff + shdr_size < ehdr->e_shoff) { in elf_is_ehdr_sane()
110 } else if (ehdr->e_shoff + shdr_size > buf_len) { in elf_is_ehdr_sane()
119 static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr) in elf_read_ehdr() argument
128 memset(ehdr, 0, sizeof(*ehdr)); in elf_read_ehdr()
129 memcpy(ehdr->e_ident, buf, sizeof(ehdr->e_ident)); in elf_read_ehdr()
130 if (!elf_is_elf_file(ehdr)) { in elf_read_ehdr()
135 if (ehdr->e_ident[EI_CLASS] != ELF_CLASS) { in elf_read_ehdr()
138 } else if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB && in elf_read_ehdr()
139 ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { in elf_read_ehdr()
145 if (elf16_to_cpu(ehdr, buf_ehdr->e_ehsize) != sizeof(*buf_ehdr)) { in elf_read_ehdr()
150 ehdr->e_type = elf16_to_cpu(ehdr, buf_ehdr->e_type); in elf_read_ehdr()
151 ehdr->e_machine = elf16_to_cpu(ehdr, buf_ehdr->e_machine); in elf_read_ehdr()
152 ehdr->e_version = elf32_to_cpu(ehdr, buf_ehdr->e_version); in elf_read_ehdr()
153 ehdr->e_flags = elf32_to_cpu(ehdr, buf_ehdr->e_flags); in elf_read_ehdr()
154 ehdr->e_phentsize = elf16_to_cpu(ehdr, buf_ehdr->e_phentsize); in elf_read_ehdr()
155 ehdr->e_phnum = elf16_to_cpu(ehdr, buf_ehdr->e_phnum); in elf_read_ehdr()
156 ehdr->e_shentsize = elf16_to_cpu(ehdr, buf_ehdr->e_shentsize); in elf_read_ehdr()
157 ehdr->e_shnum = elf16_to_cpu(ehdr, buf_ehdr->e_shnum); in elf_read_ehdr()
158 ehdr->e_shstrndx = elf16_to_cpu(ehdr, buf_ehdr->e_shstrndx); in elf_read_ehdr()
160 switch (ehdr->e_ident[EI_CLASS]) { in elf_read_ehdr()
162 ehdr->e_entry = elf64_to_cpu(ehdr, buf_ehdr->e_entry); in elf_read_ehdr()
163 ehdr->e_phoff = elf64_to_cpu(ehdr, buf_ehdr->e_phoff); in elf_read_ehdr()
164 ehdr->e_shoff = elf64_to_cpu(ehdr, buf_ehdr->e_shoff); in elf_read_ehdr()
168 ehdr->e_entry = elf32_to_cpu(ehdr, buf_ehdr->e_entry); in elf_read_ehdr()
169 ehdr->e_phoff = elf32_to_cpu(ehdr, buf_ehdr->e_phoff); in elf_read_ehdr()
170 ehdr->e_shoff = elf32_to_cpu(ehdr, buf_ehdr->e_shoff); in elf_read_ehdr()
178 return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC; in elf_read_ehdr()
208 const struct elfhdr *ehdr = elf_info->ehdr; in elf_read_phdr() local
212 pbuf = buf + elf_info->ehdr->e_phoff + (idx * sizeof(*buf_phdr)); in elf_read_phdr()
215 phdr->p_type = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_type); in elf_read_phdr()
216 phdr->p_flags = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_flags); in elf_read_phdr()
218 switch (ehdr->e_ident[EI_CLASS]) { in elf_read_phdr()
220 phdr->p_offset = elf64_to_cpu(ehdr, buf_phdr->p_offset); in elf_read_phdr()
221 phdr->p_paddr = elf64_to_cpu(ehdr, buf_phdr->p_paddr); in elf_read_phdr()
222 phdr->p_vaddr = elf64_to_cpu(ehdr, buf_phdr->p_vaddr); in elf_read_phdr()
223 phdr->p_filesz = elf64_to_cpu(ehdr, buf_phdr->p_filesz); in elf_read_phdr()
224 phdr->p_memsz = elf64_to_cpu(ehdr, buf_phdr->p_memsz); in elf_read_phdr()
225 phdr->p_align = elf64_to_cpu(ehdr, buf_phdr->p_align); in elf_read_phdr()
229 phdr->p_offset = elf32_to_cpu(ehdr, buf_phdr->p_offset); in elf_read_phdr()
230 phdr->p_paddr = elf32_to_cpu(ehdr, buf_phdr->p_paddr); in elf_read_phdr()
231 phdr->p_vaddr = elf32_to_cpu(ehdr, buf_phdr->p_vaddr); in elf_read_phdr()
232 phdr->p_filesz = elf32_to_cpu(ehdr, buf_phdr->p_filesz); in elf_read_phdr()
233 phdr->p_memsz = elf32_to_cpu(ehdr, buf_phdr->p_memsz); in elf_read_phdr()
234 phdr->p_align = elf32_to_cpu(ehdr, buf_phdr->p_align); in elf_read_phdr()
255 const struct elfhdr *ehdr = elf_info->ehdr; in elf_read_phdrs() local
261 phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum; in elf_read_phdrs()
267 for (i = 0; i < ehdr->e_phnum; i++) { in elf_read_phdrs()
297 struct elfhdr *ehdr, in elf_read_from_buffer() argument
302 ret = elf_read_ehdr(buf, len, ehdr); in elf_read_from_buffer()
307 elf_info->ehdr = ehdr; in elf_read_from_buffer()
308 if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) { in elf_read_from_buffer()
327 int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr, in kexec_build_elf_info() argument
333 ret = elf_read_from_buffer(buf, len, ehdr, elf_info); in kexec_build_elf_info()
338 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) { in kexec_build_elf_info()
346 for (i = 0; i < ehdr->e_phnum; i++) { in kexec_build_elf_info()
367 struct elfhdr ehdr; in kexec_elf_probe() local
371 ret = kexec_build_elf_info(buf, len, &ehdr, &elf_info); in kexec_elf_probe()
377 return elf_check_arch(&ehdr) ? 0 : -ENOEXEC; in kexec_elf_probe()
388 int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr, in kexec_elf_load() argument
398 for (i = 0; i < ehdr->e_phnum; i++) { in kexec_elf_load()