[PATCH] ld: Use stat to check if linker script appears multiple times
H.J. Lu
hjl.tools@gmail.com
Mon Aug 11 16:05:53 GMT 2025
Use stat, instead of strcmp, to check if the same linker script file
appears multiple times for
$ ld -L... -T ././/script.t -T script.t ...
Although ././/script.t and script.t access the same file, but their
filenames are different. strcmp won't work here.
PR ld/24576
* ldfile.c: Include "../gnulib/import/same-inode.h".
(ldfile_find_command_file): Change the second argument from bool
to enum script_open_style. Check if the same linker script file
appears multiple times by using stat, instead using strcmp.
(ldfile_open_command_file_1): Don't check if the same linker
script file appears multiple times here.
* testsuite/ld-scripts/pr24576-2.d: New.
* testsuite/ld-scripts/script.exp: Run pr24576-2.
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
ld/ldfile.c | 72 ++++++++++++++++-------------
ld/testsuite/ld-scripts/pr24576-2.d | 3 ++
ld/testsuite/ld-scripts/script.exp | 1 +
3 files changed, 45 insertions(+), 31 deletions(-)
create mode 100644 ld/testsuite/ld-scripts/pr24576-2.d
diff --git a/ld/ldfile.c b/ld/ldfile.c
index e642c7f6620..a92d9162501 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -35,6 +35,7 @@
#include "libiberty.h"
#include "filenames.h"
#include <fnmatch.h>
+#include "../gnulib/import/same-inode.h"
#if BFD_SUPPORTS_PLUGINS
#include "plugin.h"
#endif /* BFD_SUPPORTS_PLUGINS */
@@ -828,19 +829,26 @@ find_scripts_dir (void)
static FILE *
ldfile_find_command_file (const char *name,
- bool default_only,
+ enum script_open_style open_how,
bool *sysrooted)
{
search_dirs_type *search;
FILE *result = NULL;
- char *path;
+ char *path = NULL;
+ const char *filename = NULL;
+ struct script_name_list *script;
+ size_t len;
+ struct stat sbuf1;
- if (!default_only)
+ if (open_how != script_defaultT)
{
/* First try raw name. */
result = try_open (name, sysrooted);
if (result != NULL)
- return result;
+ {
+ filename = name;
+ goto success;
+ }
}
if (!script_search)
@@ -861,20 +869,45 @@ ldfile_find_command_file (const char *name,
*search_tail_ptr = script_search;
/* Try now prefixes. */
- for (search = default_only ? script_search : search_head;
+ for (search = open_how == script_defaultT ? script_search : search_head;
search != NULL;
search = search->next)
{
path = concat (search->name, slash, name, (const char *) NULL);
result = try_open (path, sysrooted);
- free (path);
if (result)
- break;
+ {
+ filename = path;
+ break;
+ }
}
/* Restore the original path list. */
*search_tail_ptr = NULL;
+success:
+ /* PR 24576: Catch the case where the user has accidentally included
+ the same linker script twice. */
+ if (stat (filename, &sbuf1) == 0)
+ for (script = processed_scripts; script != NULL; script = script->next)
+ if (open_how != script_nonT || script->open_how != script_nonT)
+ {
+ struct stat sbuf2;
+ if (stat (script->name, &sbuf2) == 0
+ && SAME_INODE (sbuf1, sbuf2))
+ fatal (_("%P: error: linker script file '%s'"
+ " appears multiple times\n"), name);
+ }
+
+ len = strlen (filename);
+ script = xmalloc (sizeof (*script) + len);
+ script->next = processed_scripts;
+ script->open_how = open_how;
+ memcpy (script->name, filename, len + 1);
+ processed_scripts = script;
+
+ free (path);
+
return result;
}
@@ -886,31 +919,8 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
{
FILE *ldlex_input_stack;
bool sysrooted;
- struct script_name_list *script;
- size_t len;
-
- /* PR 24576: Catch the case where the user has accidentally included
- the same linker script twice. */
- for (script = processed_scripts; script != NULL; script = script->next)
- {
- if ((open_how != script_nonT || script->open_how != script_nonT)
- && strcmp (name, script->name) == 0)
- {
- fatal (_("%P: error: linker script file '%s'"
- " appears multiple times\n"), name);
- return;
- }
- }
-
- len = strlen (name);
- script = xmalloc (sizeof (*script) + len);
- script->next = processed_scripts;
- script->open_how = open_how;
- memcpy (script->name, name, len + 1);
- processed_scripts = script;
- ldlex_input_stack = ldfile_find_command_file (name,
- open_how == script_defaultT,
+ ldlex_input_stack = ldfile_find_command_file (name, open_how,
&sysrooted);
if (ldlex_input_stack == NULL)
{
diff --git a/ld/testsuite/ld-scripts/pr24576-2.d b/ld/testsuite/ld-scripts/pr24576-2.d
new file mode 100644
index 00000000000..7cd45f9e9cd
--- /dev/null
+++ b/ld/testsuite/ld-scripts/pr24576-2.d
@@ -0,0 +1,3 @@
+#source: default-script.s
+#ld: -defsym _START=0x800 -T ././/default-script.t -T default-script.t
+#error: .*default-script.t' appears multiple times
diff --git a/ld/testsuite/ld-scripts/script.exp b/ld/testsuite/ld-scripts/script.exp
index c223135ae3d..0b37675ebe8 100644
--- a/ld/testsuite/ld-scripts/script.exp
+++ b/ld/testsuite/ld-scripts/script.exp
@@ -234,6 +234,7 @@ run_dump_test "output-section-types"
run_dump_test "ld-version"
run_dump_test "ld-version-2"
run_dump_test "pr24576-1"
+run_dump_test "pr24576-2"
run_dump_test "segment-start" {{name (default)}}
run_dump_test "segment-start" {{name (overridden)} \
--
2.50.1
More information about the Binutils
mailing list