[PATCH v2] ld: Check input section garbage collection error

Alan Modra amodra@gmail.com
Thu Aug 6 01:06:32 GMT 2026


On Thu, Aug 06, 2026 at 07:34:43AM +0800, H.J. Lu wrote:
> diff --git a/bfd/elflink.c b/bfd/elflink.c
> index 31c652b17c9..343fc880cda 100644
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -14776,7 +14776,8 @@ bfd_elf_gc_sections (bfd *obfd, struct bfd_link_info *info)
>      }
>  
>    /* Allow the backend to mark additional target specific sections.  */
> -  obed->gc_mark_extra_sections (info, gc_mark_hook);
> +  if (!obed->gc_mark_extra_sections (info, gc_mark_hook))
> +    return false;
>  
>    /* ... and mark SEC_EXCLUDE for those that go.  */
>    return elf_gc_sweep (obfd, info);
> diff --git a/ld/ldlang.c b/ld/ldlang.c
> index 886c49a1861..f8e9d2496a4 100644
> --- a/ld/ldlang.c
> +++ b/ld/ldlang.c
> @@ -8165,8 +8165,10 @@ lang_gc_sections (void)
>  	}
>      }
>  
> -  if (link_info.gc_sections)
> -    bfd_gc_sections (link_info.output_bfd, &link_info);
> +  if (link_info.gc_sections
> +      && !bfd_gc_sections (link_info.output_bfd, &link_info)
> +      && bfd_get_error () == bfd_error_bad_value)

OK without the bfd_get_error test.  If there is some condition under
which bfd_gc_sections returns false where we'd like the linker to
continue, then we'll fix bfd_gc_sections to return true.  As far as I
can see with a quick scan over the code, bfd_elf_gc_sections returns
false only on an error reading relocs, and I think all of the possible
false returns from elf_link_read_relocs_from_section ought to cause a
fatal error.

> +    fatal (_("%P: --gc-sections failed: %E\n"));
>  }
>  
>  /* Worker for lang_find_relro_sections_1.  */
> -- 
> 2.55.0
> 


-- 
Alan Modra


More information about the Binutils mailing list