xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision 5dda2efd)
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 output sections, input sections, input files and
11 // symbol:
12 //
13 //   Address  Size     Align Out     In      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 "MapFile.h"
22 #include "InputFiles.h"
23 #include "LinkerScript.h"
24 #include "OutputSections.h"
25 #include "SymbolTable.h"
26 #include "Symbols.h"
27 #include "SyntheticSections.h"
28 #include "lld/Common/Strings.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/Support/Parallel.h"
32 #include "llvm/Support/TimeProfiler.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 using namespace llvm;
36 using namespace llvm::object;
37 using namespace lld;
38 using namespace lld::elf;
39 
40 using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
41 
42 static constexpr char indent8[] = "        ";          // 8 spaces
43 static constexpr char indent16[] = "                "; // 16 spaces
44 
45 // Print out the first three columns of a line.
46 static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
47                         uint64_t size, uint64_t align) {
48   if (config->is64)
49     os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
50   else
51     os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
52 }
53 
54 // Returns a list of all symbols that we want to print out.
55 static std::vector<Defined *> getSymbols() {
56   std::vector<Defined *> v;
57   for (InputFile *file : objectFiles)
58     for (Symbol *b : file->getSymbols())
59       if (auto *dr = dyn_cast<Defined>(b))
60         if (!dr->isSection() && dr->section && dr->section->isLive() &&
61             (dr->file == file || dr->needsPltAddr || dr->section->bss))
62           v.push_back(dr);
63   return v;
64 }
65 
66 // Returns a map from sections to their symbols.
67 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
68   SymbolMapTy ret;
69   for (Defined *dr : syms)
70     ret[dr->section].push_back(dr);
71 
72   // Sort symbols by address. We want to print out symbols in the
73   // order in the output file rather than the order they appeared
74   // in the input files.
75   for (auto &it : ret)
76     llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
77       return a->getVA() < b->getVA();
78     });
79   return ret;
80 }
81 
82 // Construct a map from symbols to their stringified representations.
83 // Demangling symbols (which is what toString() does) is slow, so
84 // we do that in batch using parallel-for.
85 static DenseMap<Symbol *, std::string>
86 getSymbolStrings(ArrayRef<Defined *> syms) {
87   std::vector<std::string> str(syms.size());
88   parallelForEachN(0, syms.size(), [&](size_t i) {
89     raw_string_ostream os(str[i]);
90     OutputSection *osec = syms[i]->getOutputSection();
91     uint64_t vma = syms[i]->getVA();
92     uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
93     writeHeader(os, vma, lma, syms[i]->getSize(), 1);
94     os << indent16 << toString(*syms[i]);
95   });
96 
97   DenseMap<Symbol *, std::string> ret;
98   for (size_t i = 0, e = syms.size(); i < e; ++i)
99     ret[syms[i]] = std::move(str[i]);
100   return ret;
101 }
102 
103 // Print .eh_frame contents. Since the section consists of EhSectionPieces,
104 // we need a specialized printer for that section.
105 //
106 // .eh_frame tend to contain a lot of section pieces that are contiguous
107 // both in input file and output file. Such pieces are squashed before
108 // being displayed to make output compact.
109 static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
110   std::vector<EhSectionPiece> pieces;
111 
112   auto add = [&](const EhSectionPiece &p) {
113     // If P is adjacent to Last, squash the two.
114     if (!pieces.empty()) {
115       EhSectionPiece &last = pieces.back();
116       if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
117           last.outputOff + last.size == p.outputOff) {
118         last.size += p.size;
119         return;
120       }
121     }
122     pieces.push_back(p);
123   };
124 
125   // Gather section pieces.
126   for (const CieRecord *rec : sec->getCieRecords()) {
127     add(*rec->cie);
128     for (const EhSectionPiece *fde : rec->fdes)
129       add(*fde);
130   }
131 
132   // Print out section pieces.
133   const OutputSection *osec = sec->getOutputSection();
134   for (EhSectionPiece &p : pieces) {
135     writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
136                 p.size, 1);
137     os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
138        << Twine::utohexstr(p.inputOff) + ")\n";
139   }
140 }
141 
142 static void writeMapFile(raw_fd_ostream &os) {
143   // Collect symbol info that we want to print out.
144   std::vector<Defined *> syms = getSymbols();
145   SymbolMapTy sectionSyms = getSectionSyms(syms);
146   DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
147 
148   // Print out the header line.
149   int w = config->is64 ? 16 : 8;
150   os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
151      << "     Size Align Out     In      Symbol\n";
152 
153   OutputSection* osec = nullptr;
154   for (SectionCommand *cmd : script->sectionCommands) {
155     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
156       if (assign->provide && !assign->sym)
157         continue;
158       uint64_t lma = osec ? osec->getLMA() + assign->addr - osec->getVA(0) : 0;
159       writeHeader(os, assign->addr, lma, assign->size, 1);
160       os << assign->commandString << '\n';
161       continue;
162     }
163 
164     osec = cast<OutputSection>(cmd);
165     writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
166     os << osec->name << '\n';
167 
168     // Dump symbols for each input section.
169     for (SectionCommand *subCmd : osec->commands) {
170       if (auto *isd = dyn_cast<InputSectionDescription>(subCmd)) {
171         for (InputSection *isec : isd->sections) {
172           if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
173             printEhFrame(os, ehSec);
174             continue;
175           }
176 
177           writeHeader(os, isec->getVA(), osec->getLMA() + isec->outSecOff,
178                       isec->getSize(), isec->alignment);
179           os << indent8 << toString(isec) << '\n';
180           for (Symbol *sym : sectionSyms[isec])
181             os << symStr[sym] << '\n';
182         }
183         continue;
184       }
185 
186       if (auto *data = dyn_cast<ByteCommand>(subCmd)) {
187         writeHeader(os, osec->addr + data->offset,
188                     osec->getLMA() + data->offset, data->size, 1);
189         os << indent8 << data->commandString << '\n';
190         continue;
191       }
192 
193       if (auto *assign = dyn_cast<SymbolAssignment>(subCmd)) {
194         if (assign->provide && !assign->sym)
195           continue;
196         writeHeader(os, assign->addr,
197                     osec->getLMA() + assign->addr - osec->getVA(0),
198                     assign->size, 1);
199         os << indent8 << assign->commandString << '\n';
200         continue;
201       }
202     }
203   }
204 }
205 
206 void elf::writeWhyExtract() {
207   if (config->whyExtract.empty())
208     return;
209 
210   std::error_code ec;
211   raw_fd_ostream os(config->whyExtract, ec, sys::fs::OF_None);
212   if (ec) {
213     error("cannot open --why-extract= file " + config->whyExtract + ": " +
214           ec.message());
215     return;
216   }
217 
218   os << "reference\textracted\tsymbol\n";
219   for (auto &entry : whyExtract) {
220     os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
221        << toString(std::get<2>(entry)) << '\n';
222   }
223 }
224 
225 // Output a cross reference table to stdout. This is for --cref.
226 //
227 // For each global symbol, we print out a file that defines the symbol
228 // followed by files that uses that symbol. Here is an example.
229 //
230 //     strlen     /lib/x86_64-linux-gnu/libc.so.6
231 //                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
232 //                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
233 //
234 // In this case, strlen is defined by libc.so.6 and used by other two
235 // files.
236 static void writeCref(raw_fd_ostream &os) {
237   // Collect symbols and files.
238   MapVector<Symbol *, SetVector<InputFile *>> map;
239   for (InputFile *file : objectFiles) {
240     for (Symbol *sym : file->getSymbols()) {
241       if (isa<SharedSymbol>(sym))
242         map[sym].insert(file);
243       if (auto *d = dyn_cast<Defined>(sym))
244         if (!d->isLocal() && (!d->section || d->section->isLive()))
245           map[d].insert(file);
246     }
247   }
248 
249   auto print = [&](StringRef a, StringRef b) {
250     os << left_justify(a, 49) << ' ' << b << '\n';
251   };
252 
253   // Print a blank line and a header. The format matches GNU ld.
254   os << "\nCross Reference Table\n\n";
255   print("Symbol", "File");
256 
257   // Print out a table.
258   for (auto kv : map) {
259     Symbol *sym = kv.first;
260     SetVector<InputFile *> &files = kv.second;
261 
262     print(toString(*sym), toString(sym->file));
263     for (InputFile *file : files)
264       if (file != sym->file)
265         print("", toString(file));
266   }
267 }
268 
269 void elf::writeMapAndCref() {
270   if (config->mapFile.empty() && !config->cref)
271     return;
272 
273   llvm::TimeTraceScope timeScope("Write map file");
274 
275   // Open a map file for writing.
276   std::error_code ec;
277   StringRef mapFile = config->mapFile.empty() ? "-" : config->mapFile;
278   raw_fd_ostream os(mapFile, ec, sys::fs::OF_None);
279   if (ec) {
280     error("cannot open " + mapFile + ": " + ec.message());
281     return;
282   }
283 
284   if (!config->mapFile.empty())
285     writeMapFile(os);
286   if (config->cref)
287     writeCref(os);
288 }
289 
290 void elf::writeArchiveStats() {
291   if (config->printArchiveStats.empty())
292     return;
293 
294   std::error_code ec;
295   raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None);
296   if (ec) {
297     error("--print-archive-stats=: cannot open " + config->printArchiveStats +
298           ": " + ec.message());
299     return;
300   }
301 
302   os << "members\textracted\tarchive\n";
303   for (const ArchiveFile *f : archiveFiles)
304     os << f->getMemberCount() << '\t' << f->getExtractedMemberCount() << '\t'
305        << f->getName() << '\n';
306 }
307