]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/MapFile.cpp
Merge ^/head r317971 through r318379.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / MapFile.cpp
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 "Strings.h"
25 #include "SymbolTable.h"
26 #include "Threads.h"
27
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31 using namespace llvm::object;
32
33 using namespace lld;
34 using namespace lld::elf;
35
36 typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
37     SymbolMapTy;
38
39 // Print out the first three columns of a line.
40 template <class ELFT>
41 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
42                         uint64_t Align) {
43   int W = ELFT::Is64Bits ? 16 : 8;
44   OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
45 }
46
47 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
48
49 // Returns a list of all symbols that we want to print out.
50 template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
51   std::vector<DefinedRegular *> V;
52   for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
53     for (SymbolBody *B : File->getSymbols())
54       if (B->File == File && !B->isSection())
55         if (auto *Sym = dyn_cast<DefinedRegular>(B))
56           if (Sym->Section)
57             V.push_back(Sym);
58   return V;
59 }
60
61 // Returns a map from sections to their symbols.
62 template <class ELFT>
63 SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
64   SymbolMapTy Ret;
65   for (DefinedRegular *S : Syms)
66     Ret[S->Section].push_back(S);
67
68   // Sort symbols by address. We want to print out symbols in the
69   // order in the output file rather than the order they appeared
70   // in the input files.
71   for (auto &It : Ret) {
72     SmallVectorImpl<DefinedRegular *> &V = It.second;
73     std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
74       return A->getVA() < B->getVA();
75     });
76   }
77   return Ret;
78 }
79
80 // Construct a map from symbols to their stringified representations.
81 // Demangling symbols (which is what toString() does) is slow, so
82 // we do that in batch using parallel-for.
83 template <class ELFT>
84 DenseMap<DefinedRegular *, std::string>
85 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
86   std::vector<std::string> Str(Syms.size());
87   parallelFor(0, Syms.size(), [&](size_t I) {
88     raw_string_ostream OS(Str[I]);
89     writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
90                       0);
91     OS << indent(2) << toString(*Syms[I]);
92   });
93
94   DenseMap<DefinedRegular *, std::string> Ret;
95   for (size_t I = 0, E = Syms.size(); I < E; ++I)
96     Ret[Syms[I]] = std::move(Str[I]);
97   return Ret;
98 }
99
100 template <class ELFT>
101 void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
102   if (Config->MapFile.empty())
103     return;
104
105   // Open a map file for writing.
106   std::error_code EC;
107   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
108   if (EC) {
109     error("cannot open " + Config->MapFile + ": " + EC.message());
110     return;
111   }
112
113   // Collect symbol info that we want to print out.
114   std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
115   SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
116   DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
117
118   // Print out the header line.
119   int W = ELFT::Is64Bits ? 16 : 8;
120   OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
121      << " Align Out     In      Symbol\n";
122
123   // Print out file contents.
124   for (OutputSection *OSec : OutputSections) {
125     writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
126     OS << OSec->Name << '\n';
127
128     // Dump symbols for each input section.
129     for (InputSection *IS : OSec->Sections) {
130       writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
131                         IS->Alignment);
132       OS << indent(1) << toString(IS) << '\n';
133       for (DefinedRegular *Sym : SectionSyms[IS])
134         OS << SymStr[Sym] << '\n';
135     }
136   }
137 }
138
139 template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>);
140 template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>);
141 template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>);
142 template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>);