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