xref: /freebsd/contrib/llvm-project/lld/ELF/MapFile.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the -Map option. It shows lists in order and
100b57cec5SDimitry Andric // hierarchically the output sections, input sections, input files and
110b57cec5SDimitry Andric // symbol:
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //   Address  Size     Align Out     In      Symbol
140b57cec5SDimitry Andric //   00201000 00000015     4 .text
150b57cec5SDimitry Andric //   00201000 0000000e     4         test.o:(.text)
160b57cec5SDimitry Andric //   0020100e 00000000     0                 local
170b57cec5SDimitry Andric //   00201005 00000000     0                 f(int)
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #include "MapFile.h"
220b57cec5SDimitry Andric #include "InputFiles.h"
230b57cec5SDimitry Andric #include "LinkerScript.h"
240b57cec5SDimitry Andric #include "OutputSections.h"
250b57cec5SDimitry Andric #include "SymbolTable.h"
260b57cec5SDimitry Andric #include "Symbols.h"
270b57cec5SDimitry Andric #include "SyntheticSections.h"
280b57cec5SDimitry Andric #include "lld/Common/Strings.h"
290b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
300b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
31*5ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
320b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric using namespace llvm;
350b57cec5SDimitry Andric using namespace llvm::object;
36*5ffd83dbSDimitry Andric using namespace lld;
37*5ffd83dbSDimitry Andric using namespace lld::elf;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
400b57cec5SDimitry Andric 
4185868e8aSDimitry Andric static constexpr char indent8[] = "        ";          // 8 spaces
4285868e8aSDimitry Andric static constexpr char indent16[] = "                "; // 16 spaces
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric // Print out the first three columns of a line.
450b57cec5SDimitry Andric static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
460b57cec5SDimitry Andric                         uint64_t size, uint64_t align) {
470b57cec5SDimitry Andric   if (config->is64)
480b57cec5SDimitry Andric     os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
490b57cec5SDimitry Andric   else
500b57cec5SDimitry Andric     os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric // Returns a list of all symbols that we want to print out.
540b57cec5SDimitry Andric static std::vector<Defined *> getSymbols() {
550b57cec5SDimitry Andric   std::vector<Defined *> v;
560b57cec5SDimitry Andric   for (InputFile *file : objectFiles)
570b57cec5SDimitry Andric     for (Symbol *b : file->getSymbols())
580b57cec5SDimitry Andric       if (auto *dr = dyn_cast<Defined>(b))
590b57cec5SDimitry Andric         if (!dr->isSection() && dr->section && dr->section->isLive() &&
600b57cec5SDimitry Andric             (dr->file == file || dr->needsPltAddr || dr->section->bss))
610b57cec5SDimitry Andric           v.push_back(dr);
620b57cec5SDimitry Andric   return v;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric // Returns a map from sections to their symbols.
660b57cec5SDimitry Andric static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
670b57cec5SDimitry Andric   SymbolMapTy ret;
680b57cec5SDimitry Andric   for (Defined *dr : syms)
690b57cec5SDimitry Andric     ret[dr->section].push_back(dr);
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   // Sort symbols by address. We want to print out symbols in the
720b57cec5SDimitry Andric   // order in the output file rather than the order they appeared
730b57cec5SDimitry Andric   // in the input files.
740b57cec5SDimitry Andric   for (auto &it : ret)
750b57cec5SDimitry Andric     llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
760b57cec5SDimitry Andric       return a->getVA() < b->getVA();
770b57cec5SDimitry Andric     });
780b57cec5SDimitry Andric   return ret;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric // Construct a map from symbols to their stringified representations.
820b57cec5SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so
830b57cec5SDimitry Andric // we do that in batch using parallel-for.
840b57cec5SDimitry Andric static DenseMap<Symbol *, std::string>
850b57cec5SDimitry Andric getSymbolStrings(ArrayRef<Defined *> syms) {
860b57cec5SDimitry Andric   std::vector<std::string> str(syms.size());
870b57cec5SDimitry Andric   parallelForEachN(0, syms.size(), [&](size_t i) {
880b57cec5SDimitry Andric     raw_string_ostream os(str[i]);
890b57cec5SDimitry Andric     OutputSection *osec = syms[i]->getOutputSection();
900b57cec5SDimitry Andric     uint64_t vma = syms[i]->getVA();
910b57cec5SDimitry Andric     uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
920b57cec5SDimitry Andric     writeHeader(os, vma, lma, syms[i]->getSize(), 1);
930b57cec5SDimitry Andric     os << indent16 << toString(*syms[i]);
940b57cec5SDimitry Andric   });
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   DenseMap<Symbol *, std::string> ret;
970b57cec5SDimitry Andric   for (size_t i = 0, e = syms.size(); i < e; ++i)
980b57cec5SDimitry Andric     ret[syms[i]] = std::move(str[i]);
990b57cec5SDimitry Andric   return ret;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric // Print .eh_frame contents. Since the section consists of EhSectionPieces,
1030b57cec5SDimitry Andric // we need a specialized printer for that section.
1040b57cec5SDimitry Andric //
1050b57cec5SDimitry Andric // .eh_frame tend to contain a lot of section pieces that are contiguous
1060b57cec5SDimitry Andric // both in input file and output file. Such pieces are squashed before
1070b57cec5SDimitry Andric // being displayed to make output compact.
1080b57cec5SDimitry Andric static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
1090b57cec5SDimitry Andric   std::vector<EhSectionPiece> pieces;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   auto add = [&](const EhSectionPiece &p) {
1120b57cec5SDimitry Andric     // If P is adjacent to Last, squash the two.
1130b57cec5SDimitry Andric     if (!pieces.empty()) {
1140b57cec5SDimitry Andric       EhSectionPiece &last = pieces.back();
1150b57cec5SDimitry Andric       if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
1160b57cec5SDimitry Andric           last.outputOff + last.size == p.outputOff) {
1170b57cec5SDimitry Andric         last.size += p.size;
1180b57cec5SDimitry Andric         return;
1190b57cec5SDimitry Andric       }
1200b57cec5SDimitry Andric     }
1210b57cec5SDimitry Andric     pieces.push_back(p);
1220b57cec5SDimitry Andric   };
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   // Gather section pieces.
1250b57cec5SDimitry Andric   for (const CieRecord *rec : sec->getCieRecords()) {
1260b57cec5SDimitry Andric     add(*rec->cie);
1270b57cec5SDimitry Andric     for (const EhSectionPiece *fde : rec->fdes)
1280b57cec5SDimitry Andric       add(*fde);
1290b57cec5SDimitry Andric   }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   // Print out section pieces.
1320b57cec5SDimitry Andric   const OutputSection *osec = sec->getOutputSection();
1330b57cec5SDimitry Andric   for (EhSectionPiece &p : pieces) {
1340b57cec5SDimitry Andric     writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
1350b57cec5SDimitry Andric                 p.size, 1);
1360b57cec5SDimitry Andric     os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
1370b57cec5SDimitry Andric        << Twine::utohexstr(p.inputOff) + ")\n";
1380b57cec5SDimitry Andric   }
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
141*5ffd83dbSDimitry Andric void elf::writeMapFile() {
1420b57cec5SDimitry Andric   if (config->mapFile.empty())
1430b57cec5SDimitry Andric     return;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   // Open a map file for writing.
1460b57cec5SDimitry Andric   std::error_code ec;
14785868e8aSDimitry Andric   raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
1480b57cec5SDimitry Andric   if (ec) {
1490b57cec5SDimitry Andric     error("cannot open " + config->mapFile + ": " + ec.message());
1500b57cec5SDimitry Andric     return;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   // Collect symbol info that we want to print out.
1540b57cec5SDimitry Andric   std::vector<Defined *> syms = getSymbols();
1550b57cec5SDimitry Andric   SymbolMapTy sectionSyms = getSectionSyms(syms);
1560b57cec5SDimitry Andric   DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // Print out the header line.
1590b57cec5SDimitry Andric   int w = config->is64 ? 16 : 8;
1600b57cec5SDimitry Andric   os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
1610b57cec5SDimitry Andric      << "     Size Align Out     In      Symbol\n";
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   OutputSection* osec = nullptr;
1640b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands) {
1650b57cec5SDimitry Andric     if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
1660b57cec5SDimitry Andric       if (cmd->provide && !cmd->sym)
1670b57cec5SDimitry Andric         continue;
1680b57cec5SDimitry Andric       uint64_t lma = osec ? osec->getLMA() + cmd->addr - osec->getVA(0) : 0;
1690b57cec5SDimitry Andric       writeHeader(os, cmd->addr, lma, cmd->size, 1);
1700b57cec5SDimitry Andric       os << cmd->commandString << '\n';
1710b57cec5SDimitry Andric       continue;
1720b57cec5SDimitry Andric     }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric     osec = cast<OutputSection>(base);
1750b57cec5SDimitry Andric     writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
1760b57cec5SDimitry Andric     os << osec->name << '\n';
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric     // Dump symbols for each input section.
1790b57cec5SDimitry Andric     for (BaseCommand *base : osec->sectionCommands) {
1800b57cec5SDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
1810b57cec5SDimitry Andric         for (InputSection *isec : isd->sections) {
1820b57cec5SDimitry Andric           if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
1830b57cec5SDimitry Andric             printEhFrame(os, ehSec);
1840b57cec5SDimitry Andric             continue;
1850b57cec5SDimitry Andric           }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric           writeHeader(os, isec->getVA(0), osec->getLMA() + isec->getOffset(0),
1880b57cec5SDimitry Andric                       isec->getSize(), isec->alignment);
1890b57cec5SDimitry Andric           os << indent8 << toString(isec) << '\n';
1900b57cec5SDimitry Andric           for (Symbol *sym : sectionSyms[isec])
1910b57cec5SDimitry Andric             os << symStr[sym] << '\n';
1920b57cec5SDimitry Andric         }
1930b57cec5SDimitry Andric         continue;
1940b57cec5SDimitry Andric       }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric       if (auto *cmd = dyn_cast<ByteCommand>(base)) {
1970b57cec5SDimitry Andric         writeHeader(os, osec->addr + cmd->offset, osec->getLMA() + cmd->offset,
1980b57cec5SDimitry Andric                     cmd->size, 1);
1990b57cec5SDimitry Andric         os << indent8 << cmd->commandString << '\n';
2000b57cec5SDimitry Andric         continue;
2010b57cec5SDimitry Andric       }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric       if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
2040b57cec5SDimitry Andric         if (cmd->provide && !cmd->sym)
2050b57cec5SDimitry Andric           continue;
2060b57cec5SDimitry Andric         writeHeader(os, cmd->addr, osec->getLMA() + cmd->addr - osec->getVA(0),
2070b57cec5SDimitry Andric                     cmd->size, 1);
2080b57cec5SDimitry Andric         os << indent8 << cmd->commandString << '\n';
2090b57cec5SDimitry Andric         continue;
2100b57cec5SDimitry Andric       }
2110b57cec5SDimitry Andric     }
2120b57cec5SDimitry Andric   }
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric static void print(StringRef a, StringRef b) {
216480093f4SDimitry Andric   lld::outs() << left_justify(a, 49) << " " << b << "\n";
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric // Output a cross reference table to stdout. This is for --cref.
2200b57cec5SDimitry Andric //
2210b57cec5SDimitry Andric // For each global symbol, we print out a file that defines the symbol
2220b57cec5SDimitry Andric // followed by files that uses that symbol. Here is an example.
2230b57cec5SDimitry Andric //
2240b57cec5SDimitry Andric //     strlen     /lib/x86_64-linux-gnu/libc.so.6
2250b57cec5SDimitry Andric //                tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
2260b57cec5SDimitry Andric //                lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
2270b57cec5SDimitry Andric //
2280b57cec5SDimitry Andric // In this case, strlen is defined by libc.so.6 and used by other two
2290b57cec5SDimitry Andric // files.
230*5ffd83dbSDimitry Andric void elf::writeCrossReferenceTable() {
2310b57cec5SDimitry Andric   if (!config->cref)
2320b57cec5SDimitry Andric     return;
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   // Collect symbols and files.
2350b57cec5SDimitry Andric   MapVector<Symbol *, SetVector<InputFile *>> map;
2360b57cec5SDimitry Andric   for (InputFile *file : objectFiles) {
2370b57cec5SDimitry Andric     for (Symbol *sym : file->getSymbols()) {
2380b57cec5SDimitry Andric       if (isa<SharedSymbol>(sym))
2390b57cec5SDimitry Andric         map[sym].insert(file);
2400b57cec5SDimitry Andric       if (auto *d = dyn_cast<Defined>(sym))
2410b57cec5SDimitry Andric         if (!d->isLocal() && (!d->section || d->section->isLive()))
2420b57cec5SDimitry Andric           map[d].insert(file);
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric   }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   // Print out a header.
247480093f4SDimitry Andric   lld::outs() << "Cross Reference Table\n\n";
2480b57cec5SDimitry Andric   print("Symbol", "File");
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   // Print out a table.
2510b57cec5SDimitry Andric   for (auto kv : map) {
2520b57cec5SDimitry Andric     Symbol *sym = kv.first;
2530b57cec5SDimitry Andric     SetVector<InputFile *> &files = kv.second;
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric     print(toString(*sym), toString(sym->file));
2560b57cec5SDimitry Andric     for (InputFile *file : files)
2570b57cec5SDimitry Andric       if (file != sym->file)
2580b57cec5SDimitry Andric         print("", toString(file));
2590b57cec5SDimitry Andric   }
2600b57cec5SDimitry Andric }
26185868e8aSDimitry Andric 
262*5ffd83dbSDimitry Andric void elf::writeArchiveStats() {
263*5ffd83dbSDimitry Andric   if (config->printArchiveStats.empty())
264*5ffd83dbSDimitry Andric     return;
265*5ffd83dbSDimitry Andric 
266*5ffd83dbSDimitry Andric   std::error_code ec;
267*5ffd83dbSDimitry Andric   raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None);
268*5ffd83dbSDimitry Andric   if (ec) {
269*5ffd83dbSDimitry Andric     error("--print-archive-stats=: cannot open " + config->printArchiveStats +
270*5ffd83dbSDimitry Andric           ": " + ec.message());
271*5ffd83dbSDimitry Andric     return;
272*5ffd83dbSDimitry Andric   }
273*5ffd83dbSDimitry Andric 
274*5ffd83dbSDimitry Andric   os << "members\tfetched\tarchive\n";
275*5ffd83dbSDimitry Andric   for (const ArchiveFile *f : archiveFiles)
276*5ffd83dbSDimitry Andric     os << f->getMemberCount() << '\t' << f->getFetchedMemberCount() << '\t'
277*5ffd83dbSDimitry Andric        << f->getName() << '\n';
278*5ffd83dbSDimitry Andric }
279