]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - COFF/MapFile.cpp
Vendor import of lld trunk r338150:
[FreeBSD/FreeBSD.git] / COFF / 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 /lldmap 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     File    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 "SymbolTable.h"
24 #include "Symbols.h"
25 #include "Writer.h"
26 #include "lld/Common/ErrorHandler.h"
27 #include "llvm/Support/Parallel.h"
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::coff;
35
36 typedef DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>
37     SymbolMapTy;
38
39 static const std::string Indent8 = "        ";          // 8 spaces
40 static const std::string Indent16 = "                "; // 16 spaces
41
42 // Print out the first three columns of a line.
43 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
44                         uint64_t Align) {
45   OS << format("%08llx %08llx %5lld ", Addr, Size, Align);
46 }
47
48 // Returns a list of all symbols that we want to print out.
49 static std::vector<DefinedRegular *> getSymbols() {
50   std::vector<DefinedRegular *> V;
51   for (ObjFile *File : ObjFile::Instances)
52     for (Symbol *B : File->getSymbols())
53       if (auto *Sym = dyn_cast_or_null<DefinedRegular>(B))
54         if (Sym && !Sym->getCOFFSymbol().isSectionDefinition())
55           V.push_back(Sym);
56   return V;
57 }
58
59 // Returns a map from sections to their symbols.
60 static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
61   SymbolMapTy Ret;
62   for (DefinedRegular *S : Syms)
63     Ret[S->getChunk()].push_back(S);
64
65   // Sort symbols by address.
66   for (auto &It : Ret) {
67     SmallVectorImpl<DefinedRegular *> &V = It.second;
68     std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
69       return A->getRVA() < B->getRVA();
70     });
71   }
72   return Ret;
73 }
74
75 // Construct a map from symbols to their stringified representations.
76 static DenseMap<DefinedRegular *, std::string>
77 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
78   std::vector<std::string> Str(Syms.size());
79   for_each_n(parallel::par, (size_t)0, Syms.size(), [&](size_t I) {
80     raw_string_ostream OS(Str[I]);
81     writeHeader(OS, Syms[I]->getRVA(), 0, 0);
82     OS << Indent16 << toString(*Syms[I]);
83   });
84
85   DenseMap<DefinedRegular *, std::string> Ret;
86   for (size_t I = 0, E = Syms.size(); I < E; ++I)
87     Ret[Syms[I]] = std::move(Str[I]);
88   return Ret;
89 }
90
91 void coff::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
92   if (Config->MapFile.empty())
93     return;
94
95   std::error_code EC;
96   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
97   if (EC)
98     fatal("cannot open " + Config->MapFile + ": " + EC.message());
99
100   // Collect symbol info that we want to print out.
101   std::vector<DefinedRegular *> Syms = getSymbols();
102   SymbolMapTy SectionSyms = getSectionSyms(Syms);
103   DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings(Syms);
104
105   // Print out the header line.
106   OS << "Address  Size     Align Out     In      Symbol\n";
107
108   // Print out file contents.
109   for (OutputSection *Sec : OutputSections) {
110     writeHeader(OS, Sec->getRVA(), Sec->getVirtualSize(), /*Align=*/PageSize);
111     OS << Sec->Name << '\n';
112
113     for (Chunk *C : Sec->getChunks()) {
114       auto *SC = dyn_cast<SectionChunk>(C);
115       if (!SC)
116         continue;
117
118       writeHeader(OS, SC->getRVA(), SC->getSize(), SC->Alignment);
119       OS << Indent8 << SC->File->getName() << ":(" << SC->getSectionName()
120          << ")\n";
121       for (DefinedRegular *Sym : SectionSyms[SC])
122         OS << SymStr[Sym] << '\n';
123     }
124   }
125 }