1 //===- MapFile.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the -Map option. It shows lists in order and
11 // hierarchically the output sections, input sections, input files and
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)
20 //===----------------------------------------------------------------------===//
23 #include "InputFiles.h"
24 #include "LinkerScript.h"
25 #include "OutputSections.h"
27 #include "SymbolTable.h"
30 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm::object;
36 using namespace lld::elf;
38 typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
41 // Print out the first three columns of a line.
43 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
45 int W = ELFT::Is64Bits ? 16 : 8;
46 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
49 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
51 // Returns a list of all symbols that we want to print out.
52 template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
53 std::vector<DefinedRegular *> V;
54 for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
55 for (SymbolBody *B : File->getSymbols())
56 if (B->File == File && !B->isSection())
57 if (auto *Sym = dyn_cast<DefinedRegular>(B))
58 if (Sym->Section && Sym->Section->Live)
63 // Returns a map from sections to their symbols.
65 SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
67 for (DefinedRegular *S : Syms)
68 Ret[S->Section].push_back(S);
70 // Sort symbols by address. We want to print out symbols in the
71 // order in the output file rather than the order they appeared
72 // in the input files.
73 for (auto &It : Ret) {
74 SmallVectorImpl<DefinedRegular *> &V = It.second;
75 std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
76 return A->getVA() < B->getVA();
82 // Construct a map from symbols to their stringified representations.
83 // Demangling symbols (which is what toString() does) is slow, so
84 // we do that in batch using parallel-for.
86 DenseMap<DefinedRegular *, std::string>
87 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
88 std::vector<std::string> Str(Syms.size());
89 parallelForEachN(0, Syms.size(), [&](size_t I) {
90 raw_string_ostream OS(Str[I]);
91 writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
93 OS << indent(2) << toString(*Syms[I]);
96 DenseMap<DefinedRegular *, std::string> Ret;
97 for (size_t I = 0, E = Syms.size(); I < E; ++I)
98 Ret[Syms[I]] = std::move(Str[I]);
102 template <class ELFT>
103 void elf::writeMapFile(llvm::ArrayRef<OutputSectionCommand *> Script) {
104 if (Config->MapFile.empty())
107 // Open a map file for writing.
109 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
111 error("cannot open " + Config->MapFile + ": " + EC.message());
115 // Collect symbol info that we want to print out.
116 std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
117 SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
118 DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
120 // Print out the header line.
121 int W = ELFT::Is64Bits ? 16 : 8;
122 OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
123 << " Align Out In Symbol\n";
125 // Print out file contents.
126 for (OutputSectionCommand *Cmd : Script) {
127 OutputSection *OSec = Cmd->Sec;
128 writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
129 OS << OSec->Name << '\n';
131 // Dump symbols for each input section.
132 for (BaseCommand *Base : Cmd->Commands) {
133 auto *ISD = dyn_cast<InputSectionDescription>(Base);
136 for (InputSection *IS : ISD->Sections) {
137 writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
139 OS << indent(1) << toString(IS) << '\n';
140 for (DefinedRegular *Sym : SectionSyms[IS])
141 OS << SymStr[Sym] << '\n';
147 template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSectionCommand *>);
148 template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSectionCommand *>);
149 template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSectionCommand *>);
150 template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSectionCommand *>);