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 "lld/Common/Threads.h"
30 #include "llvm/ADT/MapVector.h"
31 #include "llvm/ADT/SetVector.h"
32 #include "llvm/Support/raw_ostream.h"
35 using namespace llvm::object;
38 using namespace lld::elf;
40 using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
42 static const std::string indent8 = " "; // 8 spaces
43 static const std::string indent16 = " "; // 16 spaces
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) {
49 os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
51 os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
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))
66 // Returns a map from sections to their symbols.
67 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
69 for (Defined *dr : syms)
70 ret[dr->section].push_back(dr);
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.
76 llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
77 return a->getVA() < b->getVA();
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]);
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]);
103 // Print .eh_frame contents. Since the section consists of EhSectionPieces,
104 // we need a specialized printer for that section.
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;
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) {
125 // Gather section pieces.
126 for (const CieRecord *rec : sec->getCieRecords()) {
128 for (const EhSectionPiece *fde : rec->fdes)
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,
137 os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
138 << Twine::utohexstr(p.inputOff) + ")\n";
142 void elf::writeMapFile() {
143 if (config->mapFile.empty())
146 // Open a map file for writing.
148 raw_fd_ostream os(config->mapFile, ec, sys::fs::F_None);
150 error("cannot open " + config->mapFile + ": " + ec.message());
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);
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";
164 OutputSection* osec = nullptr;
165 for (BaseCommand *base : script->sectionCommands) {
166 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
167 if (cmd->provide && !cmd->sym)
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';
175 osec = cast<OutputSection>(base);
176 writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
177 os << osec->name << '\n';
179 // Dump symbols for each input section.
180 for (BaseCommand *base : osec->sectionCommands) {
181 if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
182 for (InputSection *isec : isd->sections) {
183 if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
184 printEhFrame(os, ehSec);
188 writeHeader(os, isec->getVA(0), osec->getLMA() + isec->getOffset(0),
189 isec->getSize(), isec->alignment);
190 os << indent8 << toString(isec) << '\n';
191 for (Symbol *sym : sectionSyms[isec])
192 os << symStr[sym] << '\n';
197 if (auto *cmd = dyn_cast<ByteCommand>(base)) {
198 writeHeader(os, osec->addr + cmd->offset, osec->getLMA() + cmd->offset,
200 os << indent8 << cmd->commandString << '\n';
204 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
205 if (cmd->provide && !cmd->sym)
207 writeHeader(os, cmd->addr, osec->getLMA() + cmd->addr - osec->getVA(0),
209 os << indent8 << cmd->commandString << '\n';
216 static void print(StringRef a, StringRef b) {
217 outs() << left_justify(a, 49) << " " << b << "\n";
220 // Output a cross reference table to stdout. This is for --cref.
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.
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)
229 // In this case, strlen is defined by libc.so.6 and used by other two
231 void elf::writeCrossReferenceTable() {
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()))
247 // Print out a header.
248 outs() << "Cross Reference Table\n\n";
249 print("Symbol", "File");
251 // Print out a table.
252 for (auto kv : map) {
253 Symbol *sym = kv.first;
254 SetVector<InputFile *> &files = kv.second;
256 print(toString(*sym), toString(sym->file));
257 for (InputFile *file : files)
258 if (file != sym->file)
259 print("", toString(file));