I started to take a look at our use of temporary files, with the eventual goal of never leaving a temporary file in case of crash.
The first thing I am looking at is where we use the version of createUniqueFile that doesn't open the file. In general that can race. In lld I think there is only one use and it is simple to avoid.
It is used to create a temp name to pass to rename, but there are simpler ways of keeping a reference to a file than renaming it. In fact, just opening the file will do it.
I tested that the close is indeed where the time is spent by wrapping both in std::chrono::system_clock::now. The remove timed at about 5.065000e-06 seconds, the close at 3.852581e-01 seconds.
It broke lld stand-alone builds: