[PATCH] ld: Prohibit input name of have the same name as one of its inputs
Renato Alencar
renatoalencar.73@gmail.com
Mon Dec 6 12:50:30 GMT 2021
When doing partial linking, one of the inputs may have the same name as the
output, which generates an incorrect object file because it overwrites the file
before actually reading it. This patch proposes prohibiting that, just gcc does
for compile only cases.
That's the case of this issue on the OCaml compiler (link below),
which generates
an invalid object file with several undefined entries, when they
should be defined.
Although OCaml may have their ways of handling it (there's PR
submitted for that)
this should also be handled by ld in some way. Either by reading the whole file
before starting to writing it or prohibiting that, which is the
current behavior for gcc,
and so I'm adopting it.
* https://discuss.ocaml.org/t/bad-object-file-from-ocamlopt-output-obj-o-foo-o-foo-ml/8753
---
ld/lexsup.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/ld/lexsup.c b/ld/lexsup.c
index a626d7ff6d4..127a3a67201 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -1798,6 +1798,20 @@ parse_args (unsigned argc, char **argv)
}
}
+ for (lang_input_statement_type *statement =
&input_file_chain.head->input_statement;
+ statement != NULL;
+ statement = statement->next_real_file)
+ {
+ if (statement->filename != NULL
+ && output_filename != NULL
+ && strcmp(statement->filename, output_filename) == 0)
+ {
+ einfo (_("%P: input file '%s' is the same as output file\n"),
+ statement->filename);
+ break;
+ }
+ }
+
if (command_line.soname && command_line.soname[0] == '\0')
{
einfo (_("%P: SONAME must not be empty string; ignored\n"));
--
2.33.1
More information about the Binutils
mailing list