1 //===- MapFile.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the -Map option. It shows lists in order and
10 // hierarchically the output sections, input sections, input files and
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)
19 //===----------------------------------------------------------------------===//
22 #include "InputFiles.h"
23 #include "LinkerScript.h"
24 #include "OutputSections.h"
25 #include "SymbolTable.h"
27 #include "SyntheticSections.h"
28 #include "lld/Common/Strings.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/Support/Parallel.h"
32 #include "llvm/Support/raw_ostream.h"
35 using namespace llvm::object;
37 using namespace lld::elf;
39 using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
41 static constexpr char indent8[] = " "; // 8 spaces
42 static constexpr char indent16[] = " "; // 16 spaces
44 // Print out the first three columns of a line.
45 static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
46 uint64_t size, uint64_t align) {
48 os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
50 os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
53 // Returns a list of all symbols that we want to print out.
54 static std::vector<Defined *> getSymbols() {
55 std::vector<Defined *> v;
56 for (InputFile *file : objectFiles)
57 for (Symbol *b : file->getSymbols())
58 if (auto *dr = dyn_cast<Defined>(b))
59 if (!dr->isSection() && dr->section && dr->section->isLive() &&
60 (dr->file == file || dr->needsPltAddr || dr->section->bss))
65 // Returns a map from sections to their symbols.
66 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
68 for (Defined *dr : syms)
69 ret[dr->section].push_back(dr);
71 // Sort symbols by address. We want to print out symbols in the
72 // order in the output file rather than the order they appeared
73 // in the input files.
75 llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
76 return a->getVA() < b->getVA();
81 // Construct a map from symbols to their stringified representations.
82 // Demangling symbols (which is what toString() does) is slow, so
83 // we do that in batch using parallel-for.
84 static DenseMap<Symbol *, std::string>
85 getSymbolStrings(ArrayRef<Defined *> syms) {
86 std::vector<std::string> str(syms.size());
87 parallelForEachN(0, syms.size(), [&](size_t i) {
88 raw_string_ostream os(str[i]);
89 OutputSection *osec = syms[i]->getOutputSection();
90 uint64_t vma = syms[i]->getVA();
91 uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
92 writeHeader(os, vma, lma, syms[i]->getSize(), 1);
93 os << indent16 << toString(*syms[i]);
96 DenseMap<Symbol *, std::string> ret;
97 for (size_t i = 0, e = syms.size(); i < e; ++i)
98 ret[syms[i]] = std::move(str[i]);
102 // Print .eh_frame contents. Since the section consists of EhSectionPieces,
103 // we need a specialized printer for that section.
105 // .eh_frame tend to contain a lot of section pieces that are contiguous
106 // both in input file and output file. Such pieces are squashed before
107 // being displayed to make output compact.
108 static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
109 std::vector<EhSectionPiece> pieces;
111 auto add = [&](const EhSectionPiece &p) {
112 // If P is adjacent to Last, squash the two.
113 if (!pieces.empty()) {
114 EhSectionPiece &last = pieces.back();
115 if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
116 last.outputOff + last.size == p.outputOff) {
124 // Gather section pieces.
125 for (const CieRecord *rec : sec->getCieRecords()) {
127 for (const EhSectionPiece *fde : rec->fdes)
131 // Print out section pieces.
132 const OutputSection *osec = sec->getOutputSection();
133 for (EhSectionPiece &p : pieces) {
134 writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
136 os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
137 << Twine::utohexstr(p.inputOff) + ")\n";
141 void elf::writeMapFile() {
142 if (config->mapFile.empty())
145 // Open a map file for writing.
147 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
149 error("cannot open " + config->mapFile + ": " + ec.message());
153 // Collect symbol info that we want to print out.
154 std::vector<Defined *> syms = getSymbols();
155 SymbolMapTy sectionSyms = getSectionSyms(syms);
156 DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
158 // Print out the header line.
159 int w = config->is64 ? 16 : 8;
160 os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
161 << " Size Align Out In Symbol\n";
163 OutputSection* osec = nullptr;
164 for (BaseCommand *base : script->sectionCommands) {
165 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
166 if (cmd->provide && !cmd->sym)
168 uint64_t lma = osec ? osec->getLMA() + cmd->addr - osec->getVA(0) : 0;
169 writeHeader(os, cmd->addr, lma, cmd->size, 1);
170 os << cmd->commandString << '\n';
174 osec = cast<OutputSection>(base);
175 writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
176 os << osec->name << '\n';
178 // Dump symbols for each input section.
179 for (BaseCommand *base : osec->sectionCommands) {
180 if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
181 for (InputSection *isec : isd->sections) {
182 if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
183 printEhFrame(os, ehSec);
187 writeHeader(os, isec->getVA(0), osec->getLMA() + isec->getOffset(0),
188 isec->getSize(), isec->alignment);
189 os << indent8 << toString(isec) << '\n';
190 for (Symbol *sym : sectionSyms[isec])
191 os << symStr[sym] << '\n';
196 if (auto *cmd = dyn_cast<ByteCommand>(base)) {
197 writeHeader(os, osec->addr + cmd->offset, osec->getLMA() + cmd->offset,
199 os << indent8 << cmd->commandString << '\n';
203 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
204 if (cmd->provide && !cmd->sym)
206 writeHeader(os, cmd->addr, osec->getLMA() + cmd->addr - osec->getVA(0),
208 os << indent8 << cmd->commandString << '\n';
215 static void print(StringRef a, StringRef b) {
216 lld::outs() << left_justify(a, 49) << " " << b << "\n";
219 // Output a cross reference table to stdout. This is for --cref.
221 // For each global symbol, we print out a file that defines the symbol
222 // followed by files that uses that symbol. Here is an example.
224 // strlen /lib/x86_64-linux-gnu/libc.so.6
225 // tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
226 // lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
228 // In this case, strlen is defined by libc.so.6 and used by other two
230 void elf::writeCrossReferenceTable() {
234 // Collect symbols and files.
235 MapVector<Symbol *, SetVector<InputFile *>> map;
236 for (InputFile *file : objectFiles) {
237 for (Symbol *sym : file->getSymbols()) {
238 if (isa<SharedSymbol>(sym))
239 map[sym].insert(file);
240 if (auto *d = dyn_cast<Defined>(sym))
241 if (!d->isLocal() && (!d->section || d->section->isLive()))
246 // Print out a header.
247 lld::outs() << "Cross Reference Table\n\n";
248 print("Symbol", "File");
250 // Print out a table.
251 for (auto kv : map) {
252 Symbol *sym = kv.first;
253 SetVector<InputFile *> &files = kv.second;
255 print(toString(*sym), toString(sym->file));
256 for (InputFile *file : files)
257 if (file != sym->file)
258 print("", toString(file));
262 void elf::writeArchiveStats() {
263 if (config->printArchiveStats.empty())
267 raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None);
269 error("--print-archive-stats=: cannot open " + config->printArchiveStats +
270 ": " + ec.message());
274 os << "members\tfetched\tarchive\n";
275 for (const ArchiveFile *f : archiveFiles)
276 os << f->getMemberCount() << '\t' << f->getFetchedMemberCount() << '\t'
277 << f->getName() << '\n';