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