[PATCH v1] ld: fix segfault when linker script is not found

Matthieu Longo matthieu.longo@arm.com
Wed Sep 10 09:26:37 GMT 2025


On 2025-09-09 11:43, Jan Beulich wrote:
> On 04.09.2025 17:19, Matthieu Longo wrote:
>> ld previously crashed with a segmentation fault if the specified linker
>> script could not be found. Surprisingly, ldfile_find_command_file() has
>> never covered the very common case where an invalid filename was set.
> 
> Imo that's not a correct way to put it. You may want to take a look at the
> 2.40 variant of the function, where everything was still fine afaics. It
> was a later addition which wasn't done as carefully as would have been
> needed. (The same change also looks to have introduced a UBSAN issue, in
> that struct script_name_list has an array-of-1-element field instead of a
> flexible array member.)
> 

Indeed my statement was wrong. I should not have said "never".
The issue seems to have been introduced recently by d048eee2910 [1].

Regarding the UBSAN issue, I have run the sanitizer with the error case 
without the fix, and got an error about the NULL pointer. And once I 
reintroduced the fix, no error was triggered. Thus I am not sure if the 
UBSAN issue you mentioned is the same.
What issue exactly do you reference regarding struct script_name_list. 
Please could you dump the sanitizer trace to give me more context ?
Also, do you want me to fix this UBSAN issue as a part of this patch ?

[1]: 
https://inbox.sourceware.org/binutils/20250812143757.3565482-1-hjl.tools@gmail.com/

>> --- /dev/null
>> +++ b/ld/testsuite/ld-scripts/linker-script-not-found.d
>> @@ -0,0 +1,3 @@
>> +#source: default-script.s
>> +#ld: -T misspelled.ld
>> +#error: cannot open linker script file misspelled\.ld: No such file or directory$
> 
> Everything up to the latter colon is fine, but can we really expect the
> potentially libc-provided error message to exactly match "No such file or
> directory"? (I'm not even entirely certain whether ENOENT is the only
> possible errno value that can come back here.)
> 
> Jan

Yes, you are right. There is a chance that stat() might return a 
different error code. I removed it from the pattern. See the change for 
the next revision below.

#error: cannot open linker script file misspelled\.ld: .*\Z

Matthieu


More information about the Binutils mailing list