146b69c69SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===// 246b69c69SDimitry Andric // 346b69c69SDimitry Andric // The LLVM Linker 446b69c69SDimitry Andric // 546b69c69SDimitry Andric // This file is distributed under the University of Illinois Open Source 646b69c69SDimitry Andric // License. See LICENSE.TXT for details. 746b69c69SDimitry Andric // 846b69c69SDimitry Andric //===----------------------------------------------------------------------===// 946b69c69SDimitry Andric // 1046b69c69SDimitry Andric // This file implements the -Map option. It shows lists in order and 1146b69c69SDimitry Andric // hierarchically the output sections, input sections, input files and 1246b69c69SDimitry Andric // symbol: 1346b69c69SDimitry Andric // 14f37b6182SDimitry Andric // Address Size Align Out In Symbol 1546b69c69SDimitry Andric // 00201000 00000015 4 .text 16f37b6182SDimitry Andric // 00201000 0000000e 4 test.o:(.text) 1746b69c69SDimitry Andric // 0020100e 00000000 0 local 1846b69c69SDimitry Andric // 00201005 00000000 0 f(int) 1946b69c69SDimitry Andric // 2046b69c69SDimitry Andric //===----------------------------------------------------------------------===// 2146b69c69SDimitry Andric 2246b69c69SDimitry Andric #include "MapFile.h" 2346b69c69SDimitry Andric #include "InputFiles.h" 2446b69c69SDimitry Andric #include "Strings.h" 25f37b6182SDimitry Andric #include "SymbolTable.h" 26f37b6182SDimitry Andric #include "Threads.h" 2746b69c69SDimitry Andric 2846b69c69SDimitry Andric #include "llvm/Support/raw_ostream.h" 2946b69c69SDimitry Andric 3046b69c69SDimitry Andric using namespace llvm; 3146b69c69SDimitry Andric using namespace llvm::object; 3246b69c69SDimitry Andric 3346b69c69SDimitry Andric using namespace lld; 3446b69c69SDimitry Andric using namespace lld::elf; 3546b69c69SDimitry Andric 36f37b6182SDimitry Andric typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>> 37f37b6182SDimitry Andric SymbolMapTy; 3846b69c69SDimitry Andric 39f37b6182SDimitry Andric // Print out the first three columns of a line. 4046b69c69SDimitry Andric template <class ELFT> 41f37b6182SDimitry Andric static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, 42f37b6182SDimitry Andric uint64_t Align) { 43f37b6182SDimitry Andric int W = ELFT::Is64Bits ? 16 : 8; 44f37b6182SDimitry Andric OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align); 4546b69c69SDimitry Andric } 4646b69c69SDimitry Andric 47f37b6182SDimitry Andric static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } 4846b69c69SDimitry Andric 49f37b6182SDimitry Andric // Returns a list of all symbols that we want to print out. 50f37b6182SDimitry Andric template <class ELFT> std::vector<DefinedRegular *> getSymbols() { 51f37b6182SDimitry Andric std::vector<DefinedRegular *> V; 52f37b6182SDimitry Andric for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) 53f37b6182SDimitry Andric for (SymbolBody *B : File->getSymbols()) 54f37b6182SDimitry Andric if (B->File == File && !B->isSection()) 55f37b6182SDimitry Andric if (auto *Sym = dyn_cast<DefinedRegular>(B)) 56f37b6182SDimitry Andric if (Sym->Section) 57f37b6182SDimitry Andric V.push_back(Sym); 58f37b6182SDimitry Andric return V; 5946b69c69SDimitry Andric } 6046b69c69SDimitry Andric 61f37b6182SDimitry Andric // Returns a map from sections to their symbols. 6246b69c69SDimitry Andric template <class ELFT> 63f37b6182SDimitry Andric SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) { 64f37b6182SDimitry Andric SymbolMapTy Ret; 65f37b6182SDimitry Andric for (DefinedRegular *S : Syms) 66f37b6182SDimitry Andric Ret[S->Section].push_back(S); 6746b69c69SDimitry Andric 68f37b6182SDimitry Andric // Sort symbols by address. We want to print out symbols in the 69f37b6182SDimitry Andric // order in the output file rather than the order they appeared 70f37b6182SDimitry Andric // in the input files. 71f37b6182SDimitry Andric for (auto &It : Ret) { 72f37b6182SDimitry Andric SmallVectorImpl<DefinedRegular *> &V = It.second; 73f37b6182SDimitry Andric std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) { 74f37b6182SDimitry Andric return A->getVA() < B->getVA(); 75f37b6182SDimitry Andric }); 7646b69c69SDimitry Andric } 77f37b6182SDimitry Andric return Ret; 7846b69c69SDimitry Andric } 79f37b6182SDimitry Andric 80f37b6182SDimitry Andric // Construct a map from symbols to their stringified representations. 81f37b6182SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so 82f37b6182SDimitry Andric // we do that in batch using parallel-for. 83f37b6182SDimitry Andric template <class ELFT> 84f37b6182SDimitry Andric DenseMap<DefinedRegular *, std::string> 85f37b6182SDimitry Andric getSymbolStrings(ArrayRef<DefinedRegular *> Syms) { 86f37b6182SDimitry Andric std::vector<std::string> Str(Syms.size()); 875517e702SDimitry Andric parallelForEachN(0, Syms.size(), [&](size_t I) { 88f37b6182SDimitry Andric raw_string_ostream OS(Str[I]); 89f37b6182SDimitry Andric writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(), 90f37b6182SDimitry Andric 0); 91f37b6182SDimitry Andric OS << indent(2) << toString(*Syms[I]); 92f37b6182SDimitry Andric }); 93f37b6182SDimitry Andric 94f37b6182SDimitry Andric DenseMap<DefinedRegular *, std::string> Ret; 95f37b6182SDimitry Andric for (size_t I = 0, E = Syms.size(); I < E; ++I) 96f37b6182SDimitry Andric Ret[Syms[I]] = std::move(Str[I]); 97f37b6182SDimitry Andric return Ret; 9846b69c69SDimitry Andric } 9946b69c69SDimitry Andric 10046b69c69SDimitry Andric template <class ELFT> 10146b69c69SDimitry Andric void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) { 10246b69c69SDimitry Andric if (Config->MapFile.empty()) 10346b69c69SDimitry Andric return; 10446b69c69SDimitry Andric 105f37b6182SDimitry Andric // Open a map file for writing. 10646b69c69SDimitry Andric std::error_code EC; 10746b69c69SDimitry Andric raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 108f37b6182SDimitry Andric if (EC) { 10946b69c69SDimitry Andric error("cannot open " + Config->MapFile + ": " + EC.message()); 110f37b6182SDimitry Andric return; 111f37b6182SDimitry Andric } 112f37b6182SDimitry Andric 113f37b6182SDimitry Andric // Collect symbol info that we want to print out. 114f37b6182SDimitry Andric std::vector<DefinedRegular *> Syms = getSymbols<ELFT>(); 115f37b6182SDimitry Andric SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms); 116f37b6182SDimitry Andric DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms); 117f37b6182SDimitry Andric 118f37b6182SDimitry Andric // Print out the header line. 119f37b6182SDimitry Andric int W = ELFT::Is64Bits ? 16 : 8; 120f37b6182SDimitry Andric OS << left_justify("Address", W) << ' ' << left_justify("Size", W) 121f37b6182SDimitry Andric << " Align Out In Symbol\n"; 122f37b6182SDimitry Andric 123f37b6182SDimitry Andric // Print out file contents. 124f37b6182SDimitry Andric for (OutputSection *OSec : OutputSections) { 125f37b6182SDimitry Andric writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment); 126f37b6182SDimitry Andric OS << OSec->Name << '\n'; 127f37b6182SDimitry Andric 128f37b6182SDimitry Andric // Dump symbols for each input section. 129f37b6182SDimitry Andric for (InputSection *IS : OSec->Sections) { 130f37b6182SDimitry Andric writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), 131f37b6182SDimitry Andric IS->Alignment); 132f37b6182SDimitry Andric OS << indent(1) << toString(IS) << '\n'; 133f37b6182SDimitry Andric for (DefinedRegular *Sym : SectionSyms[IS]) 134f37b6182SDimitry Andric OS << SymStr[Sym] << '\n'; 135f37b6182SDimitry Andric } 136f37b6182SDimitry Andric } 13746b69c69SDimitry Andric } 13846b69c69SDimitry Andric 13946b69c69SDimitry Andric template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>); 14046b69c69SDimitry Andric template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>); 14146b69c69SDimitry Andric template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>); 14246b69c69SDimitry Andric template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>); 143