xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision e1847da9)
11ebfc59cSRafael Espindola //===- MapFile.cpp --------------------------------------------------------===//
21ebfc59cSRafael Espindola //
31ebfc59cSRafael Espindola //                             The LLVM Linker
41ebfc59cSRafael Espindola //
51ebfc59cSRafael Espindola // This file is distributed under the University of Illinois Open Source
61ebfc59cSRafael Espindola // License. See LICENSE.TXT for details.
71ebfc59cSRafael Espindola //
81ebfc59cSRafael Espindola //===----------------------------------------------------------------------===//
91ebfc59cSRafael Espindola //
101ebfc59cSRafael Espindola // This file implements the -Map option. It shows lists in order and
111ebfc59cSRafael Espindola // hierarchically the output sections, input sections, input files and
121ebfc59cSRafael Espindola // symbol:
131ebfc59cSRafael Espindola //
143012b371SRui Ueyama //   Address  Size     Align Out     In      Symbol
151ebfc59cSRafael Espindola //   00201000 00000015     4 .text
1637811379SRui Ueyama //   00201000 0000000e     4         test.o:(.text)
171ebfc59cSRafael Espindola //   0020100e 00000000     0                 local
181ebfc59cSRafael Espindola //   00201005 00000000     0                 f(int)
191ebfc59cSRafael Espindola //
201ebfc59cSRafael Espindola //===----------------------------------------------------------------------===//
211ebfc59cSRafael Espindola 
221ebfc59cSRafael Espindola #include "MapFile.h"
231ebfc59cSRafael Espindola #include "InputFiles.h"
24a46f688eSRafael Espindola #include "LinkerScript.h"
251b414090SRafael Espindola #include "OutputSections.h"
26b882e591SRui Ueyama #include "SymbolTable.h"
27d26b52fdSRafael Espindola #include "Symbols.h"
28f2fe963dSGeorge Rimar #include "SyntheticSections.h"
29ee173718SRui Ueyama #include "lld/Common/Strings.h"
304f5c8c29SBob Haarman #include "lld/Common/Threads.h"
31543731f9SRui Ueyama #include "llvm/Support/raw_ostream.h"
321ebfc59cSRafael Espindola 
331ebfc59cSRafael Espindola using namespace llvm;
341ebfc59cSRafael Espindola using namespace llvm::object;
351ebfc59cSRafael Espindola 
361ebfc59cSRafael Espindola using namespace lld;
371ebfc59cSRafael Espindola using namespace lld::elf;
381ebfc59cSRafael Espindola 
3931cd5da5SGeorge Rimar typedef DenseMap<const SectionBase *, SmallVector<Symbol *, 4>> SymbolMapTy;
40b882e591SRui Ueyama 
41*e1847da9SRui Ueyama static const std::string Indent8 = "        ";          // 8 spaces
42*e1847da9SRui Ueyama static const std::string Indent16 = "                "; // 16 spaces
439e0171fbSRui Ueyama 
44b882e591SRui Ueyama // Print out the first three columns of a line.
45b882e591SRui Ueyama static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
46b882e591SRui Ueyama                         uint64_t Align) {
477b30dd1cSGeorge Rimar   int W = Config->Is64 ? 16 : 8;
48b882e591SRui Ueyama   OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
491ebfc59cSRafael Espindola }
501ebfc59cSRafael Espindola 
513151958cSRui Ueyama // Returns a list of all symbols that we want to print out.
5231cd5da5SGeorge Rimar static std::vector<Symbol *> getSymbols() {
5331cd5da5SGeorge Rimar   std::vector<Symbol *> V;
5431cd5da5SGeorge Rimar   for (InputFile *File : ObjectFiles) {
5531cd5da5SGeorge Rimar     for (Symbol *B : File->getSymbols()) {
5631cd5da5SGeorge Rimar       if (auto *SS = dyn_cast<SharedSymbol>(B))
57ba7afd70SGeorge Rimar         if (SS->CopyRelSec || SS->NeedsPltAddr)
5831cd5da5SGeorge Rimar           V.push_back(SS);
59e9a9e0a1SPeter Collingbourne       if (auto *DR = dyn_cast<Defined>(B))
60dfebd360SRafael Espindola         if (DR->File == File && !DR->isSection() && DR->Section &&
61f456f572SRafael Espindola             DR->Section->Live)
62f456f572SRafael Espindola           V.push_back(DR);
6331cd5da5SGeorge Rimar     }
6431cd5da5SGeorge Rimar   }
653151958cSRui Ueyama   return V;
663151958cSRui Ueyama }
67b882e591SRui Ueyama 
683151958cSRui Ueyama // Returns a map from sections to their symbols.
6931cd5da5SGeorge Rimar static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> Syms) {
703151958cSRui Ueyama   SymbolMapTy Ret;
7131cd5da5SGeorge Rimar   for (Symbol *S : Syms) {
72ba7afd70SGeorge Rimar     if (auto *DR = dyn_cast<Defined>(S)) {
7331cd5da5SGeorge Rimar       Ret[DR->Section].push_back(S);
74ba7afd70SGeorge Rimar       continue;
75ba7afd70SGeorge Rimar     }
76ba7afd70SGeorge Rimar 
77ba7afd70SGeorge Rimar     SharedSymbol *SS = cast<SharedSymbol>(S);
78ba7afd70SGeorge Rimar     if (SS->CopyRelSec)
79ba7afd70SGeorge Rimar       Ret[SS->CopyRelSec].push_back(S);
8031cd5da5SGeorge Rimar     else
81ba7afd70SGeorge Rimar       Ret[InX::Plt].push_back(S);
8231cd5da5SGeorge Rimar   }
83b882e591SRui Ueyama 
84b882e591SRui Ueyama   // Sort symbols by address. We want to print out symbols in the
85b882e591SRui Ueyama   // order in the output file rather than the order they appeared
86b882e591SRui Ueyama   // in the input files.
873151958cSRui Ueyama   for (auto &It : Ret) {
8831cd5da5SGeorge Rimar     SmallVectorImpl<Symbol *> &V = It.second;
89f2fe963dSGeorge Rimar     std::sort(V.begin(), V.end(),
9031cd5da5SGeorge Rimar               [](Symbol *A, Symbol *B) { return A->getVA() < B->getVA(); });
911ebfc59cSRafael Espindola   }
923151958cSRui Ueyama   return Ret;
933151958cSRui Ueyama }
941ebfc59cSRafael Espindola 
95b882e591SRui Ueyama // Construct a map from symbols to their stringified representations.
963151958cSRui Ueyama // Demangling symbols (which is what toString() does) is slow, so
973151958cSRui Ueyama // we do that in batch using parallel-for.
9831cd5da5SGeorge Rimar static DenseMap<Symbol *, std::string>
9931cd5da5SGeorge Rimar getSymbolStrings(ArrayRef<Symbol *> Syms) {
100b882e591SRui Ueyama   std::vector<std::string> Str(Syms.size());
10133d903d1SRui Ueyama   parallelForEachN(0, Syms.size(), [&](size_t I) {
102b882e591SRui Ueyama     raw_string_ostream OS(Str[I]);
1037f9694a4SRui Ueyama     writeHeader(OS, Syms[I]->getVA(), Syms[I]->getSize(), 0);
104*e1847da9SRui Ueyama     OS << Indent16 << toString(*Syms[I]);
105b882e591SRui Ueyama   });
1063151958cSRui Ueyama 
10731cd5da5SGeorge Rimar   DenseMap<Symbol *, std::string> Ret;
108b882e591SRui Ueyama   for (size_t I = 0, E = Syms.size(); I < E; ++I)
1093151958cSRui Ueyama     Ret[Syms[I]] = std::move(Str[I]);
1103151958cSRui Ueyama   return Ret;
1111ebfc59cSRafael Espindola }
1121ebfc59cSRafael Espindola 
1137f9694a4SRui Ueyama void elf::writeMapFile() {
114543731f9SRui Ueyama   if (Config->MapFile.empty())
1151ebfc59cSRafael Espindola     return;
1161ebfc59cSRafael Espindola 
1173151958cSRui Ueyama   // Open a map file for writing.
118543731f9SRui Ueyama   std::error_code EC;
119543731f9SRui Ueyama   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
1203151958cSRui Ueyama   if (EC) {
121c9807c33SRui Ueyama     error("cannot open " + Config->MapFile + ": " + EC.message());
1223151958cSRui Ueyama     return;
1233151958cSRui Ueyama   }
1243151958cSRui Ueyama 
1253151958cSRui Ueyama   // Collect symbol info that we want to print out.
12631cd5da5SGeorge Rimar   std::vector<Symbol *> Syms = getSymbols();
1277b30dd1cSGeorge Rimar   SymbolMapTy SectionSyms = getSectionSyms(Syms);
12831cd5da5SGeorge Rimar   DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
1293151958cSRui Ueyama 
1303151958cSRui Ueyama   // Print out the header line.
1317f9694a4SRui Ueyama   int W = Config->Is64 ? 16 : 8;
1323151958cSRui Ueyama   OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
1333151958cSRui Ueyama      << " Align Out     In      Symbol\n";
1343151958cSRui Ueyama 
1353151958cSRui Ueyama   // Print out file contents.
1368c022ca7SRafael Espindola   for (OutputSection *OSec : OutputSections) {
1377b30dd1cSGeorge Rimar     writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
1383151958cSRui Ueyama     OS << OSec->Name << '\n';
1393151958cSRui Ueyama 
1403151958cSRui Ueyama     // Dump symbols for each input section.
141563e4f2fSGeorge Rimar     for (InputSection *IS : getInputSections(OSec)) {
142563e4f2fSGeorge Rimar       writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), IS->Alignment);
143*e1847da9SRui Ueyama       OS << Indent8 << toString(IS) << '\n';
14431cd5da5SGeorge Rimar       for (Symbol *Sym : SectionSyms[IS])
1453151958cSRui Ueyama         OS << SymStr[Sym] << '\n';
1463151958cSRui Ueyama     }
1473151958cSRui Ueyama   }
1481ebfc59cSRafael Espindola }
149