1 //===- MapFile.cpp --------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the -Map option. It shows lists in order and 11 // hierarchically the output sections, input sections, input files and 12 // symbol: 13 // 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) 19 // 20 //===----------------------------------------------------------------------===// 21 22 #include "MapFile.h" 23 #include "InputFiles.h" 24 #include "LinkerScript.h" 25 #include "OutputSections.h" 26 #include "Strings.h" 27 #include "SymbolTable.h" 28 #include "SyntheticSections.h" 29 #include "lld/Common/Threads.h" 30 #include "llvm/Support/raw_ostream.h" 31 32 using namespace llvm; 33 using namespace llvm::object; 34 35 using namespace lld; 36 using namespace lld::elf; 37 38 typedef DenseMap<const SectionBase *, SmallVector<Symbol *, 4>> SymbolMapTy; 39 40 // Print out the first three columns of a line. 41 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, 42 uint64_t Align) { 43 int W = Config->Is64 ? 16 : 8; 44 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align); 45 } 46 47 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } 48 49 // Returns a list of all symbols that we want to print out. 50 static std::vector<Symbol *> getSymbols() { 51 std::vector<Symbol *> V; 52 for (InputFile *File : ObjectFiles) { 53 for (Symbol *B : File->getSymbols()) { 54 if (auto *SS = dyn_cast<SharedSymbol>(B)) 55 if (SS->CopyRelSec || SS->NeedsPltAddr) 56 V.push_back(SS); 57 if (auto *DR = dyn_cast<Defined>(B)) 58 if (DR->File == File && !DR->isSection() && DR->Section && 59 DR->Section->Live) 60 V.push_back(DR); 61 } 62 } 63 return V; 64 } 65 66 // Returns a map from sections to their symbols. 67 static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> Syms) { 68 SymbolMapTy Ret; 69 for (Symbol *S : Syms) { 70 if (auto *DR = dyn_cast<Defined>(S)) { 71 Ret[DR->Section].push_back(S); 72 continue; 73 } 74 75 SharedSymbol *SS = cast<SharedSymbol>(S); 76 if (SS->CopyRelSec) 77 Ret[SS->CopyRelSec].push_back(S); 78 else 79 Ret[InX::Plt].push_back(S); 80 } 81 82 // Sort symbols by address. We want to print out symbols in the 83 // order in the output file rather than the order they appeared 84 // in the input files. 85 for (auto &It : Ret) { 86 SmallVectorImpl<Symbol *> &V = It.second; 87 std::sort(V.begin(), V.end(), 88 [](Symbol *A, Symbol *B) { return A->getVA() < B->getVA(); }); 89 } 90 return Ret; 91 } 92 93 // Construct a map from symbols to their stringified representations. 94 // Demangling symbols (which is what toString() does) is slow, so 95 // we do that in batch using parallel-for. 96 static DenseMap<Symbol *, std::string> 97 getSymbolStrings(ArrayRef<Symbol *> Syms) { 98 std::vector<std::string> Str(Syms.size()); 99 parallelForEachN(0, Syms.size(), [&](size_t I) { 100 raw_string_ostream OS(Str[I]); 101 writeHeader(OS, Syms[I]->getVA(), Syms[I]->getSize(), 0); 102 OS << indent(2) << toString(*Syms[I]); 103 }); 104 105 DenseMap<Symbol *, std::string> Ret; 106 for (size_t I = 0, E = Syms.size(); I < E; ++I) 107 Ret[Syms[I]] = std::move(Str[I]); 108 return Ret; 109 } 110 111 void elf::writeMapFile() { 112 if (Config->MapFile.empty()) 113 return; 114 115 // Open a map file for writing. 116 std::error_code EC; 117 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 118 if (EC) { 119 error("cannot open " + Config->MapFile + ": " + EC.message()); 120 return; 121 } 122 123 // Collect symbol info that we want to print out. 124 std::vector<Symbol *> Syms = getSymbols(); 125 SymbolMapTy SectionSyms = getSectionSyms(Syms); 126 DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms); 127 128 // Print out the header line. 129 int W = Config->Is64 ? 16 : 8; 130 OS << left_justify("Address", W) << ' ' << left_justify("Size", W) 131 << " Align Out In Symbol\n"; 132 133 // Print out file contents. 134 for (OutputSection *OSec : OutputSections) { 135 writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment); 136 OS << OSec->Name << '\n'; 137 138 // Dump symbols for each input section. 139 for (BaseCommand *Base : OSec->SectionCommands) { 140 auto *ISD = dyn_cast<InputSectionDescription>(Base); 141 if (!ISD) 142 continue; 143 for (InputSection *IS : ISD->Sections) { 144 writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), 145 IS->Alignment); 146 OS << indent(1) << toString(IS) << '\n'; 147 for (Symbol *Sym : SectionSyms[IS]) 148 OS << SymStr[Sym] << '\n'; 149 } 150 } 151 } 152 } 153