Segfault on phdrs allocated but not created

Message ID 20180226002050.GT3812@bubble.grove.modra.org
State New
Headers show
Series
  • Segfault on phdrs allocated but not created
Related show

Commit Message

Alan Modra Feb. 26, 2018, 12:20 a.m.
A number of ARM tests show results like:
ld-new: no address assigned to the veneers output section .gnu.sgstubs
ld-new: can not size stub section: invalid operation
ld-new: warning: cannot find entry symbol _start; defaulting to 0000000000010074
ld-new: tmpdir/cmse-veneers-no-gnu_sgstubs: warning: allocated section `.text' not in segment
Segmentation fault
PASS: Secure gateway veneers: no .gnu.sgstubs section

The PASS despite the segv shows the danger of using "#..." to match
error output.  This patch doesn't try to fix the testuite, but does
prevent the segfault.

	* elf.c (assign_file_positions_except_relocs): Don't segfault
	when actual number of phdrs is less than allocated.  Delete
	outdated comment.  Formatting.


-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/bfd/elf.c b/bfd/elf.c
index 184ab31..5d39a98 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6127,9 +6127,7 @@  assign_file_positions_except_relocs (bfd *abfd,
 	}
 
       /* Write out the program headers.  */
-      alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
-
-      /* Sort the program headers into the ordering required by the ELF standard.  */
+      alloc = elf_elfheader (abfd)->e_phnum;
       if (alloc == 0)
 	return TRUE;
 
@@ -6151,14 +6149,14 @@  assign_file_positions_except_relocs (bfd *abfd,
 							alloc))
 	  && tdata->phdr[1].p_type == PT_LOAD
 	  && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
-	      || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz)
-	      <  (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
+	      || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
+		  < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
 	{
 	  /* The fix for this error is usually to edit the linker script being
 	     used and set up the program headers manually.  Either that or
 	     leave room for the headers at the start of the SECTIONS.  */
-	  _bfd_error_handler (_("\
-%pB: error: PHDR segment not covered by LOAD segment"),
+	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
+				" by LOAD segment"),
 			      abfd);
 	  return FALSE;
 	}