xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision 8dc6e98d)
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->getFile() == 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     if (auto *DR = dyn_cast<Defined>(S))
66       Ret[DR->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<Defined *> &V = It.second;
73     std::sort(V.begin(), V.end(),
74               [](Defined *A, Defined *B) { return A->getVA() < B->getVA(); });
75   }
76   return Ret;
77 }
78 
79 // Construct a map from symbols to their stringified representations.
80 // Demangling symbols (which is what toString() does) is slow, so
81 // we do that in batch using parallel-for.
82 static DenseMap<Defined *, std::string>
83 getSymbolStrings(ArrayRef<Defined *> Syms) {
84   std::vector<std::string> Str(Syms.size());
85   parallelForEachN(0, Syms.size(), [&](size_t I) {
86     raw_string_ostream OS(Str[I]);
87     writeHeader(OS, Syms[I]->getVA(), Syms[I]->getSize(), 0);
88     OS << indent(2) << toString(*Syms[I]);
89   });
90 
91   DenseMap<Defined *, std::string> Ret;
92   for (size_t I = 0, E = Syms.size(); I < E; ++I)
93     Ret[Syms[I]] = std::move(Str[I]);
94   return Ret;
95 }
96 
97 void elf::writeMapFile() {
98   if (Config->MapFile.empty())
99     return;
100 
101   // Open a map file for writing.
102   std::error_code EC;
103   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
104   if (EC) {
105     error("cannot open " + Config->MapFile + ": " + EC.message());
106     return;
107   }
108 
109   // Collect symbol info that we want to print out.
110   std::vector<Defined *> Syms = getSymbols();
111   SymbolMapTy SectionSyms = getSectionSyms(Syms);
112   DenseMap<Defined *, std::string> SymStr = getSymbolStrings(Syms);
113 
114   // Print out the header line.
115   int W = Config->Is64 ? 16 : 8;
116   OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
117      << " Align Out     In      Symbol\n";
118 
119   // Print out file contents.
120   for (OutputSection *OSec : OutputSections) {
121     writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
122     OS << OSec->Name << '\n';
123 
124     // Dump symbols for each input section.
125     for (BaseCommand *Base : OSec->SectionCommands) {
126       auto *ISD = dyn_cast<InputSectionDescription>(Base);
127       if (!ISD)
128         continue;
129       for (InputSection *IS : ISD->Sections) {
130         writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
131                     IS->Alignment);
132         OS << indent(1) << toString(IS) << '\n';
133         for (Defined *Sym : SectionSyms[IS])
134           OS << SymStr[Sym] << '\n';
135       }
136     }
137   }
138 }
139