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

Message ID 20250910113412.3059236-1-matthieu.longo@arm.com
State New
Headers
Series [v2] ld: fix segfault when linker script is not found |

Commit Message

Matthieu Longo Sept. 10, 2025, 11:34 a.m. UTC
  ld previously crashed with a segmentation fault if the specified linker
script could not be found. The issue seems to have been introduced
recently by d048eee2910 [1].

This patch adds a check to ensure that a filename was found after
searching the possible prefixes. If no filename was found, the function
returns NULL, and ldfile_open_command_file_1() emits a proper fatal
error message.
This change prevents the crash and provides a clear diagnostic.

A new generic test was also added to cover this error case.

[1]: https://inbox.sourceware.org/binutils/20250812143757.3565482-1-hjl
     .tools@gmail.com/
---
 ld/ldfile.c                                       | 3 +++
 ld/testsuite/ld-scripts/linker-script-not-found.d | 3 +++
 ld/testsuite/ld-scripts/script.exp                | 1 +
 3 files changed, 7 insertions(+)
 create mode 100644 ld/testsuite/ld-scripts/linker-script-not-found.d
  

Comments

Jan Beulich Sept. 10, 2025, 1:01 p.m. UTC | #1
On 10.09.2025 13:34, Matthieu Longo wrote:
> ld previously crashed with a segmentation fault if the specified linker
> script could not be found. The issue seems to have been introduced
> recently by d048eee2910 [1].
> 
> This patch adds a check to ensure that a filename was found after
> searching the possible prefixes. If no filename was found, the function
> returns NULL, and ldfile_open_command_file_1() emits a proper fatal
> error message.
> This change prevents the crash and provides a clear diagnostic.
> 
> A new generic test was also added to cover this error case.
> 
> [1]: https://inbox.sourceware.org/binutils/20250812143757.3565482-1-hjl
>      .tools@gmail.com/

Okay, and thanks for fixing this.

Jan
  
Matthieu Longo Sept. 10, 2025, 1:04 p.m. UTC | #2
On 2025-09-10 14:01, Jan Beulich wrote:
> On 10.09.2025 13:34, Matthieu Longo wrote:
>> ld previously crashed with a segmentation fault if the specified linker
>> script could not be found. The issue seems to have been introduced
>> recently by d048eee2910 [1].
>>
>> This patch adds a check to ensure that a filename was found after
>> searching the possible prefixes. If no filename was found, the function
>> returns NULL, and ldfile_open_command_file_1() emits a proper fatal
>> error message.
>> This change prevents the crash and provides a clear diagnostic.
>>
>> A new generic test was also added to cover this error case.
>>
>> [1]: https://inbox.sourceware.org/binutils/20250812143757.3565482-1-hjl
>>       .tools@gmail.com/
> 
> Okay, and thanks for fixing this.
> 
> Jan

Merged: f04c774b7d49912cc9dcb0cd613e819edb21c3a2

Matthieu
  

Patch

diff --git a/ld/ldfile.c b/ld/ldfile.c
index bed04fbf9eb..83591fe21db 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -877,6 +877,9 @@  ldfile_find_command_file (const char *name,
   /* Restore the original path list.  */
   *search_tail_ptr = NULL;
 
+  if (!filename)
+    return NULL;
+
  success:
   /* PR 24576: Catch the case where the user has accidentally included
      the same linker script twice.  */
diff --git a/ld/testsuite/ld-scripts/linker-script-not-found.d b/ld/testsuite/ld-scripts/linker-script-not-found.d
new file mode 100644
index 00000000000..61d4d8aa57d
--- /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: .*\Z
diff --git a/ld/testsuite/ld-scripts/script.exp b/ld/testsuite/ld-scripts/script.exp
index 40bfe1a56e5..7a23f435121 100644
--- a/ld/testsuite/ld-scripts/script.exp
+++ b/ld/testsuite/ld-scripts/script.exp
@@ -235,6 +235,7 @@  run_dump_test "ld-version"
 run_dump_test "ld-version-2"
 run_dump_test "pr24576-1"
 run_dump_test "pr24576-2"
+run_dump_test "linker-script-not-found"
 
 run_dump_test "segment-start" {{name (default)}}
 run_dump_test "segment-start" {{name (overridden)} \