Path: blob/main/contrib/llvm-project/lld/MachO/MapFile.cpp
34869 views
//===- MapFile.cpp --------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the -map option, which maps address ranges to their9// respective contents, plus the input file these contents were originally from.10// The contents (typically symbols) are listed in address order. Dead-stripped11// contents are included as well.12//13// # Path: test14// # Arch: x86_8415// # Object files:16// [ 0] linker synthesized17// [ 1] a.o18// # Sections:19// # Address Size Segment Section20// 0x1000005C0 0x0000004C __TEXT __text21// # Symbols:22// # Address Size File Name23// 0x1000005C0 0x00000001 [ 1] _main24// # Dead Stripped Symbols:25// # Size File Name26// <<dead>> 0x00000001 [ 1] _foo27//28//===----------------------------------------------------------------------===//2930#include "MapFile.h"31#include "ConcatOutputSection.h"32#include "Config.h"33#include "InputFiles.h"34#include "InputSection.h"35#include "OutputSegment.h"36#include "Symbols.h"37#include "SyntheticSections.h"38#include "Target.h"39#include "lld/Common/ErrorHandler.h"40#include "llvm/ADT/DenseMap.h"41#include "llvm/Support/Parallel.h"42#include "llvm/Support/TimeProfiler.h"4344using namespace llvm;45using namespace llvm::sys;46using namespace lld;47using namespace lld::macho;4849struct CStringInfo {50uint32_t fileIndex;51StringRef str;52};5354struct MapInfo {55SmallVector<InputFile *> files;56SmallVector<Defined *> deadSymbols;57DenseMap<const OutputSection *,58SmallVector<std::pair<uint64_t /*addr*/, CStringInfo>>>59liveCStringsForSection;60SmallVector<CStringInfo> deadCStrings;61};6263static MapInfo gatherMapInfo() {64MapInfo info;65for (InputFile *file : inputFiles) {66bool isReferencedFile = false;6768if (isa<ObjFile>(file) || isa<BitcodeFile>(file)) {69uint32_t fileIndex = info.files.size() + 1;7071// Gather the dead symbols. We don't have to bother with the live ones72// because we will pick them up as we iterate over the OutputSections73// later.74for (Symbol *sym : file->symbols) {75if (auto *d = dyn_cast_or_null<Defined>(sym))76// Only emit the prevailing definition of a symbol. Also, don't emit77// the symbol if it is part of a cstring section (we use the literal78// value instead, similar to ld64)79if (d->isec() && d->getFile() == file &&80!isa<CStringInputSection>(d->isec())) {81isReferencedFile = true;82if (!d->isLive())83info.deadSymbols.push_back(d);84}85}8687// Gather all the cstrings (both live and dead). A CString(Output)Section88// doesn't provide us a way of figuring out which InputSections its89// cstring contents came from, so we need to build up that mapping here.90for (const Section *sec : file->sections) {91for (const Subsection &subsec : sec->subsections) {92if (auto isec = dyn_cast<CStringInputSection>(subsec.isec)) {93auto &liveCStrings = info.liveCStringsForSection[isec->parent];94for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {95if (piece.live)96liveCStrings.push_back({isec->parent->addr + piece.outSecOff,97{fileIndex, isec->getStringRef(i)}});98else99info.deadCStrings.push_back({fileIndex, isec->getStringRef(i)});100isReferencedFile = true;101}102} else {103break;104}105}106}107} else if (const auto *dylibFile = dyn_cast<DylibFile>(file)) {108isReferencedFile = dylibFile->isReferenced();109}110111if (isReferencedFile)112info.files.push_back(file);113}114115// cstrings are not stored in sorted order in their OutputSections, so we sort116// them here.117for (auto &liveCStrings : info.liveCStringsForSection)118parallelSort(liveCStrings.second, [](const auto &p1, const auto &p2) {119return p1.first < p2.first;120});121return info;122}123124// We use this instead of `toString(const InputFile *)` as we don't want to125// include the dylib install name in our output.126static void printFileName(raw_fd_ostream &os, const InputFile *f) {127if (f->archiveName.empty())128os << f->getName();129else130os << f->archiveName << "(" << path::filename(f->getName()) + ")";131}132133// For printing the contents of the __stubs and __la_symbol_ptr sections.134static void printStubsEntries(135raw_fd_ostream &os,136const DenseMap<lld::macho::InputFile *, uint32_t> &readerToFileOrdinal,137const OutputSection *osec, size_t entrySize) {138for (const Symbol *sym : in.stubs->getEntries())139os << format("0x%08llX\t0x%08zX\t[%3u] %s\n",140osec->addr + sym->stubsIndex * entrySize, entrySize,141readerToFileOrdinal.lookup(sym->getFile()),142sym->getName().str().data());143}144145static void printNonLazyPointerSection(raw_fd_ostream &os,146NonLazyPointerSectionBase *osec) {147// ld64 considers stubs to belong to particular files, but considers GOT148// entries to be linker-synthesized. Not sure why they made that decision, but149// I think we can follow suit unless there's demand for better symbol-to-file150// associations.151for (const Symbol *sym : osec->getEntries())152os << format("0x%08llX\t0x%08zX\t[ 0] non-lazy-pointer-to-local: %s\n",153osec->addr + sym->gotIndex * target->wordSize,154target->wordSize, sym->getName().str().data());155}156157static uint64_t getSymSizeForMap(Defined *sym) {158if (sym->wasIdenticalCodeFolded)159return 0;160return sym->size;161}162163void macho::writeMapFile() {164if (config->mapFile.empty())165return;166167TimeTraceScope timeScope("Write map file");168169// Open a map file for writing.170std::error_code ec;171raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);172if (ec) {173error("cannot open " + config->mapFile + ": " + ec.message());174return;175}176177os << format("# Path: %s\n", config->outputFile.str().c_str());178os << format("# Arch: %s\n",179getArchitectureName(config->arch()).str().c_str());180181MapInfo info = gatherMapInfo();182183os << "# Object files:\n";184os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");185uint32_t fileIndex = 1;186DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;187for (InputFile *file : info.files) {188os << format("[%3u] ", fileIndex);189printFileName(os, file);190os << "\n";191readerToFileOrdinal[file] = fileIndex++;192}193194os << "# Sections:\n";195os << "# Address\tSize \tSegment\tSection\n";196for (OutputSegment *seg : outputSegments)197for (OutputSection *osec : seg->getSections()) {198if (osec->isHidden())199continue;200201os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),202seg->name.str().c_str(), osec->name.str().c_str());203}204205// Shared function to print an array of symbols.206auto printIsecArrSyms = [&](const std::vector<ConcatInputSection *> &arr) {207for (const ConcatInputSection *isec : arr) {208for (Defined *sym : isec->symbols) {209if (!(isPrivateLabel(sym->getName()) && getSymSizeForMap(sym) == 0))210os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(),211getSymSizeForMap(sym),212readerToFileOrdinal[sym->getFile()],213sym->getName().str().data());214}215}216};217218os << "# Symbols:\n";219os << "# Address\tSize \tFile Name\n";220for (const OutputSegment *seg : outputSegments) {221for (const OutputSection *osec : seg->getSections()) {222if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) {223printIsecArrSyms(concatOsec->inputs);224} else if (osec == in.cStringSection || osec == in.objcMethnameSection) {225const auto &liveCStrings = info.liveCStringsForSection.lookup(osec);226uint64_t lastAddr = 0; // strings will never start at address 0, so this227// is a sentinel value228for (const auto &[addr, info] : liveCStrings) {229uint64_t size = 0;230if (addr != lastAddr)231size = info.str.size() + 1; // include null terminator232lastAddr = addr;233os << format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr, size,234info.fileIndex);235os.write_escaped(info.str) << "\n";236}237} else if (osec == (void *)in.unwindInfo) {238os << format("0x%08llX\t0x%08llX\t[ 0] compact unwind info\n",239osec->addr, osec->getSize());240} else if (osec == in.stubs) {241printStubsEntries(os, readerToFileOrdinal, osec, target->stubSize);242} else if (osec == in.lazyPointers) {243printStubsEntries(os, readerToFileOrdinal, osec, target->wordSize);244} else if (osec == in.stubHelper) {245// yes, ld64 calls it "helper helper"...246os << format("0x%08llX\t0x%08llX\t[ 0] helper helper\n", osec->addr,247osec->getSize());248} else if (osec == in.got) {249printNonLazyPointerSection(os, in.got);250} else if (osec == in.tlvPointers) {251printNonLazyPointerSection(os, in.tlvPointers);252} else if (osec == in.objcMethList) {253printIsecArrSyms(in.objcMethList->getInputs());254}255// TODO print other synthetic sections256}257}258259if (config->deadStrip) {260os << "# Dead Stripped Symbols:\n";261os << "# \tSize \tFile Name\n";262for (Defined *sym : info.deadSymbols) {263assert(!sym->isLive());264os << format("<<dead>>\t0x%08llX\t[%3u] %s\n", getSymSizeForMap(sym),265readerToFileOrdinal[sym->getFile()],266sym->getName().str().data());267}268for (CStringInfo &cstrInfo : info.deadCStrings) {269os << format("<<dead>>\t0x%08zX\t[%3u] literal string: ",270cstrInfo.str.size() + 1, cstrInfo.fileIndex);271os.write_escaped(cstrInfo.str) << "\n";272}273}274}275276277