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" 29*4f5c8c29SBob Haarman 30*4f5c8c29SBob Haarman #include "lld/Common/Threads.h" 311ebfc59cSRafael Espindola 32543731f9SRui Ueyama #include "llvm/Support/raw_ostream.h" 331ebfc59cSRafael Espindola 341ebfc59cSRafael Espindola using namespace llvm; 351ebfc59cSRafael Espindola using namespace llvm::object; 361ebfc59cSRafael Espindola 371ebfc59cSRafael Espindola using namespace lld; 381ebfc59cSRafael Espindola using namespace lld::elf; 391ebfc59cSRafael Espindola 40f2fe963dSGeorge Rimar typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy; 41b882e591SRui Ueyama 42b882e591SRui Ueyama // Print out the first three columns of a line. 43b882e591SRui Ueyama static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, 44b882e591SRui Ueyama uint64_t Align) { 457b30dd1cSGeorge Rimar int W = Config->Is64 ? 16 : 8; 46b882e591SRui Ueyama OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align); 471ebfc59cSRafael Espindola } 481ebfc59cSRafael Espindola 49b882e591SRui Ueyama static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } 50b882e591SRui Ueyama 513151958cSRui Ueyama // Returns a list of all symbols that we want to print out. 52f2fe963dSGeorge Rimar template <class ELFT> static std::vector<Defined *> getSymbols() { 53f2fe963dSGeorge Rimar std::vector<Defined *> V; 54696a7f9aSGeorge Rimar for (InputFile *File : ObjectFiles) { 55f2fe963dSGeorge Rimar for (SymbolBody *B : File->getSymbols()) { 56f2fe963dSGeorge Rimar if (auto *DR = dyn_cast<DefinedRegular>(B)) { 576e93d054SRafael Espindola if (DR->getFile() == File && !DR->isSection() && DR->Section && 58f456f572SRafael Espindola DR->Section->Live) 59f456f572SRafael Espindola V.push_back(DR); 60f2fe963dSGeorge Rimar } else if (auto *DC = dyn_cast<DefinedCommon>(B)) { 611e30f07cSDmitry Mikulin if (DC->Section) 6245d2767cSGeorge Rimar V.push_back(DC); 63f2fe963dSGeorge Rimar } 64f2fe963dSGeorge Rimar } 65f2fe963dSGeorge Rimar } 663151958cSRui Ueyama return V; 673151958cSRui Ueyama } 68b882e591SRui Ueyama 693151958cSRui Ueyama // Returns a map from sections to their symbols. 70f2fe963dSGeorge Rimar static SymbolMapTy getSectionSyms(ArrayRef<Defined *> Syms) { 713151958cSRui Ueyama SymbolMapTy Ret; 72f2fe963dSGeorge Rimar for (Defined *S : Syms) { 73f2fe963dSGeorge Rimar if (auto *DR = dyn_cast<DefinedRegular>(S)) 74f2fe963dSGeorge Rimar Ret[DR->Section].push_back(S); 751e30f07cSDmitry Mikulin else if (auto *DC = dyn_cast<DefinedCommon>(S)) 761e30f07cSDmitry Mikulin Ret[DC->Section].push_back(S); 77f2fe963dSGeorge Rimar } 78b882e591SRui Ueyama 79b882e591SRui Ueyama // Sort symbols by address. We want to print out symbols in the 80b882e591SRui Ueyama // order in the output file rather than the order they appeared 81b882e591SRui Ueyama // in the input files. 823151958cSRui Ueyama for (auto &It : Ret) { 83f2fe963dSGeorge Rimar SmallVectorImpl<Defined *> &V = It.second; 84f2fe963dSGeorge Rimar std::sort(V.begin(), V.end(), 85f2fe963dSGeorge Rimar [](Defined *A, Defined *B) { return A->getVA() < B->getVA(); }); 861ebfc59cSRafael Espindola } 873151958cSRui Ueyama return Ret; 883151958cSRui Ueyama } 891ebfc59cSRafael Espindola 90b882e591SRui Ueyama // Construct a map from symbols to their stringified representations. 913151958cSRui Ueyama // Demangling symbols (which is what toString() does) is slow, so 923151958cSRui Ueyama // we do that in batch using parallel-for. 933151958cSRui Ueyama template <class ELFT> 94f2fe963dSGeorge Rimar static DenseMap<Defined *, std::string> 95f2fe963dSGeorge Rimar getSymbolStrings(ArrayRef<Defined *> Syms) { 96b882e591SRui Ueyama std::vector<std::string> Str(Syms.size()); 9733d903d1SRui Ueyama parallelForEachN(0, Syms.size(), [&](size_t I) { 98b882e591SRui Ueyama raw_string_ostream OS(Str[I]); 997b30dd1cSGeorge Rimar writeHeader(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(), 0); 1003151958cSRui Ueyama OS << indent(2) << toString(*Syms[I]); 101b882e591SRui Ueyama }); 1023151958cSRui Ueyama 103f2fe963dSGeorge Rimar DenseMap<Defined *, std::string> Ret; 104b882e591SRui Ueyama for (size_t I = 0, E = Syms.size(); I < E; ++I) 1053151958cSRui Ueyama Ret[Syms[I]] = std::move(Str[I]); 1063151958cSRui Ueyama return Ret; 1071ebfc59cSRafael Espindola } 1081ebfc59cSRafael Espindola 1098c022ca7SRafael Espindola template <class ELFT> void elf::writeMapFile() { 110543731f9SRui Ueyama if (Config->MapFile.empty()) 1111ebfc59cSRafael Espindola return; 1121ebfc59cSRafael Espindola 1133151958cSRui Ueyama // Open a map file for writing. 114543731f9SRui Ueyama std::error_code EC; 115543731f9SRui Ueyama raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 1163151958cSRui Ueyama if (EC) { 117c9807c33SRui Ueyama error("cannot open " + Config->MapFile + ": " + EC.message()); 1183151958cSRui Ueyama return; 1193151958cSRui Ueyama } 1203151958cSRui Ueyama 1213151958cSRui Ueyama // Collect symbol info that we want to print out. 122f2fe963dSGeorge Rimar std::vector<Defined *> Syms = getSymbols<ELFT>(); 1237b30dd1cSGeorge Rimar SymbolMapTy SectionSyms = getSectionSyms(Syms); 124f2fe963dSGeorge Rimar DenseMap<Defined *, std::string> SymStr = getSymbolStrings<ELFT>(Syms); 1253151958cSRui Ueyama 1263151958cSRui Ueyama // Print out the header line. 1273151958cSRui Ueyama int W = ELFT::Is64Bits ? 16 : 8; 1283151958cSRui Ueyama OS << left_justify("Address", W) << ' ' << left_justify("Size", W) 1293151958cSRui Ueyama << " Align Out In Symbol\n"; 1303151958cSRui Ueyama 1313151958cSRui Ueyama // Print out file contents. 1328c022ca7SRafael Espindola for (OutputSection *OSec : OutputSections) { 1337b30dd1cSGeorge Rimar writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment); 1343151958cSRui Ueyama OS << OSec->Name << '\n'; 1353151958cSRui Ueyama 1363151958cSRui Ueyama // Dump symbols for each input section. 1376b394caaSRui Ueyama for (BaseCommand *Base : OSec->SectionCommands) { 138aeb4b7d2SRafael Espindola auto *ISD = dyn_cast<InputSectionDescription>(Base); 139aeb4b7d2SRafael Espindola if (!ISD) 140aeb4b7d2SRafael Espindola continue; 141aeb4b7d2SRafael Espindola for (InputSection *IS : ISD->Sections) { 1427b30dd1cSGeorge Rimar writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), 1433151958cSRui Ueyama IS->Alignment); 1443151958cSRui Ueyama OS << indent(1) << toString(IS) << '\n'; 145f2fe963dSGeorge Rimar for (Defined *Sym : SectionSyms[IS]) 1463151958cSRui Ueyama OS << SymStr[Sym] << '\n'; 1473151958cSRui Ueyama } 1483151958cSRui Ueyama } 1491ebfc59cSRafael Espindola } 150aeb4b7d2SRafael Espindola } 1511ebfc59cSRafael Espindola 1528c022ca7SRafael Espindola template void elf::writeMapFile<ELF32LE>(); 1538c022ca7SRafael Espindola template void elf::writeMapFile<ELF32BE>(); 1548c022ca7SRafael Espindola template void elf::writeMapFile<ELF64LE>(); 1558c022ca7SRafael Espindola template void elf::writeMapFile<ELF64BE>(); 156