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 "SyntheticSections.h" 35 #include "Target.h" 36 #include "llvm/Support/Parallel.h" 37 #include "llvm/Support/TimeProfiler.h" 38 39 using namespace llvm; 40 using namespace llvm::sys; 41 using namespace lld; 42 using namespace lld::macho; 43 44 using Symbols = std::vector<Defined *>; 45 // Returns a pair where the left element is a container of all live Symbols and 46 // the right element is a container of all dead symbols. 47 static std::pair<Symbols, Symbols> getSymbols() { 48 Symbols liveSymbols, deadSymbols; 49 for (InputFile *file : inputFiles) 50 if (isa<ObjFile>(file)) 51 for (Symbol *sym : file->symbols) 52 if (auto *d = dyn_cast_or_null<Defined>(sym)) 53 if (d->isec && d->getFile() == file) { 54 if (d->isLive()) { 55 assert(!shouldOmitFromOutput(d->isec)); 56 liveSymbols.push_back(d); 57 } else { 58 deadSymbols.push_back(d); 59 } 60 } 61 parallelSort(liveSymbols.begin(), liveSymbols.end(), 62 [](Defined *a, Defined *b) { 63 return a->getVA() != b->getVA() ? a->getVA() < b->getVA() 64 : a->getName() < b->getName(); 65 }); 66 parallelSort( 67 deadSymbols.begin(), deadSymbols.end(), 68 [](Defined *a, Defined *b) { return a->getName() < b->getName(); }); 69 return {std::move(liveSymbols), std::move(deadSymbols)}; 70 } 71 72 // Construct a map from symbols to their stringified representations. 73 // Demangling symbols (which is what toString() does) is slow, so 74 // we do that in batch using parallel-for. 75 static DenseMap<Symbol *, std::string> 76 getSymbolStrings(ArrayRef<Defined *> syms) { 77 std::vector<std::string> str(syms.size()); 78 parallelFor(0, syms.size(), [&](size_t i) { 79 raw_string_ostream os(str[i]); 80 Defined *sym = syms[i]; 81 82 switch (sym->isec->kind()) { 83 case InputSection::CStringLiteralKind: { 84 // Output "literal string: <string literal>" 85 const auto *isec = cast<CStringInputSection>(sym->isec); 86 const StringPiece &piece = isec->getStringPiece(sym->value); 87 assert( 88 sym->value == piece.inSecOff && 89 "We expect symbols to always point to the start of a StringPiece."); 90 StringRef str = isec->getStringRef(&piece - &(*isec->pieces.begin())); 91 assert(str.back() == '\000'); 92 (os << "literal string: ") 93 // Remove null sequence at the end 94 .write_escaped(str.substr(0, str.size() - 1)); 95 break; 96 } 97 case InputSection::ConcatKind: 98 case InputSection::WordLiteralKind: 99 os << toString(*sym); 100 } 101 }); 102 103 DenseMap<Symbol *, std::string> ret; 104 for (size_t i = 0, e = syms.size(); i < e; ++i) 105 ret[syms[i]] = std::move(str[i]); 106 return ret; 107 } 108 109 void macho::writeMapFile() { 110 if (config->mapFile.empty()) 111 return; 112 113 TimeTraceScope timeScope("Write map file"); 114 115 // Open a map file for writing. 116 std::error_code ec; 117 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None); 118 if (ec) { 119 error("cannot open " + config->mapFile + ": " + ec.message()); 120 return; 121 } 122 123 // Dump output path. 124 os << format("# Path: %s\n", config->outputFile.str().c_str()); 125 126 // Dump output architecture. 127 os << format("# Arch: %s\n", 128 getArchitectureName(config->arch()).str().c_str()); 129 130 // Dump table of object files. 131 os << "# Object files:\n"; 132 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized"); 133 uint32_t fileIndex = 1; 134 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal; 135 for (InputFile *file : inputFiles) { 136 if (isa<ObjFile>(file)) { 137 os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str()); 138 readerToFileOrdinal[file] = fileIndex++; 139 } 140 } 141 142 // Dump table of sections 143 os << "# Sections:\n"; 144 os << "# Address\tSize \tSegment\tSection\n"; 145 for (OutputSegment *seg : outputSegments) 146 for (OutputSection *osec : seg->getSections()) { 147 if (osec->isHidden()) 148 continue; 149 150 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(), 151 seg->name.str().c_str(), osec->name.str().c_str()); 152 } 153 154 // Dump table of symbols 155 Symbols liveSymbols, deadSymbols; 156 std::tie(liveSymbols, deadSymbols) = getSymbols(); 157 158 DenseMap<Symbol *, std::string> liveSymbolStrings = 159 getSymbolStrings(liveSymbols); 160 os << "# Symbols:\n"; 161 os << "# Address\t File Name\n"; 162 for (Symbol *sym : liveSymbols) { 163 assert(sym->isLive()); 164 os << format("0x%08llX\t[%3u] %s\n", sym->getVA(), 165 readerToFileOrdinal[sym->getFile()], 166 liveSymbolStrings[sym].c_str()); 167 } 168 169 if (config->deadStrip) { 170 DenseMap<Symbol *, std::string> deadSymbolStrings = 171 getSymbolStrings(deadSymbols); 172 os << "# Dead Stripped Symbols:\n"; 173 os << "# Address\t File Name\n"; 174 for (Symbol *sym : deadSymbols) { 175 assert(!sym->isLive()); 176 os << format("<<dead>>\t[%3u] %s\n", readerToFileOrdinal[sym->getFile()], 177 deadSymbolStrings[sym].c_str()); 178 } 179 } 180 } 181