xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision 33d903d1)
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"
241ebfc59cSRafael Espindola #include "Strings.h"
25b882e591SRui Ueyama #include "SymbolTable.h"
26b882e591SRui Ueyama #include "Threads.h"
271ebfc59cSRafael Espindola 
28543731f9SRui Ueyama #include "llvm/Support/raw_ostream.h"
291ebfc59cSRafael Espindola 
301ebfc59cSRafael Espindola using namespace llvm;
311ebfc59cSRafael Espindola using namespace llvm::object;
321ebfc59cSRafael Espindola 
331ebfc59cSRafael Espindola using namespace lld;
341ebfc59cSRafael Espindola using namespace lld::elf;
351ebfc59cSRafael Espindola 
363151958cSRui Ueyama typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
373151958cSRui Ueyama     SymbolMapTy;
38b882e591SRui Ueyama 
39b882e591SRui Ueyama // Print out the first three columns of a line.
40b882e591SRui Ueyama template <class ELFT>
41b882e591SRui Ueyama static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
42b882e591SRui Ueyama                         uint64_t Align) {
43b882e591SRui Ueyama   int W = ELFT::Is64Bits ? 16 : 8;
44b882e591SRui Ueyama   OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
451ebfc59cSRafael Espindola }
461ebfc59cSRafael Espindola 
47b882e591SRui Ueyama static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
48b882e591SRui Ueyama 
493151958cSRui Ueyama // Returns a list of all symbols that we want to print out.
503151958cSRui Ueyama template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
513151958cSRui Ueyama   std::vector<DefinedRegular *> V;
52b882e591SRui Ueyama   for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
53b882e591SRui Ueyama     for (SymbolBody *B : File->getSymbols())
54b882e591SRui Ueyama       if (B->File == File && !B->isSection())
55b882e591SRui Ueyama         if (auto *Sym = dyn_cast<DefinedRegular>(B))
56b882e591SRui Ueyama           if (Sym->Section)
573151958cSRui Ueyama             V.push_back(Sym);
583151958cSRui Ueyama   return V;
593151958cSRui Ueyama }
60b882e591SRui Ueyama 
613151958cSRui Ueyama // Returns a map from sections to their symbols.
623151958cSRui Ueyama template <class ELFT>
633151958cSRui Ueyama SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
643151958cSRui Ueyama   SymbolMapTy Ret;
653151958cSRui Ueyama   for (DefinedRegular *S : Syms)
663151958cSRui Ueyama     Ret[S->Section].push_back(S);
67b882e591SRui Ueyama 
68b882e591SRui Ueyama   // Sort symbols by address. We want to print out symbols in the
69b882e591SRui Ueyama   // order in the output file rather than the order they appeared
70b882e591SRui Ueyama   // in the input files.
713151958cSRui Ueyama   for (auto &It : Ret) {
72b882e591SRui Ueyama     SmallVectorImpl<DefinedRegular *> &V = It.second;
73b882e591SRui Ueyama     std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
74b882e591SRui Ueyama       return A->getVA() < B->getVA();
75b882e591SRui Ueyama     });
761ebfc59cSRafael Espindola   }
773151958cSRui Ueyama   return Ret;
783151958cSRui Ueyama }
791ebfc59cSRafael Espindola 
80b882e591SRui Ueyama // Construct a map from symbols to their stringified representations.
813151958cSRui Ueyama // Demangling symbols (which is what toString() does) is slow, so
823151958cSRui Ueyama // we do that in batch using parallel-for.
833151958cSRui Ueyama template <class ELFT>
843151958cSRui Ueyama DenseMap<DefinedRegular *, std::string>
853151958cSRui Ueyama getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
86b882e591SRui Ueyama   std::vector<std::string> Str(Syms.size());
87*33d903d1SRui Ueyama   parallelForEachN(0, Syms.size(), [&](size_t I) {
88b882e591SRui Ueyama     raw_string_ostream OS(Str[I]);
89b882e591SRui Ueyama     writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
90b882e591SRui Ueyama                       0);
913151958cSRui Ueyama     OS << indent(2) << toString(*Syms[I]);
92b882e591SRui Ueyama   });
933151958cSRui Ueyama 
943151958cSRui Ueyama   DenseMap<DefinedRegular *, std::string> Ret;
95b882e591SRui Ueyama   for (size_t I = 0, E = Syms.size(); I < E; ++I)
963151958cSRui Ueyama     Ret[Syms[I]] = std::move(Str[I]);
973151958cSRui Ueyama   return Ret;
981ebfc59cSRafael Espindola }
991ebfc59cSRafael Espindola 
1001ebfc59cSRafael Espindola template <class ELFT>
10124e6f363SRafael Espindola void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
102543731f9SRui Ueyama   if (Config->MapFile.empty())
1031ebfc59cSRafael Espindola     return;
1041ebfc59cSRafael Espindola 
1053151958cSRui Ueyama   // Open a map file for writing.
106543731f9SRui Ueyama   std::error_code EC;
107543731f9SRui Ueyama   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
1083151958cSRui Ueyama   if (EC) {
109c9807c33SRui Ueyama     error("cannot open " + Config->MapFile + ": " + EC.message());
1103151958cSRui Ueyama     return;
1113151958cSRui Ueyama   }
1123151958cSRui Ueyama 
1133151958cSRui Ueyama   // Collect symbol info that we want to print out.
1143151958cSRui Ueyama   std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
1153151958cSRui Ueyama   SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
1163151958cSRui Ueyama   DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
1173151958cSRui Ueyama 
1183151958cSRui Ueyama   // Print out the header line.
1193151958cSRui Ueyama   int W = ELFT::Is64Bits ? 16 : 8;
1203151958cSRui Ueyama   OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
1213151958cSRui Ueyama      << " Align Out     In      Symbol\n";
1223151958cSRui Ueyama 
1233151958cSRui Ueyama   // Print out file contents.
1243151958cSRui Ueyama   for (OutputSection *OSec : OutputSections) {
1253151958cSRui Ueyama     writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
1263151958cSRui Ueyama     OS << OSec->Name << '\n';
1273151958cSRui Ueyama 
1283151958cSRui Ueyama     // Dump symbols for each input section.
1293151958cSRui Ueyama     for (InputSection *IS : OSec->Sections) {
1303151958cSRui Ueyama       writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
1313151958cSRui Ueyama                         IS->Alignment);
1323151958cSRui Ueyama       OS << indent(1) << toString(IS) << '\n';
1333151958cSRui Ueyama       for (DefinedRegular *Sym : SectionSyms[IS])
1343151958cSRui Ueyama         OS << SymStr[Sym] << '\n';
1353151958cSRui Ueyama     }
1363151958cSRui Ueyama   }
1371ebfc59cSRafael Espindola }
1381ebfc59cSRafael Espindola 
13924e6f363SRafael Espindola template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>);
14024e6f363SRafael Espindola template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>);
14124e6f363SRafael Espindola template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>);
14224e6f363SRafael Espindola template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>);
143