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