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

Jan Beulich jbeulich@suse.com
Thu Aug 14 16:00:49 GMT 2025


On 14.08.2025 17:47, H.J. Lu wrote:
> On Thu, Aug 14, 2025 at 8:26 AM Jan Beulich <jbeulich@suse.com> wrote:
>> On 14.08.2025 17:05, H.J. Lu wrote:
>>> When a linker script is included recursively by mistake, issue an error
>>> instead of hang forever without outputting any error message.
>>>
>>>       PR ld/33265
>>>       * ldfile.c (opened_scripts): New.
>>>       (ldfile_try_open_bfd): After reading a linker script, clear the
>>>       opened linker script list and free its memory.
>>>       (ldfile_find_command_file): Issue a fatal error when the linker
>>>       script is included recursively.  Add the linker script to the
>>>       linked list of opened linker scripts.
>>>       * testsuite/ld-scripts/libpr33265-1.a: New file.
>>>       * testsuite/ld-scripts/libpr33265-2.a: Likewise.
>>>       * testsuite/ld-scripts/pr33265-1.d: Likewise.
>>>       * testsuite/ld-scripts/pr33265-2.d: Likewise.
>>>       * testsuite/ld-scripts/script.exp: Run PR ld/33265 tests.
>>>
>>> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
>>
>> Is this a v2, or merely a re-submission of v1 (for whatever reason)? In
>> any event, my prior question ...
> 
> It is a rebase.  It should be v2.
> 
>>> @@ -475,6 +480,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_script_free (&opened_scripts);
>>
>> ... here remains: We can't get here recursively, in which case - aiui -
>> the freeing would happen too early?
> 
> True, we won't get there recursively since a recursion will cause a fatal
> error before we get here.  The opened script list only applies to
> the linker script file we are currently reading.  After closing the file,
> the opened script list is no longer valid and should be freed.

Wait - I expect there can be multiple levels of nesting. What if a script
at level 1 includes one (level 2) which includes another one (level 3),
and then level 2 includes the script included at level 1 again? Won't you
have lost track of everything at the point the level 3 script is closed?

Jan


More information about the Binutils mailing list