xref: /freebsd/contrib/llvm-project/lld/COFF/LLDMapFile.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/raw_ostream.h"
29 
30 using namespace llvm;
31 using namespace llvm::object;
32 using namespace lld;
33 using namespace lld::coff;
34 
35 using SymbolMapTy =
36     DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>;
37 
38 static constexpr char indent8[] = "        ";          // 8 spaces
39 static constexpr char indent16[] = "                "; // 16 spaces
40 
41 // Print out the first three columns of a line.
42 static void writeHeader(raw_ostream &os, uint64_t addr, uint64_t size,
43                         uint64_t align) {
44   os << format("%08llx %08llx %5lld ", addr, size, align);
45 }
46 
47 // Returns a list of all symbols that we want to print out.
48 static std::vector<DefinedRegular *> getSymbols(const COFFLinkerContext &ctx) {
49   std::vector<DefinedRegular *> v;
50   for (ObjFile *file : ctx.objFileInstances)
51     for (Symbol *b : file->getSymbols())
52       if (auto *sym = dyn_cast_or_null<DefinedRegular>(b))
53         if (sym && !sym->getCOFFSymbol().isSectionDefinition())
54           v.push_back(sym);
55   return v;
56 }
57 
58 // Returns a map from sections to their symbols.
59 static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> syms) {
60   SymbolMapTy ret;
61   for (DefinedRegular *s : syms)
62     ret[s->getChunk()].push_back(s);
63 
64   // Sort symbols by address.
65   for (auto &it : ret) {
66     SmallVectorImpl<DefinedRegular *> &v = it.second;
67     std::stable_sort(v.begin(), v.end(), [](DefinedRegular *a, DefinedRegular *b) {
68       return a->getRVA() < b->getRVA();
69     });
70   }
71   return ret;
72 }
73 
74 // Construct a map from symbols to their stringified representations.
75 static DenseMap<DefinedRegular *, std::string>
76 getSymbolStrings(const COFFLinkerContext &ctx,
77                  ArrayRef<DefinedRegular *> syms) {
78   std::vector<std::string> str(syms.size());
79   parallelFor((size_t)0, syms.size(), [&](size_t i) {
80     raw_string_ostream os(str[i]);
81     writeHeader(os, syms[i]->getRVA(), 0, 0);
82     os << indent16 << toString(ctx, *syms[i]);
83   });
84 
85   DenseMap<DefinedRegular *, std::string> ret;
86   for (size_t i = 0, e = syms.size(); i < e; ++i)
87     ret[syms[i]] = std::move(str[i]);
88   return ret;
89 }
90 
91 void lld::coff::writeLLDMapFile(const COFFLinkerContext &ctx) {
92   if (ctx.config.lldmapFile.empty())
93     return;
94 
95   std::error_code ec;
96   raw_fd_ostream os(ctx.config.lldmapFile, ec, sys::fs::OF_None);
97   if (ec)
98     fatal("cannot open " + ctx.config.lldmapFile + ": " + ec.message());
99 
100   // Collect symbol info that we want to print out.
101   std::vector<DefinedRegular *> syms = getSymbols(ctx);
102   SymbolMapTy sectionSyms = getSectionSyms(syms);
103   DenseMap<DefinedRegular *, std::string> symStr = getSymbolStrings(ctx, syms);
104 
105   // Print out the header line.
106   os << "Address  Size     Align Out     In      Symbol\n";
107 
108   // Print out file contents.
109   for (OutputSection *sec : ctx.outputSections) {
110     writeHeader(os, sec->getRVA(), sec->getVirtualSize(), /*align=*/pageSize);
111     os << sec->name << '\n';
112 
113     for (Chunk *c : sec->chunks) {
114       auto *sc = dyn_cast<SectionChunk>(c);
115       if (!sc)
116         continue;
117 
118       writeHeader(os, sc->getRVA(), sc->getSize(), sc->getAlignment());
119       os << indent8 << sc->file->getName() << ":(" << sc->getSectionName()
120          << ")\n";
121       for (DefinedRegular *sym : sectionSyms[sc])
122         os << symStr[sym] << '\n';
123     }
124   }
125 }
126