xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision d29b12ef)
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 "Threads.h"
29 
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<DefinedRegular *, 4>>
39     SymbolMapTy;
40 
41 // Print out the first three columns of a line.
42 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
43                         uint64_t Align) {
44   int W = Config->Is64 ? 16 : 8;
45   OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
46 }
47 
48 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
49 
50 // Returns a list of all symbols that we want to print out.
51 template <class ELFT> static std::vector<DefinedRegular *> getSymbols() {
52   std::vector<DefinedRegular *> V;
53   for (ObjFile<ELFT> *File : ObjFile<ELFT>::Instances)
54     for (SymbolBody *B : File->getSymbols())
55       if (B->File == File && !B->isSection())
56         if (auto *Sym = dyn_cast<DefinedRegular>(B))
57           if (Sym->Section && Sym->Section->Live)
58             V.push_back(Sym);
59   return V;
60 }
61 
62 // Returns a map from sections to their symbols.
63 static 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 static DenseMap<DefinedRegular *, std::string>
85 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
86   std::vector<std::string> Str(Syms.size());
87   parallelForEachN(0, Syms.size(), [&](size_t I) {
88     raw_string_ostream OS(Str[I]);
89     writeHeader(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(), 0);
90     OS << indent(2) << toString(*Syms[I]);
91   });
92 
93   DenseMap<DefinedRegular *, std::string> Ret;
94   for (size_t I = 0, E = Syms.size(); I < E; ++I)
95     Ret[Syms[I]] = std::move(Str[I]);
96   return Ret;
97 }
98 
99 template <class ELFT> void elf::writeMapFile() {
100   if (Config->MapFile.empty())
101     return;
102 
103   // Open a map file for writing.
104   std::error_code EC;
105   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
106   if (EC) {
107     error("cannot open " + Config->MapFile + ": " + EC.message());
108     return;
109   }
110 
111   // Collect symbol info that we want to print out.
112   std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
113   SymbolMapTy SectionSyms = getSectionSyms(Syms);
114   DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
115 
116   // Print out the header line.
117   int W = ELFT::Is64Bits ? 16 : 8;
118   OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
119      << " Align Out     In      Symbol\n";
120 
121   // Print out file contents.
122   for (OutputSection *OSec : OutputSections) {
123     writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
124     OS << OSec->Name << '\n';
125 
126     // Dump symbols for each input section.
127     for (BaseCommand *Base : OSec->Commands) {
128       auto *ISD = dyn_cast<InputSectionDescription>(Base);
129       if (!ISD)
130         continue;
131       for (InputSection *IS : ISD->Sections) {
132         writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
133                     IS->Alignment);
134         OS << indent(1) << toString(IS) << '\n';
135         for (DefinedRegular *Sym : SectionSyms[IS])
136           OS << SymStr[Sym] << '\n';
137       }
138     }
139   }
140 }
141 
142 template void elf::writeMapFile<ELF32LE>();
143 template void elf::writeMapFile<ELF32BE>();
144 template void elf::writeMapFile<ELF64LE>();
145 template void elf::writeMapFile<ELF64BE>();
146