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 SymbolMapTy = DenseMap<const InputSection *, SmallVector<Defined *, 4>>; 44 45 // Returns a map from sections to their symbols. 46 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) { 47 SymbolMapTy ret; 48 for (Defined *dr : syms) 49 ret[dr->isec].push_back(dr); 50 51 // Sort symbols by address. We want to print out symbols in the 52 // order in the output file rather than the order they appeared 53 // in the input files. 54 for (auto &it : ret) 55 llvm::stable_sort(it.second, [](Defined *a, Defined *b) { 56 return a->getVA() < b->getVA(); 57 }); 58 return ret; 59 } 60 61 // Returns a list of all symbols that we want to print out. 62 static std::vector<Defined *> getSymbols() { 63 std::vector<Defined *> v; 64 for (InputFile *file : inputFiles) 65 if (isa<ObjFile>(file)) 66 for (Symbol *sym : file->symbols) { 67 if (sym == nullptr) 68 continue; 69 if (auto *d = dyn_cast<Defined>(sym)) 70 if (d->isec && d->getFile() == file) 71 v.push_back(d); 72 } 73 return v; 74 } 75 76 // Construct a map from symbols to their stringified representations. 77 // Demangling symbols (which is what toString() does) is slow, so 78 // we do that in batch using parallel-for. 79 static DenseMap<Symbol *, std::string> 80 getSymbolStrings(ArrayRef<Defined *> syms) { 81 std::vector<std::string> str(syms.size()); 82 parallelForEachN(0, syms.size(), [&](size_t i) { 83 raw_string_ostream os(str[i]); 84 os << toString(*syms[i]); 85 }); 86 87 DenseMap<Symbol *, std::string> ret; 88 for (size_t i = 0, e = syms.size(); i < e; ++i) 89 ret[syms[i]] = std::move(str[i]); 90 return ret; 91 } 92 93 void macho::writeMapFile() { 94 if (config->mapFile.empty()) 95 return; 96 97 TimeTraceScope timeScope("Write map file"); 98 99 // Open a map file for writing. 100 std::error_code ec; 101 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None); 102 if (ec) { 103 error("cannot open " + config->mapFile + ": " + ec.message()); 104 return; 105 } 106 107 // Dump output path. 108 os << format("# Path: %s\n", config->outputFile.str().c_str()); 109 110 // Dump output architecture. 111 os << format("# Arch: %s\n", 112 getArchitectureName(config->arch()).str().c_str()); 113 114 // Dump table of object files. 115 os << "# Object files:\n"; 116 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized"); 117 uint32_t fileIndex = 1; 118 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal; 119 for (InputFile *file : inputFiles) { 120 if (isa<ObjFile>(file)) { 121 os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str()); 122 readerToFileOrdinal[file] = fileIndex++; 123 } 124 } 125 126 // Collect symbol info that we want to print out. 127 std::vector<Defined *> syms = getSymbols(); 128 SymbolMapTy sectionSyms = getSectionSyms(syms); 129 DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms); 130 131 // Dump table of sections 132 os << "# Sections:\n"; 133 os << "# Address\tSize \tSegment\tSection\n"; 134 for (OutputSegment *seg : outputSegments) 135 for (OutputSection *osec : seg->getSections()) { 136 if (osec->isHidden()) 137 continue; 138 139 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(), 140 seg->name.str().c_str(), osec->name.str().c_str()); 141 } 142 143 // Dump table of symbols 144 os << "# Symbols:\n"; 145 os << "# Address\t File Name\n"; 146 for (InputSection *isec : inputSections) { 147 for (Symbol *sym : sectionSyms[isec]) { 148 os << format("0x%08llX\t[%3u] %s\n", sym->getVA(), 149 readerToFileOrdinal[sym->getFile()], symStr[sym].c_str()); 150 } 151 } 152 153 // TODO: when we implement -dead_strip, we should dump dead stripped symbols 154 } 155