Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/MapFile.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MapFile.cpp --------------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file implements the /lldmap option. It shows lists in order and
11
// hierarchically the output sections, input sections, input files and
12
// symbol:
13
//
14
//   Address  Size     Align Out     File    Symbol
15
//   00201000 00000015     4 .text
16
//   00201000 0000000e     4         test.o:(.text)
17
//   0020100e 00000000     0                 local
18
//   00201005 00000000     0                 f(int)
19
//
20
//===----------------------------------------------------------------------===//
21
22
#include "MapFile.h"
23
#include "SymbolTable.h"
24
#include "Symbols.h"
25
#include "Writer.h"
26
#include "lld/Common/ErrorHandler.h"
27
#include "llvm/Support/Parallel.h"
28
#include "llvm/Support/raw_ostream.h"
29
30
using namespace llvm;
31
using namespace llvm::object;
32
33
using namespace lld;
34
using namespace lld::coff;
35
36
typedef DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>
37
    SymbolMapTy;
38
39
static const std::string Indent8 = "        ";          // 8 spaces
40
static const std::string Indent16 = "                "; // 16 spaces
41
42
// Print out the first three columns of a line.
43
static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
44
88
                        uint64_t Align) {
45
88
  OS << format("%08llx %08llx %5lld ", Addr, Size, Align);
46
88
}
47
48
// Returns a list of all symbols that we want to print out.
49
14
static std::vector<DefinedRegular *> getSymbols() {
50
14
  std::vector<DefinedRegular *> V;
51
14
  for (ObjFile *File : ObjFile::Instances)
52
19
    for (Symbol *B : File->getSymbols())
53
150
      if (auto *Sym = dyn_cast_or_null<DefinedRegular>(B))
54
89
        if (Sym && !Sym->getCOFFSymbol().isSectionDefinition())
55
31
          V.push_back(Sym);
56
14
  return V;
57
14
}
58
59
// Returns a map from sections to their symbols.
60
14
static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
61
14
  SymbolMapTy Ret;
62
14
  for (DefinedRegular *S : Syms)
63
31
    Ret[S->getChunk()].push_back(S);
64
14
65
14
  // Sort symbols by address.
66
30
  for (auto &It : Ret) {
67
30
    SmallVectorImpl<DefinedRegular *> &V = It.second;
68
30
    std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
69
1
      return A->getRVA() < B->getRVA();
70
1
    });
71
30
  }
72
14
  return Ret;
73
14
}
74
75
// Construct a map from symbols to their stringified representations.
76
static DenseMap<DefinedRegular *, std::string>
77
14
getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
78
14
  std::vector<std::string> Str(Syms.size());
79
31
  for_each_n(parallel::par, (size_t)0, Syms.size(), [&](size_t I) {
80
31
    raw_string_ostream OS(Str[I]);
81
31
    writeHeader(OS, Syms[I]->getRVA(), 0, 0);
82
31
    OS << Indent16 << toString(*Syms[I]);
83
31
  });
84
14
85
14
  DenseMap<DefinedRegular *, std::string> Ret;
86
45
  for (size_t I = 0, E = Syms.size(); I < E; 
++I31
)
87
31
    Ret[Syms[I]] = std::move(Str[I]);
88
14
  return Ret;
89
14
}
90
91
385
void coff::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
92
385
  if (Config->MapFile.empty())
93
371
    return;
94
14
95
14
  std::error_code EC;
96
14
  raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
97
14
  if (EC)
98
0
    fatal("cannot open " + Config->MapFile + ": " + EC.message());
99
14
100
14
  // Collect symbol info that we want to print out.
101
14
  std::vector<DefinedRegular *> Syms = getSymbols();
102
14
  SymbolMapTy SectionSyms = getSectionSyms(Syms);
103
14
  DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings(Syms);
104
14
105
14
  // Print out the header line.
106
14
  OS << "Address  Size     Align Out     In      Symbol\n";
107
14
108
14
  // Print out file contents.
109
18
  for (OutputSection *Sec : OutputSections) {
110
18
    writeHeader(OS, Sec->getRVA(), Sec->getVirtualSize(), /*Align=*/PageSize);
111
18
    OS << Sec->Name << '\n';
112
18
113
43
    for (Chunk *C : Sec->getChunks()) {
114
43
      auto *SC = dyn_cast<SectionChunk>(C);
115
43
      if (!SC)
116
4
        continue;
117
39
118
39
      writeHeader(OS, SC->getRVA(), SC->getSize(), SC->Alignment);
119
39
      OS << Indent8 << SC->File->getName() << ":(" << SC->getSectionName()
120
39
         << ")\n";
121
39
      for (DefinedRegular *Sym : SectionSyms[SC])
122
30
        OS << SymStr[Sym] << '\n';
123
39
    }
124
18
  }
125
14
}