xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision 7fc6a556)
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 using namespace lld;
38 using namespace lld::elf;
39 
40 using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
41 
42 static const std::string Indent8 = "        ";          // 8 spaces
43 static const std::string 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 void elf::writeMapFile() {
143   if (Config->MapFile.empty())
144     return;
145 
146   // Open a map file for writing.
147   std::error_code EC;
148   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
149   if (EC) {
150     error("cannot open " + Config->MapFile + ": " + EC.message());
151     return;
152   }
153 
154   // Collect symbol info that we want to print out.
155   std::vector<Defined *> Syms = getSymbols();
156   SymbolMapTy SectionSyms = getSectionSyms(Syms);
157   DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
158 
159   // Print out the header line.
160   int W = Config->Is64 ? 16 : 8;
161   OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W)
162      << "     Size Align Out     In      Symbol\n";
163 
164   OutputSection* OSec = nullptr;
165   for (BaseCommand *Base : Script->SectionCommands) {
166     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
167       if (Cmd->Provide && !Cmd->Sym)
168         continue;
169       uint64_t LMA = OSec ? OSec->getLMA() + Cmd->Addr - OSec->getVA(0) : 0;
170       writeHeader(OS, Cmd->Addr, LMA, Cmd->Size, 1);
171       OS << Cmd->CommandString << '\n';
172       continue;
173     }
174 
175     OSec = cast<OutputSection>(Base);
176     writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment);
177     OS << OSec->Name << '\n';
178 
179     // Dump symbols for each input section.
180     for (BaseCommand *Base : OSec->SectionCommands) {
181       if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
182         for (InputSection *IS : ISD->Sections) {
183           if (auto *EhSec = dyn_cast<EhFrameSection>(IS)) {
184             printEhFrame(OS, EhSec);
185             continue;
186           }
187 
188           writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0),
189                       IS->getSize(), IS->Alignment);
190           OS << Indent8 << toString(IS) << '\n';
191           for (Symbol *Sym : SectionSyms[IS])
192             OS << SymStr[Sym] << '\n';
193         }
194         continue;
195       }
196 
197       if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
198         writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset,
199                     Cmd->Size, 1);
200         OS << Indent8 << Cmd->CommandString << '\n';
201         continue;
202       }
203 
204       if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
205         if (Cmd->Provide && !Cmd->Sym)
206           continue;
207         writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0),
208                     Cmd->Size, 1);
209         OS << Indent8 << Cmd->CommandString << '\n';
210         continue;
211       }
212     }
213   }
214 }
215 
216 static void print(StringRef A, StringRef B) {
217   outs() << left_justify(A, 49) << " " << B << "\n";
218 }
219 
220 // Output a cross reference table to stdout. This is for --cref.
221 //
222 // For each global symbol, we print out a file that defines the symbol
223 // followed by files that uses that symbol. Here is an example.
224 //
225 //     strlen     /lib/x86_64-linux-gnu/libc.so.6
226 //                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
227 //                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
228 //
229 // In this case, strlen is defined by libc.so.6 and used by other two
230 // files.
231 void elf::writeCrossReferenceTable() {
232   if (!Config->Cref)
233     return;
234 
235   // Collect symbols and files.
236   MapVector<Symbol *, SetVector<InputFile *>> Map;
237   for (InputFile *File : ObjectFiles) {
238     for (Symbol *Sym : File->getSymbols()) {
239       if (isa<SharedSymbol>(Sym))
240         Map[Sym].insert(File);
241       if (auto *D = dyn_cast<Defined>(Sym))
242         if (!D->isLocal() && (!D->Section || D->Section->isLive()))
243           Map[D].insert(File);
244     }
245   }
246 
247   // Print out a header.
248   outs() << "Cross Reference Table\n\n";
249   print("Symbol", "File");
250 
251   // Print out a table.
252   for (auto KV : Map) {
253     Symbol *Sym = KV.first;
254     SetVector<InputFile *> &Files = KV.second;
255 
256     print(toString(*Sym), toString(Sym->File));
257     for (InputFile *File : Files)
258       if (File != Sym->File)
259         print("", toString(File));
260   }
261 }
262