Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Error.h"
24
#include "SymbolTable.h"
25
#include "Symbols.h"
26
#include "Writer.h"
27
28
#include "llvm/Support/Parallel.h"
29
#include "llvm/Support/raw_ostream.h"
30
31
using namespace llvm;
32
using namespace llvm::object;
33
34
using namespace lld;
35
using namespace lld::coff;
36
37
typedef DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>
38
    SymbolMapTy;
39
40
// Print out the first three columns of a line.
41
static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
42
28
                        uint64_t Align) {
43
28
  OS << format("%08llx %08llx %5lld ", Addr, Size, Align);
44
28
}
45
46
85
static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
47
48
// Returns a list of all symbols that we want to print out.
49
7
static std::vector<DefinedRegular *> getSymbols() {
50
7
  std::vector<DefinedRegular *> V;
51
7
  for (ObjFile *File : ObjFile::Instances)
52
10
    for (SymbolBody *B : File->getSymbols())
53
32
      
if (auto *32
Sym32
= dyn_cast<DefinedRegular>(B))
54
32
        
if (32
Sym && 32
!Sym->getCOFFSymbol().isSectionDefinition()32
)
55
11
          V.push_back(Sym);
56
7
  return V;
57
7
}
58
59
// Returns a map from sections to their symbols.
60
7
static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
61
7
  SymbolMapTy Ret;
62
7
  for (DefinedRegular *S : Syms)
63
11
    Ret[S->getChunk()].push_back(S);
64
7
65
7
  // Sort symbols by address.
66
10
  for (auto &It : Ret) {
67
10
    SmallVectorImpl<DefinedRegular *> &V = It.second;
68
1
    std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
69
1
      return A->getRVA() < B->getRVA();
70
1
    });
71
10
  }
72
7
  return Ret;
73
7
}
74
75
// Construct a map from symbols to their stringified representations.
76
static DenseMap<DefinedRegular *, std::string>
77
7
getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
78
7
  std::vector<std::string> Str(Syms.size());
79
11
  for_each_n(parallel::par, (size_t)0, Syms.size(), [&](size_t I) {
80
11
    raw_string_ostream OS(Str[I]);
81
11
    writeHeader(OS, Syms[I]->getRVA(), 0, 0);
82
11
    OS << indent(2) << toString(*Syms[I]);
83
11
  });
84
7
85
7
  DenseMap<DefinedRegular *, std::string> Ret;
86
18
  for (size_t I = 0, E = Syms.size(); 
I < E18
;
++I11
)
87
11
    Ret[Syms[I]] = std::move(Str[I]);
88
7
  return Ret;
89
7
}
90
91
257
void coff::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
92
257
  if (Config->MapFile.empty())
93
250
    return;
94
7
95
7
  std::error_code EC;
96
7
  raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
97
7
  if (EC)
98
0
    fatal("cannot open " + Config->MapFile + ": " + EC.message());
99
7
100
7
  // Collect symbol info that we want to print out.
101
7
  std::vector<DefinedRegular *> Syms = getSymbols();
102
7
  SymbolMapTy SectionSyms = getSectionSyms(Syms);
103
7
  DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings(Syms);
104
7
105
7
  // Print out the header line.
106
7
  OS << "Address  Size     Align Out     In      Symbol\n";
107
7
108
7
  // Print out file contents.
109
7
  for (OutputSection *Sec : OutputSections) {
110
7
    writeHeader(OS, Sec->getRVA(), Sec->getVirtualSize(), /*Align=*/PageSize);
111
7
    OS << Sec->getName() << '\n';
112
7
113
10
    for (Chunk *C : Sec->getChunks()) {
114
10
      auto *SC = dyn_cast<SectionChunk>(C);
115
10
      if (!SC)
116
0
        continue;
117
10
118
10
      writeHeader(OS, SC->getRVA(), SC->getSize(), SC->Alignment);
119
10
      OS << indent(1) << SC->File->getName() << ":(" << SC->getSectionName()
120
10
         << ")\n";
121
10
      for (DefinedRegular *Sym : SectionSyms[SC])
122
11
        OS << SymStr[Sym] << '\n';
123
10
    }
124
7
  }
125
257
}