blob: 23c63e845c9a7f7c6dbaf87fbe29b0c4fdc2a223 [file] [log] [blame]
Rafael Espindola1ebfc592017-01-13 21:05:46 +00001//===- 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//
Rui Ueyama3012b372017-04-28 20:38:27 +000014// Address Size Align Out In Symbol
15// 00201000 00000015 4 .text
Rui Ueyama37811372017-04-30 20:58:20 +000016// 00201000 0000000e 4 test.o:(.text)
Rui Ueyama3012b372017-04-28 20:38:27 +000017// 0020100e 00000000 0 local
18// 00201005 00000000 0 f(int)
Rafael Espindola1ebfc592017-01-13 21:05:46 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "MapFile.h"
23#include "InputFiles.h"
24#include "Strings.h"
Rui Ueyamab882e592017-04-28 17:19:13 +000025#include "SymbolTable.h"
26#include "Threads.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000027
Rui Ueyama543731f2017-01-15 00:41:21 +000028#include "llvm/Support/raw_ostream.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000029
30using namespace llvm;
31using namespace llvm::object;
32
33using namespace lld;
34using namespace lld::elf;
35
Rui Ueyama31519582017-04-28 23:29:15 +000036typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
37 SymbolMapTy;
Rui Ueyamab882e592017-04-28 17:19:13 +000038
39// Print out the first three columns of a line.
40template <class ELFT>
41static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
42 uint64_t Align) {
43 int W = ELFT::Is64Bits ? 16 : 8;
44 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
Rafael Espindola1ebfc592017-01-13 21:05:46 +000045}
46
Rui Ueyamab882e592017-04-28 17:19:13 +000047static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
Rafael Espindola1ebfc592017-01-13 21:05:46 +000048
Rui Ueyama31519582017-04-28 23:29:15 +000049// Returns a list of all symbols that we want to print out.
50template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
51 std::vector<DefinedRegular *> V;
Rui Ueyamab882e592017-04-28 17:19:13 +000052 for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
53 for (SymbolBody *B : File->getSymbols())
54 if (B->File == File && !B->isSection())
55 if (auto *Sym = dyn_cast<DefinedRegular>(B))
56 if (Sym->Section)
Rui Ueyama31519582017-04-28 23:29:15 +000057 V.push_back(Sym);
58 return V;
59}
Rafael Espindola1ebfc592017-01-13 21:05:46 +000060
Rui Ueyama31519582017-04-28 23:29:15 +000061// Returns a map from sections to their symbols.
62template <class ELFT>
63SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
64 SymbolMapTy Ret;
65 for (DefinedRegular *S : Syms)
66 Ret[S->Section].push_back(S);
Rui Ueyamab882e592017-04-28 17:19:13 +000067
68 // Sort symbols by address. We want to print out symbols in the
69 // order in the output file rather than the order they appeared
70 // in the input files.
Rui Ueyama31519582017-04-28 23:29:15 +000071 for (auto &It : Ret) {
Rui Ueyamab882e592017-04-28 17:19:13 +000072 SmallVectorImpl<DefinedRegular *> &V = It.second;
73 std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
74 return A->getVA() < B->getVA();
75 });
76 }
Rui Ueyama31519582017-04-28 23:29:15 +000077 return Ret;
78}
Rui Ueyamab882e592017-04-28 17:19:13 +000079
Rui Ueyama31519582017-04-28 23:29:15 +000080// Construct a map from symbols to their stringified representations.
81// Demangling symbols (which is what toString() does) is slow, so
82// we do that in batch using parallel-for.
83template <class ELFT>
84DenseMap<DefinedRegular *, std::string>
85getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
Rui Ueyamab882e592017-04-28 17:19:13 +000086 std::vector<std::string> Str(Syms.size());
Rui Ueyama33d903d2017-05-10 20:02:19 +000087 parallelForEachN(0, Syms.size(), [&](size_t I) {
Rui Ueyamab882e592017-04-28 17:19:13 +000088 raw_string_ostream OS(Str[I]);
89 writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
90 0);
Rui Ueyama31519582017-04-28 23:29:15 +000091 OS << indent(2) << toString(*Syms[I]);
Rui Ueyamab882e592017-04-28 17:19:13 +000092 });
Rui Ueyama31519582017-04-28 23:29:15 +000093
94 DenseMap<DefinedRegular *, std::string> Ret;
Rui Ueyamab882e592017-04-28 17:19:13 +000095 for (size_t I = 0, E = Syms.size(); I < E; ++I)
Rui Ueyama31519582017-04-28 23:29:15 +000096 Ret[Syms[I]] = std::move(Str[I]);
97 return Ret;
Rafael Espindola1ebfc592017-01-13 21:05:46 +000098}
99
100template <class ELFT>
Rafael Espindola24e6f362017-02-24 15:07:30 +0000101void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
Rui Ueyama543731f2017-01-15 00:41:21 +0000102 if (Config->MapFile.empty())
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000103 return;
104
Rui Ueyama31519582017-04-28 23:29:15 +0000105 // Open a map file for writing.
Rui Ueyama543731f2017-01-15 00:41:21 +0000106 std::error_code EC;
107 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
Rui Ueyama31519582017-04-28 23:29:15 +0000108 if (EC) {
Rui Ueyamac9807c32017-01-16 01:07:19 +0000109 error("cannot open " + Config->MapFile + ": " + EC.message());
Rui Ueyama31519582017-04-28 23:29:15 +0000110 return;
111 }
112
113 // Collect symbol info that we want to print out.
114 std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
115 SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
116 DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
117
118 // Print out the header line.
119 int W = ELFT::Is64Bits ? 16 : 8;
120 OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
121 << " Align Out In Symbol\n";
122
123 // Print out file contents.
124 for (OutputSection *OSec : OutputSections) {
125 writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
126 OS << OSec->Name << '\n';
127
128 // Dump symbols for each input section.
129 for (InputSection *IS : OSec->Sections) {
130 writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
131 IS->Alignment);
132 OS << indent(1) << toString(IS) << '\n';
133 for (DefinedRegular *Sym : SectionSyms[IS])
134 OS << SymStr[Sym] << '\n';
135 }
136 }
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000137}
138
Rafael Espindola24e6f362017-02-24 15:07:30 +0000139template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>);
140template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>);
141template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>);
142template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>);