[PATCH] Enhance collect2 to preserve ro outputs as the linker would

Ian Lance Taylor iant@google.com
Wed May 21 23:06:00 GMT 2008


Olivier Hainque <hainque@adacore.com> writes:

> While some target linkers silently replace read-only output files,
> others report an error in such circumstances. For instance, on AIX
> with a dummy program in m.c:
>
>     $ gcc -c m.c
>     $ ld -o m m.o /lib/crt0.o -lc
>     $ chmod -w m
>     $ ld -o m m.o /lib/crt0.o -lc
>     ld: 0711-931 SEVERE ERROR: Output file: m
> 	    The file is write-protected.
>
> collect2 doesn't mimic this behavior and always replaces, which might
> cause suprises to some users. The attached patch is a suggestion to
> improve this situation, with two parts:
>
>     1/ Enhance collect2 so that it mimics the target linker's
>        behavior, as reflected by a new "COLLECT_PRESERVE_RO_OUTPUT"
>        target macro definition.
>
>     2/ Define the macro for ppc-aix.
>
> After which the behavior on this target is
>
>     $ gcc -o m m.c
>     $ chmod -w m
>     $ gcc -o m m.c
>     collect2: output file m exists and is read-only

I don't really like having this be target dependent.  I think that
instead you should change collect2 so that it only removes the output
file if it believes that it created the output file.  That is,
introduce some global variable which says whether to delete the output
file, and initialize it to false.  If tlink_execute succeeds, then set
the variable to true, so that the output file is deleted if collect2
fails.  If tlink_execute fails, don't have collect2 delete the output
file; let the invoked linker make the decision in that case.

Ian



More information about the Gcc-patches mailing list