[PATCH] ld: Issue an error for recursively included linker script

Jan Beulich jbeulich@suse.com
Mon Aug 11 07:22:40 GMT 2025


On 10.08.2025 15:24, H.J. Lu wrote:
> --- a/ld/ldfile.c
> +++ b/ld/ldfile.c
> @@ -75,6 +75,28 @@ typedef struct input_remap
>  
>  static struct input_remap * input_remaps = NULL;
>  
> +struct opened_script_list
> +{
> +  struct opened_script_list *next;
> +  const char *name;
> +};
> +
> +/* The linked list of opened linker scripts in a linker script.  */
> +static struct opened_script_list *opened_scripts = NULL;
> +
> +/* Clear the opened linker script list and free its memory.  */
> +
> +static void
> +ldfile_opened_script_list_free (struct opened_script_list **root)
> +{
> +  struct opened_script_list *ent;
> +  while ((ent = *root) != NULL)
> +    {
> +      *root = ent->next;
> +      free (ent);
> +    }
> +}
> +
>  void
>  ldfile_add_remap (const char * pattern, const char * renamed)
>  {
> @@ -474,6 +496,11 @@ ldfile_try_open_bfd (const char *attempt,
>  		  ldfile_assumed_script = false;
>  		  fclose (yyin);
>  		  yyin = NULL;
> +
> +		  /* After reading a linker script, clear the opened
> +		     linker script list and free its memory.  */
> +		  ldfile_opened_script_list_free (&opened_scripts);

And we can't get here recursively, in which case - aiui - the freeing
would happen too early?

Jan


More information about the Binutils mailing list