Coverage Report

Created: 2019-07-24 05:18

/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
88
                        uint64_t align) {
44
88
  os << format("%08llx %08llx %5lld ", addr, size, align);
45
88
}
46
47
// Returns a list of all symbols that we want to print out.
48
14
static std::vector<DefinedRegular *> getSymbols() {
49
14
  std::vector<DefinedRegular *> v;
50
14
  for (ObjFile *file : ObjFile::instances)
51
19
    for (Symbol *b : file->getSymbols())
52
174
      if (auto *sym = dyn_cast_or_null<DefinedRegular>(b))
53
89
        if (sym && !sym->getCOFFSymbol().isSectionDefinition())
54
31
          v.push_back(sym);
55
14
  return v;
56
14
}
57
58
// Returns a map from sections to their symbols.
59
14
static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> syms) {
60
14
  SymbolMapTy ret;
61
14
  for (DefinedRegular *s : syms)
62
31
    ret[s->getChunk()].push_back(s);
63
14
64
14
  // Sort symbols by address.
65
30
  for (auto &it : ret) {
66
30
    SmallVectorImpl<DefinedRegular *> &v = it.second;
67
30
    std::sort(v.begin(), v.end(), [](DefinedRegular *a, DefinedRegular *b) {
68
1
      return a->getRVA() < b->getRVA();
69
1
    });
70
30
  }
71
14
  return ret;
72
14
}
73
74
// Construct a map from symbols to their stringified representations.
75
static DenseMap<DefinedRegular *, std::string>
76
14
getSymbolStrings(ArrayRef<DefinedRegular *> syms) {
77
14
  std::vector<std::string> str(syms.size());
78
31
  parallelForEachN((size_t)0, syms.size(), [&](size_t i) {
79
31
    raw_string_ostream os(str[i]);
80
31
    writeHeader(os, syms[i]->getRVA(), 0, 0);
81
31
    os << indent16 << toString(*syms[i]);
82
31
  });
83
14
84
14
  DenseMap<DefinedRegular *, std::string> ret;
85
45
  for (size_t i = 0, e = syms.size(); i < e; 
++i31
)
86
31
    ret[syms[i]] = std::move(str[i]);
87
14
  return ret;
88
14
}
89
90
576
void coff::writeMapFile(ArrayRef<OutputSection *> outputSections) {
91
576
  if (config->mapFile.empty())
92
562
    return;
93
14
94
14
  std::error_code ec;
95
14
  raw_fd_ostream os(config->mapFile, ec, sys::fs::F_None);
96
14
  if (ec)
97
0
    fatal("cannot open " + config->mapFile + ": " + ec.message());
98
14
99
14
  // Collect symbol info that we want to print out.
100
14
  std::vector<DefinedRegular *> syms = getSymbols();
101
14
  SymbolMapTy sectionSyms = getSectionSyms(syms);
102
14
  DenseMap<DefinedRegular *, std::string> symStr = getSymbolStrings(syms);
103
14
104
14
  // Print out the header line.
105
14
  os << "Address  Size     Align Out     In      Symbol\n";
106
14
107
14
  // Print out file contents.
108
18
  for (OutputSection *sec : outputSections) {
109
18
    writeHeader(os, sec->getRVA(), sec->getVirtualSize(), /*align=*/pageSize);
110
18
    os << sec->name << '\n';
111
18
112
43
    for (Chunk *c : sec->chunks) {
113
43
      auto *sc = dyn_cast<SectionChunk>(c);
114
43
      if (!sc)
115
4
        continue;
116
39
117
39
      writeHeader(os, sc->getRVA(), sc->getSize(), sc->getAlignment());
118
39
      os << indent8 << sc->file->getName() << ":(" << sc->getSectionName()
119
39
         << ")\n";
120
39
      for (DefinedRegular *sym : sectionSyms[sc])
121
30
        os << symStr[sym] << '\n';
122
39
    }
123
18
  }
124
14
}