1 //===- MapFile.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the -map option. It shows lists in order and 10 // hierarchically the outputFile, arch, input files, output sections and 11 // symbol: 12 // 13 // # Path: test 14 // # Arch: x86_84 15 // # Object files: 16 // [ 0] linker synthesized 17 // [ 1] a.o 18 // # Sections: 19 // # Address Size Segment Section 20 // 0x1000005C0 0x0000004C __TEXT __text 21 // # Symbols: 22 // # Address File Name 23 // 0x1000005C0 [ 1] _main 24 // 25 //===----------------------------------------------------------------------===// 26 27 #include "MapFile.h" 28 #include "Config.h" 29 #include "InputFiles.h" 30 #include "InputSection.h" 31 #include "OutputSection.h" 32 #include "OutputSegment.h" 33 #include "Symbols.h" 34 #include "Target.h" 35 #include "llvm/Support/Parallel.h" 36 #include "llvm/Support/TimeProfiler.h" 37 38 using namespace llvm; 39 using namespace llvm::sys; 40 using namespace lld; 41 using namespace lld::macho; 42 43 using Symbols = std::vector<Defined *>; 44 // Returns a pair where the left element is a container of all live Symbols and 45 // the right element is a container of all dead symbols. 46 static std::pair<Symbols, Symbols> getSymbols() { 47 Symbols liveSymbols, deadSymbols; 48 for (InputFile *file : inputFiles) 49 if (isa<ObjFile>(file)) 50 for (Symbol *sym : file->symbols) 51 if (auto *d = dyn_cast_or_null<Defined>(sym)) 52 if (d->isec && d->getFile() == file) { 53 if (d->isLive()) { 54 assert(!shouldOmitFromOutput(d->isec)); 55 liveSymbols.push_back(d); 56 } else { 57 deadSymbols.push_back(d); 58 } 59 } 60 parallelSort(liveSymbols.begin(), liveSymbols.end(), 61 [](Defined *a, Defined *b) { 62 return a->getVA() != b->getVA() ? a->getVA() < b->getVA() 63 : a->getName() < b->getName(); 64 }); 65 parallelSort( 66 deadSymbols.begin(), deadSymbols.end(), 67 [](Defined *a, Defined *b) { return a->getName() < b->getName(); }); 68 return {std::move(liveSymbols), std::move(deadSymbols)}; 69 } 70 71 // Construct a map from symbols to their stringified representations. 72 // Demangling symbols (which is what toString() does) is slow, so 73 // we do that in batch using parallel-for. 74 static DenseMap<Symbol *, std::string> 75 getSymbolStrings(ArrayRef<Defined *> syms) { 76 std::vector<std::string> str(syms.size()); 77 parallelForEachN(0, syms.size(), [&](size_t i) { 78 raw_string_ostream os(str[i]); 79 os << toString(*syms[i]); 80 }); 81 82 DenseMap<Symbol *, std::string> ret; 83 for (size_t i = 0, e = syms.size(); i < e; ++i) 84 ret[syms[i]] = std::move(str[i]); 85 return ret; 86 } 87 88 void macho::writeMapFile() { 89 if (config->mapFile.empty()) 90 return; 91 92 TimeTraceScope timeScope("Write map file"); 93 94 // Open a map file for writing. 95 std::error_code ec; 96 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None); 97 if (ec) { 98 error("cannot open " + config->mapFile + ": " + ec.message()); 99 return; 100 } 101 102 // Dump output path. 103 os << format("# Path: %s\n", config->outputFile.str().c_str()); 104 105 // Dump output architecture. 106 os << format("# Arch: %s\n", 107 getArchitectureName(config->arch()).str().c_str()); 108 109 // Dump table of object files. 110 os << "# Object files:\n"; 111 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized"); 112 uint32_t fileIndex = 1; 113 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal; 114 for (InputFile *file : inputFiles) { 115 if (isa<ObjFile>(file)) { 116 os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str()); 117 readerToFileOrdinal[file] = fileIndex++; 118 } 119 } 120 121 // Dump table of sections 122 os << "# Sections:\n"; 123 os << "# Address\tSize \tSegment\tSection\n"; 124 for (OutputSegment *seg : outputSegments) 125 for (OutputSection *osec : seg->getSections()) { 126 if (osec->isHidden()) 127 continue; 128 129 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(), 130 seg->name.str().c_str(), osec->name.str().c_str()); 131 } 132 133 // Dump table of symbols 134 Symbols liveSymbols, deadSymbols; 135 std::tie(liveSymbols, deadSymbols) = getSymbols(); 136 137 DenseMap<Symbol *, std::string> liveSymbolStrings = 138 getSymbolStrings(liveSymbols); 139 os << "# Symbols:\n"; 140 os << "# Address\t File Name\n"; 141 for (Symbol *sym : liveSymbols) { 142 assert(sym->isLive()); 143 os << format("0x%08llX\t[%3u] %s\n", sym->getVA(), 144 readerToFileOrdinal[sym->getFile()], 145 liveSymbolStrings[sym].c_str()); 146 } 147 148 if (config->deadStrip) { 149 DenseMap<Symbol *, std::string> deadSymbolStrings = 150 getSymbolStrings(deadSymbols); 151 os << "# Dead Stripped Symbols:\n"; 152 os << "# Address\t File Name\n"; 153 for (Symbol *sym : deadSymbols) { 154 assert(!sym->isLive()); 155 os << format("<<dead>>\t[%3u] %s\n", readerToFileOrdinal[sym->getFile()], 156 deadSymbolStrings[sym].c_str()); 157 } 158 } 159 } 160