[lld] [lld] lld/ELF/Writer.cpp: write directly to outs when output is "-" (PR #72061)

Keith Winstein via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 12 10:52:03 PST 2023


https://github.com/keithw created https://github.com/llvm/llvm-project/pull/72061

This PR lets the LLD ELF driver write directly to outs (skipping the filesystem) when the output filename is "-".
This is a step towards being able to use LLD as a library without using an `open` syscall.

>From 1e16e4aee4009f74edbcfb442a7d1aa16dc8364d Mon Sep 17 00:00:00 2001
From: Keith Winstein <keithw at cs.stanford.edu>
Date: Sun, 12 Nov 2023 10:44:50 -0800
Subject: [PATCH] [lld] lld/ELF/Writer.cpp: write directly to outs when output
 is "-"

---
 lld/ELF/Writer.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index a84e4864ab0e5a5..b5d0c0cafd230dd 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -652,6 +652,13 @@ template <class ELFT> void Writer<ELFT>::run() {
     if (errorCount())
       return;
 
+    if (config->outputFile == "-") {
+      lld::outs() << StringRef((const char *)buffer->getBufferStart(),
+                               buffer->getBufferSize());
+      lld::outs().flush();
+      return;
+    }
+
     if (auto e = buffer->commit())
       fatal("failed to write output '" + buffer->getPath() +
             "': " + toString(std::move(e)));



More information about the llvm-commits mailing list