diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -3167,24 +3167,14 @@ void RewriteInstance::emitAndLink() { NamedRegionTimer T("emitAndLink", "emit and link", TimerGroupName, TimerGroupDesc, opts::TimeRewrite); - std::error_code EC; - - // This is an object file, which we keep for debugging purposes. - // Once we decide it's useless, we should create it in memory. - SmallString<128> OutObjectPath; - sys::fs::getPotentiallyUniqueTempFileName("output", "o", OutObjectPath); - std::unique_ptr TempOut = - std::make_unique(OutObjectPath, EC, sys::fs::OF_None); - check_error(EC, "cannot create output object file"); - std::unique_ptr BOS = - std::make_unique(TempOut->os()); - raw_pwrite_stream *OS = BOS.get(); + SmallString<0> ObjectBuffer; + raw_svector_ostream OS(ObjectBuffer); // Implicitly MCObjectStreamer takes ownership of MCAsmBackend (MAB) // and MCCodeEmitter (MCE). ~MCObjectStreamer() will delete these // two instances. - std::unique_ptr Streamer = BC->createStreamer(*OS); + std::unique_ptr Streamer = BC->createStreamer(OS); if (EHFrameSection) { if (opts::UseOldText || opts::StrictMode) { @@ -3205,6 +3195,18 @@ exit(1); } + if (opts::KeepTmp) { + SmallString<128> OutObjectPath; + sys::fs::getPotentiallyUniqueTempFileName("output", "o", OutObjectPath); + std::error_code EC; + raw_fd_ostream FOS(OutObjectPath, EC); + check_error(EC, "cannot create output object file"); + FOS << ObjectBuffer; + outs() << "BOLT-INFO: intermediary output object file saved for debugging " + "purposes: " + << OutObjectPath << "\n"; + } + ErrorOr TextSection = BC->getUniqueSectionByName(BC->getMainCodeSectionName()); if (BC->HasRelocations && TextSection) @@ -3216,7 +3218,7 @@ // Get output object as ObjectFile. std::unique_ptr ObjectMemBuffer = - MemoryBuffer::getMemBuffer(BOS->str(), "in-memory object file", false); + MemoryBuffer::getMemBuffer(ObjectBuffer, "in-memory object file", false); auto EFMM = std::make_unique(*BC); EFMM->setNewSecPrefix(getNewSecPrefix()); @@ -3270,13 +3272,6 @@ outs() << "BOLT-INFO: cache metrics after emitting functions:\n"; CacheMetrics::printAll(BC->getSortedFunctions()); } - - if (opts::KeepTmp) { - TempOut->keep(); - outs() << "BOLT-INFO: intermediary output object file saved for debugging " - "purposes: " - << OutObjectPath << "\n"; - } } void RewriteInstance::updateMetadata() {