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