Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/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 -Map 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     In      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 "InputFiles.h"
23
#include "LinkerScript.h"
24
#include "OutputSections.h"
25
#include "SymbolTable.h"
26
#include "Symbols.h"
27
#include "SyntheticSections.h"
28
#include "lld/Common/Strings.h"
29
#include "lld/Common/Threads.h"
30
#include "llvm/ADT/MapVector.h"
31
#include "llvm/ADT/SetVector.h"
32
#include "llvm/Support/raw_ostream.h"
33
34
using namespace llvm;
35
using namespace llvm::object;
36
37
using namespace lld;
38
using namespace lld::elf;
39
40
using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
41
42
static const std::string indent8 = "        ";          // 8 spaces
43
static const std::string indent16 = "                "; // 16 spaces
44
45
// Print out the first three columns of a line.
46
static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
47
502
                        uint64_t size, uint64_t align) {
48
502
  if (config->is64)
49
491
    os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
50
11
  else
51
11
    os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
52
502
}
53
54
// Returns a list of all symbols that we want to print out.
55
static std::vector<Defined *> getSymbols() {
56
  std::vector<Defined *> v;
57
  for (InputFile *file : objectFiles)
58
    for (Symbol *b : file->getSymbols())
59
      if (auto *dr = dyn_cast<Defined>(b))
60
        if (!dr->isSection() && dr->section && dr->section->isLive() &&
61
            (dr->file == file || dr->needsPltAddr || dr->section->bss))
62
          v.push_back(dr);
63
  return v;
64
}
65
66
// Returns a map from sections to their symbols.
67
18
static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
68
18
  SymbolMapTy ret;
69
18
  for (Defined *dr : syms)
70
95
    ret[dr->section].push_back(dr);
71
18
72
18
  // Sort symbols by address. We want to print out symbols in the
73
18
  // order in the output file rather than the order they appeared
74
18
  // in the input files.
75
18
  for (auto &it : ret)
76
67
    llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
77
31
      return a->getVA() < b->getVA();
78
31
    });
79
18
  return ret;
80
18
}
81
82
// Construct a map from symbols to their stringified representations.
83
// Demangling symbols (which is what toString() does) is slow, so
84
// we do that in batch using parallel-for.
85
static DenseMap<Symbol *, std::string>
86
18
getSymbolStrings(ArrayRef<Defined *> syms) {
87
18
  std::vector<std::string> str(syms.size());
88
95
  parallelForEachN(0, syms.size(), [&](size_t i) {
89
95
    raw_string_ostream os(str[i]);
90
95
    OutputSection *osec = syms[i]->getOutputSection();
91
95
    uint64_t vma = syms[i]->getVA();
92
95
    uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 
00
;
93
95
    writeHeader(os, vma, lma, syms[i]->getSize(), 1);
94
95
    os << indent16 << toString(*syms[i]);
95
95
  });
96
18
97
18
  DenseMap<Symbol *, std::string> ret;
98
113
  for (size_t i = 0, e = syms.size(); i < e; 
++i95
)
99
95
    ret[syms[i]] = std::move(str[i]);
100
18
  return ret;
101
18
}
102
103
// Print .eh_frame contents. Since the section consists of EhSectionPieces,
104
// we need a specialized printer for that section.
105
//
106
// .eh_frame tend to contain a lot of section pieces that are contiguous
107
// both in input file and output file. Such pieces are squashed before
108
// being displayed to make output compact.
109
4
static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
110
4
  std::vector<EhSectionPiece> pieces;
111
4
112
14
  auto add = [&](const EhSectionPiece &p) {
113
14
    // If P is adjacent to Last, squash the two.
114
14
    if (!pieces.empty()) {
115
10
      EhSectionPiece &last = pieces.back();
116
10
      if (last.sec == p.sec && 
last.inputOff + last.size == p.inputOff7
&&
117
10
          
last.outputOff + last.size == p.outputOff7
) {
118
4
        last.size += p.size;
119
4
        return;
120
4
      }
121
10
    }
122
10
    pieces.push_back(p);
123
10
  };
124
4
125
4
  // Gather section pieces.
126
4
  for (const CieRecord *rec : sec->getCieRecords()) {
127
4
    add(*rec->cie);
128
4
    for (const EhSectionPiece *fde : rec->fdes)
129
10
      add(*fde);
130
4
  }
131
4
132
4
  // Print out section pieces.
133
4
  const OutputSection *osec = sec->getOutputSection();
134
10
  for (EhSectionPiece &p : pieces) {
135
10
    writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
136
10
                p.size, 1);
137
10
    os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
138
10
       << Twine::utohexstr(p.inputOff) + ")\n";
139
10
  }
140
4
}
141
142
2.50k
void elf::writeMapFile() {
143
2.50k
  if (config->mapFile.empty())
144
2.48k
    return;
145
18
146
18
  // Open a map file for writing.
147
18
  std::error_code ec;
148
18
  raw_fd_ostream os(config->mapFile, ec, sys::fs::F_None);
149
18
  if (ec) {
150
0
    error("cannot open " + config->mapFile + ": " + ec.message());
151
0
    return;
152
0
  }
153
18
154
18
  // Collect symbol info that we want to print out.
155
18
  std::vector<Defined *> syms = getSymbols();
156
18
  SymbolMapTy sectionSyms = getSectionSyms(syms);
157
18
  DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
158
18
159
18
  // Print out the header line.
160
18
  int w = config->is64 ? 
1617
:
81
;
161
18
  os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
162
18
     << "     Size Align Out     In      Symbol\n";
163
18
164
18
  OutputSection* osec = nullptr;
165
172
  for (BaseCommand *base : script->sectionCommands) {
166
172
    if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
167
8
      if (cmd->provide && 
!cmd->sym1
)
168
1
        continue;
169
7
      uint64_t lma = osec ? 
osec->getLMA() + cmd->addr - osec->getVA(0)5
:
02
;
170
7
      writeHeader(os, cmd->addr, lma, cmd->size, 1);
171
7
      os << cmd->commandString << '\n';
172
7
      continue;
173
7
    }
174
164
175
164
    osec = cast<OutputSection>(base);
176
164
    writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
177
164
    os << osec->name << '\n';
178
164
179
164
    // Dump symbols for each input section.
180
179
    for (BaseCommand *base : osec->sectionCommands) {
181
179
      if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
182
218
        for (InputSection *isec : isd->sections) {
183
218
          if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
184
4
            printEhFrame(os, ehSec);
185
4
            continue;
186
4
          }
187
214
188
214
          writeHeader(os, isec->getVA(0), osec->getLMA() + isec->getOffset(0),
189
214
                      isec->getSize(), isec->alignment);
190
214
          os << indent8 << toString(isec) << '\n';
191
214
          for (Symbol *sym : sectionSyms[isec])
192
95
            os << symStr[sym] << '\n';
193
214
        }
194
166
        continue;
195
166
      }
196
13
197
13
      if (auto *cmd = dyn_cast<ByteCommand>(base)) {
198
5
        writeHeader(os, osec->addr + cmd->offset, osec->getLMA() + cmd->offset,
199
5
                    cmd->size, 1);
200
5
        os << indent8 << cmd->commandString << '\n';
201
5
        continue;
202
5
      }
203
8
204
8
      if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
205
8
        if (cmd->provide && 
!cmd->sym1
)
206
1
          continue;
207
7
        writeHeader(os, cmd->addr, osec->getLMA() + cmd->addr - osec->getVA(0),
208
7
                    cmd->size, 1);
209
7
        os << indent8 << cmd->commandString << '\n';
210
7
        continue;
211
7
      }
212
8
    }
213
164
  }
214
18
}
215
216
13
static void print(StringRef a, StringRef b) {
217
13
  outs() << left_justify(a, 49) << " " << b << "\n";
218
13
}
219
220
// Output a cross reference table to stdout. This is for --cref.
221
//
222
// For each global symbol, we print out a file that defines the symbol
223
// followed by files that uses that symbol. Here is an example.
224
//
225
//     strlen     /lib/x86_64-linux-gnu/libc.so.6
226
//                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
227
//                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
228
//
229
// In this case, strlen is defined by libc.so.6 and used by other two
230
// files.
231
2.50k
void elf::writeCrossReferenceTable() {
232
2.50k
  if (!config->cref)
233
2.50k
    return;
234
2
235
2
  // Collect symbols and files.
236
2
  MapVector<Symbol *, SetVector<InputFile *>> map;
237
4
  for (InputFile *file : objectFiles) {
238
16
    for (Symbol *sym : file->getSymbols()) {
239
16
      if (isa<SharedSymbol>(sym))
240
2
        map[sym].insert(file);
241
16
      if (auto *d = dyn_cast<Defined>(sym))
242
10
        if (!d->isLocal() && 
(9
!d->section9
||
d->section->isLive()7
))
243
8
          map[d].insert(file);
244
16
    }
245
4
  }
246
2
247
2
  // Print out a header.
248
2
  outs() << "Cross Reference Table\n\n";
249
2
  print("Symbol", "File");
250
2
251
2
  // Print out a table.
252
7
  for (auto kv : map) {
253
7
    Symbol *sym = kv.first;
254
7
    SetVector<InputFile *> &files = kv.second;
255
7
256
7
    print(toString(*sym), toString(sym->file));
257
7
    for (InputFile *file : files)
258
10
      if (file != sym->file)
259
4
        print("", toString(file));
260
7
  }
261
2
}