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"
25 #include "SymbolTable.h"
28 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm::object;
34 using namespace lld::elf;
36 typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
39 // Print out the first three columns of a line.
41 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
43 int W = ELFT::Is64Bits ? 16 : 8;
44 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
47 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
49 // Returns a list of all symbols that we want to print out.
50 template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
51 std::vector<DefinedRegular *> V;
52 for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
53 for (SymbolBody *B : File->getSymbols())
54 if (B->File == File && !B->isSection())
55 if (auto *Sym = dyn_cast<DefinedRegular>(B))
61 // Returns a map from sections to their symbols.
63 SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
65 for (DefinedRegular *S : Syms)
66 Ret[S->Section].push_back(S);
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();
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.
84 DenseMap<DefinedRegular *, std::string>
85 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
86 std::vector<std::string> Str(Syms.size());
87 parallelFor(0, Syms.size(), [&](size_t I) {
88 raw_string_ostream OS(Str[I]);
89 writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
91 OS << indent(2) << toString(*Syms[I]);
94 DenseMap<DefinedRegular *, std::string> Ret;
95 for (size_t I = 0, E = Syms.size(); I < E; ++I)
96 Ret[Syms[I]] = std::move(Str[I]);
100 template <class ELFT>
101 void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
102 if (Config->MapFile.empty())
105 // Open a map file for writing.
107 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
109 error("cannot open " + Config->MapFile + ": " + EC.message());
113 // Collect symbol info that we want to print out.
114 std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
115 SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
116 DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
118 // Print out the header line.
119 int W = ELFT::Is64Bits ? 16 : 8;
120 OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
121 << " Align Out In Symbol\n";
123 // Print out file contents.
124 for (OutputSection *OSec : OutputSections) {
125 writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
126 OS << OSec->Name << '\n';
128 // Dump symbols for each input section.
129 for (InputSection *IS : OSec->Sections) {
130 writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
132 OS << indent(1) << toString(IS) << '\n';
133 for (DefinedRegular *Sym : SectionSyms[IS])
134 OS << SymStr[Sym] << '\n';
139 template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>);
140 template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>);
141 template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>);
142 template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>);