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"
29 #include "SyntheticSections.h"
30 #include "lld/Common/Threads.h"
31 #include "llvm/Support/raw_ostream.h"
34 using namespace llvm::object;
37 using namespace lld::elf;
39 typedef DenseMap<const SectionBase *, SmallVector<Symbol *, 4>> SymbolMapTy;
41 // Print out the first three columns of a line.
42 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
44 int W = Config->Is64 ? 16 : 8;
45 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
48 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
50 // Returns a list of all symbols that we want to print out.
51 static std::vector<Symbol *> getSymbols() {
52 std::vector<Symbol *> V;
53 for (InputFile *File : ObjectFiles) {
54 for (Symbol *B : File->getSymbols()) {
55 if (auto *SS = dyn_cast<SharedSymbol>(B))
56 if (SS->CopyRelSec || SS->NeedsPltAddr)
58 if (auto *DR = dyn_cast<Defined>(B))
59 if (DR->File == File && !DR->isSection() && DR->Section &&
67 // Returns a map from sections to their symbols.
68 static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> Syms) {
70 for (Symbol *S : Syms) {
71 if (auto *DR = dyn_cast<Defined>(S)) {
72 Ret[DR->Section].push_back(S);
76 SharedSymbol *SS = cast<SharedSymbol>(S);
78 Ret[SS->CopyRelSec].push_back(S);
80 Ret[InX::Plt].push_back(S);
83 // Sort symbols by address. We want to print out symbols in the
84 // order in the output file rather than the order they appeared
85 // in the input files.
86 for (auto &It : Ret) {
87 SmallVectorImpl<Symbol *> &V = It.second;
88 std::sort(V.begin(), V.end(),
89 [](Symbol *A, Symbol *B) { return A->getVA() < B->getVA(); });
94 // Construct a map from symbols to their stringified representations.
95 // Demangling symbols (which is what toString() does) is slow, so
96 // we do that in batch using parallel-for.
97 static DenseMap<Symbol *, std::string>
98 getSymbolStrings(ArrayRef<Symbol *> Syms) {
99 std::vector<std::string> Str(Syms.size());
100 parallelForEachN(0, Syms.size(), [&](size_t I) {
101 raw_string_ostream OS(Str[I]);
102 writeHeader(OS, Syms[I]->getVA(), Syms[I]->getSize(), 0);
103 OS << indent(2) << toString(*Syms[I]);
106 DenseMap<Symbol *, std::string> Ret;
107 for (size_t I = 0, E = Syms.size(); I < E; ++I)
108 Ret[Syms[I]] = std::move(Str[I]);
112 void elf::writeMapFile() {
113 if (Config->MapFile.empty())
116 // Open a map file for writing.
118 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
120 error("cannot open " + Config->MapFile + ": " + EC.message());
124 // Collect symbol info that we want to print out.
125 std::vector<Symbol *> Syms = getSymbols();
126 SymbolMapTy SectionSyms = getSectionSyms(Syms);
127 DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
129 // Print out the header line.
130 int W = Config->Is64 ? 16 : 8;
131 OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
132 << " Align Out In Symbol\n";
134 // Print out file contents.
135 for (OutputSection *OSec : OutputSections) {
136 writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
137 OS << OSec->Name << '\n';
139 // Dump symbols for each input section.
140 for (BaseCommand *Base : OSec->SectionCommands) {
141 auto *ISD = dyn_cast<InputSectionDescription>(Base);
144 for (InputSection *IS : ISD->Sections) {
145 writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
147 OS << indent(1) << toString(IS) << '\n';
148 for (Symbol *Sym : SectionSyms[IS])
149 OS << SymStr[Sym] << '\n';