]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/MapFile.cpp
Update to Zstandard 1.4.2
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / MapFile.cpp
1 //===- MapFile.cpp --------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the -Map option. It shows lists in order and
11 // hierarchically the output sections, input sections, input files and
12 // symbol:
13 //
14 //   Address  Size     Align Out     In      Symbol
15 //   00201000 00000015     4 .text
16 //   00201000 0000000e     4         test.o:(.text)
17 //   0020100e 00000000     0                 local
18 //   00201005 00000000     0                 f(int)
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "MapFile.h"
23 #include "InputFiles.h"
24 #include "LinkerScript.h"
25 #include "OutputSections.h"
26 #include "SymbolTable.h"
27 #include "Symbols.h"
28 #include "SyntheticSections.h"
29 #include "lld/Common/Strings.h"
30 #include "lld/Common/Threads.h"
31 #include "llvm/ADT/MapVector.h"
32 #include "llvm/ADT/SetVector.h"
33 #include "llvm/Support/raw_ostream.h"
34
35 using namespace llvm;
36 using namespace llvm::object;
37
38 using namespace lld;
39 using namespace lld::elf;
40
41 typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy;
42
43 static const std::string Indent8 = "        ";          // 8 spaces
44 static const std::string 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 (InputFile *File : ObjectFiles)
59     for (Symbol *B : File->getSymbols())
60       if (auto *DR = dyn_cast<Defined>(B))
61         if (!DR->isSection() && DR->Section && DR->Section->Live &&
62             (DR->File == File || DR->NeedsPltAddr || 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].push_back(DR);
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   for (auto &It : Ret) {
77     SmallVectorImpl<Defined *> &V = It.second;
78     std::stable_sort(V.begin(), V.end(), [](Defined *A, Defined *B) {
79       return A->getVA() < B->getVA();
80     });
81   }
82   return Ret;
83 }
84
85 // Construct a map from symbols to their stringified representations.
86 // Demangling symbols (which is what toString() does) is slow, so
87 // we do that in batch using parallel-for.
88 static DenseMap<Symbol *, std::string>
89 getSymbolStrings(ArrayRef<Defined *> Syms) {
90   std::vector<std::string> Str(Syms.size());
91   parallelForEachN(0, Syms.size(), [&](size_t I) {
92     raw_string_ostream OS(Str[I]);
93     OutputSection *OSec = Syms[I]->getOutputSection();
94     uint64_t VMA = Syms[I]->getVA();
95     uint64_t LMA = OSec ? OSec->getLMA() + VMA - OSec->getVA(0) : 0;
96     writeHeader(OS, VMA, LMA, Syms[I]->getSize(), 1);
97     OS << Indent16 << toString(*Syms[I]);
98   });
99
100   DenseMap<Symbol *, std::string> Ret;
101   for (size_t I = 0, E = Syms.size(); I < E; ++I)
102     Ret[Syms[I]] = std::move(Str[I]);
103   return Ret;
104 }
105
106 // Print .eh_frame contents. Since the section consists of EhSectionPieces,
107 // we need a specialized printer for that section.
108 //
109 // .eh_frame tend to contain a lot of section pieces that are contiguous
110 // both in input file and output file. Such pieces are squashed before
111 // being displayed to make output compact.
112 static void printEhFrame(raw_ostream &OS, OutputSection *OSec) {
113   std::vector<EhSectionPiece> Pieces;
114
115   auto Add = [&](const EhSectionPiece &P) {
116     // If P is adjacent to Last, squash the two.
117     if (!Pieces.empty()) {
118       EhSectionPiece &Last = Pieces.back();
119       if (Last.Sec == P.Sec && Last.InputOff + Last.Size == P.InputOff &&
120           Last.OutputOff + Last.Size == P.OutputOff) {
121         Last.Size += P.Size;
122         return;
123       }
124     }
125     Pieces.push_back(P);
126   };
127
128   // Gather section pieces.
129   for (const CieRecord *Rec : In.EhFrame->getCieRecords()) {
130     Add(*Rec->Cie);
131     for (const EhSectionPiece *Fde : Rec->Fdes)
132       Add(*Fde);
133   }
134
135   // Print out section pieces.
136   for (EhSectionPiece &P : Pieces) {
137     writeHeader(OS, OSec->Addr + P.OutputOff, OSec->getLMA() + P.OutputOff,
138                 P.Size, 1);
139     OS << Indent8 << toString(P.Sec->File) << ":(" << P.Sec->Name << "+0x"
140        << Twine::utohexstr(P.InputOff) + ")\n";
141   }
142 }
143
144 void elf::writeMapFile() {
145   if (Config->MapFile.empty())
146     return;
147
148   // Open a map file for writing.
149   std::error_code EC;
150   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
151   if (EC) {
152     error("cannot open " + Config->MapFile + ": " + EC.message());
153     return;
154   }
155
156   // Collect symbol info that we want to print out.
157   std::vector<Defined *> Syms = getSymbols();
158   SymbolMapTy SectionSyms = getSectionSyms(Syms);
159   DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
160
161   // Print out the header line.
162   int W = Config->Is64 ? 16 : 8;
163   OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W)
164      << "     Size Align Out     In      Symbol\n";
165
166   OutputSection* OSec = nullptr;
167   for (BaseCommand *Base : Script->SectionCommands) {
168     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
169       if (Cmd->Provide && !Cmd->Sym)
170         continue;
171       uint64_t LMA = OSec ? OSec->getLMA() + Cmd->Addr - OSec->getVA(0) : 0;
172       writeHeader(OS, Cmd->Addr, LMA, Cmd->Size, 1);
173       OS << Cmd->CommandString << '\n';
174       continue;
175     }
176
177     OSec = cast<OutputSection>(Base);
178     writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment);
179     OS << OSec->Name << '\n';
180
181     // Dump symbols for each input section.
182     for (BaseCommand *Base : OSec->SectionCommands) {
183       if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
184         for (InputSection *IS : ISD->Sections) {
185           if (IS == In.EhFrame) {
186             printEhFrame(OS, OSec);
187             continue;
188           }
189
190           writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0),
191                       IS->getSize(), IS->Alignment);
192           OS << Indent8 << toString(IS) << '\n';
193           for (Symbol *Sym : SectionSyms[IS])
194             OS << SymStr[Sym] << '\n';
195         }
196         continue;
197       }
198
199       if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
200         writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset,
201                     Cmd->Size, 1);
202         OS << Indent8 << Cmd->CommandString << '\n';
203         continue;
204       }
205
206       if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
207         if (Cmd->Provide && !Cmd->Sym)
208           continue;
209         writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0),
210                     Cmd->Size, 1);
211         OS << Indent8 << Cmd->CommandString << '\n';
212         continue;
213       }
214     }
215   }
216 }
217
218 static void print(StringRef A, StringRef B) {
219   outs() << left_justify(A, 49) << " " << B << "\n";
220 }
221
222 // Output a cross reference table to stdout. This is for --cref.
223 //
224 // For each global symbol, we print out a file that defines the symbol
225 // followed by files that uses that symbol. Here is an example.
226 //
227 //     strlen     /lib/x86_64-linux-gnu/libc.so.6
228 //                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
229 //                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
230 //
231 // In this case, strlen is defined by libc.so.6 and used by other two
232 // files.
233 void elf::writeCrossReferenceTable() {
234   if (!Config->Cref)
235     return;
236
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->Live))
245           Map[D].insert(File);
246     }
247   }
248
249   // Print out a header.
250   outs() << "Cross Reference Table\n\n";
251   print("Symbol", "File");
252
253   // Print out a table.
254   for (auto KV : Map) {
255     Symbol *Sym = KV.first;
256     SetVector<InputFile *> &Files = KV.second;
257
258     print(toString(*Sym), toString(Sym->File));
259     for (InputFile *File : Files)
260       if (File != Sym->File)
261         print("", toString(File));
262   }
263 }