1*0b57cec5SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements the -Map option. It shows lists in order and
10*0b57cec5SDimitry Andric // hierarchically the output sections, input sections, input files and
11*0b57cec5SDimitry Andric // symbol:
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric //   Address  Size     Align Out     In      Symbol
14*0b57cec5SDimitry Andric //   00201000 00000015     4 .text
15*0b57cec5SDimitry Andric //   00201000 0000000e     4         test.o:(.text)
16*0b57cec5SDimitry Andric //   0020100e 00000000     0                 local
17*0b57cec5SDimitry Andric //   00201005 00000000     0                 f(int)
18*0b57cec5SDimitry Andric //
19*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric #include "MapFile.h"
22*0b57cec5SDimitry Andric #include "InputFiles.h"
23*0b57cec5SDimitry Andric #include "LinkerScript.h"
24*0b57cec5SDimitry Andric #include "OutputSections.h"
25*0b57cec5SDimitry Andric #include "SymbolTable.h"
26*0b57cec5SDimitry Andric #include "Symbols.h"
27*0b57cec5SDimitry Andric #include "SyntheticSections.h"
28*0b57cec5SDimitry Andric #include "lld/Common/Strings.h"
29*0b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
30*0b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
315ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
32e8d8bef9SDimitry Andric #include "llvm/Support/TimeProfiler.h"
33*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric using namespace llvm;
36*0b57cec5SDimitry Andric using namespace llvm::object;
375ffd83dbSDimitry Andric using namespace lld;
385ffd83dbSDimitry Andric using namespace lld::elf;
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
41*0b57cec5SDimitry Andric 
4285868e8aSDimitry Andric static constexpr char indent8[] = "        ";          // 8 spaces
4385868e8aSDimitry Andric static constexpr char indent16[] = "                "; // 16 spaces
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric // Print out the first three columns of a line.
46*0b57cec5SDimitry Andric static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
47*0b57cec5SDimitry Andric                         uint64_t size, uint64_t align) {
48*0b57cec5SDimitry Andric   if (config->is64)
49*0b57cec5SDimitry Andric     os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
50*0b57cec5SDimitry Andric   else
51*0b57cec5SDimitry Andric     os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
52*0b57cec5SDimitry Andric }
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric // Returns a list of all symbols that we want to print out.
55*0b57cec5SDimitry Andric static std::vector<Defined *> getSymbols() {
56*0b57cec5SDimitry Andric   std::vector<Defined *> v;
57*0b57cec5SDimitry Andric   for (InputFile *file : objectFiles)
58*0b57cec5SDimitry Andric     for (Symbol *b : file->getSymbols())
59*0b57cec5SDimitry Andric       if (auto *dr = dyn_cast<Defined>(b))
60*0b57cec5SDimitry Andric         if (!dr->isSection() && dr->section && dr->section->isLive() &&
61*0b57cec5SDimitry Andric             (dr->file == file || dr->needsPltAddr || dr->section->bss))
62*0b57cec5SDimitry Andric           v.push_back(dr);
63*0b57cec5SDimitry Andric   return v;
64*0b57cec5SDimitry Andric }
65*0b57cec5SDimitry Andric 
66*0b57cec5SDimitry Andric // Returns a map from sections to their symbols.
67*0b57cec5SDimitry Andric static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
68*0b57cec5SDimitry Andric   SymbolMapTy ret;
69*0b57cec5SDimitry Andric   for (Defined *dr : syms)
70*0b57cec5SDimitry Andric     ret[dr->section].push_back(dr);
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric   // Sort symbols by address. We want to print out symbols in the
73*0b57cec5SDimitry Andric   // order in the output file rather than the order they appeared
74*0b57cec5SDimitry Andric   // in the input files.
75*0b57cec5SDimitry Andric   for (auto &it : ret)
76*0b57cec5SDimitry Andric     llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
77*0b57cec5SDimitry Andric       return a->getVA() < b->getVA();
78*0b57cec5SDimitry Andric     });
79*0b57cec5SDimitry Andric   return ret;
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric // Construct a map from symbols to their stringified representations.
83*0b57cec5SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so
84*0b57cec5SDimitry Andric // we do that in batch using parallel-for.
85*0b57cec5SDimitry Andric static DenseMap<Symbol *, std::string>
86*0b57cec5SDimitry Andric getSymbolStrings(ArrayRef<Defined *> syms) {
87*0b57cec5SDimitry Andric   std::vector<std::string> str(syms.size());
88*0b57cec5SDimitry Andric   parallelForEachN(0, syms.size(), [&](size_t i) {
89*0b57cec5SDimitry Andric     raw_string_ostream os(str[i]);
90*0b57cec5SDimitry Andric     OutputSection *osec = syms[i]->getOutputSection();
91*0b57cec5SDimitry Andric     uint64_t vma = syms[i]->getVA();
92*0b57cec5SDimitry Andric     uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
93*0b57cec5SDimitry Andric     writeHeader(os, vma, lma, syms[i]->getSize(), 1);
94*0b57cec5SDimitry Andric     os << indent16 << toString(*syms[i]);
95*0b57cec5SDimitry Andric   });
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric   DenseMap<Symbol *, std::string> ret;
98*0b57cec5SDimitry Andric   for (size_t i = 0, e = syms.size(); i < e; ++i)
99*0b57cec5SDimitry Andric     ret[syms[i]] = std::move(str[i]);
100*0b57cec5SDimitry Andric   return ret;
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric // Print .eh_frame contents. Since the section consists of EhSectionPieces,
104*0b57cec5SDimitry Andric // we need a specialized printer for that section.
105*0b57cec5SDimitry Andric //
106*0b57cec5SDimitry Andric // .eh_frame tend to contain a lot of section pieces that are contiguous
107*0b57cec5SDimitry Andric // both in input file and output file. Such pieces are squashed before
108*0b57cec5SDimitry Andric // being displayed to make output compact.
109*0b57cec5SDimitry Andric static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
110*0b57cec5SDimitry Andric   std::vector<EhSectionPiece> pieces;
111*0b57cec5SDimitry Andric 
112*0b57cec5SDimitry Andric   auto add = [&](const EhSectionPiece &p) {
113*0b57cec5SDimitry Andric     // If P is adjacent to Last, squash the two.
114*0b57cec5SDimitry Andric     if (!pieces.empty()) {
115*0b57cec5SDimitry Andric       EhSectionPiece &last = pieces.back();
116*0b57cec5SDimitry Andric       if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
117*0b57cec5SDimitry Andric           last.outputOff + last.size == p.outputOff) {
118*0b57cec5SDimitry Andric         last.size += p.size;
119*0b57cec5SDimitry Andric         return;
120*0b57cec5SDimitry Andric       }
121*0b57cec5SDimitry Andric     }
122*0b57cec5SDimitry Andric     pieces.push_back(p);
123*0b57cec5SDimitry Andric   };
124*0b57cec5SDimitry Andric 
125*0b57cec5SDimitry Andric   // Gather section pieces.
126*0b57cec5SDimitry Andric   for (const CieRecord *rec : sec->getCieRecords()) {
127*0b57cec5SDimitry Andric     add(*rec->cie);
128*0b57cec5SDimitry Andric     for (const EhSectionPiece *fde : rec->fdes)
129*0b57cec5SDimitry Andric       add(*fde);
130*0b57cec5SDimitry Andric   }
131*0b57cec5SDimitry Andric 
132*0b57cec5SDimitry Andric   // Print out section pieces.
133*0b57cec5SDimitry Andric   const OutputSection *osec = sec->getOutputSection();
134*0b57cec5SDimitry Andric   for (EhSectionPiece &p : pieces) {
135*0b57cec5SDimitry Andric     writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
136*0b57cec5SDimitry Andric                 p.size, 1);
137*0b57cec5SDimitry Andric     os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
138*0b57cec5SDimitry Andric        << Twine::utohexstr(p.inputOff) + ")\n";
139*0b57cec5SDimitry Andric   }
140*0b57cec5SDimitry Andric }
141*0b57cec5SDimitry Andric 
1425ffd83dbSDimitry Andric void elf::writeMapFile() {
143*0b57cec5SDimitry Andric   if (config->mapFile.empty())
144*0b57cec5SDimitry Andric     return;
145*0b57cec5SDimitry Andric 
146e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Write map file");
147e8d8bef9SDimitry Andric 
148*0b57cec5SDimitry Andric   // Open a map file for writing.
149*0b57cec5SDimitry Andric   std::error_code ec;
15085868e8aSDimitry Andric   raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
151*0b57cec5SDimitry Andric   if (ec) {
152*0b57cec5SDimitry Andric     error("cannot open " + config->mapFile + ": " + ec.message());
153*0b57cec5SDimitry Andric     return;
154*0b57cec5SDimitry Andric   }
155*0b57cec5SDimitry Andric 
156*0b57cec5SDimitry Andric   // Collect symbol info that we want to print out.
157*0b57cec5SDimitry Andric   std::vector<Defined *> syms = getSymbols();
158*0b57cec5SDimitry Andric   SymbolMapTy sectionSyms = getSectionSyms(syms);
159*0b57cec5SDimitry Andric   DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
160*0b57cec5SDimitry Andric 
161*0b57cec5SDimitry Andric   // Print out the header line.
162*0b57cec5SDimitry Andric   int w = config->is64 ? 16 : 8;
163*0b57cec5SDimitry Andric   os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
164*0b57cec5SDimitry Andric      << "     Size Align Out     In      Symbol\n";
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric   OutputSection* osec = nullptr;
167*0b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands) {
168*0b57cec5SDimitry Andric     if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
169*0b57cec5SDimitry Andric       if (cmd->provide && !cmd->sym)
170*0b57cec5SDimitry Andric         continue;
171*0b57cec5SDimitry Andric       uint64_t lma = osec ? osec->getLMA() + cmd->addr - osec->getVA(0) : 0;
172*0b57cec5SDimitry Andric       writeHeader(os, cmd->addr, lma, cmd->size, 1);
173*0b57cec5SDimitry Andric       os << cmd->commandString << '\n';
174*0b57cec5SDimitry Andric       continue;
175*0b57cec5SDimitry Andric     }
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric     osec = cast<OutputSection>(base);
178*0b57cec5SDimitry Andric     writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
179*0b57cec5SDimitry Andric     os << osec->name << '\n';
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric     // Dump symbols for each input section.
182*0b57cec5SDimitry Andric     for (BaseCommand *base : osec->sectionCommands) {
183*0b57cec5SDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
184*0b57cec5SDimitry Andric         for (InputSection *isec : isd->sections) {
185*0b57cec5SDimitry Andric           if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
186*0b57cec5SDimitry Andric             printEhFrame(os, ehSec);
187*0b57cec5SDimitry Andric             continue;
188*0b57cec5SDimitry Andric           }
189*0b57cec5SDimitry Andric 
190*0b57cec5SDimitry Andric           writeHeader(os, isec->getVA(0), osec->getLMA() + isec->getOffset(0),
191*0b57cec5SDimitry Andric                       isec->getSize(), isec->alignment);
192*0b57cec5SDimitry Andric           os << indent8 << toString(isec) << '\n';
193*0b57cec5SDimitry Andric           for (Symbol *sym : sectionSyms[isec])
194*0b57cec5SDimitry Andric             os << symStr[sym] << '\n';
195*0b57cec5SDimitry Andric         }
196*0b57cec5SDimitry Andric         continue;
197*0b57cec5SDimitry Andric       }
198*0b57cec5SDimitry Andric 
199*0b57cec5SDimitry Andric       if (auto *cmd = dyn_cast<ByteCommand>(base)) {
200*0b57cec5SDimitry Andric         writeHeader(os, osec->addr + cmd->offset, osec->getLMA() + cmd->offset,
201*0b57cec5SDimitry Andric                     cmd->size, 1);
202*0b57cec5SDimitry Andric         os << indent8 << cmd->commandString << '\n';
203*0b57cec5SDimitry Andric         continue;
204*0b57cec5SDimitry Andric       }
205*0b57cec5SDimitry Andric 
206*0b57cec5SDimitry Andric       if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
207*0b57cec5SDimitry Andric         if (cmd->provide && !cmd->sym)
208*0b57cec5SDimitry Andric           continue;
209*0b57cec5SDimitry Andric         writeHeader(os, cmd->addr, osec->getLMA() + cmd->addr - osec->getVA(0),
210*0b57cec5SDimitry Andric                     cmd->size, 1);
211*0b57cec5SDimitry Andric         os << indent8 << cmd->commandString << '\n';
212*0b57cec5SDimitry Andric         continue;
213*0b57cec5SDimitry Andric       }
214*0b57cec5SDimitry Andric     }
215*0b57cec5SDimitry Andric   }
216*0b57cec5SDimitry Andric }
217*0b57cec5SDimitry Andric 
218*0b57cec5SDimitry Andric static void print(StringRef a, StringRef b) {
219480093f4SDimitry Andric   lld::outs() << left_justify(a, 49) << " " << b << "\n";
220*0b57cec5SDimitry Andric }
221*0b57cec5SDimitry Andric 
222*0b57cec5SDimitry Andric // Output a cross reference table to stdout. This is for --cref.
223*0b57cec5SDimitry Andric //
224*0b57cec5SDimitry Andric // For each global symbol, we print out a file that defines the symbol
225*0b57cec5SDimitry Andric // followed by files that uses that symbol. Here is an example.
226*0b57cec5SDimitry Andric //
227*0b57cec5SDimitry Andric //     strlen     /lib/x86_64-linux-gnu/libc.so.6
228*0b57cec5SDimitry Andric //                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
229*0b57cec5SDimitry Andric //                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
230*0b57cec5SDimitry Andric //
231*0b57cec5SDimitry Andric // In this case, strlen is defined by libc.so.6 and used by other two
232*0b57cec5SDimitry Andric // files.
2335ffd83dbSDimitry Andric void elf::writeCrossReferenceTable() {
234*0b57cec5SDimitry Andric   if (!config->cref)
235*0b57cec5SDimitry Andric     return;
236*0b57cec5SDimitry Andric 
237*0b57cec5SDimitry Andric   // Collect symbols and files.
238*0b57cec5SDimitry Andric   MapVector<Symbol *, SetVector<InputFile *>> map;
239*0b57cec5SDimitry Andric   for (InputFile *file : objectFiles) {
240*0b57cec5SDimitry Andric     for (Symbol *sym : file->getSymbols()) {
241*0b57cec5SDimitry Andric       if (isa<SharedSymbol>(sym))
242*0b57cec5SDimitry Andric         map[sym].insert(file);
243*0b57cec5SDimitry Andric       if (auto *d = dyn_cast<Defined>(sym))
244*0b57cec5SDimitry Andric         if (!d->isLocal() && (!d->section || d->section->isLive()))
245*0b57cec5SDimitry Andric           map[d].insert(file);
246*0b57cec5SDimitry Andric     }
247*0b57cec5SDimitry Andric   }
248*0b57cec5SDimitry Andric 
249*0b57cec5SDimitry Andric   // Print out a header.
250480093f4SDimitry Andric   lld::outs() << "Cross Reference Table\n\n";
251*0b57cec5SDimitry Andric   print("Symbol", "File");
252*0b57cec5SDimitry Andric 
253*0b57cec5SDimitry Andric   // Print out a table.
254*0b57cec5SDimitry Andric   for (auto kv : map) {
255*0b57cec5SDimitry Andric     Symbol *sym = kv.first;
256*0b57cec5SDimitry Andric     SetVector<InputFile *> &files = kv.second;
257*0b57cec5SDimitry Andric 
258*0b57cec5SDimitry Andric     print(toString(*sym), toString(sym->file));
259*0b57cec5SDimitry Andric     for (InputFile *file : files)
260*0b57cec5SDimitry Andric       if (file != sym->file)
261*0b57cec5SDimitry Andric         print("", toString(file));
262*0b57cec5SDimitry Andric   }
263*0b57cec5SDimitry Andric }
26485868e8aSDimitry Andric 
2655ffd83dbSDimitry Andric void elf::writeArchiveStats() {
2665ffd83dbSDimitry Andric   if (config->printArchiveStats.empty())
2675ffd83dbSDimitry Andric     return;
2685ffd83dbSDimitry Andric 
2695ffd83dbSDimitry Andric   std::error_code ec;
2705ffd83dbSDimitry Andric   raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None);
2715ffd83dbSDimitry Andric   if (ec) {
2725ffd83dbSDimitry Andric     error("--print-archive-stats=: cannot open " + config->printArchiveStats +
2735ffd83dbSDimitry Andric           ": " + ec.message());
2745ffd83dbSDimitry Andric     return;
2755ffd83dbSDimitry Andric   }
2765ffd83dbSDimitry Andric 
2775ffd83dbSDimitry Andric   os << "members\tfetched\tarchive\n";
2785ffd83dbSDimitry Andric   for (const ArchiveFile *f : archiveFiles)
2795ffd83dbSDimitry Andric     os << f->getMemberCount() << '\t' << f->getFetchedMemberCount() << '\t'
2805ffd83dbSDimitry Andric        << f->getName() << '\n';
2815ffd83dbSDimitry Andric }
282