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, which maps address ranges to their 10 // respective contents, plus the input file these contents were originally from. 11 // The contents (typically symbols) are listed in address order. Dead-stripped 12 // contents are included as well. 13 // 14 // # Path: test 15 // # Arch: x86_84 16 // # Object files: 17 // [ 0] linker synthesized 18 // [ 1] a.o 19 // # Sections: 20 // # Address Size Segment Section 21 // 0x1000005C0 0x0000004C __TEXT __text 22 // # Symbols: 23 // # Address Size File Name 24 // 0x1000005C0 0x00000001 [ 1] _main 25 // # Dead Stripped Symbols: 26 // # Size File Name 27 // <<dead>> 0x00000001 [ 1] _foo 28 // 29 //===----------------------------------------------------------------------===// 30 31 #include "MapFile.h" 32 #include "ConcatOutputSection.h" 33 #include "Config.h" 34 #include "InputFiles.h" 35 #include "InputSection.h" 36 #include "OutputSegment.h" 37 #include "Symbols.h" 38 #include "SyntheticSections.h" 39 #include "Target.h" 40 #include "lld/Common/ErrorHandler.h" 41 #include "llvm/ADT/DenseMap.h" 42 #include "llvm/Support/Parallel.h" 43 #include "llvm/Support/TimeProfiler.h" 44 45 using namespace llvm; 46 using namespace llvm::sys; 47 using namespace lld; 48 using namespace lld::macho; 49 50 struct CStringInfo { 51 uint32_t fileIndex; 52 StringRef str; 53 }; 54 55 struct MapInfo { 56 SmallVector<InputFile *> files; 57 SmallVector<Defined *> deadSymbols; 58 DenseMap<const OutputSection *, 59 SmallVector<std::pair<uint64_t /*addr*/, CStringInfo>>> 60 liveCStringsForSection; 61 SmallVector<CStringInfo> deadCStrings; 62 }; 63 64 static MapInfo gatherMapInfo() { 65 MapInfo info; 66 for (InputFile *file : inputFiles) { 67 bool isReferencedFile = false; 68 69 if (isa<ObjFile>(file) || isa<BitcodeFile>(file)) { 70 uint32_t fileIndex = info.files.size() + 1; 71 72 // Gather the dead symbols. We don't have to bother with the live ones 73 // because we will pick them up as we iterate over the OutputSections 74 // later. 75 for (Symbol *sym : file->symbols) { 76 if (auto *d = dyn_cast_or_null<Defined>(sym)) 77 // Only emit the prevailing definition of a symbol. Also, don't emit 78 // the symbol if it is part of a cstring section (we use the literal 79 // value instead, similar to ld64) 80 if (d->isec && d->getFile() == file && 81 !isa<CStringInputSection>(d->isec)) { 82 isReferencedFile = true; 83 if (!d->isLive()) 84 info.deadSymbols.push_back(d); 85 } 86 } 87 88 // Gather all the cstrings (both live and dead). A CString(Output)Section 89 // doesn't provide us a way of figuring out which InputSections its 90 // cstring contents came from, so we need to build up that mapping here. 91 for (const Section *sec : file->sections) { 92 for (const Subsection &subsec : sec->subsections) { 93 if (auto isec = dyn_cast<CStringInputSection>(subsec.isec)) { 94 auto &liveCStrings = info.liveCStringsForSection[isec->parent]; 95 for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { 96 if (piece.live) 97 liveCStrings.push_back({isec->parent->addr + piece.outSecOff, 98 {fileIndex, isec->getStringRef(i)}}); 99 else 100 info.deadCStrings.push_back({fileIndex, isec->getStringRef(i)}); 101 isReferencedFile = true; 102 } 103 } else { 104 break; 105 } 106 } 107 } 108 } else if (const auto *dylibFile = dyn_cast<DylibFile>(file)) { 109 isReferencedFile = dylibFile->isReferenced(); 110 } 111 112 if (isReferencedFile) 113 info.files.push_back(file); 114 } 115 116 // cstrings are not stored in sorted order in their OutputSections, so we sort 117 // them here. 118 for (auto &liveCStrings : info.liveCStringsForSection) 119 parallelSort(liveCStrings.second, [](const auto &p1, const auto &p2) { 120 return p1.first < p2.first; 121 }); 122 return info; 123 } 124 125 // We use this instead of `toString(const InputFile *)` as we don't want to 126 // include the dylib install name in our output. 127 static void printFileName(raw_fd_ostream &os, const InputFile *f) { 128 if (f->archiveName.empty()) 129 os << f->getName(); 130 else 131 os << f->archiveName << "(" << path::filename(f->getName()) + ")"; 132 } 133 134 // For printing the contents of the __stubs and __la_symbol_ptr sections. 135 static void printStubsEntries( 136 raw_fd_ostream &os, 137 const DenseMap<lld::macho::InputFile *, uint32_t> &readerToFileOrdinal, 138 const OutputSection *osec, size_t entrySize) { 139 for (const Symbol *sym : in.stubs->getEntries()) 140 os << format("0x%08llX\t0x%08zX\t[%3u] %s\n", 141 osec->addr + sym->stubsIndex * entrySize, entrySize, 142 readerToFileOrdinal.lookup(sym->getFile()), 143 sym->getName().str().data()); 144 } 145 146 static void printNonLazyPointerSection(raw_fd_ostream &os, 147 NonLazyPointerSectionBase *osec) { 148 // ld64 considers stubs to belong to particular files, but considers GOT 149 // entries to be linker-synthesized. Not sure why they made that decision, but 150 // I think we can follow suit unless there's demand for better symbol-to-file 151 // associations. 152 for (const Symbol *sym : osec->getEntries()) 153 os << format("0x%08llX\t0x%08zX\t[ 0] non-lazy-pointer-to-local: %s\n", 154 osec->addr + sym->gotIndex * target->wordSize, 155 target->wordSize, sym->getName().str().data()); 156 } 157 158 void macho::writeMapFile() { 159 if (config->mapFile.empty()) 160 return; 161 162 TimeTraceScope timeScope("Write map file"); 163 164 // Open a map file for writing. 165 std::error_code ec; 166 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None); 167 if (ec) { 168 error("cannot open " + config->mapFile + ": " + ec.message()); 169 return; 170 } 171 172 os << format("# Path: %s\n", config->outputFile.str().c_str()); 173 os << format("# Arch: %s\n", 174 getArchitectureName(config->arch()).str().c_str()); 175 176 MapInfo info = gatherMapInfo(); 177 178 os << "# Object files:\n"; 179 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized"); 180 uint32_t fileIndex = 1; 181 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal; 182 for (InputFile *file : info.files) { 183 os << format("[%3u] ", fileIndex); 184 printFileName(os, file); 185 os << "\n"; 186 readerToFileOrdinal[file] = fileIndex++; 187 } 188 189 os << "# Sections:\n"; 190 os << "# Address\tSize \tSegment\tSection\n"; 191 for (OutputSegment *seg : outputSegments) 192 for (OutputSection *osec : seg->getSections()) { 193 if (osec->isHidden()) 194 continue; 195 196 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(), 197 seg->name.str().c_str(), osec->name.str().c_str()); 198 } 199 200 os << "# Symbols:\n"; 201 os << "# Address\tSize \tFile Name\n"; 202 for (const OutputSegment *seg : outputSegments) { 203 for (const OutputSection *osec : seg->getSections()) { 204 if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) { 205 for (const InputSection *isec : concatOsec->inputs) { 206 for (Defined *sym : isec->symbols) 207 if (!(isPrivateLabel(sym->getName()) && sym->size == 0)) 208 os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(), 209 sym->size, readerToFileOrdinal[sym->getFile()], 210 sym->getName().str().data()); 211 } 212 } else if (osec == in.cStringSection || osec == in.objcMethnameSection) { 213 const auto &liveCStrings = info.liveCStringsForSection.lookup(osec); 214 uint64_t lastAddr = 0; // strings will never start at address 0, so this 215 // is a sentinel value 216 for (const auto &[addr, info] : liveCStrings) { 217 uint64_t size = 0; 218 if (addr != lastAddr) 219 size = info.str.size() + 1; // include null terminator 220 lastAddr = addr; 221 os << format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr, size, 222 info.fileIndex); 223 os.write_escaped(info.str) << "\n"; 224 } 225 } else if (osec == (void *)in.unwindInfo) { 226 os << format("0x%08llX\t0x%08llX\t[ 0] compact unwind info\n", 227 osec->addr, osec->getSize()); 228 } else if (osec == in.stubs) { 229 printStubsEntries(os, readerToFileOrdinal, osec, target->stubSize); 230 } else if (osec == in.lazyPointers) { 231 printStubsEntries(os, readerToFileOrdinal, osec, target->wordSize); 232 } else if (osec == in.stubHelper) { 233 // yes, ld64 calls it "helper helper"... 234 os << format("0x%08llX\t0x%08llX\t[ 0] helper helper\n", osec->addr, 235 osec->getSize()); 236 } else if (osec == in.got) { 237 printNonLazyPointerSection(os, in.got); 238 } else if (osec == in.tlvPointers) { 239 printNonLazyPointerSection(os, in.tlvPointers); 240 } 241 // TODO print other synthetic sections 242 } 243 } 244 245 if (config->deadStrip) { 246 os << "# Dead Stripped Symbols:\n"; 247 os << "# \tSize \tFile Name\n"; 248 for (Defined *sym : info.deadSymbols) { 249 assert(!sym->isLive()); 250 os << format("<<dead>>\t0x%08llX\t[%3u] %s\n", sym->size, 251 readerToFileOrdinal[sym->getFile()], 252 sym->getName().str().data()); 253 } 254 for (CStringInfo &cstrInfo : info.deadCStrings) { 255 os << format("<<dead>>\t0x%08zX\t[%3u] literal string: ", 256 cstrInfo.str.size() + 1, cstrInfo.fileIndex); 257 os.write_escaped(cstrInfo.str) << "\n"; 258 } 259 } 260 } 261