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