]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ld: fix segfault when linker script is not found
authorMatthieu Longo <matthieu.longo@arm.com>
Thu, 4 Sep 2025 14:25:21 +0000 (15:25 +0100)
committerMatthieu Longo <matthieu.longo@arm.com>
Wed, 10 Sep 2025 13:03:35 +0000 (14:03 +0100)
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
ld/testsuite/ld-scripts/linker-script-not-found.d [new file with mode: 0644]
ld/testsuite/ld-scripts/script.exp

index bed04fbf9eb9b395dcef02460c98dd3c6241388c..83591fe21db1ff03f9dd8821b004a2107f30ee5e 100644 (file)
@@ -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 (file)
index 0000000..61d4d8a
--- /dev/null
@@ -0,0 +1,3 @@
+#source: default-script.s
+#ld: -T misspelled.ld
+#error: cannot open linker script file misspelled\.ld: .*\Z
index 40bfe1a56e5ba94cb3672cc2bd956a122802cbd7..7a23f435121bcea1583cedd6cb9e9aeb6a13aecb 100644 (file)
@@ -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)} \