Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 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 | // |
Rui Ueyama | 3012b37 | 2017-04-28 20:38:27 +0000 | [diff] [blame] | 14 | // Address Size Align Out In Symbol |
| 15 | // 00201000 00000015 4 .text |
Rui Ueyama | 3781137 | 2017-04-30 20:58:20 +0000 | [diff] [blame] | 16 | // 00201000 0000000e 4 test.o:(.text) |
Rui Ueyama | 3012b37 | 2017-04-28 20:38:27 +0000 | [diff] [blame] | 17 | // 0020100e 00000000 0 local |
| 18 | // 00201005 00000000 0 f(int) |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "MapFile.h" |
| 23 | #include "InputFiles.h" |
| 24 | #include "Strings.h" |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 25 | #include "SymbolTable.h" |
| 26 | #include "Threads.h" |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 27 | |
Rui Ueyama | 543731f | 2017-01-15 00:41:21 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace llvm::object; |
| 32 | |
| 33 | using namespace lld; |
| 34 | using namespace lld::elf; |
| 35 | |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 36 | typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>> |
| 37 | SymbolMapTy; |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 38 | |
| 39 | // Print out the first three columns of a line. |
| 40 | template <class ELFT> |
| 41 | static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, |
| 42 | uint64_t Align) { |
| 43 | int W = ELFT::Is64Bits ? 16 : 8; |
| 44 | OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align); |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 47 | static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 48 | |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 49 | // Returns a list of all symbols that we want to print out. |
| 50 | template <class ELFT> std::vector<DefinedRegular *> getSymbols() { |
| 51 | std::vector<DefinedRegular *> V; |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 52 | for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) |
| 53 | for (SymbolBody *B : File->getSymbols()) |
| 54 | if (B->File == File && !B->isSection()) |
| 55 | if (auto *Sym = dyn_cast<DefinedRegular>(B)) |
| 56 | if (Sym->Section) |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 57 | V.push_back(Sym); |
| 58 | return V; |
| 59 | } |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 60 | |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 61 | // Returns a map from sections to their symbols. |
| 62 | template <class ELFT> |
| 63 | SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) { |
| 64 | SymbolMapTy Ret; |
| 65 | for (DefinedRegular *S : Syms) |
| 66 | Ret[S->Section].push_back(S); |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 67 | |
| 68 | // Sort symbols by address. We want to print out symbols in the |
| 69 | // order in the output file rather than the order they appeared |
| 70 | // in the input files. |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 71 | for (auto &It : Ret) { |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 72 | SmallVectorImpl<DefinedRegular *> &V = It.second; |
| 73 | std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) { |
| 74 | return A->getVA() < B->getVA(); |
| 75 | }); |
| 76 | } |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 77 | return Ret; |
| 78 | } |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 79 | |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 80 | // Construct a map from symbols to their stringified representations. |
| 81 | // Demangling symbols (which is what toString() does) is slow, so |
| 82 | // we do that in batch using parallel-for. |
| 83 | template <class ELFT> |
| 84 | DenseMap<DefinedRegular *, std::string> |
| 85 | getSymbolStrings(ArrayRef<DefinedRegular *> Syms) { |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 86 | std::vector<std::string> Str(Syms.size()); |
Rui Ueyama | 33d903d | 2017-05-10 20:02:19 +0000 | [diff] [blame^] | 87 | parallelForEachN(0, Syms.size(), [&](size_t I) { |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 88 | raw_string_ostream OS(Str[I]); |
| 89 | writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(), |
| 90 | 0); |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 91 | OS << indent(2) << toString(*Syms[I]); |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 92 | }); |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 93 | |
| 94 | DenseMap<DefinedRegular *, std::string> Ret; |
Rui Ueyama | b882e59 | 2017-04-28 17:19:13 +0000 | [diff] [blame] | 95 | for (size_t I = 0, E = Syms.size(); I < E; ++I) |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 96 | Ret[Syms[I]] = std::move(Str[I]); |
| 97 | return Ret; |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template <class ELFT> |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 101 | void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) { |
Rui Ueyama | 543731f | 2017-01-15 00:41:21 +0000 | [diff] [blame] | 102 | if (Config->MapFile.empty()) |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 103 | return; |
| 104 | |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 105 | // Open a map file for writing. |
Rui Ueyama | 543731f | 2017-01-15 00:41:21 +0000 | [diff] [blame] | 106 | std::error_code EC; |
| 107 | raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 108 | if (EC) { |
Rui Ueyama | c9807c3 | 2017-01-16 01:07:19 +0000 | [diff] [blame] | 109 | error("cannot open " + Config->MapFile + ": " + EC.message()); |
Rui Ueyama | 3151958 | 2017-04-28 23:29:15 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | // Collect symbol info that we want to print out. |
| 114 | std::vector<DefinedRegular *> Syms = getSymbols<ELFT>(); |
| 115 | SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms); |
| 116 | DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms); |
| 117 | |
| 118 | // Print out the header line. |
| 119 | int W = ELFT::Is64Bits ? 16 : 8; |
| 120 | OS << left_justify("Address", W) << ' ' << left_justify("Size", W) |
| 121 | << " Align Out In Symbol\n"; |
| 122 | |
| 123 | // Print out file contents. |
| 124 | for (OutputSection *OSec : OutputSections) { |
| 125 | writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment); |
| 126 | OS << OSec->Name << '\n'; |
| 127 | |
| 128 | // Dump symbols for each input section. |
| 129 | for (InputSection *IS : OSec->Sections) { |
| 130 | writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), |
| 131 | IS->Alignment); |
| 132 | OS << indent(1) << toString(IS) << '\n'; |
| 133 | for (DefinedRegular *Sym : SectionSyms[IS]) |
| 134 | OS << SymStr[Sym] << '\n'; |
| 135 | } |
| 136 | } |
Rafael Espindola | 1ebfc59 | 2017-01-13 21:05:46 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 139 | template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>); |
| 140 | template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>); |
| 141 | template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>); |
| 142 | template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>); |