xref: /llvm-project-15.0.7/lld/MachO/MapFile.cpp (revision 4f7fa06a)
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 
37 using namespace llvm;
38 using namespace llvm::sys;
39 using namespace lld;
40 using namespace lld::macho;
41 
42 using SymbolMapTy = DenseMap<const InputSection *, SmallVector<Defined *, 4>>;
43 
44 // Returns a map from sections to their symbols.
45 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
46   SymbolMapTy ret;
47   for (Defined *dr : syms)
48     ret[dr->isec].push_back(dr);
49 
50   // Sort symbols by address. We want to print out symbols in the
51   // order in the output file rather than the order they appeared
52   // in the input files.
53   for (auto &it : ret)
54     llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
55       return a->getVA() < b->getVA();
56     });
57   return ret;
58 }
59 
60 // Returns a list of all symbols that we want to print out.
61 static std::vector<Defined *> getSymbols() {
62   std::vector<Defined *> v;
63   for (InputFile *file : inputFiles)
64     if (isa<ObjFile>(file))
65       for (Symbol *sym : file->symbols) {
66         if (sym == nullptr)
67           continue;
68         if (auto *d = dyn_cast<Defined>(sym))
69           if (d->isec && d->getFile() == file)
70             v.push_back(d);
71       }
72   return v;
73 }
74 
75 // Construct a map from symbols to their stringified representations.
76 // Demangling symbols (which is what toString() does) is slow, so
77 // we do that in batch using parallel-for.
78 static DenseMap<macho::Symbol *, std::string>
79 getSymbolStrings(ArrayRef<Defined *> syms) {
80   std::vector<std::string> str(syms.size());
81   parallelForEachN(0, syms.size(), [&](size_t i) {
82     raw_string_ostream os(str[i]);
83     os << toString(*syms[i]);
84   });
85 
86   DenseMap<macho::Symbol *, std::string> ret;
87   for (size_t i = 0, e = syms.size(); i < e; ++i)
88     ret[syms[i]] = std::move(str[i]);
89   return ret;
90 }
91 
92 void macho::writeMapFile() {
93   if (config->mapFile.empty())
94     return;
95 
96   // Open a map file for writing.
97   std::error_code ec;
98   raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
99   if (ec) {
100     error("cannot open " + config->mapFile + ": " + ec.message());
101     return;
102   }
103 
104   // Dump output path
105   os << format("# Path: %s\n", config->outputFile.str().c_str());
106 
107   // Dump output architecure
108   os << format("# Arch: %s\n",
109                getArchitectureName(config->target.Arch).str().c_str());
110 
111   // Dump table of object files
112   os << "# Object files:\n";
113   os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
114   uint32_t fileIndex = 1;
115   DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
116   for (InputFile *file : inputFiles) {
117     if (isa<ObjFile>(file)) {
118       os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str());
119       readerToFileOrdinal[file] = fileIndex++;
120     }
121   }
122 
123   // Collect symbol info that we want to print out.
124   std::vector<Defined *> syms = getSymbols();
125   SymbolMapTy sectionSyms = getSectionSyms(syms);
126   DenseMap<lld::macho::Symbol *, std::string> symStr = getSymbolStrings(syms);
127 
128   // Dump table of sections
129   os << "# Sections:\n";
130   os << "# Address\tSize    \tSegment\tSection\n";
131   for (OutputSegment *seg : outputSegments)
132     for (OutputSection *osec : seg->getSections()) {
133       if (osec->isHidden())
134         continue;
135 
136       os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
137                    seg->name.str().c_str(), osec->name.str().c_str());
138     }
139 
140   // Dump table of symbols
141   os << "# Symbols:\n";
142   os << "# Address\t    File  Name\n";
143   for (InputSection *isec : inputSections) {
144     for (macho::Symbol *sym : sectionSyms[isec]) {
145       os << format("0x%08llX\t[%3u] %s\n", sym->getVA(),
146                    readerToFileOrdinal[sym->getFile()], symStr[sym].c_str());
147     }
148   }
149 
150   // TODO: when we implement -dead_strip, we should dump dead stripped symbols
151 }
152