146b69c69SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===//
246b69c69SDimitry Andric //
346b69c69SDimitry Andric //                             The LLVM Linker
446b69c69SDimitry Andric //
546b69c69SDimitry Andric // This file is distributed under the University of Illinois Open Source
646b69c69SDimitry Andric // License. See LICENSE.TXT for details.
746b69c69SDimitry Andric //
846b69c69SDimitry Andric //===----------------------------------------------------------------------===//
946b69c69SDimitry Andric //
1046b69c69SDimitry Andric // This file implements the -Map option. It shows lists in order and
1146b69c69SDimitry Andric // hierarchically the output sections, input sections, input files and
1246b69c69SDimitry Andric // symbol:
1346b69c69SDimitry Andric //
14f37b6182SDimitry Andric //   Address  Size     Align Out     In      Symbol
1546b69c69SDimitry Andric //   00201000 00000015     4 .text
16f37b6182SDimitry Andric //   00201000 0000000e     4         test.o:(.text)
1746b69c69SDimitry Andric //   0020100e 00000000     0                 local
1846b69c69SDimitry Andric //   00201005 00000000     0                 f(int)
1946b69c69SDimitry Andric //
2046b69c69SDimitry Andric //===----------------------------------------------------------------------===//
2146b69c69SDimitry Andric 
2246b69c69SDimitry Andric #include "MapFile.h"
2346b69c69SDimitry Andric #include "InputFiles.h"
24d8866befSDimitry Andric #include "LinkerScript.h"
25d8866befSDimitry Andric #include "OutputSections.h"
2646b69c69SDimitry Andric #include "Strings.h"
27f37b6182SDimitry Andric #include "SymbolTable.h"
28f37b6182SDimitry Andric #include "Threads.h"
2946b69c69SDimitry Andric 
3046b69c69SDimitry Andric #include "llvm/Support/raw_ostream.h"
3146b69c69SDimitry Andric 
3246b69c69SDimitry Andric using namespace llvm;
3346b69c69SDimitry Andric using namespace llvm::object;
3446b69c69SDimitry Andric 
3546b69c69SDimitry Andric using namespace lld;
3646b69c69SDimitry Andric using namespace lld::elf;
3746b69c69SDimitry Andric 
38f37b6182SDimitry Andric typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
39f37b6182SDimitry Andric     SymbolMapTy;
4046b69c69SDimitry Andric 
41f37b6182SDimitry Andric // Print out the first three columns of a line.
4246b69c69SDimitry Andric template <class ELFT>
43f37b6182SDimitry Andric static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
44f37b6182SDimitry Andric                         uint64_t Align) {
45f37b6182SDimitry Andric   int W = ELFT::Is64Bits ? 16 : 8;
46f37b6182SDimitry Andric   OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
4746b69c69SDimitry Andric }
4846b69c69SDimitry Andric 
49f37b6182SDimitry Andric static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
5046b69c69SDimitry Andric 
51f37b6182SDimitry Andric // Returns a list of all symbols that we want to print out.
52f37b6182SDimitry Andric template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
53f37b6182SDimitry Andric   std::vector<DefinedRegular *> V;
54f37b6182SDimitry Andric   for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
55f37b6182SDimitry Andric     for (SymbolBody *B : File->getSymbols())
56f37b6182SDimitry Andric       if (B->File == File && !B->isSection())
57f37b6182SDimitry Andric         if (auto *Sym = dyn_cast<DefinedRegular>(B))
58f37b6182SDimitry Andric           if (Sym->Section)
59f37b6182SDimitry Andric             V.push_back(Sym);
60f37b6182SDimitry Andric   return V;
6146b69c69SDimitry Andric }
6246b69c69SDimitry Andric 
63f37b6182SDimitry Andric // Returns a map from sections to their symbols.
6446b69c69SDimitry Andric template <class ELFT>
65f37b6182SDimitry Andric SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
66f37b6182SDimitry Andric   SymbolMapTy Ret;
67f37b6182SDimitry Andric   for (DefinedRegular *S : Syms)
68f37b6182SDimitry Andric     Ret[S->Section].push_back(S);
6946b69c69SDimitry Andric 
70f37b6182SDimitry Andric   // Sort symbols by address. We want to print out symbols in the
71f37b6182SDimitry Andric   // order in the output file rather than the order they appeared
72f37b6182SDimitry Andric   // in the input files.
73f37b6182SDimitry Andric   for (auto &It : Ret) {
74f37b6182SDimitry Andric     SmallVectorImpl<DefinedRegular *> &V = It.second;
75f37b6182SDimitry Andric     std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
76f37b6182SDimitry Andric       return A->getVA() < B->getVA();
77f37b6182SDimitry Andric     });
7846b69c69SDimitry Andric   }
79f37b6182SDimitry Andric   return Ret;
8046b69c69SDimitry Andric }
81f37b6182SDimitry Andric 
82f37b6182SDimitry Andric // Construct a map from symbols to their stringified representations.
83f37b6182SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so
84f37b6182SDimitry Andric // we do that in batch using parallel-for.
85f37b6182SDimitry Andric template <class ELFT>
86f37b6182SDimitry Andric DenseMap<DefinedRegular *, std::string>
87f37b6182SDimitry Andric getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
88f37b6182SDimitry Andric   std::vector<std::string> Str(Syms.size());
895517e702SDimitry Andric   parallelForEachN(0, Syms.size(), [&](size_t I) {
90f37b6182SDimitry Andric     raw_string_ostream OS(Str[I]);
91f37b6182SDimitry Andric     writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
92f37b6182SDimitry Andric                       0);
93f37b6182SDimitry Andric     OS << indent(2) << toString(*Syms[I]);
94f37b6182SDimitry Andric   });
95f37b6182SDimitry Andric 
96f37b6182SDimitry Andric   DenseMap<DefinedRegular *, std::string> Ret;
97f37b6182SDimitry Andric   for (size_t I = 0, E = Syms.size(); I < E; ++I)
98f37b6182SDimitry Andric     Ret[Syms[I]] = std::move(Str[I]);
99f37b6182SDimitry Andric   return Ret;
10046b69c69SDimitry Andric }
10146b69c69SDimitry Andric 
10246b69c69SDimitry Andric template <class ELFT>
103d8866befSDimitry Andric void elf::writeMapFile(llvm::ArrayRef<BaseCommand *> Script) {
10446b69c69SDimitry Andric   if (Config->MapFile.empty())
10546b69c69SDimitry Andric     return;
10646b69c69SDimitry Andric 
107f37b6182SDimitry Andric   // Open a map file for writing.
10846b69c69SDimitry Andric   std::error_code EC;
10946b69c69SDimitry Andric   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
110f37b6182SDimitry Andric   if (EC) {
11146b69c69SDimitry Andric     error("cannot open " + Config->MapFile + ": " + EC.message());
112f37b6182SDimitry Andric     return;
113f37b6182SDimitry Andric   }
114f37b6182SDimitry Andric 
115f37b6182SDimitry Andric   // Collect symbol info that we want to print out.
116f37b6182SDimitry Andric   std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
117f37b6182SDimitry Andric   SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
118f37b6182SDimitry Andric   DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
119f37b6182SDimitry Andric 
120f37b6182SDimitry Andric   // Print out the header line.
121f37b6182SDimitry Andric   int W = ELFT::Is64Bits ? 16 : 8;
122f37b6182SDimitry Andric   OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
123f37b6182SDimitry Andric      << " Align Out     In      Symbol\n";
124f37b6182SDimitry Andric 
125f37b6182SDimitry Andric   // Print out file contents.
126d8866befSDimitry Andric   for (BaseCommand *Base : Script) {
127d8866befSDimitry Andric     auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
128d8866befSDimitry Andric     if (!Cmd)
129d8866befSDimitry Andric       continue;
130d8866befSDimitry Andric     OutputSection *OSec = Cmd->Sec;
131f37b6182SDimitry Andric     writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
132f37b6182SDimitry Andric     OS << OSec->Name << '\n';
133f37b6182SDimitry Andric 
134f37b6182SDimitry Andric     // Dump symbols for each input section.
135f37b6182SDimitry Andric     for (InputSection *IS : OSec->Sections) {
136f37b6182SDimitry Andric       writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
137f37b6182SDimitry Andric                         IS->Alignment);
138f37b6182SDimitry Andric       OS << indent(1) << toString(IS) << '\n';
139f37b6182SDimitry Andric       for (DefinedRegular *Sym : SectionSyms[IS])
140f37b6182SDimitry Andric         OS << SymStr[Sym] << '\n';
141f37b6182SDimitry Andric     }
142f37b6182SDimitry Andric   }
14346b69c69SDimitry Andric }
14446b69c69SDimitry Andric 
145d8866befSDimitry Andric template void elf::writeMapFile<ELF32LE>(ArrayRef<BaseCommand *>);
146d8866befSDimitry Andric template void elf::writeMapFile<ELF32BE>(ArrayRef<BaseCommand *>);
147d8866befSDimitry Andric template void elf::writeMapFile<ELF64LE>(ArrayRef<BaseCommand *>);
148d8866befSDimitry Andric template void elf::writeMapFile<ELF64BE>(ArrayRef<BaseCommand *>);
149