RFC: PR 31761: Stop the linker from overwriting source files

Georg-Johann Lay avr@gjlay.de
Tue Jun 18 17:56:56 GMT 2024



Am 18.06.24 um 19:04 schrieb Nick Clifton:
> Hi Guys,
> 
>    Attached is a proposed patch to address one of the issues raised in
>    PR 31761 - namely that the linker will happily overwrite a source file
>    if it was invoked by mistake.  eg:
> 
>      ld -o foo.c foo.o
> 
>    The patch does not change the linker's behaviour of deleting the
>    output file before writing to it, even if errors occur during the
>    link.  I still consider this to be the correct behaviour.
> 
>    With the patch applied the linker will now behave like this:
> 
>      $ touch foo.c foo.o
>      $ ld -o foo.c foo.o
>      ld: fatal: output file 'foo.c' exists and is a known input file type
>      You can suppress this error by linking with --noinhibit-exec
>      $ echo $?
>      1
> 
>    Any comments or suggestions ?
> 
> Cheers
>    Nick

> +static bool
> +has_known_file_extension (const char * name)
> +{
> +  if (name == NULL)
> +    return false;
> +
> +  size_t namelen = strlen (name);
> +
> +  if (namelen < 3)
> +    return false;
> +
> +  /* FIXME: We could optimize this lookup a lot.  But it is coded
> +     this way for simplicity and ease of extension.  */
> +  static const char * extensions[] =
> +    {
> +      ".c", ".h", ".s", ".C", ".S", ".o", ".t",
> +      ".ld",
> +      ".cpp", ".cxx"
> +    };


GCC support much more file extensions like .sx, .CPP, .cc (GCC's own C++
sources), .f, .for, .f90, ...

https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/gcc.cc;h=d80b604a48dfacd09007c9b19c63e6851b7b4087;hb=HEAD#l1418

And maybe sort them according to the recognized language (and
mention the language) like C++.

Johann


More information about the Binutils mailing list