annotate lld/COFF/MapFile.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 0572611fdcc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- MapFile.cpp --------------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements the /lldmap option. It shows lists in order and
anatofuz
parents:
diff changeset
10 // hierarchically the output sections, input sections, input files and
anatofuz
parents:
diff changeset
11 // symbol:
anatofuz
parents:
diff changeset
12 //
anatofuz
parents:
diff changeset
13 // Address Size Align Out File Symbol
anatofuz
parents:
diff changeset
14 // 00201000 00000015 4 .text
anatofuz
parents:
diff changeset
15 // 00201000 0000000e 4 test.o:(.text)
anatofuz
parents:
diff changeset
16 // 0020100e 00000000 0 local
anatofuz
parents:
diff changeset
17 // 00201005 00000000 0 f(int)
anatofuz
parents:
diff changeset
18 //
anatofuz
parents:
diff changeset
19 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 #include "MapFile.h"
anatofuz
parents:
diff changeset
22 #include "SymbolTable.h"
anatofuz
parents:
diff changeset
23 #include "Symbols.h"
anatofuz
parents:
diff changeset
24 #include "Writer.h"
anatofuz
parents:
diff changeset
25 #include "lld/Common/ErrorHandler.h"
anatofuz
parents:
diff changeset
26 #include "lld/Common/Threads.h"
anatofuz
parents:
diff changeset
27 #include "llvm/Support/raw_ostream.h"
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 using namespace llvm;
anatofuz
parents:
diff changeset
30 using namespace llvm::object;
anatofuz
parents:
diff changeset
31
anatofuz
parents:
diff changeset
32 namespace lld {
anatofuz
parents:
diff changeset
33 namespace coff {
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 using SymbolMapTy =
anatofuz
parents:
diff changeset
36 DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>;
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 static constexpr char indent8[] = " "; // 8 spaces
anatofuz
parents:
diff changeset
39 static constexpr char indent16[] = " "; // 16 spaces
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 // Print out the first three columns of a line.
anatofuz
parents:
diff changeset
42 static void writeHeader(raw_ostream &os, uint64_t addr, uint64_t size,
anatofuz
parents:
diff changeset
43 uint64_t align) {
anatofuz
parents:
diff changeset
44 os << format("%08llx %08llx %5lld ", addr, size, align);
anatofuz
parents:
diff changeset
45 }
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 // Returns a list of all symbols that we want to print out.
anatofuz
parents:
diff changeset
48 static std::vector<DefinedRegular *> getSymbols() {
anatofuz
parents:
diff changeset
49 std::vector<DefinedRegular *> v;
anatofuz
parents:
diff changeset
50 for (ObjFile *file : ObjFile::instances)
anatofuz
parents:
diff changeset
51 for (Symbol *b : file->getSymbols())
anatofuz
parents:
diff changeset
52 if (auto *sym = dyn_cast_or_null<DefinedRegular>(b))
anatofuz
parents:
diff changeset
53 if (sym && !sym->getCOFFSymbol().isSectionDefinition())
anatofuz
parents:
diff changeset
54 v.push_back(sym);
anatofuz
parents:
diff changeset
55 return v;
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 // Returns a map from sections to their symbols.
anatofuz
parents:
diff changeset
59 static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> syms) {
anatofuz
parents:
diff changeset
60 SymbolMapTy ret;
anatofuz
parents:
diff changeset
61 for (DefinedRegular *s : syms)
anatofuz
parents:
diff changeset
62 ret[s->getChunk()].push_back(s);
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 // Sort symbols by address.
anatofuz
parents:
diff changeset
65 for (auto &it : ret) {
anatofuz
parents:
diff changeset
66 SmallVectorImpl<DefinedRegular *> &v = it.second;
anatofuz
parents:
diff changeset
67 std::sort(v.begin(), v.end(), [](DefinedRegular *a, DefinedRegular *b) {
anatofuz
parents:
diff changeset
68 return a->getRVA() < b->getRVA();
anatofuz
parents:
diff changeset
69 });
anatofuz
parents:
diff changeset
70 }
anatofuz
parents:
diff changeset
71 return ret;
anatofuz
parents:
diff changeset
72 }
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 // Construct a map from symbols to their stringified representations.
anatofuz
parents:
diff changeset
75 static DenseMap<DefinedRegular *, std::string>
anatofuz
parents:
diff changeset
76 getSymbolStrings(ArrayRef<DefinedRegular *> syms) {
anatofuz
parents:
diff changeset
77 std::vector<std::string> str(syms.size());
anatofuz
parents:
diff changeset
78 parallelForEachN((size_t)0, syms.size(), [&](size_t i) {
anatofuz
parents:
diff changeset
79 raw_string_ostream os(str[i]);
anatofuz
parents:
diff changeset
80 writeHeader(os, syms[i]->getRVA(), 0, 0);
anatofuz
parents:
diff changeset
81 os << indent16 << toString(*syms[i]);
anatofuz
parents:
diff changeset
82 });
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 DenseMap<DefinedRegular *, std::string> ret;
anatofuz
parents:
diff changeset
85 for (size_t i = 0, e = syms.size(); i < e; ++i)
anatofuz
parents:
diff changeset
86 ret[syms[i]] = std::move(str[i]);
anatofuz
parents:
diff changeset
87 return ret;
anatofuz
parents:
diff changeset
88 }
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 void writeMapFile(ArrayRef<OutputSection *> outputSections) {
anatofuz
parents:
diff changeset
91 if (config->mapFile.empty())
anatofuz
parents:
diff changeset
92 return;
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 std::error_code ec;
anatofuz
parents:
diff changeset
95 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
anatofuz
parents:
diff changeset
96 if (ec)
anatofuz
parents:
diff changeset
97 fatal("cannot open " + config->mapFile + ": " + ec.message());
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 // Collect symbol info that we want to print out.
anatofuz
parents:
diff changeset
100 std::vector<DefinedRegular *> syms = getSymbols();
anatofuz
parents:
diff changeset
101 SymbolMapTy sectionSyms = getSectionSyms(syms);
anatofuz
parents:
diff changeset
102 DenseMap<DefinedRegular *, std::string> symStr = getSymbolStrings(syms);
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 // Print out the header line.
anatofuz
parents:
diff changeset
105 os << "Address Size Align Out In Symbol\n";
anatofuz
parents:
diff changeset
106
anatofuz
parents:
diff changeset
107 // Print out file contents.
anatofuz
parents:
diff changeset
108 for (OutputSection *sec : outputSections) {
anatofuz
parents:
diff changeset
109 writeHeader(os, sec->getRVA(), sec->getVirtualSize(), /*align=*/pageSize);
anatofuz
parents:
diff changeset
110 os << sec->name << '\n';
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 for (Chunk *c : sec->chunks) {
anatofuz
parents:
diff changeset
113 auto *sc = dyn_cast<SectionChunk>(c);
anatofuz
parents:
diff changeset
114 if (!sc)
anatofuz
parents:
diff changeset
115 continue;
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 writeHeader(os, sc->getRVA(), sc->getSize(), sc->getAlignment());
anatofuz
parents:
diff changeset
118 os << indent8 << sc->file->getName() << ":(" << sc->getSectionName()
anatofuz
parents:
diff changeset
119 << ")\n";
anatofuz
parents:
diff changeset
120 for (DefinedRegular *sym : sectionSyms[sc])
anatofuz
parents:
diff changeset
121 os << symStr[sym] << '\n';
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123 }
anatofuz
parents:
diff changeset
124 }
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 } // namespace coff
anatofuz
parents:
diff changeset
127 } // namespace lld