blob: 58ad4f3a178651c7853fd74e24094edf145edb71 [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"
Rafael Espindolaa46f6882017-05-18 21:30:14 +000024#include "LinkerScript.h"
Rafael Espindola1b414092017-05-18 17:26:00 +000025#include "OutputSections.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000026#include "Strings.h"
Rui Ueyamab882e592017-04-28 17:19:13 +000027#include "SymbolTable.h"
28#include "Threads.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000029
Rui Ueyama543731f2017-01-15 00:41:21 +000030#include "llvm/Support/raw_ostream.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000031
32using namespace llvm;
33using namespace llvm::object;
34
35using namespace lld;
36using namespace lld::elf;
37
Rui Ueyama31519582017-04-28 23:29:15 +000038typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
39 SymbolMapTy;
Rui Ueyamab882e592017-04-28 17:19:13 +000040
41// Print out the first three columns of a line.
Rui Ueyamab882e592017-04-28 17:19:13 +000042static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
43 uint64_t Align) {
George Rimar7b30dd12017-07-28 11:13:21 +000044 int W = Config->Is64 ? 16 : 8;
Rui Ueyamab882e592017-04-28 17:19:13 +000045 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
Rafael Espindola1ebfc592017-01-13 21:05:46 +000046}
47
Rui Ueyamab882e592017-04-28 17:19:13 +000048static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
Rafael Espindola1ebfc592017-01-13 21:05:46 +000049
Rui Ueyama31519582017-04-28 23:29:15 +000050// Returns a list of all symbols that we want to print out.
George Rimar7b30dd12017-07-28 11:13:21 +000051template <class ELFT> static std::vector<DefinedRegular *> getSymbols() {
Rui Ueyama31519582017-04-28 23:29:15 +000052 std::vector<DefinedRegular *> V;
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000053 for (ObjFile<ELFT> *File : ObjFile<ELFT>::Instances)
Rui Ueyamab882e592017-04-28 17:19:13 +000054 for (SymbolBody *B : File->getSymbols())
55 if (B->File == File && !B->isSection())
56 if (auto *Sym = dyn_cast<DefinedRegular>(B))
Peter Collingbourneb069a362017-07-14 00:31:56 +000057 if (Sym->Section && Sym->Section->Live)
Rui Ueyama31519582017-04-28 23:29:15 +000058 V.push_back(Sym);
59 return V;
60}
Rafael Espindola1ebfc592017-01-13 21:05:46 +000061
Rui Ueyama31519582017-04-28 23:29:15 +000062// Returns a map from sections to their symbols.
George Rimar7b30dd12017-07-28 11:13:21 +000063static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
Rui Ueyama31519582017-04-28 23:29:15 +000064 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>
George Rimar7b30dd12017-07-28 11:13:21 +000084static DenseMap<DefinedRegular *, std::string>
Rui Ueyama31519582017-04-28 23:29:15 +000085getSymbolStrings(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]);
George Rimar7b30dd12017-07-28 11:13:21 +000089 writeHeader(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(), 0);
Rui Ueyama31519582017-04-28 23:29:15 +000090 OS << indent(2) << toString(*Syms[I]);
Rui Ueyamab882e592017-04-28 17:19:13 +000091 });
Rui Ueyama31519582017-04-28 23:29:15 +000092
93 DenseMap<DefinedRegular *, std::string> Ret;
Rui Ueyamab882e592017-04-28 17:19:13 +000094 for (size_t I = 0, E = Syms.size(); I < E; ++I)
Rui Ueyama31519582017-04-28 23:29:15 +000095 Ret[Syms[I]] = std::move(Str[I]);
96 return Ret;
Rafael Espindola1ebfc592017-01-13 21:05:46 +000097}
98
Rafael Espindola8c022ca2017-07-27 19:22:43 +000099template <class ELFT> void elf::writeMapFile() {
Rui Ueyama543731f2017-01-15 00:41:21 +0000100 if (Config->MapFile.empty())
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000101 return;
102
Rui Ueyama31519582017-04-28 23:29:15 +0000103 // Open a map file for writing.
Rui Ueyama543731f2017-01-15 00:41:21 +0000104 std::error_code EC;
105 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
Rui Ueyama31519582017-04-28 23:29:15 +0000106 if (EC) {
Rui Ueyamac9807c32017-01-16 01:07:19 +0000107 error("cannot open " + Config->MapFile + ": " + EC.message());
Rui Ueyama31519582017-04-28 23:29:15 +0000108 return;
109 }
110
111 // Collect symbol info that we want to print out.
112 std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
George Rimar7b30dd12017-07-28 11:13:21 +0000113 SymbolMapTy SectionSyms = getSectionSyms(Syms);
Rui Ueyama31519582017-04-28 23:29:15 +0000114 DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
115
116 // Print out the header line.
117 int W = ELFT::Is64Bits ? 16 : 8;
118 OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
119 << " Align Out In Symbol\n";
120
121 // Print out file contents.
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000122 for (OutputSection *OSec : OutputSections) {
George Rimar7b30dd12017-07-28 11:13:21 +0000123 writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
Rui Ueyama31519582017-04-28 23:29:15 +0000124 OS << OSec->Name << '\n';
125
126 // Dump symbols for each input section.
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000127 for (BaseCommand *Base : OSec->Commands) {
Rafael Espindolaaeb4b7d2017-05-23 22:54:06 +0000128 auto *ISD = dyn_cast<InputSectionDescription>(Base);
129 if (!ISD)
130 continue;
131 for (InputSection *IS : ISD->Sections) {
George Rimar7b30dd12017-07-28 11:13:21 +0000132 writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
133 IS->Alignment);
Rafael Espindolaaeb4b7d2017-05-23 22:54:06 +0000134 OS << indent(1) << toString(IS) << '\n';
135 for (DefinedRegular *Sym : SectionSyms[IS])
136 OS << SymStr[Sym] << '\n';
137 }
Rui Ueyama31519582017-04-28 23:29:15 +0000138 }
139 }
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000140}
141
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000142template void elf::writeMapFile<ELF32LE>();
143template void elf::writeMapFile<ELF32BE>();
144template void elf::writeMapFile<ELF64LE>();
145template void elf::writeMapFile<ELF64BE>();