xref: /llvm-project-15.0.7/lld/MachO/MapFile.cpp (revision ff52ef33)
1 //===- MapFile.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 -map option. It shows lists in order and
10 // hierarchically the outputFile, arch, input files, output sections and
11 // symbol:
12 //
13 // # Path: test
14 // # Arch: x86_84
15 // # Object files:
16 // [  0] linker synthesized
17 // [  1] a.o
18 // # Sections:
19 // # Address  Size      Segment  Section
20 // 0x1000005C0  0x0000004C  __TEXT  __text
21 // # Symbols:
22 // # Address  File  Name
23 // 0x1000005C0  [  1] _main
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "MapFile.h"
28 #include "Config.h"
29 #include "InputFiles.h"
30 #include "InputSection.h"
31 #include "OutputSection.h"
32 #include "OutputSegment.h"
33 #include "Symbols.h"
34 #include "Target.h"
35 #include "llvm/Support/Parallel.h"
36 #include "llvm/Support/TimeProfiler.h"
37 
38 using namespace llvm;
39 using namespace llvm::sys;
40 using namespace lld;
41 using namespace lld::macho;
42 
43 // Returns a list of all symbols that we want to print out.
44 static std::vector<Defined *> getSymbols() {
45   std::vector<Defined *> v;
46   for (InputFile *file : inputFiles)
47     if (isa<ObjFile>(file))
48       for (Symbol *sym : file->symbols)
49         if (auto *d = dyn_cast_or_null<Defined>(sym))
50           if (d->isLive() && d->isec && d->getFile() == file) {
51             assert(!shouldOmitFromOutput(d->isec));
52             v.push_back(d);
53           }
54   return v;
55 }
56 
57 // Construct a map from symbols to their stringified representations.
58 // Demangling symbols (which is what toString() does) is slow, so
59 // we do that in batch using parallel-for.
60 static DenseMap<Symbol *, std::string>
61 getSymbolStrings(ArrayRef<Defined *> syms) {
62   std::vector<std::string> str(syms.size());
63   parallelForEachN(0, syms.size(), [&](size_t i) {
64     raw_string_ostream os(str[i]);
65     os << toString(*syms[i]);
66   });
67 
68   DenseMap<Symbol *, std::string> ret;
69   for (size_t i = 0, e = syms.size(); i < e; ++i)
70     ret[syms[i]] = std::move(str[i]);
71   return ret;
72 }
73 
74 void macho::writeMapFile() {
75   if (config->mapFile.empty())
76     return;
77 
78   TimeTraceScope timeScope("Write map file");
79 
80   // Open a map file for writing.
81   std::error_code ec;
82   raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
83   if (ec) {
84     error("cannot open " + config->mapFile + ": " + ec.message());
85     return;
86   }
87 
88   // Dump output path.
89   os << format("# Path: %s\n", config->outputFile.str().c_str());
90 
91   // Dump output architecture.
92   os << format("# Arch: %s\n",
93                getArchitectureName(config->arch()).str().c_str());
94 
95   // Dump table of object files.
96   os << "# Object files:\n";
97   os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
98   uint32_t fileIndex = 1;
99   DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
100   for (InputFile *file : inputFiles) {
101     if (isa<ObjFile>(file)) {
102       os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str());
103       readerToFileOrdinal[file] = fileIndex++;
104     }
105   }
106 
107   // Collect symbol info that we want to print out.
108   std::vector<Defined *> syms = getSymbols();
109   parallelSort(syms.begin(), syms.end(), [](Defined *a, Defined *b) {
110     return a->getVA() != b->getVA() ? a->getVA() < b->getVA()
111                                     : a->getName() < b->getName();
112   });
113   DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
114 
115   // Dump table of sections
116   os << "# Sections:\n";
117   os << "# Address\tSize    \tSegment\tSection\n";
118   for (OutputSegment *seg : outputSegments)
119     for (OutputSection *osec : seg->getSections()) {
120       if (osec->isHidden())
121         continue;
122 
123       os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
124                    seg->name.str().c_str(), osec->name.str().c_str());
125     }
126 
127   // Dump table of symbols
128   os << "# Symbols:\n";
129   os << "# Address\t    File  Name\n";
130   for (Symbol *sym : syms) {
131     os << format("0x%08llX\t[%3u] %s\n", sym->getVA(),
132                  readerToFileOrdinal[sym->getFile()], symStr[sym].c_str());
133   }
134 
135   // TODO: when we implement -dead_strip, we should dump dead stripped symbols
136 }
137