unsigned int phindex;
struct elf_backend_data *ebd;
struct bfd_preserve preserve;
- struct elf_obj_tdata *new_tdata = NULL;
bfd_size_type amt;
- preserve.arch_info = abfd->arch_info;
+ preserve.marker = NULL;
/* Read in the ELF header in external format. */
if (bfd_bread ((PTR) &x_ehdr, (bfd_size_type) sizeof (x_ehdr), abfd)
!= sizeof (x_ehdr))
{
if (bfd_get_error () != bfd_error_system_call)
- bfd_set_error (bfd_error_wrong_format);
- return NULL;
+ goto wrong;
+ else
+ goto fail;
}
/* Check the magic number. */
/* Give abfd an elf_obj_tdata. */
amt = sizeof (struct elf_obj_tdata);
- new_tdata = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
- if (new_tdata == NULL)
- return NULL;
- preserve.tdata = elf_tdata (abfd);
- elf_tdata (abfd) = new_tdata;
-
- /* Clear section information, since there might be a recognized bfd that
- we now check if we can replace, and we don't want to append to it. */
- preserve.sections = abfd->sections;
- preserve.section_tail = abfd->section_tail;
- preserve.section_count = abfd->section_count;
- preserve.section_htab = abfd->section_htab;
- abfd->sections = NULL;
- abfd->section_tail = &abfd->sections;
- abfd->section_count = 0;
- if (!bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc))
+ preserve.marker = bfd_zalloc (abfd, amt);
+ if (preserve.marker == NULL)
+ goto fail;
+ if (!bfd_preserve_save (abfd, &preserve))
goto fail;
+ elf_tdata (abfd) = preserve.marker;
+
/* Swap in the rest of the header, now that we have the byte order. */
i_ehdrp = elf_elfheader (abfd);
elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
goto wrong;
}
- bfd_hash_table_free (&preserve.section_htab);
+ bfd_preserve_finish (abfd, &preserve);
return abfd->xvec;
wrong:
bfd_set_error (bfd_error_wrong_format);
fail:
- abfd->arch_info = preserve.arch_info;
- if (new_tdata != NULL)
- {
- /* bfd_release frees all memory more recently bfd_alloc'd than
- its arg, as well as its arg. */
- bfd_release (abfd, new_tdata);
- elf_tdata (abfd) = preserve.tdata;
- abfd->section_htab = preserve.section_htab;
- abfd->sections = preserve.sections;
- abfd->section_tail = preserve.section_tail;
- abfd->section_count = preserve.section_count;
- }
+ if (preserve.marker != NULL)
+ bfd_preserve_restore (abfd, &preserve);
return NULL;
}