+2018-09-03  Jozef Lawrynowicz <jozef.l@mittosystems.com>
+           Alan Modra  <amodra@gmail.com>
+
+       PR 23595
+       elf.c (copy_elf_program_header): When first segment contains
+       only the headers and SHT_NOBITS sections, use segment p_filesz
+       to calculate header and padding size.  Use filepos of the first
+       section otherwise.
+
 018-08-31  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/23600
 
       Elf_Internal_Shdr *this_hdr;
       asection *first_section = NULL;
       asection *lowest_section;
+      bfd_boolean no_contents = TRUE;
 
       /* Compute how many sections are in this segment.  */
       for (section = ibfd->sections, section_count = 0;
            {
              if (first_section == NULL)
                first_section = section;
+             if (elf_section_type (section) != SHT_NOBITS)
+               no_contents = FALSE;
              section_count++;
            }
        }
        }
 
       if (map->includes_filehdr && lowest_section != NULL)
-       /* We need to keep the space used by the headers fixed.  */
-       map->header_size = lowest_section->vma - segment->p_vaddr;
+       {
+         /* Try to keep the space used by the headers plus any
+            padding fixed.  If there are sections with file contents
+            in this segment then the lowest sh_offset is the best
+            guess.  Otherwise the segment only has file contents for
+            the headers, and p_filesz is the best guess.  */
+         if (no_contents)
+           map->header_size = segment->p_filesz;
+         else
+           map->header_size = lowest_section->filepos;
+       }
 
       if (!map->includes_phdrs
          && !map->includes_filehdr