llvm-project
127 строк · 4.3 Кб
1//===- LLDMapFile.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 /lldmap option. It shows lists in order and
10// hierarchically the output sections, input sections, input files and
11// symbol:
12//
13// Address Size Align Out File Symbol
14// 00201000 00000015 4 .text
15// 00201000 0000000e 4 test.o:(.text)
16// 0020100e 00000000 0 local
17// 00201005 00000000 0 f(int)
18//
19//===----------------------------------------------------------------------===//
20
21#include "LLDMapFile.h"22#include "COFFLinkerContext.h"23#include "SymbolTable.h"24#include "Symbols.h"25#include "Writer.h"26#include "lld/Common/ErrorHandler.h"27#include "llvm/Support/Parallel.h"28#include "llvm/Support/TimeProfiler.h"29#include "llvm/Support/raw_ostream.h"30
31using namespace llvm;32using namespace llvm::object;33using namespace lld;34using namespace lld::coff;35
36using SymbolMapTy =37DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>;38
39static constexpr char indent8[] = " "; // 8 spaces40static constexpr char indent16[] = " "; // 16 spaces41
42// Print out the first three columns of a line.
43static void writeHeader(raw_ostream &os, uint64_t addr, uint64_t size,44uint64_t align) {45os << format("%08llx %08llx %5lld ", addr, size, align);46}
47
48// Returns a list of all symbols that we want to print out.
49static std::vector<DefinedRegular *> getSymbols(const COFFLinkerContext &ctx) {50std::vector<DefinedRegular *> v;51for (ObjFile *file : ctx.objFileInstances)52for (Symbol *b : file->getSymbols())53if (auto *sym = dyn_cast_or_null<DefinedRegular>(b))54if (sym && !sym->getCOFFSymbol().isSectionDefinition())55v.push_back(sym);56return v;57}
58
59// Returns a map from sections to their symbols.
60static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> syms) {61SymbolMapTy ret;62for (DefinedRegular *s : syms)63ret[s->getChunk()].push_back(s);64
65// Sort symbols by address.66for (auto &it : ret) {67SmallVectorImpl<DefinedRegular *> &v = it.second;68std::stable_sort(v.begin(), v.end(), [](DefinedRegular *a, DefinedRegular *b) {69return a->getRVA() < b->getRVA();70});71}72return ret;73}
74
75// Construct a map from symbols to their stringified representations.
76static DenseMap<DefinedRegular *, std::string>77getSymbolStrings(const COFFLinkerContext &ctx,78ArrayRef<DefinedRegular *> syms) {79std::vector<std::string> str(syms.size());80parallelFor((size_t)0, syms.size(), [&](size_t i) {81raw_string_ostream os(str[i]);82writeHeader(os, syms[i]->getRVA(), 0, 0);83os << indent16 << toString(ctx, *syms[i]);84});85
86DenseMap<DefinedRegular *, std::string> ret;87for (size_t i = 0, e = syms.size(); i < e; ++i)88ret[syms[i]] = std::move(str[i]);89return ret;90}
91
92void lld::coff::writeLLDMapFile(const COFFLinkerContext &ctx) {93if (ctx.config.lldmapFile.empty())94return;95
96llvm::TimeTraceScope timeScope(".lldmap file");97std::error_code ec;98raw_fd_ostream os(ctx.config.lldmapFile, ec, sys::fs::OF_None);99if (ec)100fatal("cannot open " + ctx.config.lldmapFile + ": " + ec.message());101
102// Collect symbol info that we want to print out.103std::vector<DefinedRegular *> syms = getSymbols(ctx);104SymbolMapTy sectionSyms = getSectionSyms(syms);105DenseMap<DefinedRegular *, std::string> symStr = getSymbolStrings(ctx, syms);106
107// Print out the header line.108os << "Address Size Align Out In Symbol\n";109
110// Print out file contents.111for (OutputSection *sec : ctx.outputSections) {112writeHeader(os, sec->getRVA(), sec->getVirtualSize(), /*align=*/pageSize);113os << sec->name << '\n';114
115for (Chunk *c : sec->chunks) {116auto *sc = dyn_cast<SectionChunk>(c);117if (!sc)118continue;119
120writeHeader(os, sc->getRVA(), sc->getSize(), sc->getAlignment());121os << indent8 << sc->file->getName() << ":(" << sc->getSectionName()122<< ")\n";123for (DefinedRegular *sym : sectionSyms[sc])124os << symStr[sym] << '\n';125}126}127}
128