Path: blob/main/contrib/llvm-project/lld/COFF/LLDMapFile.cpp
34879 views
//===- LLDMapFile.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 /lldmap option. It shows lists in order and9// hierarchically the output sections, input sections, input files and10// symbol:11//12// Address Size Align Out File Symbol13// 00201000 00000015 4 .text14// 00201000 0000000e 4 test.o:(.text)15// 0020100e 00000000 0 local16// 00201005 00000000 0 f(int)17//18//===----------------------------------------------------------------------===//1920#include "LLDMapFile.h"21#include "COFFLinkerContext.h"22#include "SymbolTable.h"23#include "Symbols.h"24#include "Writer.h"25#include "lld/Common/ErrorHandler.h"26#include "llvm/Support/Parallel.h"27#include "llvm/Support/TimeProfiler.h"28#include "llvm/Support/raw_ostream.h"2930using namespace llvm;31using namespace llvm::object;32using namespace lld;33using namespace lld::coff;3435using SymbolMapTy =36DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>;3738static constexpr char indent8[] = " "; // 8 spaces39static constexpr char indent16[] = " "; // 16 spaces4041// Print out the first three columns of a line.42static void writeHeader(raw_ostream &os, uint64_t addr, uint64_t size,43uint64_t align) {44os << format("%08llx %08llx %5lld ", addr, size, align);45}4647// Returns a list of all symbols that we want to print out.48static std::vector<DefinedRegular *> getSymbols(const COFFLinkerContext &ctx) {49std::vector<DefinedRegular *> v;50for (ObjFile *file : ctx.objFileInstances)51for (Symbol *b : file->getSymbols())52if (auto *sym = dyn_cast_or_null<DefinedRegular>(b))53if (sym && !sym->getCOFFSymbol().isSectionDefinition())54v.push_back(sym);55return v;56}5758// Returns a map from sections to their symbols.59static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> syms) {60SymbolMapTy ret;61for (DefinedRegular *s : syms)62ret[s->getChunk()].push_back(s);6364// Sort symbols by address.65for (auto &it : ret) {66SmallVectorImpl<DefinedRegular *> &v = it.second;67std::stable_sort(v.begin(), v.end(), [](DefinedRegular *a, DefinedRegular *b) {68return a->getRVA() < b->getRVA();69});70}71return ret;72}7374// Construct a map from symbols to their stringified representations.75static DenseMap<DefinedRegular *, std::string>76getSymbolStrings(const COFFLinkerContext &ctx,77ArrayRef<DefinedRegular *> syms) {78std::vector<std::string> str(syms.size());79parallelFor((size_t)0, syms.size(), [&](size_t i) {80raw_string_ostream os(str[i]);81writeHeader(os, syms[i]->getRVA(), 0, 0);82os << indent16 << toString(ctx, *syms[i]);83});8485DenseMap<DefinedRegular *, std::string> ret;86for (size_t i = 0, e = syms.size(); i < e; ++i)87ret[syms[i]] = std::move(str[i]);88return ret;89}9091void lld::coff::writeLLDMapFile(const COFFLinkerContext &ctx) {92if (ctx.config.lldmapFile.empty())93return;9495llvm::TimeTraceScope timeScope(".lldmap file");96std::error_code ec;97raw_fd_ostream os(ctx.config.lldmapFile, ec, sys::fs::OF_None);98if (ec)99fatal("cannot open " + ctx.config.lldmapFile + ": " + ec.message());100101// Collect symbol info that we want to print out.102std::vector<DefinedRegular *> syms = getSymbols(ctx);103SymbolMapTy sectionSyms = getSectionSyms(syms);104DenseMap<DefinedRegular *, std::string> symStr = getSymbolStrings(ctx, syms);105106// Print out the header line.107os << "Address Size Align Out In Symbol\n";108109// Print out file contents.110for (OutputSection *sec : ctx.outputSections) {111writeHeader(os, sec->getRVA(), sec->getVirtualSize(), /*align=*/pageSize);112os << sec->name << '\n';113114for (Chunk *c : sec->chunks) {115auto *sc = dyn_cast<SectionChunk>(c);116if (!sc)117continue;118119writeHeader(os, sc->getRVA(), sc->getSize(), sc->getAlignment());120os << indent8 << sc->file->getName() << ":(" << sc->getSectionName()121<< ")\n";122for (DefinedRegular *sym : sectionSyms[sc])123os << symStr[sym] << '\n';124}125}126}127128129