Coverage Report

Created: 2018-06-18 20:01

/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
//                             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 -Map 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     In      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 "InputFiles.h"
24
#include "LinkerScript.h"
25
#include "OutputSections.h"
26
#include "SymbolTable.h"
27
#include "Symbols.h"
28
#include "SyntheticSections.h"
29
#include "lld/Common/Strings.h"
30
#include "lld/Common/Threads.h"
31
#include "llvm/ADT/MapVector.h"
32
#include "llvm/ADT/SetVector.h"
33
#include "llvm/Support/raw_ostream.h"
34
35
using namespace llvm;
36
using namespace llvm::object;
37
38
using namespace lld;
39
using namespace lld::elf;
40
41
typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy;
42
43
static const std::string Indent8 = "        ";          // 8 spaces
44
static const std::string Indent16 = "                "; // 16 spaces
45
46
// Print out the first three columns of a line.
47
static void writeHeader(raw_ostream &OS, uint64_t VMA, uint64_t LMA,
48
298
                        uint64_t Size, uint64_t Align) {
49
298
  if (Config->Is64)
50
298
    OS << format("%16llx %16llx %8llx %5lld ", VMA, LMA, Size, Align);
51
0
  else
52
0
    OS << format("%8llx %8llx %8llx %5lld ", VMA, LMA, Size, Align);
53
298
}
54
55
// Returns a list of all symbols that we want to print out.
56
static std::vector<Defined *> getSymbols() {
57
  std::vector<Defined *> V;
58
  for (InputFile *File : ObjectFiles)
59
    for (Symbol *B : File->getSymbols())
60
      if (auto *DR = dyn_cast<Defined>(B))
61
        if (!DR->isSection() && DR->Section && DR->Section->Live &&
62
            (DR->File == File || DR->NeedsPltAddr || DR->Section->Bss))
63
          V.push_back(DR);
64
  return V;
65
}
66
67
// Returns a map from sections to their symbols.
68
11
static SymbolMapTy getSectionSyms(ArrayRef<Defined *> Syms) {
69
11
  SymbolMapTy Ret;
70
11
  for (Defined *DR : Syms)
71
49
    Ret[DR->Section].push_back(DR);
72
11
73
11
  // Sort symbols by address. We want to print out symbols in the
74
11
  // order in the output file rather than the order they appeared
75
11
  // in the input files.
76
36
  for (auto &It : Ret) {
77
36
    SmallVectorImpl<Defined *> &V = It.second;
78
36
    std::stable_sort(V.begin(), V.end(), [](Defined *A, Defined *B) {
79
16
      return A->getVA() < B->getVA();
80
16
    });
81
36
  }
82
11
  return Ret;
83
11
}
84
85
// Construct a map from symbols to their stringified representations.
86
// Demangling symbols (which is what toString() does) is slow, so
87
// we do that in batch using parallel-for.
88
static DenseMap<Symbol *, std::string>
89
11
getSymbolStrings(ArrayRef<Defined *> Syms) {
90
11
  std::vector<std::string> Str(Syms.size());
91
49
  parallelForEachN(0, Syms.size(), [&](size_t I) {
92
49
    raw_string_ostream OS(Str[I]);
93
49
    OutputSection *OSec = Syms[I]->getOutputSection();
94
49
    uint64_t VMA = Syms[I]->getVA();
95
49
    uint64_t LMA = OSec ? OSec->getLMA() + VMA - OSec->getVA(0) : 
00
;
96
49
    writeHeader(OS, VMA, LMA, Syms[I]->getSize(), 1);
97
49
    OS << Indent16 << toString(*Syms[I]);
98
49
  });
99
11
100
11
  DenseMap<Symbol *, std::string> Ret;
101
60
  for (size_t I = 0, E = Syms.size(); I < E; 
++I49
)
102
49
    Ret[Syms[I]] = std::move(Str[I]);
103
11
  return Ret;
104
11
}
105
106
// Print .eh_frame contents. Since the section consists of EhSectionPieces,
107
// we need a specialized printer for that section.
108
//
109
// .eh_frame tend to contain a lot of section pieces that are contiguous
110
// both in input file and output file. Such pieces are squashed before
111
// being displayed to make output compact.
112
4
static void printEhFrame(raw_ostream &OS, OutputSection *OSec) {
113
4
  std::vector<EhSectionPiece> Pieces;
114
4
115
14
  auto Add = [&](const EhSectionPiece &P) {
116
14
    // If P is adjacent to Last, squash the two.
117
14
    if (!Pieces.empty()) {
118
10
      EhSectionPiece &Last = Pieces.back();
119
10
      if (Last.Sec == P.Sec && 
Last.InputOff + Last.Size == P.InputOff7
&&
120
10
          
Last.OutputOff + Last.Size == P.OutputOff7
) {
121
4
        Last.Size += P.Size;
122
4
        return;
123
4
      }
124
10
    }
125
10
    Pieces.push_back(P);
126
10
  };
127
4
128
4
  // Gather section pieces.
129
4
  for (const CieRecord *Rec : InX::EhFrame->getCieRecords()) {
130
4
    Add(*Rec->Cie);
131
4
    for (const EhSectionPiece *Fde : Rec->Fdes)
132
10
      Add(*Fde);
133
4
  }
134
4
135
4
  // Print out section pieces.
136
10
  for (EhSectionPiece &P : Pieces) {
137
10
    writeHeader(OS, OSec->Addr + P.OutputOff, OSec->getLMA() + P.OutputOff,
138
10
                P.Size, 1);
139
10
    OS << Indent8 << toString(P.Sec->File) << ":(" << P.Sec->Name << "+0x"
140
10
       << Twine::utohexstr(P.InputOff) + ")\n";
141
10
  }
142
4
}
143
144
1.96k
void elf::writeMapFile() {
145
1.96k
  if (Config->MapFile.empty())
146
1.95k
    return;
147
11
148
11
  // Open a map file for writing.
149
11
  std::error_code EC;
150
11
  raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
151
11
  if (EC) {
152
0
    error("cannot open " + Config->MapFile + ": " + EC.message());
153
0
    return;
154
0
  }
155
11
156
11
  // Collect symbol info that we want to print out.
157
11
  std::vector<Defined *> Syms = getSymbols();
158
11
  SymbolMapTy SectionSyms = getSectionSyms(Syms);
159
11
  DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
160
11
161
11
  // Print out the header line.
162
11
  int W = Config->Is64 ? 16 : 
80
;
163
11
  OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W)
164
11
     << "     Size Align Out     In      Symbol\n";
165
11
166
110
  for (BaseCommand *Base : Script->SectionCommands) {
167
110
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
168
7
      if (Cmd->Provide && 
!Cmd->Sym1
)
169
1
        continue;
170
6
      //FIXME: calculate and print LMA.
171
6
      writeHeader(OS, Cmd->Addr, 0, Cmd->Size, 1);
172
6
      OS << Cmd->CommandString << '\n';
173
6
      continue;
174
6
    }
175
103
176
103
    auto *OSec = dyn_cast<OutputSection>(Base);
177
103
    if (!OSec)
178
0
      continue;
179
103
180
103
    writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment);
181
103
    OS << OSec->Name << '\n';
182
103
183
103
    // Dump symbols for each input section.
184
116
    for (BaseCommand *Base : OSec->SectionCommands) {
185
116
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
186
122
        for (InputSection *IS : ISD->Sections) {
187
122
          if (IS == InX::EhFrame) {
188
4
            printEhFrame(OS, OSec);
189
4
            continue;
190
4
          }
191
118
192
118
          writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0),
193
118
                      IS->getSize(), IS->Alignment);
194
118
          OS << Indent8 << toString(IS) << '\n';
195
118
          for (Symbol *Sym : SectionSyms[IS])
196
49
            OS << SymStr[Sym] << '\n';
197
118
        }
198
103
        continue;
199
103
      }
200
13
201
13
      if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
202
5
        writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset,
203
5
                    Cmd->Size, 1);
204
5
        OS << Indent8 << Cmd->CommandString << '\n';
205
5
        continue;
206
5
      }
207
8
208
8
      if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
209
8
        if (Cmd->Provide && 
!Cmd->Sym1
)
210
1
          continue;
211
7
        writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0),
212
7
                    Cmd->Size, 1);
213
7
        OS << Indent8 << Cmd->CommandString << '\n';
214
7
        continue;
215
7
      }
216
8
    }
217
103
  }
218
11
}
219
220
13
static void print(StringRef A, StringRef B) {
221
13
  outs() << left_justify(A, 49) << " " << B << "\n";
222
13
}
223
224
// Output a cross reference table to stdout. This is for --cref.
225
//
226
// For each global symbol, we print out a file that defines the symbol
227
// followed by files that uses that symbol. Here is an example.
228
//
229
//     strlen     /lib/x86_64-linux-gnu/libc.so.6
230
//                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
231
//                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
232
//
233
// In this case, strlen is defined by libc.so.6 and used by other two
234
// files.
235
1.96k
void elf::writeCrossReferenceTable() {
236
1.96k
  if (!Config->Cref)
237
1.96k
    return;
238
2
239
2
  // Collect symbols and files.
240
2
  MapVector<Symbol *, SetVector<InputFile *>> Map;
241
4
  for (InputFile *File : ObjectFiles) {
242
16
    for (Symbol *Sym : File->getSymbols()) {
243
16
      if (isa<SharedSymbol>(Sym))
244
2
        Map[Sym].insert(File);
245
16
      if (auto *D = dyn_cast<Defined>(Sym))
246
10
        if (!D->isLocal() && 
(9
!D->Section9
||
D->Section->Live7
))
247
8
          Map[D].insert(File);
248
16
    }
249
4
  }
250
2
251
2
  // Print out a header.
252
2
  outs() << "Cross Reference Table\n\n";
253
2
  print("Symbol", "File");
254
2
255
2
  // Print out a table.
256
7
  for (auto KV : Map) {
257
7
    Symbol *Sym = KV.first;
258
7
    SetVector<InputFile *> &Files = KV.second;
259
7
260
7
    print(toString(*Sym), toString(Sym->File));
261
7
    for (InputFile *File : Files)
262
10
      if (File != Sym->File)
263
4
        print("", toString(File));
264
7
  }
265
2
}