[PATCH] elf: Verify section size for mixed ordered/unordered inputs

Alan Modra amodra@gmail.com
Sat Jan 9 05:02:33 GMT 2021


On Fri, Jan 08, 2021 at 06:20:44AM -0800, H.J. Lu wrote:
> When fixing up SHF_LINK_ORDER, issue a fatal error if the output section
> size is increased.  Otherwise, bfd_set_section_contents will fail later
> when attempting to write contents past the end of the output section.
> 
> 	PR ld/26256
> 	* elflink.c (elf_fixup_link_order): Verify that fixing up
> 	SHF_LINK_ORDER doesn't increase the output section size.
> ---
>  bfd/elflink.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/bfd/elflink.c b/bfd/elflink.c
> index a7c952b6fc1..acc959d526a 100644
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -12039,6 +12039,14 @@ elf_fixup_link_order (struct bfd_link_info *info, bfd *abfd, asection *o)
>        offset = sections[n]->offset + sections[n]->size;
>  
>    free (sections);
> +
> +  /* Verify that fixing up SHF_LINK_ORDER doesn't increase the section
> +     size.  */
> +  if (offset > o->size)
> +    info->callbacks->einfo
> +      (_("%F%P: %pA has ordered sections with incompatible alignments\n"),
> +       o);
> +
>    return TRUE;
>  }

Better than an error without much clue as to what has gone wrong, so
OK.  This function should really be moved to ldelf_before_allocation.

Another thing, the qsort implementation may not be a stable sort.  A
zero return from compare_link_order therefore may result in reordering
input sections unnecessarily.  That isn't nice and should be fixed.

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list