Coverage Report

Created: 2019-05-22 02:55

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