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