[lld] r292041 - Directly write to a -Map file.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 14 16:41:22 PST 2017


Author: ruiu
Date: Sat Jan 14 18:41:21 2017
New Revision: 292041

URL: http://llvm.org/viewvc/llvm-project?rev=292041&view=rev
Log:
Directly write to a -Map file.

Previous code had a bug that if the program exits with an assert() or
fail() before the control reaches end of writeMapFile(), it leaves a
temporary file, because FileRemover's dtor isn't called in that case.

I could fix that by removeFileOnSignal() and other functions, but
I think we can simply write to the result file directly. I think
that is straightforward and easy to understand.

Additionally, that allows something like `-Map /dev/null` or a bash
hack such as `-Map >(grep symbol-im-looking-for)`. Previously,
that kind of things didn't work.

Differential Revision: https://reviews.llvm.org/D28714

Modified:
    lld/trunk/COFF/MapFile.cpp
    lld/trunk/ELF/MapFile.cpp

Modified: lld/trunk/COFF/MapFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/MapFile.cpp?rev=292041&r1=292040&r2=292041&view=diff
==============================================================================
--- lld/trunk/COFF/MapFile.cpp (original)
+++ lld/trunk/COFF/MapFile.cpp Sat Jan 14 18:41:21 2017
@@ -26,7 +26,7 @@
 #include "Symbols.h"
 #include "Writer.h"
 
-#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -87,12 +87,12 @@ static void writeSectionChunk(raw_fd_ost
   }
 }
 
-static void writeMapFile2(int FD,
+static void writeMapFile2(raw_fd_ostream &OS,
                           ArrayRef<OutputSection *> OutputSections) {
-  raw_fd_ostream OS(FD, true);
   OS << left_justify("Address", 8) << ' ' << left_justify("Size", 8)
      << ' ' << left_justify("Align", 5) << ' ' << left_justify("Out", 7) << ' '
      << left_justify("In", 7) << ' ' << left_justify("File", 7) << " Symbol\n";
+
   for (OutputSection *Sec : OutputSections) {
     uint32_t VA = Sec->getRVA();
     writeOutSecLine(OS, VA, Sec->getVirtualSize(), /*Align=*/PageSize,
@@ -106,20 +106,12 @@ static void writeMapFile2(int FD,
 }
 
 void coff::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
-  StringRef MapFile = Config->MapFile;
-  if (MapFile.empty())
+  if (Config->MapFile.empty())
     return;
 
-  // Create new file in same directory but with random name.
-  SmallString<128> TempPath;
-  int FD;
-  std::error_code EC =
-      sys::fs::createUniqueFile(Twine(MapFile) + ".tmp%%%%%%%", FD, TempPath);
-  if (EC)
-    fatal(EC.message());
-  FileRemover RAII(TempPath);
-  writeMapFile2(FD, OutputSections);
-  EC = sys::fs::rename(TempPath, MapFile);
+  std::error_code EC;
+  raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
   if (EC)
-    fatal(EC.message());
+    fatal("cannot open " + Config->MapFile + ": " + EC.message());
+  writeMapFile2(OS, OutputSections);
 }

Modified: lld/trunk/ELF/MapFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MapFile.cpp?rev=292041&r1=292040&r2=292041&view=diff
==============================================================================
--- lld/trunk/ELF/MapFile.cpp (original)
+++ lld/trunk/ELF/MapFile.cpp Sat Jan 14 18:41:21 2017
@@ -25,7 +25,7 @@
 #include "InputFiles.h"
 #include "Strings.h"
 
-#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -95,9 +95,8 @@ static void writeInputSection(raw_fd_ost
 }
 
 template <class ELFT>
-static void writeMapFile2(int FD,
+static void writeMapFile2(raw_fd_ostream &OS,
                           ArrayRef<OutputSectionBase *> OutputSections) {
-  raw_fd_ostream OS(FD, true);
   int Width = ELFT::Is64Bits ? 16 : 8;
 
   OS << left_justify("Address", Width) << ' ' << left_justify("Size", Width)
@@ -119,22 +118,14 @@ static void writeMapFile2(int FD,
 
 template <class ELFT>
 void elf::writeMapFile(ArrayRef<OutputSectionBase *> OutputSections) {
-  StringRef MapFile = Config->MapFile;
-  if (MapFile.empty())
+  if (Config->MapFile.empty())
     return;
 
-  // Create new file in same directory but with random name.
-  SmallString<128> TempPath;
-  int FD;
-  std::error_code EC =
-      sys::fs::createUniqueFile(Twine(MapFile) + ".tmp%%%%%%%", FD, TempPath);
+  std::error_code EC;
+  raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
   if (EC)
-    fatal(EC.message());
-  FileRemover RAII(TempPath);
-  writeMapFile2<ELFT>(FD, OutputSections);
-  EC = sys::fs::rename(TempPath, MapFile);
-  if (EC)
-    fatal(EC.message());
+    fatal("cannot open " + Config->MapFile + ": " + EC.message());
+  writeMapFile2<ELFT>(OS, OutputSections);
 }
 
 template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSectionBase *>);




More information about the llvm-commits mailing list