I have a question about overwriting a running executable, or overwriting a shared library (.so) file that's in use by one or more running programs.
Back in the day, for the obvious reasons, overwriting a running executable didn't work. There's even a specific errno value, ETXTBSY, that covers this case.
But for quite a while now, I've noticed that when I accidentally try to overwrite a running executable (for example, by firing off a build whose last step is
cc -o exefile on an
exefile that happens to be running), it works!
So my questions are, how does this work, is it documented anywhere, and is it safe to depend on it?
It looks like someone may have tweaked
ld to unlink its output file and create a new one, just to eliminate errors in this case. I can't quite tell if it's doing this all the time, or only if it needs to (that is, perhaps after it tries to overwrite the existing file, and encounters ETXTBSY). And I don't see any mention of this on
ld's man page. (And I wonder why people aren't complaining that
ld may now be breaking their hard links, or changing file ownership, and like that.)
Addendum: The question wasn't specifically about
ld (although that does end up being a big part of the answer); the question was really just "How come I never see ETXTBSY any more? Is it still an error?" And the answer is, yes, it is still an error, just a rare one in practice. (See also the clarifying answer I just posted to my own question.)