diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -33,6 +33,7 @@ #include "llvm/Support/xxhash.h" #include +#include using namespace llvm; using namespace llvm::MachO; @@ -1147,9 +1148,13 @@ sortSegmentsAndSections(); createLoadCommands(); finalizeAddresses(); + std::future writeMapAsync(std::async(writeMapFile)); finalizeLinkEditSegment(); - writeMapFile(); writeOutputFile(); + + // Ensure writeMapFile completes before the main thread returns + // to prevent undefined behavior. + writeMapAsync.get(); } template void macho::writeResult() { Writer().run(); }