From a485e98ea0cbb61ea9da1e7858da545e0bcf1a46 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 10 Sep 2014 14:26:25 +0930 Subject: Move ELF section headers to end of object file Currently, section ordering differs a little for non-loaded reloc sections output by ld -emit-relocs or ld -r and that after passing such objects through objcopy. Not that it really matters, but it would be better for a simple objcopy to produce an unchanged output object file. Also, section headers are put somewhere in the middle of the non-loaded sections, again slightly differently for ld and objcopy. This patch fixes these discrepancies and puts section headers last, which is where gold puts them, and is where bfd_from_remote_memory wrongly assumed they will be found. bfd/ * elf.c (assign_file_positions_except_relocs): Move section header placement to.. (_bfd_elf_assign_file_positions_for_relocs): ..here. Make static. * elf-bfd.h (_bfd_elf_assign_file_positions_for_relocs): Delete. * elflink.c (bfd_elf_final_link): Don't call above function. gas/testsuite/ * gas/arm/got_prel.d: Adjust for changed section header placement. * gas/i386/ilp32/x86-64-size-1.d: Likewise. * gas/i386/ilp32/x86-64-size-3.d: Likewise. * gas/i386/ilp32/x86-64-size-5.d: Likewise. * gas/i386/ilp32/x86-64-unwind.d: Likewise. * gas/i386/size-1.d: Likewise. * gas/i386/size-3.d: Likewise. * gas/i386/x86-64-size-1.d: Likewise. * gas/i386/x86-64-size-3.d: Likewise. * gas/i386/x86-64-size-5.d: Likewise. * gas/i386/x86-64-unwind.d: Likewise. * gas/ia64/alias-ilp32.d: Likewise. * gas/ia64/alias.d: Likewise. * gas/ia64/group-1.d: Likewise. * gas/ia64/group-2.d: Likewise. * gas/ia64/secname-ilp32.d: Likewise. * gas/ia64/secname.d: Likewise. * gas/ia64/unwind-ilp32.d: Likewise. * gas/ia64/unwind.d: Likewise. * gas/mmix/bspec-1.d: Likewise. * gas/mmix/bspec-2.d: Likewise. * gas/mmix/byte-1.d: Likewise. * gas/mmix/loc-1.d: Likewise. * gas/mmix/loc-2.d: Likewise. * gas/mmix/loc-3.d: Likewise. * gas/mmix/loc-4.d: Likewise. * gas/mmix/loc-5.d: Likewise. * gas/tic6x/scomm-directive-4.d: Likewise. ld/testsuite/ * ld-aarch64/emit-relocs-local-addend.d: Adjust for changed section header placement. * ld-aarch64/local-addend-r.d: Likewise. * ld-mmix/bspec1.d: Likewise. * ld-mmix/bspec2.d: Likewise. * ld-mmix/local1.d: Likewise. * ld-mmix/local3.d: Likewise. * ld-mmix/local5.d: Likewise. * ld-mmix/local7.d: Likewise. * ld-mmix/undef-3.d: Likewise. * ld-sh/sh64/crange3-cmpct.rd: Likewise. * ld-sh/sh64/crange3-media.rd: Likewise. * ld-sh/sh64/crangerel1.rd: Likewise. * ld-sh/sh64/crangerel2.rd: Likewise. * ld-tic6x/common.d: Likewise. * ld-tic6x/shlib-1.rd: Likewise. * ld-tic6x/shlib-1b.rd: Likewise. * ld-tic6x/shlib-1r.rd: Likewise. * ld-tic6x/shlib-1rb.rd: Likewise. * ld-tic6x/shlib-app-1.rd: Likewise. * ld-tic6x/shlib-app-1b.rd: Likewise. * ld-tic6x/shlib-app-1r.rd: Likewise. * ld-tic6x/shlib-app-1rb.rd: Likewise. * ld-tic6x/shlib-noindex.rd: Likewise. * ld-tic6x/static-app-1.rd: Likewise. * ld-tic6x/static-app-1b.rd: Likewise. * ld-tic6x/static-app-1r.rd: Likewise. * ld-tic6x/static-app-1rb.rd: Likewise. * ld-x86-64/ilp32-4.d: Likewise. * ld-x86-64/split-by-file-nacl.rd: Likewise. * ld-x86-64/split-by-file.rd: Likewise. --- bfd/ChangeLog | 8 ++++++++ bfd/elf-bfd.h | 2 -- bfd/elf.c | 25 +++++++++++++------------ bfd/elflink.c | 8 +++----- 4 files changed, 24 insertions(+), 19 deletions(-) (limited to 'bfd') diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 8cbab99..882d994 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,11 @@ +2014-09-10 Alan Modra + + * elf.c (assign_file_positions_except_relocs): Move section header + placement to.. + (_bfd_elf_assign_file_positions_for_relocs): ..here. Make static. + * elf-bfd.h (_bfd_elf_assign_file_positions_for_relocs): Delete. + * elflink.c (bfd_elf_final_link): Don't call above function. + 2014-08-30 Alan Modra * elf32-ppc.c (ppc_elf_relax_section): Fix off by one error. diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 63ffa85..2f24274 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1988,8 +1988,6 @@ extern long _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *, bfd *, long); extern bfd_boolean _bfd_elf_compute_section_file_positions (bfd *, struct bfd_link_info *); -extern void _bfd_elf_assign_file_positions_for_relocs - (bfd *); extern file_ptr _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *, file_ptr, bfd_boolean); diff --git a/bfd/elf.c b/bfd/elf.c index 9dc6b6d..a772db0 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -5124,7 +5124,6 @@ assign_file_positions_except_relocs (bfd *abfd, { struct elf_obj_tdata *tdata = elf_tdata (abfd); Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); - file_ptr off; const struct elf_backend_data *bed = get_elf_backend_data (abfd); if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 @@ -5134,6 +5133,7 @@ assign_file_positions_except_relocs (bfd *abfd, unsigned int num_sec = elf_numsections (abfd); Elf_Internal_Shdr **hdrpp; unsigned int i; + file_ptr off; /* Start after the ELF header. */ off = i_ehdrp->e_ehsize; @@ -5157,6 +5157,8 @@ assign_file_positions_except_relocs (bfd *abfd, else off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); } + + elf_next_file_pos (abfd) = off; } else { @@ -5203,17 +5205,8 @@ assign_file_positions_except_relocs (bfd *abfd, if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) return FALSE; - - off = elf_next_file_pos (abfd); } - /* Place the section headers. */ - off = align_file_position (off, 1 << bed->s->log_file_align); - i_ehdrp->e_shoff = off; - off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; - - elf_next_file_pos (abfd) = off; - return TRUE; } @@ -5306,14 +5299,16 @@ prep_headers (bfd *abfd) } /* Assign file positions for all the reloc sections which are not part - of the loadable file image. */ + of the loadable file image, and the file position of section headers. */ -void +static void _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) { file_ptr off; unsigned int i, num_sec; Elf_Internal_Shdr **shdrpp; + Elf_Internal_Ehdr *i_ehdrp; + const struct elf_backend_data *bed; off = elf_next_file_pos (abfd); @@ -5328,6 +5323,12 @@ _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); } +/* Place the section headers. */ + i_ehdrp = elf_elfheader (abfd); + bed = get_elf_backend_data (abfd); + off = align_file_position (off, 1 << bed->s->log_file_align); + i_ehdrp->e_shoff = off; + off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; elf_next_file_pos (abfd) = off; } diff --git a/bfd/elflink.c b/bfd/elflink.c index c1e7948..d33efe0 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -10691,12 +10691,10 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) esdo->rela.count = 0; } - _bfd_elf_assign_file_positions_for_relocs (abfd); - /* We have now assigned file positions for all the sections except - .symtab and .strtab. We start the .symtab section at the current - file position, and write directly to it. We build the .strtab - section in memory. */ + .symtab, .strtab, and non-loaded reloc sections. We start the + .symtab section at the current file position, and write directly + to it. We build the .strtab section in memory. */ bfd_get_symcount (abfd) = 0; symtab_hdr = &elf_tdata (abfd)->symtab_hdr; /* sh_name is set in prep_headers. */ -- cgit v1.1