[PATCH v1 5/5] ld: clarify comments on /DISCARD/ output section behavior

Jan Beulich jbeulich@suse.com
Fri Sep 26 13:19:32 GMT 2025


On 18.09.2025 17:06, Matthieu Longo wrote:
> The previous comments made it difficult to understand how the /DISCARD/
> output section interacts with non-contiguous regions.
> 
> In summary, the general rule is that the first (top-most) clause takes
> precedence over subsequent ones:
> - If /DISCARD/ appears first, the section is dropped. There is no need
>   to warn about potential behavior changes with non-contiguous regions
>   when the section is already discarded.
> - If /DISCARD/ follows clauses that assign the input section to an output
>   section, /DISCARD/ is ignored, and the section is kept. In this case,
>   the linker must warn that the section may not be discarded.

Looks okay to me, just that again ...

> --- a/ld/ldlang.c
> +++ b/ld/ldlang.c
> @@ -2709,18 +2709,25 @@ wont_add_section_p (asection *section,
>  
>    if (discard)
>      {
> +      /* /DISCARD/ is seen first and the top-most clause has precedence on the
> +	 next ones, thus the section will be dropped.  No need to warn about
> +	 potential change in behavior with non-contiguous regions when the
> +	 section is already dropped.  */
>        if (section->output_section == NULL)
>  	{
>  	  /* This prevents future calls from assigning this section or
>  	     warning about it again.  */
>  	  section->output_section = bfd_abs_section_ptr;
>  	}
> -      else if (bfd_is_abs_section (section->output_section))
> -	;
> -      else if (link_info.non_contiguous_regions_warnings)
> +      /* The /DISCARD/ clause appears after previous ones which assigned the
> +	 input section to an output section.  /DISCARD/ does not have the
> +	 precedence, so the section will be kept.  */
> +      else if (! bfd_is_abs_section (section->output_section)
> +	       && link_info.non_contiguous_regions_warnings)
>  	einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
> -		 "section `%pA' from `%pB' match /DISCARD/ clause.\n"),
> -	       NULL, section, section->owner);
> +		 "section `%pA' from `%pB' match /DISCARD/ clause.  If the "
> +		 "section can be assigned to an output section, it won't be "
> +		 "discarded.\n"), NULL, section, section->owner);

... original line splitting style wants retaining here.

Jan


More information about the Binutils mailing list