| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| char* |
| elf_core_file_failing_command (bfd *abfd) |
| { |
| return elf_tdata (abfd)->core->command; |
| } |
| |
| int |
| elf_core_file_failing_signal (bfd *abfd) |
| { |
| return elf_tdata (abfd)->core->signal; |
| } |
| |
| int |
| elf_core_file_pid (bfd *abfd) |
| { |
| return elf_tdata (abfd)->core->pid; |
| } |
| |
| bfd_boolean |
| elf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd) |
| { |
| char* corename; |
| |
| |
| |
| if (core_bfd->xvec != exec_bfd->xvec) |
| { |
| bfd_set_error (bfd_error_system_call); |
| return FALSE; |
| } |
| |
| |
| corename = elf_tdata (core_bfd)->core->program; |
| if (corename != NULL) |
| { |
| const char* execname = strrchr (exec_bfd->filename, '/'); |
| |
| execname = execname ? execname + 1 : exec_bfd->filename; |
| |
| if (strcmp (execname, corename) != 0) |
| return FALSE; |
| } |
| |
| return TRUE; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const bfd_target * |
| elf_core_file_p (bfd *abfd) |
| { |
| Elf_External_Ehdr x_ehdr; |
| Elf_Internal_Ehdr *i_ehdrp; |
| Elf_Internal_Phdr *i_phdrp; |
| unsigned int phindex; |
| const struct elf_backend_data *ebd; |
| bfd_size_type amt; |
| |
| |
| if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr)) |
| { |
| if (bfd_get_error () != bfd_error_system_call) |
| goto wrong; |
| else |
| goto fail; |
| } |
| |
| |
| if (! elf_file_p (&x_ehdr)) |
| goto wrong; |
| |
| |
| |
| |
| if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS) |
| goto wrong; |
| |
| |
| switch (x_ehdr.e_ident[EI_DATA]) |
| { |
| case ELFDATA2MSB: |
| if (! bfd_big_endian (abfd)) |
| goto wrong; |
| break; |
| case ELFDATA2LSB: |
| if (! bfd_little_endian (abfd)) |
| goto wrong; |
| break; |
| default: |
| goto wrong; |
| } |
| |
| |
| if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd)) |
| goto fail; |
| |
| |
| i_ehdrp = elf_elfheader (abfd); |
| elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); |
| |
| |
| elf_debug_file (i_ehdrp); |
| |
| |
| ebd = get_elf_backend_data (abfd); |
| |
| |
| |
| |
| if (ebd->elf_machine_code != i_ehdrp->e_machine |
| && (ebd->elf_machine_alt1 == 0 |
| || i_ehdrp->e_machine != ebd->elf_machine_alt1) |
| && (ebd->elf_machine_alt2 == 0 |
| || i_ehdrp->e_machine != ebd->elf_machine_alt2)) |
| { |
| const bfd_target * const *target_ptr; |
| |
| if (ebd->elf_machine_code != EM_NONE) |
| goto wrong; |
| |
| |
| |
| |
| for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) |
| { |
| const struct elf_backend_data *back; |
| |
| if ((*target_ptr)->flavour != bfd_target_elf_flavour) |
| continue; |
| back = xvec_get_elf_backend_data (*target_ptr); |
| if (back->s->arch_size != ARCH_SIZE) |
| continue; |
| if (back->elf_machine_code == i_ehdrp->e_machine |
| || (back->elf_machine_alt1 != 0 |
| && i_ehdrp->e_machine == back->elf_machine_alt1) |
| || (back->elf_machine_alt2 != 0 |
| && i_ehdrp->e_machine == back->elf_machine_alt2)) |
| { |
| |
| |
| goto wrong; |
| } |
| } |
| } |
| |
| |
| |
| if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE) |
| goto wrong; |
| |
| |
| |
| if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr)) |
| goto wrong; |
| |
| |
| |
| if (i_ehdrp->e_shoff != 0 && i_ehdrp->e_phnum == PN_XNUM) |
| { |
| Elf_External_Shdr x_shdr; |
| Elf_Internal_Shdr i_shdr; |
| file_ptr where = (file_ptr) i_ehdrp->e_shoff; |
| |
| |
| if (bfd_seek (abfd, where, SEEK_SET) != 0) |
| goto fail; |
| |
| |
| |
| if (bfd_bread (&x_shdr, sizeof (x_shdr), abfd) != sizeof (x_shdr)) |
| goto fail; |
| elf_swap_shdr_in (abfd, &x_shdr, &i_shdr); |
| |
| if (i_shdr.sh_info != 0) |
| { |
| i_ehdrp->e_phnum = i_shdr.sh_info; |
| if (i_ehdrp->e_phnum != i_shdr.sh_info) |
| goto wrong; |
| } |
| } |
| |
| |
| |
| if (i_ehdrp->e_phnum > 1) |
| { |
| Elf_External_Phdr x_phdr; |
| Elf_Internal_Phdr i_phdr; |
| file_ptr where; |
| |
| |
| |
| if (i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (x_phdr) |
| || i_ehdrp->e_phnum > (unsigned int) -1 / sizeof (i_phdr)) |
| goto wrong; |
| |
| where = (file_ptr)(i_ehdrp->e_phoff + (i_ehdrp->e_phnum - 1) * sizeof (x_phdr)); |
| if ((bfd_size_type) where <= i_ehdrp->e_phoff) |
| goto wrong; |
| |
| if (bfd_seek (abfd, where, SEEK_SET) != 0) |
| goto fail; |
| if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr)) |
| goto fail; |
| } |
| |
| |
| if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0) |
| goto wrong; |
| |
| |
| amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum; |
| i_phdrp = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt); |
| if (!i_phdrp) |
| goto fail; |
| |
| elf_tdata (abfd)->phdr = i_phdrp; |
| |
| |
| for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex) |
| { |
| Elf_External_Phdr x_phdr; |
| |
| if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr)) |
| goto fail; |
| |
| elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex); |
| } |
| |
| |
| |
| |
| if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0) |
| |
| && ebd->elf_machine_code != EM_NONE) |
| goto fail; |
| |
| |
| |
| |
| |
| |
| if (ebd->elf_backend_object_p != NULL |
| && ! ebd->elf_backend_object_p (abfd)) |
| goto wrong; |
| |
| |
| for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex) |
| if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex)) |
| goto fail; |
| |
| |
| { |
| bfd_size_type high = 0; |
| struct stat statbuf; |
| for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex) |
| { |
| Elf_Internal_Phdr *p = i_phdrp + phindex; |
| if (p->p_filesz) |
| { |
| bfd_size_type current = p->p_offset + p->p_filesz; |
| if (high < current) |
| high = current; |
| } |
| } |
| if (bfd_stat (abfd, &statbuf) == 0) |
| { |
| if ((bfd_size_type) statbuf.st_size < high) |
| { |
| _bfd_error_handler |
| |
| (_("warning: %pB is truncated: expected core file " |
| "size >= %" PRIu64 ", found: %" PRIu64), |
| abfd, (uint64_t) high, (uint64_t) statbuf.st_size); |
| } |
| } |
| } |
| |
| |
| bfd_get_start_address (abfd) = i_ehdrp->e_entry; |
| return abfd->xvec; |
| |
| wrong: |
| bfd_set_error (bfd_error_wrong_format); |
| fail: |
| return NULL; |
| } |