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 // For printing the contents of the __stubs and __la_symbol_ptr sections. 126 void printStubsEntries( 127 raw_fd_ostream &os, 128 const DenseMap<lld::macho::InputFile *, uint32_t> &readerToFileOrdinal, 129 const OutputSection *osec, size_t entrySize) { 130 for (const Symbol *sym : in.stubs->getEntries()) 131 os << format("0x%08llX\t0x%08zX\t[%3u] %s\n", 132 osec->addr + sym->stubsIndex * entrySize, entrySize, 133 readerToFileOrdinal.lookup(sym->getFile()), 134 sym->getName().str().data()); 135 } 136 137 void printNonLazyPointerSection(raw_fd_ostream &os, 138 NonLazyPointerSectionBase *osec) { 139 // ld64 considers stubs to belong to particular files, but considers GOT 140 // entries to be linker-synthesized. Not sure why they made that decision, but 141 // I think we can follow suit unless there's demand for better symbol-to-file 142 // associations. 143 for (const Symbol *sym : osec->getEntries()) 144 os << format("0x%08llX\t0x%08zX\t[ 0] non-lazy-pointer-to-local: %s\n", 145 osec->addr + sym->gotIndex * target->wordSize, 146 target->wordSize, sym->getName().str().data()); 147 } 148 149 void macho::writeMapFile() { 150 if (config->mapFile.empty()) 151 return; 152 153 TimeTraceScope timeScope("Write map file"); 154 155 // Open a map file for writing. 156 std::error_code ec; 157 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None); 158 if (ec) { 159 error("cannot open " + config->mapFile + ": " + ec.message()); 160 return; 161 } 162 163 os << format("# Path: %s\n", config->outputFile.str().c_str()); 164 os << format("# Arch: %s\n", 165 getArchitectureName(config->arch()).str().c_str()); 166 167 MapInfo info = gatherMapInfo(); 168 169 os << "# Object files:\n"; 170 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized"); 171 uint32_t fileIndex = 1; 172 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal; 173 for (InputFile *file : info.files) { 174 os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str()); 175 readerToFileOrdinal[file] = fileIndex++; 176 } 177 178 os << "# Sections:\n"; 179 os << "# Address\tSize \tSegment\tSection\n"; 180 for (OutputSegment *seg : outputSegments) 181 for (OutputSection *osec : seg->getSections()) { 182 if (osec->isHidden()) 183 continue; 184 185 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(), 186 seg->name.str().c_str(), osec->name.str().c_str()); 187 } 188 189 os << "# Symbols:\n"; 190 os << "# Address\tSize \tFile Name\n"; 191 for (const OutputSegment *seg : outputSegments) { 192 for (const OutputSection *osec : seg->getSections()) { 193 if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) { 194 for (const InputSection *isec : concatOsec->inputs) { 195 for (Defined *sym : isec->symbols) 196 os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(), 197 sym->size, readerToFileOrdinal[sym->getFile()], 198 sym->getName().str().data()); 199 } 200 } else if (osec == in.cStringSection || osec == in.objcMethnameSection) { 201 const auto &liveCStrings = info.liveCStringsForSection.lookup(osec); 202 uint64_t lastAddr = 0; // strings will never start at address 0, so this 203 // is a sentinel value 204 for (const auto &[addr, info] : liveCStrings) { 205 uint64_t size = 0; 206 if (addr != lastAddr) 207 size = info.str.size() + 1; // include null terminator 208 lastAddr = addr; 209 os << format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr, size, 210 info.fileIndex); 211 os.write_escaped(info.str) << "\n"; 212 } 213 } else if (osec == (void *)in.unwindInfo) { 214 os << format("0x%08llX\t0x%08llX\t[ 0] compact unwind info\n", 215 osec->addr, osec->getSize()); 216 } else if (osec == in.stubs) { 217 printStubsEntries(os, readerToFileOrdinal, osec, target->stubSize); 218 } else if (osec == in.lazyPointers) { 219 printStubsEntries(os, readerToFileOrdinal, osec, target->wordSize); 220 } else if (osec == in.stubHelper) { 221 // yes, ld64 calls it "helper helper"... 222 os << format("0x%08llX\t0x%08llX\t[ 0] helper helper\n", osec->addr, 223 osec->getSize()); 224 } else if (osec == in.got) { 225 printNonLazyPointerSection(os, in.got); 226 } else if (osec == in.tlvPointers) { 227 printNonLazyPointerSection(os, in.tlvPointers); 228 } 229 // TODO print other synthetic sections 230 } 231 } 232 233 if (config->deadStrip) { 234 os << "# Dead Stripped Symbols:\n"; 235 os << "# \tSize \tFile Name\n"; 236 for (Defined *sym : info.deadSymbols) { 237 assert(!sym->isLive()); 238 os << format("<<dead>>\t0x%08llX\t[%3u] %s\n", sym->size, 239 readerToFileOrdinal[sym->getFile()], 240 sym->getName().str().data()); 241 } 242 for (CStringInfo &cstrInfo : info.deadCStrings) { 243 os << format("<<dead>>\t0x%08zX\t[%3u] literal string: ", 244 cstrInfo.str.size() + 1, cstrInfo.fileIndex); 245 os.write_escaped(cstrInfo.str) << "\n"; 246 } 247 } 248 } 249