blob: ad80b0696ebecc928785e5d86f45fa643eaf5f76 [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"
George Rimarf2fe9632017-08-11 11:34:04 +000028#include "SyntheticSections.h"
Bob Haarman4f5c8c22017-10-13 18:22:55 +000029
30#include "lld/Common/Threads.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000031
Rui Ueyama543731f2017-01-15 00:41:21 +000032#include "llvm/Support/raw_ostream.h"
Rafael Espindola1ebfc592017-01-13 21:05:46 +000033
34using namespace llvm;
35using namespace llvm::object;
36
37using namespace lld;
38using namespace lld::elf;
39
George Rimarf2fe9632017-08-11 11:34:04 +000040typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy;
Rui Ueyamab882e592017-04-28 17:19:13 +000041
42// Print out the first three columns of a line.
Rui Ueyamab882e592017-04-28 17:19:13 +000043static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
44 uint64_t Align) {
George Rimar7b30dd12017-07-28 11:13:21 +000045 int W = Config->Is64 ? 16 : 8;
Rui Ueyamab882e592017-04-28 17:19:13 +000046 OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
Rafael Espindola1ebfc592017-01-13 21:05:46 +000047}
48
Rui Ueyamab882e592017-04-28 17:19:13 +000049static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
Rafael Espindola1ebfc592017-01-13 21:05:46 +000050
Rui Ueyama31519582017-04-28 23:29:15 +000051// Returns a list of all symbols that we want to print out.
George Rimarf2fe9632017-08-11 11:34:04 +000052template <class ELFT> static std::vector<Defined *> getSymbols() {
53 std::vector<Defined *> V;
George Rimar696a7f92017-09-19 09:20:54 +000054 for (InputFile *File : ObjectFiles) {
George Rimarf2fe9632017-08-11 11:34:04 +000055 for (SymbolBody *B : File->getSymbols()) {
56 if (auto *DR = dyn_cast<DefinedRegular>(B)) {
Rafael Espindola6e93d052017-08-04 22:31:42 +000057 if (DR->getFile() == File && !DR->isSection() && DR->Section &&
Rafael Espindolaf456f572017-08-04 18:42:04 +000058 DR->Section->Live)
59 V.push_back(DR);
George Rimarf2fe9632017-08-11 11:34:04 +000060 } else if (auto *DC = dyn_cast<DefinedCommon>(B)) {
Dmitry Mikulin1e30f072017-09-08 16:22:43 +000061 if (DC->Section)
George Rimar45d27672017-08-11 11:46:56 +000062 V.push_back(DC);
George Rimarf2fe9632017-08-11 11:34:04 +000063 }
64 }
65 }
Rui Ueyama31519582017-04-28 23:29:15 +000066 return V;
67}
Rafael Espindola1ebfc592017-01-13 21:05:46 +000068
Rui Ueyama31519582017-04-28 23:29:15 +000069// Returns a map from sections to their symbols.
George Rimarf2fe9632017-08-11 11:34:04 +000070static SymbolMapTy getSectionSyms(ArrayRef<Defined *> Syms) {
Rui Ueyama31519582017-04-28 23:29:15 +000071 SymbolMapTy Ret;
George Rimarf2fe9632017-08-11 11:34:04 +000072 for (Defined *S : Syms) {
73 if (auto *DR = dyn_cast<DefinedRegular>(S))
74 Ret[DR->Section].push_back(S);
Dmitry Mikulin1e30f072017-09-08 16:22:43 +000075 else if (auto *DC = dyn_cast<DefinedCommon>(S))
76 Ret[DC->Section].push_back(S);
George Rimarf2fe9632017-08-11 11:34:04 +000077 }
Rui Ueyamab882e592017-04-28 17:19:13 +000078
79 // Sort symbols by address. We want to print out symbols in the
80 // order in the output file rather than the order they appeared
81 // in the input files.
Rui Ueyama31519582017-04-28 23:29:15 +000082 for (auto &It : Ret) {
George Rimarf2fe9632017-08-11 11:34:04 +000083 SmallVectorImpl<Defined *> &V = It.second;
84 std::sort(V.begin(), V.end(),
85 [](Defined *A, Defined *B) { return A->getVA() < B->getVA(); });
Rui Ueyamab882e592017-04-28 17:19:13 +000086 }
Rui Ueyama31519582017-04-28 23:29:15 +000087 return Ret;
88}
Rui Ueyamab882e592017-04-28 17:19:13 +000089
Rui Ueyama31519582017-04-28 23:29:15 +000090// Construct a map from symbols to their stringified representations.
91// Demangling symbols (which is what toString() does) is slow, so
92// we do that in batch using parallel-for.
93template <class ELFT>
George Rimarf2fe9632017-08-11 11:34:04 +000094static DenseMap<Defined *, std::string>
95getSymbolStrings(ArrayRef<Defined *> Syms) {
Rui Ueyamab882e592017-04-28 17:19:13 +000096 std::vector<std::string> Str(Syms.size());
Rui Ueyama33d903d2017-05-10 20:02:19 +000097 parallelForEachN(0, Syms.size(), [&](size_t I) {
Rui Ueyamab882e592017-04-28 17:19:13 +000098 raw_string_ostream OS(Str[I]);
George Rimar7b30dd12017-07-28 11:13:21 +000099 writeHeader(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(), 0);
Rui Ueyama31519582017-04-28 23:29:15 +0000100 OS << indent(2) << toString(*Syms[I]);
Rui Ueyamab882e592017-04-28 17:19:13 +0000101 });
Rui Ueyama31519582017-04-28 23:29:15 +0000102
George Rimarf2fe9632017-08-11 11:34:04 +0000103 DenseMap<Defined *, std::string> Ret;
Rui Ueyamab882e592017-04-28 17:19:13 +0000104 for (size_t I = 0, E = Syms.size(); I < E; ++I)
Rui Ueyama31519582017-04-28 23:29:15 +0000105 Ret[Syms[I]] = std::move(Str[I]);
106 return Ret;
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000107}
108
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000109template <class ELFT> void elf::writeMapFile() {
Rui Ueyama543731f2017-01-15 00:41:21 +0000110 if (Config->MapFile.empty())
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000111 return;
112
Rui Ueyama31519582017-04-28 23:29:15 +0000113 // Open a map file for writing.
Rui Ueyama543731f2017-01-15 00:41:21 +0000114 std::error_code EC;
115 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
Rui Ueyama31519582017-04-28 23:29:15 +0000116 if (EC) {
Rui Ueyamac9807c32017-01-16 01:07:19 +0000117 error("cannot open " + Config->MapFile + ": " + EC.message());
Rui Ueyama31519582017-04-28 23:29:15 +0000118 return;
119 }
120
121 // Collect symbol info that we want to print out.
George Rimarf2fe9632017-08-11 11:34:04 +0000122 std::vector<Defined *> Syms = getSymbols<ELFT>();
George Rimar7b30dd12017-07-28 11:13:21 +0000123 SymbolMapTy SectionSyms = getSectionSyms(Syms);
George Rimarf2fe9632017-08-11 11:34:04 +0000124 DenseMap<Defined *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
Rui Ueyama31519582017-04-28 23:29:15 +0000125
126 // Print out the header line.
127 int W = ELFT::Is64Bits ? 16 : 8;
128 OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
129 << " Align Out In Symbol\n";
130
131 // Print out file contents.
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000132 for (OutputSection *OSec : OutputSections) {
George Rimar7b30dd12017-07-28 11:13:21 +0000133 writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
Rui Ueyama31519582017-04-28 23:29:15 +0000134 OS << OSec->Name << '\n';
135
136 // Dump symbols for each input section.
Rui Ueyama6b394ca2017-10-11 01:50:56 +0000137 for (BaseCommand *Base : OSec->SectionCommands) {
Rafael Espindolaaeb4b7d2017-05-23 22:54:06 +0000138 auto *ISD = dyn_cast<InputSectionDescription>(Base);
139 if (!ISD)
140 continue;
141 for (InputSection *IS : ISD->Sections) {
George Rimar7b30dd12017-07-28 11:13:21 +0000142 writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
143 IS->Alignment);
Rafael Espindolaaeb4b7d2017-05-23 22:54:06 +0000144 OS << indent(1) << toString(IS) << '\n';
George Rimarf2fe9632017-08-11 11:34:04 +0000145 for (Defined *Sym : SectionSyms[IS])
Rafael Espindolaaeb4b7d2017-05-23 22:54:06 +0000146 OS << SymStr[Sym] << '\n';
147 }
Rui Ueyama31519582017-04-28 23:29:15 +0000148 }
149 }
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000150}
151
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000152template void elf::writeMapFile<ELF32LE>();
153template void elf::writeMapFile<ELF32BE>();
154template void elf::writeMapFile<ELF64LE>();
155template void elf::writeMapFile<ELF64BE>();