annotate lld/MachO/MapFile.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- MapFile.cpp --------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 // This file implements the -map option. It shows lists in order and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // hierarchically the outputFile, arch, input files, output sections and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // symbol:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 // # Path: test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 // # Arch: x86_84
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 // # Object files:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 // [ 0] linker synthesized
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 // [ 1] a.o
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 // # Sections:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 // # Address Size Segment Section
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 // 0x1000005C0 0x0000004C __TEXT __text
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 // # Symbols:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 // # Address File Name
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 // 0x1000005C0 [ 1] _main
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 #include "MapFile.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #include "Config.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 #include "InputFiles.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include "InputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 #include "OutputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 #include "OutputSegment.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 #include "Symbols.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 #include "llvm/Support/Parallel.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 #include "llvm/Support/TimeProfiler.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 using namespace llvm::sys;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 using SymbolMapTy = DenseMap<const InputSection *, SmallVector<Defined *, 4>>;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 // Returns a map from sections to their symbols.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 SymbolMapTy ret;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 for (Defined *dr : syms)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 ret[dr->isec].push_back(dr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 // Sort symbols by address. We want to print out symbols in the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 // order in the output file rather than the order they appeared
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 // in the input files.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 for (auto &it : ret)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 return a->getVA() < b->getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 return ret;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 // Returns a list of all symbols that we want to print out.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 static std::vector<Defined *> getSymbols() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 std::vector<Defined *> v;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 for (InputFile *file : inputFiles)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 if (isa<ObjFile>(file))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 for (Symbol *sym : file->symbols) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 if (auto *d = dyn_cast_or_null<Defined>(sym))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 if (d->isLive() && d->isec && d->getFile() == file) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 assert(!d->isec->isCoalescedWeak() &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 "file->symbols should store resolved symbols");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 v.push_back(d);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 return v;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 // Construct a map from symbols to their stringified representations.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 // Demangling symbols (which is what toString() does) is slow, so
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 // we do that in batch using parallel-for.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 static DenseMap<Symbol *, std::string>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 getSymbolStrings(ArrayRef<Defined *> syms) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 std::vector<std::string> str(syms.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 parallelForEachN(0, syms.size(), [&](size_t i) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 raw_string_ostream os(str[i]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 os << toString(*syms[i]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 DenseMap<Symbol *, std::string> ret;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 for (size_t i = 0, e = syms.size(); i < e; ++i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 ret[syms[i]] = std::move(str[i]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 return ret;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 void macho::writeMapFile() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 if (config->mapFile.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 TimeTraceScope timeScope("Write map file");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 // Open a map file for writing.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 std::error_code ec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 if (ec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 error("cannot open " + config->mapFile + ": " + ec.message());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 // Dump output path.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 os << format("# Path: %s\n", config->outputFile.str().c_str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 // Dump output architecture.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 os << format("# Arch: %s\n",
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 getArchitectureName(config->arch()).str().c_str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 // Dump table of object files.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 os << "# Object files:\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 uint32_t fileIndex = 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 for (InputFile *file : inputFiles) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 if (isa<ObjFile>(file)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 readerToFileOrdinal[file] = fileIndex++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 // Collect symbol info that we want to print out.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 std::vector<Defined *> syms = getSymbols();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 SymbolMapTy sectionSyms = getSectionSyms(syms);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 // Dump table of sections
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 os << "# Sections:\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 os << "# Address\tSize \tSegment\tSection\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 for (OutputSegment *seg : outputSegments)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 for (OutputSection *osec : seg->getSections()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 if (osec->isHidden())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 seg->name.str().c_str(), osec->name.str().c_str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 // Dump table of symbols
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 os << "# Symbols:\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 os << "# Address\t File Name\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 for (InputSection *isec : inputSections) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 auto symsIt = sectionSyms.find(isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 assert(!isec->shouldOmitFromOutput() || (symsIt == sectionSyms.end()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 if (symsIt == sectionSyms.end())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 for (Symbol *sym : symsIt->second) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 os << format("0x%08llX\t[%3u] %s\n", sym->getVA(),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 readerToFileOrdinal[sym->getFile()], symStr[sym].c_str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 // TODO: when we implement -dead_strip, we should dump dead stripped symbols
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 }