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 /lldmap 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 File 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 "SymbolTable.h" 24 #include "Symbols.h" 25 #include "Writer.h" 26 #include "lld/Common/ErrorHandler.h" 27 #include "llvm/Support/Parallel.h" 28 #include "llvm/Support/raw_ostream.h" 29 30 using namespace llvm; 31 using namespace llvm::object; 32 33 using namespace lld; 34 using namespace lld::coff; 35 36 typedef DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>> 37 SymbolMapTy; 38 39 // Print out the first three columns of a line. 40 static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, 41 uint64_t Align) { 42 OS << format("%08llx %08llx %5lld ", Addr, Size, Align); 43 } 44 45 static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } 46 47 // Returns a list of all symbols that we want to print out. 48 static std::vector<DefinedRegular *> getSymbols() { 49 std::vector<DefinedRegular *> V; 50 for (ObjFile *File : ObjFile::Instances) 51 for (Symbol *B : File->getSymbols()) 52 if (auto *Sym = dyn_cast_or_null<DefinedRegular>(B)) 53 if (Sym && !Sym->getCOFFSymbol().isSectionDefinition()) 54 V.push_back(Sym); 55 return V; 56 } 57 58 // Returns a map from sections to their symbols. 59 static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) { 60 SymbolMapTy Ret; 61 for (DefinedRegular *S : Syms) 62 Ret[S->getChunk()].push_back(S); 63 64 // Sort symbols by address. 65 for (auto &It : Ret) { 66 SmallVectorImpl<DefinedRegular *> &V = It.second; 67 std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) { 68 return A->getRVA() < B->getRVA(); 69 }); 70 } 71 return Ret; 72 } 73 74 // Construct a map from symbols to their stringified representations. 75 static DenseMap<DefinedRegular *, std::string> 76 getSymbolStrings(ArrayRef<DefinedRegular *> Syms) { 77 std::vector<std::string> Str(Syms.size()); 78 for_each_n(parallel::par, (size_t)0, Syms.size(), [&](size_t I) { 79 raw_string_ostream OS(Str[I]); 80 writeHeader(OS, Syms[I]->getRVA(), 0, 0); 81 OS << indent(2) << toString(*Syms[I]); 82 }); 83 84 DenseMap<DefinedRegular *, std::string> Ret; 85 for (size_t I = 0, E = Syms.size(); I < E; ++I) 86 Ret[Syms[I]] = std::move(Str[I]); 87 return Ret; 88 } 89 90 void coff::writeMapFile(ArrayRef<OutputSection *> OutputSections) { 91 if (Config->MapFile.empty()) 92 return; 93 94 std::error_code EC; 95 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 96 if (EC) 97 fatal("cannot open " + Config->MapFile + ": " + EC.message()); 98 99 // Collect symbol info that we want to print out. 100 std::vector<DefinedRegular *> Syms = getSymbols(); 101 SymbolMapTy SectionSyms = getSectionSyms(Syms); 102 DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings(Syms); 103 104 // Print out the header line. 105 OS << "Address Size Align Out In Symbol\n"; 106 107 // Print out file contents. 108 for (OutputSection *Sec : OutputSections) { 109 writeHeader(OS, Sec->getRVA(), Sec->getVirtualSize(), /*Align=*/PageSize); 110 OS << Sec->getName() << '\n'; 111 112 for (Chunk *C : Sec->getChunks()) { 113 auto *SC = dyn_cast<SectionChunk>(C); 114 if (!SC) 115 continue; 116 117 writeHeader(OS, SC->getRVA(), SC->getSize(), SC->Alignment); 118 OS << indent(1) << SC->File->getName() << ":(" << SC->getSectionName() 119 << ")\n"; 120 for (DefinedRegular *Sym : SectionSyms[SC]) 121 OS << SymStr[Sym] << '\n'; 122 } 123 } 124 } 125