- if (bed->elf_backend_modify_program_headers != NULL)
- {
- if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
- return FALSE;
- }
-
- /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
- if (link_info != NULL && bfd_link_pie (link_info))
- {
- unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
- Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
- Elf_Internal_Phdr *end_segment = &segment[num_segments];
-
- /* Find the lowest p_vaddr in PT_LOAD segments. */
- bfd_vma p_vaddr = (bfd_vma) -1;
- for (; segment < end_segment; segment++)
- if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
- p_vaddr = segment->p_vaddr;
-
- /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
- segments is non-zero. */
- if (p_vaddr)
- i_ehdrp->e_type = ET_EXEC;
- }
-
- /* Write out the program headers. */
- alloc = elf_elfheader (abfd)->e_phnum;
- if (alloc == 0)
- return TRUE;
-
- /* PR ld/20815 - Check that the program header segment, if present, will
- be loaded into memory. FIXME: The check below is not sufficient as
- really all PT_LOAD segments should be checked before issuing an error
- message. Plus the PHDR segment does not have to be the first segment
- in the program header table. But this version of the check should
- catch all real world use cases.
-
- FIXME: We used to have code here to sort the PT_LOAD segments into
- ascending order, as per the ELF spec. But this breaks some programs,
- including the Linux kernel. But really either the spec should be
- changed or the programs updated. */
- if (alloc > 1
- && tdata->phdr[0].p_type == PT_PHDR
- && (bed->elf_backend_allow_non_load_phdr == NULL
- || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
- 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)))
- {
- /* 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"),
- abfd);
- return FALSE;
- }