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

llvm-svn: 292041
This commit is contained in:
Rui Ueyama 2017-01-15 00:41:21 +00:00
parent dcd32937dc
commit 543731f96a
2 changed files with 15 additions and 32 deletions

View File

@ -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_ostream &OS, const SectionChunk *SC,
}
}
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);
std::error_code EC;
raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
if (EC)
fatal(EC.message());
FileRemover RAII(TempPath);
writeMapFile2(FD, OutputSections);
EC = sys::fs::rename(TempPath, MapFile);
if (EC)
fatal(EC.message());
fatal("cannot open " + Config->MapFile + ": " + EC.message());
writeMapFile2(OS, OutputSections);
}

View File

@ -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_ostream &OS, const InputSection<ELFT> *IS,
}
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 *>);