[PATCH] D28714: Directly write to a -Map file.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 13 16:58:27 PST 2017


ruiu created this revision.
ruiu added a reviewer: rafael.
ruiu added a subscriber: llvm-commits.

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

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 -U symbol-im-looking-for)`. Previously,
that kind of things didn't work.


https://reviews.llvm.org/D28714

Files:
  lld/ELF/MapFile.cpp


Index: lld/ELF/MapFile.cpp
===================================================================
--- lld/ELF/MapFile.cpp
+++ lld/ELF/MapFile.cpp
@@ -25,44 +25,44 @@
 #include "InputFiles.h"
 #include "Strings.h"
 
-#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 using namespace llvm::object;
 
 using namespace lld;
 using namespace lld::elf;
 
-static void writeOutSecLine(raw_fd_ostream &OS, int Width, uint64_t Address,
+static void writeOutSecLine(raw_ostream &OS, int Width, uint64_t Address,
                             uint64_t Size, uint64_t Align, StringRef Name) {
   OS << format_hex_no_prefix(Address, Width) << ' '
      << format_hex_no_prefix(Size, Width) << ' ' << format("%5x ", Align)
      << left_justify(Name, 7);
 }
 
-static void writeInSecLine(raw_fd_ostream &OS, int Width, uint64_t Address,
+static void writeInSecLine(raw_ostream &OS, int Width, uint64_t Address,
                            uint64_t Size, uint64_t Align, StringRef Name) {
   // Pass an empty name to align the text to the correct column.
   writeOutSecLine(OS, Width, Address, Size, Align, "");
   OS << ' ' << left_justify(Name, 7);
 }
 
-static void writeFileLine(raw_fd_ostream &OS, int Width, uint64_t Address,
+static void writeFileLine(raw_ostream &OS, int Width, uint64_t Address,
                           uint64_t Size, uint64_t Align, StringRef Name) {
   // Pass an empty name to align the text to the correct column.
   writeInSecLine(OS, Width, Address, Size, Align, "");
   OS << ' ' << left_justify(Name, 7);
 }
 
-static void writeSymbolLine(raw_fd_ostream &OS, int Width, uint64_t Address,
+static void writeSymbolLine(raw_ostream &OS, int Width, uint64_t Address,
                             uint64_t Size, StringRef Name) {
   // Pass an empty name to align the text to the correct column.
   writeFileLine(OS, Width, Address, Size, 0, "");
   OS << ' ' << left_justify(Name, 7);
 }
 
 template <class ELFT>
-static void writeInputSection(raw_fd_ostream &OS, const InputSection<ELFT> *IS,
+static void writeInputSection(raw_ostream &OS, const InputSection<ELFT> *IS,
                               StringRef &PrevName) {
   int Width = ELFT::Is64Bits ? 16 : 8;
   StringRef Name = IS->Name;
@@ -95,9 +95,8 @@
 }
 
 template <class ELFT>
-static void writeMapFile2(int FD,
+static void writeMapFile2(raw_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 @@
 
 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 *>);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28714.84408.patch
Type: text/x-patch
Size: 3521 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170114/666680ba/attachment.bin>


More information about the llvm-commits mailing list