11ebfc59cSRafael Espindola //===- MapFile.cpp --------------------------------------------------------===// 21ebfc59cSRafael Espindola // 31ebfc59cSRafael Espindola // The LLVM Linker 41ebfc59cSRafael Espindola // 51ebfc59cSRafael Espindola // This file is distributed under the University of Illinois Open Source 61ebfc59cSRafael Espindola // License. See LICENSE.TXT for details. 71ebfc59cSRafael Espindola // 81ebfc59cSRafael Espindola //===----------------------------------------------------------------------===// 91ebfc59cSRafael Espindola // 101ebfc59cSRafael Espindola // This file implements the -Map option. It shows lists in order and 111ebfc59cSRafael Espindola // hierarchically the output sections, input sections, input files and 121ebfc59cSRafael Espindola // symbol: 131ebfc59cSRafael Espindola // 143012b371SRui Ueyama // Address Size Align Out In Symbol 151ebfc59cSRafael Espindola // 00201000 00000015 4 .text 1637811379SRui Ueyama // 00201000 0000000e 4 test.o:(.text) 171ebfc59cSRafael Espindola // 0020100e 00000000 0 local 181ebfc59cSRafael Espindola // 00201005 00000000 0 f(int) 191ebfc59cSRafael Espindola // 201ebfc59cSRafael Espindola //===----------------------------------------------------------------------===// 211ebfc59cSRafael Espindola 221ebfc59cSRafael Espindola #include "MapFile.h" 231ebfc59cSRafael Espindola #include "InputFiles.h" 24a46f688eSRafael Espindola #include "LinkerScript.h" 251b414090SRafael Espindola #include "OutputSections.h" 261ebfc59cSRafael Espindola #include "Strings.h" 27b882e591SRui Ueyama #include "SymbolTable.h" 28f2fe963dSGeorge Rimar #include "SyntheticSections.h" 294f5c8c29SBob Haarman #include "lld/Common/Threads.h" 30543731f9SRui Ueyama #include "llvm/Support/raw_ostream.h" 311ebfc59cSRafael Espindola 321ebfc59cSRafael Espindola using namespace llvm; 331ebfc59cSRafael Espindola using namespace llvm::object; 341ebfc59cSRafael Espindola 351ebfc59cSRafael Espindola using namespace lld; 361ebfc59cSRafael Espindola using namespace lld::elf; 371ebfc59cSRafael Espindola 3831cd5da5SGeorge Rimar typedef DenseMap<const SectionBase *, SmallVector<Symbol *, 4>> SymbolMapTy; 39b882e591SRui Ueyama 40b882e591SRui Ueyama // Print out the first three columns of a line. 41b882e591SRui Ueyama static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, 42b882e591SRui Ueyama uint64_t Align) { 437b30dd1cSGeorge Rimar int W = Config->Is64 ? 16 : 8; 44b882e591SRui Ueyama OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align); 451ebfc59cSRafael Espindola } 461ebfc59cSRafael Espindola 47b882e591SRui Ueyama static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } 48b882e591SRui Ueyama 493151958cSRui Ueyama // Returns a list of all symbols that we want to print out. 5031cd5da5SGeorge Rimar static std::vector<Symbol *> getSymbols() { 5131cd5da5SGeorge Rimar std::vector<Symbol *> V; 5231cd5da5SGeorge Rimar for (InputFile *File : ObjectFiles) { 5331cd5da5SGeorge Rimar for (Symbol *B : File->getSymbols()) { 5431cd5da5SGeorge Rimar if (auto *SS = dyn_cast<SharedSymbol>(B)) 55*ba7afd70SGeorge Rimar if (SS->CopyRelSec || SS->NeedsPltAddr) 5631cd5da5SGeorge Rimar V.push_back(SS); 57e9a9e0a1SPeter Collingbourne if (auto *DR = dyn_cast<Defined>(B)) 58dfebd360SRafael Espindola if (DR->File == File && !DR->isSection() && DR->Section && 59f456f572SRafael Espindola DR->Section->Live) 60f456f572SRafael Espindola V.push_back(DR); 6131cd5da5SGeorge Rimar } 6231cd5da5SGeorge Rimar } 633151958cSRui Ueyama return V; 643151958cSRui Ueyama } 65b882e591SRui Ueyama 663151958cSRui Ueyama // Returns a map from sections to their symbols. 6731cd5da5SGeorge Rimar static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> Syms) { 683151958cSRui Ueyama SymbolMapTy Ret; 6931cd5da5SGeorge Rimar for (Symbol *S : Syms) { 70*ba7afd70SGeorge Rimar if (auto *DR = dyn_cast<Defined>(S)) { 7131cd5da5SGeorge Rimar Ret[DR->Section].push_back(S); 72*ba7afd70SGeorge Rimar continue; 73*ba7afd70SGeorge Rimar } 74*ba7afd70SGeorge Rimar 75*ba7afd70SGeorge Rimar SharedSymbol *SS = cast<SharedSymbol>(S); 76*ba7afd70SGeorge Rimar if (SS->CopyRelSec) 77*ba7afd70SGeorge Rimar Ret[SS->CopyRelSec].push_back(S); 7831cd5da5SGeorge Rimar else 79*ba7afd70SGeorge Rimar Ret[InX::Plt].push_back(S); 8031cd5da5SGeorge Rimar } 81b882e591SRui Ueyama 82b882e591SRui Ueyama // Sort symbols by address. We want to print out symbols in the 83b882e591SRui Ueyama // order in the output file rather than the order they appeared 84b882e591SRui Ueyama // in the input files. 853151958cSRui Ueyama for (auto &It : Ret) { 8631cd5da5SGeorge Rimar SmallVectorImpl<Symbol *> &V = It.second; 87f2fe963dSGeorge Rimar std::sort(V.begin(), V.end(), 8831cd5da5SGeorge Rimar [](Symbol *A, Symbol *B) { return A->getVA() < B->getVA(); }); 891ebfc59cSRafael Espindola } 903151958cSRui Ueyama return Ret; 913151958cSRui Ueyama } 921ebfc59cSRafael Espindola 93b882e591SRui Ueyama // Construct a map from symbols to their stringified representations. 943151958cSRui Ueyama // Demangling symbols (which is what toString() does) is slow, so 953151958cSRui Ueyama // we do that in batch using parallel-for. 9631cd5da5SGeorge Rimar static DenseMap<Symbol *, std::string> 9731cd5da5SGeorge Rimar getSymbolStrings(ArrayRef<Symbol *> Syms) { 98b882e591SRui Ueyama std::vector<std::string> Str(Syms.size()); 9933d903d1SRui Ueyama parallelForEachN(0, Syms.size(), [&](size_t I) { 100b882e591SRui Ueyama raw_string_ostream OS(Str[I]); 1017f9694a4SRui Ueyama writeHeader(OS, Syms[I]->getVA(), Syms[I]->getSize(), 0); 1023151958cSRui Ueyama OS << indent(2) << toString(*Syms[I]); 103b882e591SRui Ueyama }); 1043151958cSRui Ueyama 10531cd5da5SGeorge Rimar DenseMap<Symbol *, std::string> Ret; 106b882e591SRui Ueyama for (size_t I = 0, E = Syms.size(); I < E; ++I) 1073151958cSRui Ueyama Ret[Syms[I]] = std::move(Str[I]); 1083151958cSRui Ueyama return Ret; 1091ebfc59cSRafael Espindola } 1101ebfc59cSRafael Espindola 1117f9694a4SRui Ueyama void elf::writeMapFile() { 112543731f9SRui Ueyama if (Config->MapFile.empty()) 1131ebfc59cSRafael Espindola return; 1141ebfc59cSRafael Espindola 1153151958cSRui Ueyama // Open a map file for writing. 116543731f9SRui Ueyama std::error_code EC; 117543731f9SRui Ueyama raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 1183151958cSRui Ueyama if (EC) { 119c9807c33SRui Ueyama error("cannot open " + Config->MapFile + ": " + EC.message()); 1203151958cSRui Ueyama return; 1213151958cSRui Ueyama } 1223151958cSRui Ueyama 1233151958cSRui Ueyama // Collect symbol info that we want to print out. 12431cd5da5SGeorge Rimar std::vector<Symbol *> Syms = getSymbols(); 1257b30dd1cSGeorge Rimar SymbolMapTy SectionSyms = getSectionSyms(Syms); 12631cd5da5SGeorge Rimar DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms); 1273151958cSRui Ueyama 1283151958cSRui Ueyama // Print out the header line. 1297f9694a4SRui Ueyama int W = Config->Is64 ? 16 : 8; 1303151958cSRui Ueyama OS << left_justify("Address", W) << ' ' << left_justify("Size", W) 1313151958cSRui Ueyama << " Align Out In Symbol\n"; 1323151958cSRui Ueyama 1333151958cSRui Ueyama // Print out file contents. 1348c022ca7SRafael Espindola for (OutputSection *OSec : OutputSections) { 1357b30dd1cSGeorge Rimar writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment); 1363151958cSRui Ueyama OS << OSec->Name << '\n'; 1373151958cSRui Ueyama 1383151958cSRui Ueyama // Dump symbols for each input section. 1396b394caaSRui Ueyama for (BaseCommand *Base : OSec->SectionCommands) { 140aeb4b7d2SRafael Espindola auto *ISD = dyn_cast<InputSectionDescription>(Base); 141aeb4b7d2SRafael Espindola if (!ISD) 142aeb4b7d2SRafael Espindola continue; 143aeb4b7d2SRafael Espindola for (InputSection *IS : ISD->Sections) { 1447b30dd1cSGeorge Rimar writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), 1453151958cSRui Ueyama IS->Alignment); 1463151958cSRui Ueyama OS << indent(1) << toString(IS) << '\n'; 14731cd5da5SGeorge Rimar for (Symbol *Sym : SectionSyms[IS]) 1483151958cSRui Ueyama OS << SymStr[Sym] << '\n'; 1493151958cSRui Ueyama } 1503151958cSRui Ueyama } 1511ebfc59cSRafael Espindola } 152aeb4b7d2SRafael Espindola } 153