xref: /src/contrib/llvm-project/lld/ELF/MapFile.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1d2d3ebb8SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===//
2d2d3ebb8SDimitry Andric //
3f1e1c239SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f1e1c239SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5f1e1c239SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d2d3ebb8SDimitry Andric //
7d2d3ebb8SDimitry Andric //===----------------------------------------------------------------------===//
8d2d3ebb8SDimitry Andric //
9d2d3ebb8SDimitry Andric // This file implements the -Map option. It shows lists in order and
10d2d3ebb8SDimitry Andric // hierarchically the output sections, input sections, input files and
11d2d3ebb8SDimitry Andric // symbol:
12d2d3ebb8SDimitry Andric //
13274c9ff5SDimitry Andric //   Address  Size     Align Out     In      Symbol
14d2d3ebb8SDimitry Andric //   00201000 00000015     4 .text
15274c9ff5SDimitry Andric //   00201000 0000000e     4         test.o:(.text)
16d2d3ebb8SDimitry Andric //   0020100e 00000000     0                 local
17d2d3ebb8SDimitry Andric //   00201005 00000000     0                 f(int)
18d2d3ebb8SDimitry Andric //
19d2d3ebb8SDimitry Andric //===----------------------------------------------------------------------===//
20d2d3ebb8SDimitry Andric 
21d2d3ebb8SDimitry Andric #include "MapFile.h"
22d2d3ebb8SDimitry Andric #include "InputFiles.h"
23c53addf3SDimitry Andric #include "LinkerScript.h"
24c53addf3SDimitry Andric #include "OutputSections.h"
25eb1ff93dSDimitry Andric #include "Symbols.h"
26eb1ff93dSDimitry Andric #include "SyntheticSections.h"
2720d35e67SDimitry Andric #include "llvm/ADT/MapVector.h"
2820d35e67SDimitry Andric #include "llvm/ADT/SetVector.h"
29145449b1SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
30145449b1SDimitry Andric #include "llvm/Support/FileSystem.h"
31cfca06d7SDimitry Andric #include "llvm/Support/Parallel.h"
32b60736ecSDimitry Andric #include "llvm/Support/TimeProfiler.h"
33d2d3ebb8SDimitry Andric #include "llvm/Support/raw_ostream.h"
34d2d3ebb8SDimitry Andric 
35d2d3ebb8SDimitry Andric using namespace llvm;
36d2d3ebb8SDimitry Andric using namespace llvm::object;
37cfca06d7SDimitry Andric using namespace lld;
38cfca06d7SDimitry Andric using namespace lld::elf;
39d2d3ebb8SDimitry Andric 
406f8fc217SDimitry Andric using SymbolMapTy = DenseMap<const SectionBase *,
416f8fc217SDimitry Andric                              SmallVector<std::pair<Defined *, uint64_t>, 0>>;
4220d35e67SDimitry Andric 
43d2bd9e70SDimitry Andric static constexpr char indent8[] = "        ";          // 8 spaces
44d2bd9e70SDimitry Andric static constexpr char indent16[] = "                "; // 16 spaces
45d2d3ebb8SDimitry Andric 
46274c9ff5SDimitry Andric // Print out the first three columns of a line.
writeHeader(raw_ostream & os,uint64_t vma,uint64_t lma,uint64_t size,uint64_t align)47f1e1c239SDimitry Andric static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
48f1e1c239SDimitry Andric                         uint64_t size, uint64_t align) {
49f1e1c239SDimitry Andric   if (config->is64)
50f1e1c239SDimitry Andric     os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
5120d35e67SDimitry Andric   else
52f1e1c239SDimitry Andric     os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
53d2d3ebb8SDimitry Andric }
54d2d3ebb8SDimitry Andric 
55274c9ff5SDimitry Andric // Returns a list of all symbols that we want to print out.
getSymbols()5620d35e67SDimitry Andric static std::vector<Defined *> getSymbols() {
57f1e1c239SDimitry Andric   std::vector<Defined *> v;
58e3b55780SDimitry Andric   for (ELFFileBase *file : ctx.objectFiles)
59f1e1c239SDimitry Andric     for (Symbol *b : file->getSymbols())
60f1e1c239SDimitry Andric       if (auto *dr = dyn_cast<Defined>(b))
61f1e1c239SDimitry Andric         if (!dr->isSection() && dr->section && dr->section->isLive() &&
62e3b55780SDimitry Andric             (dr->file == file || dr->hasFlag(NEEDS_COPY) || dr->section->bss))
63f1e1c239SDimitry Andric           v.push_back(dr);
64f1e1c239SDimitry Andric   return v;
65d2d3ebb8SDimitry Andric }
66d2d3ebb8SDimitry Andric 
67274c9ff5SDimitry Andric // Returns a map from sections to their symbols.
getSectionSyms(ArrayRef<Defined * > syms)68f1e1c239SDimitry Andric static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
69f1e1c239SDimitry Andric   SymbolMapTy ret;
70f1e1c239SDimitry Andric   for (Defined *dr : syms)
716f8fc217SDimitry Andric     ret[dr->section].emplace_back(dr, dr->getVA());
72d2d3ebb8SDimitry Andric 
73274c9ff5SDimitry Andric   // Sort symbols by address. We want to print out symbols in the
74274c9ff5SDimitry Andric   // order in the output file rather than the order they appeared
75274c9ff5SDimitry Andric   // in the input files.
7677fc4c14SDimitry Andric   SmallPtrSet<Defined *, 4> set;
7777fc4c14SDimitry Andric   for (auto &it : ret) {
7877fc4c14SDimitry Andric     // Deduplicate symbols which need a canonical PLT entry/copy relocation.
7977fc4c14SDimitry Andric     set.clear();
806f8fc217SDimitry Andric     llvm::erase_if(it.second, [&](std::pair<Defined *, uint64_t> a) {
816f8fc217SDimitry Andric       return !set.insert(a.first).second;
8220d35e67SDimitry Andric     });
836f8fc217SDimitry Andric 
846f8fc217SDimitry Andric     llvm::stable_sort(it.second, llvm::less_second());
8577fc4c14SDimitry Andric   }
86f1e1c239SDimitry Andric   return ret;
87d2d3ebb8SDimitry Andric }
88274c9ff5SDimitry Andric 
89274c9ff5SDimitry Andric // Construct a map from symbols to their stringified representations.
90274c9ff5SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so
91274c9ff5SDimitry Andric // we do that in batch using parallel-for.
92eb1ff93dSDimitry Andric static DenseMap<Symbol *, std::string>
getSymbolStrings(ArrayRef<Defined * > syms)93f1e1c239SDimitry Andric getSymbolStrings(ArrayRef<Defined *> syms) {
946f8fc217SDimitry Andric   auto strs = std::make_unique<std::string[]>(syms.size());
95145449b1SDimitry Andric   parallelFor(0, syms.size(), [&](size_t i) {
966f8fc217SDimitry Andric     raw_string_ostream os(strs[i]);
97f1e1c239SDimitry Andric     OutputSection *osec = syms[i]->getOutputSection();
98f1e1c239SDimitry Andric     uint64_t vma = syms[i]->getVA();
99f1e1c239SDimitry Andric     uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
100f1e1c239SDimitry Andric     writeHeader(os, vma, lma, syms[i]->getSize(), 1);
101f1e1c239SDimitry Andric     os << indent16 << toString(*syms[i]);
102274c9ff5SDimitry Andric   });
103274c9ff5SDimitry Andric 
104f1e1c239SDimitry Andric   DenseMap<Symbol *, std::string> ret;
105f1e1c239SDimitry Andric   for (size_t i = 0, e = syms.size(); i < e; ++i)
1066f8fc217SDimitry Andric     ret[syms[i]] = std::move(strs[i]);
107f1e1c239SDimitry Andric   return ret;
108d2d3ebb8SDimitry Andric }
109d2d3ebb8SDimitry Andric 
11020d35e67SDimitry Andric // Print .eh_frame contents. Since the section consists of EhSectionPieces,
11120d35e67SDimitry Andric // we need a specialized printer for that section.
11220d35e67SDimitry Andric //
11320d35e67SDimitry Andric // .eh_frame tend to contain a lot of section pieces that are contiguous
11420d35e67SDimitry Andric // both in input file and output file. Such pieces are squashed before
11520d35e67SDimitry Andric // being displayed to make output compact.
printEhFrame(raw_ostream & os,const EhFrameSection * sec)116f1e1c239SDimitry Andric static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
117f1e1c239SDimitry Andric   std::vector<EhSectionPiece> pieces;
11820d35e67SDimitry Andric 
119f1e1c239SDimitry Andric   auto add = [&](const EhSectionPiece &p) {
12020d35e67SDimitry Andric     // If P is adjacent to Last, squash the two.
121f1e1c239SDimitry Andric     if (!pieces.empty()) {
122f1e1c239SDimitry Andric       EhSectionPiece &last = pieces.back();
123f1e1c239SDimitry Andric       if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
124ac9a064cSDimitry Andric           last.outputOff + last.size == (unsigned)p.outputOff) {
125f1e1c239SDimitry Andric         last.size += p.size;
12620d35e67SDimitry Andric         return;
12720d35e67SDimitry Andric       }
12820d35e67SDimitry Andric     }
129f1e1c239SDimitry Andric     pieces.push_back(p);
13020d35e67SDimitry Andric   };
13120d35e67SDimitry Andric 
13220d35e67SDimitry Andric   // Gather section pieces.
133f1e1c239SDimitry Andric   for (const CieRecord *rec : sec->getCieRecords()) {
134f1e1c239SDimitry Andric     add(*rec->cie);
135f1e1c239SDimitry Andric     for (const EhSectionPiece *fde : rec->fdes)
136f1e1c239SDimitry Andric       add(*fde);
13720d35e67SDimitry Andric   }
13820d35e67SDimitry Andric 
13920d35e67SDimitry Andric   // Print out section pieces.
140f1e1c239SDimitry Andric   const OutputSection *osec = sec->getOutputSection();
141f1e1c239SDimitry Andric   for (EhSectionPiece &p : pieces) {
142f1e1c239SDimitry Andric     writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
143f1e1c239SDimitry Andric                 p.size, 1);
144f1e1c239SDimitry Andric     os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
145f1e1c239SDimitry Andric        << Twine::utohexstr(p.inputOff) + ")\n";
14620d35e67SDimitry Andric   }
14720d35e67SDimitry Andric }
14820d35e67SDimitry Andric 
writeMapFile(raw_fd_ostream & os)149f65dcba8SDimitry Andric static void writeMapFile(raw_fd_ostream &os) {
150274c9ff5SDimitry Andric   // Collect symbol info that we want to print out.
151f1e1c239SDimitry Andric   std::vector<Defined *> syms = getSymbols();
152f1e1c239SDimitry Andric   SymbolMapTy sectionSyms = getSectionSyms(syms);
153f1e1c239SDimitry Andric   DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
154274c9ff5SDimitry Andric 
155274c9ff5SDimitry Andric   // Print out the header line.
156f1e1c239SDimitry Andric   int w = config->is64 ? 16 : 8;
157f1e1c239SDimitry Andric   os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
15820d35e67SDimitry Andric      << "     Size Align Out     In      Symbol\n";
159274c9ff5SDimitry Andric 
160f1e1c239SDimitry Andric   OutputSection *osec = nullptr;
161f65dcba8SDimitry Andric   for (SectionCommand *cmd : script->sectionCommands) {
162f65dcba8SDimitry Andric     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
163f65dcba8SDimitry Andric       if (assign->provide && !assign->sym)
16420d35e67SDimitry Andric         continue;
165f65dcba8SDimitry Andric       uint64_t lma = osec ? osec->getLMA() + assign->addr - osec->getVA(0) : 0;
166f65dcba8SDimitry Andric       writeHeader(os, assign->addr, lma, assign->size, 1);
167f65dcba8SDimitry Andric       os << assign->commandString << '\n';
16820d35e67SDimitry Andric       continue;
16920d35e67SDimitry Andric     }
17020d35e67SDimitry Andric 
171145449b1SDimitry Andric     osec = &cast<OutputDesc>(cmd)->osec;
172e3b55780SDimitry Andric     writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->addralign);
173f1e1c239SDimitry Andric     os << osec->name << '\n';
174274c9ff5SDimitry Andric 
175274c9ff5SDimitry Andric     // Dump symbols for each input section.
176f65dcba8SDimitry Andric     for (SectionCommand *subCmd : osec->commands) {
177f65dcba8SDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(subCmd)) {
178f1e1c239SDimitry Andric         for (InputSection *isec : isd->sections) {
179f1e1c239SDimitry Andric           if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
180f1e1c239SDimitry Andric             printEhFrame(os, ehSec);
18120d35e67SDimitry Andric             continue;
18220d35e67SDimitry Andric           }
18320d35e67SDimitry Andric 
184f65dcba8SDimitry Andric           writeHeader(os, isec->getVA(), osec->getLMA() + isec->outSecOff,
185e3b55780SDimitry Andric                       isec->getSize(), isec->addralign);
186f1e1c239SDimitry Andric           os << indent8 << toString(isec) << '\n';
1876f8fc217SDimitry Andric           for (Symbol *sym : llvm::make_first_range(sectionSyms[isec]))
188f1e1c239SDimitry Andric             os << symStr[sym] << '\n';
189274c9ff5SDimitry Andric         }
19020d35e67SDimitry Andric         continue;
191274c9ff5SDimitry Andric       }
19220d35e67SDimitry Andric 
193f65dcba8SDimitry Andric       if (auto *data = dyn_cast<ByteCommand>(subCmd)) {
194f65dcba8SDimitry Andric         writeHeader(os, osec->addr + data->offset,
195f65dcba8SDimitry Andric                     osec->getLMA() + data->offset, data->size, 1);
196f65dcba8SDimitry Andric         os << indent8 << data->commandString << '\n';
19720d35e67SDimitry Andric         continue;
19820d35e67SDimitry Andric       }
19920d35e67SDimitry Andric 
200f65dcba8SDimitry Andric       if (auto *assign = dyn_cast<SymbolAssignment>(subCmd)) {
201f65dcba8SDimitry Andric         if (assign->provide && !assign->sym)
20220d35e67SDimitry Andric           continue;
203f65dcba8SDimitry Andric         writeHeader(os, assign->addr,
204f65dcba8SDimitry Andric                     osec->getLMA() + assign->addr - osec->getVA(0),
205f65dcba8SDimitry Andric                     assign->size, 1);
206f65dcba8SDimitry Andric         os << indent8 << assign->commandString << '\n';
20720d35e67SDimitry Andric         continue;
20820d35e67SDimitry Andric       }
20920d35e67SDimitry Andric     }
21020d35e67SDimitry Andric   }
21120d35e67SDimitry Andric }
21220d35e67SDimitry Andric 
21320d35e67SDimitry Andric // Output a cross reference table to stdout. This is for --cref.
21420d35e67SDimitry Andric //
21520d35e67SDimitry Andric // For each global symbol, we print out a file that defines the symbol
21620d35e67SDimitry Andric // followed by files that uses that symbol. Here is an example.
21720d35e67SDimitry Andric //
21820d35e67SDimitry Andric //     strlen     /lib/x86_64-linux-gnu/libc.so.6
21920d35e67SDimitry Andric //                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
22020d35e67SDimitry Andric //                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
22120d35e67SDimitry Andric //
22220d35e67SDimitry Andric // In this case, strlen is defined by libc.so.6 and used by other two
22320d35e67SDimitry Andric // files.
writeCref(raw_fd_ostream & os)224f65dcba8SDimitry Andric static void writeCref(raw_fd_ostream &os) {
22520d35e67SDimitry Andric   // Collect symbols and files.
226f1e1c239SDimitry Andric   MapVector<Symbol *, SetVector<InputFile *>> map;
227e3b55780SDimitry Andric   for (ELFFileBase *file : ctx.objectFiles) {
228f1e1c239SDimitry Andric     for (Symbol *sym : file->getSymbols()) {
229f1e1c239SDimitry Andric       if (isa<SharedSymbol>(sym))
230f1e1c239SDimitry Andric         map[sym].insert(file);
231f1e1c239SDimitry Andric       if (auto *d = dyn_cast<Defined>(sym))
232b1c73532SDimitry Andric         if (!d->isLocal())
233f1e1c239SDimitry Andric           map[d].insert(file);
23420d35e67SDimitry Andric     }
23520d35e67SDimitry Andric   }
23620d35e67SDimitry Andric 
237f65dcba8SDimitry Andric   auto print = [&](StringRef a, StringRef b) {
238f65dcba8SDimitry Andric     os << left_justify(a, 49) << ' ' << b << '\n';
239f65dcba8SDimitry Andric   };
240f65dcba8SDimitry Andric 
241f65dcba8SDimitry Andric   // Print a blank line and a header. The format matches GNU ld.
242f65dcba8SDimitry Andric   os << "\nCross Reference Table\n\n";
24320d35e67SDimitry Andric   print("Symbol", "File");
24420d35e67SDimitry Andric 
24520d35e67SDimitry Andric   // Print out a table.
246f1e1c239SDimitry Andric   for (auto kv : map) {
247f1e1c239SDimitry Andric     Symbol *sym = kv.first;
248f1e1c239SDimitry Andric     SetVector<InputFile *> &files = kv.second;
24920d35e67SDimitry Andric 
250f1e1c239SDimitry Andric     print(toString(*sym), toString(sym->file));
251f1e1c239SDimitry Andric     for (InputFile *file : files)
252f1e1c239SDimitry Andric       if (file != sym->file)
253f1e1c239SDimitry Andric         print("", toString(file));
254d2d3ebb8SDimitry Andric   }
255bef2946cSDimitry Andric }
256d2bd9e70SDimitry Andric 
writeMapAndCref()257f65dcba8SDimitry Andric void elf::writeMapAndCref() {
258f65dcba8SDimitry Andric   if (config->mapFile.empty() && !config->cref)
259f65dcba8SDimitry Andric     return;
260f65dcba8SDimitry Andric 
261f65dcba8SDimitry Andric   llvm::TimeTraceScope timeScope("Write map file");
262f65dcba8SDimitry Andric 
263f65dcba8SDimitry Andric   // Open a map file for writing.
264f65dcba8SDimitry Andric   std::error_code ec;
265f65dcba8SDimitry Andric   StringRef mapFile = config->mapFile.empty() ? "-" : config->mapFile;
2667fa27ce4SDimitry Andric   raw_fd_ostream os = ctx.openAuxiliaryFile(mapFile, ec);
267f65dcba8SDimitry Andric   if (ec) {
268f65dcba8SDimitry Andric     error("cannot open " + mapFile + ": " + ec.message());
269f65dcba8SDimitry Andric     return;
270f65dcba8SDimitry Andric   }
271f65dcba8SDimitry Andric 
272f65dcba8SDimitry Andric   if (!config->mapFile.empty())
273f65dcba8SDimitry Andric     writeMapFile(os);
274f65dcba8SDimitry Andric   if (config->cref)
275f65dcba8SDimitry Andric     writeCref(os);
276f65dcba8SDimitry Andric }
277