1 //===- MapFile.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the -Map option. It shows lists in order and
11 // hierarchically the output sections, input sections, input files and
14 // Address Size Align Out In File Symbol
15 // =================================================================
16 // 00201000 00000015 4 .text
17 // 00201000 0000000e 4 .text
18 // 00201000 0000000e 4 test.o
19 // 0020100e 00000000 0 local
20 // 00201005 00000000 0 f(int)
22 //===----------------------------------------------------------------------===//
25 #include "InputFiles.h"
28 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm::object;
34 using namespace lld::elf;
36 static void writeOutSecLine(raw_fd_ostream &OS, int Width, uint64_t Address,
37 uint64_t Size, uint64_t Align, StringRef Name) {
38 OS << format("%0*llx %0*llx %5lld ", Width, Address, Width, Size, Align)
39 << left_justify(Name, 7);
42 static void writeInSecLine(raw_fd_ostream &OS, int Width, uint64_t Address,
43 uint64_t Size, uint64_t Align, StringRef Name) {
44 // Pass an empty name to align the text to the correct column.
45 writeOutSecLine(OS, Width, Address, Size, Align, "");
46 OS << ' ' << left_justify(Name, 7);
49 static void writeFileLine(raw_fd_ostream &OS, int Width, uint64_t Address,
50 uint64_t Size, uint64_t Align, StringRef Name) {
51 // Pass an empty name to align the text to the correct column.
52 writeInSecLine(OS, Width, Address, Size, Align, "");
53 OS << ' ' << left_justify(Name, 7);
56 static void writeSymbolLine(raw_fd_ostream &OS, int Width, uint64_t Address,
57 uint64_t Size, StringRef Name) {
58 // Pass an empty name to align the text to the correct column.
59 writeFileLine(OS, Width, Address, Size, 0, "");
60 OS << ' ' << left_justify(Name, 7);
64 static void writeInputSection(raw_fd_ostream &OS, const InputSection *IS,
65 StringRef &PrevName) {
66 int Width = ELFT::Is64Bits ? 16 : 8;
67 StringRef Name = IS->Name;
68 if (Name != PrevName) {
69 writeInSecLine(OS, Width, IS->OutSec->Addr + IS->OutSecOff, IS->getSize(),
75 elf::ObjectFile<ELFT> *File = IS->template getFile<ELFT>();
78 writeFileLine(OS, Width, IS->OutSec->Addr + IS->OutSecOff, IS->getSize(),
79 IS->Alignment, toString(File));
82 for (SymbolBody *Sym : File->getSymbols()) {
83 auto *DR = dyn_cast<DefinedRegular>(Sym);
86 if (DR->Section != IS)
90 writeSymbolLine(OS, Width, Sym->getVA(), Sym->getSize<ELFT>(),
97 static void writeMapFile2(raw_fd_ostream &OS,
98 ArrayRef<OutputSection *> OutputSections) {
99 int Width = ELFT::Is64Bits ? 16 : 8;
101 OS << left_justify("Address", Width) << ' ' << left_justify("Size", Width)
102 << " Align Out In File Symbol\n";
104 for (OutputSection *Sec : OutputSections) {
105 writeOutSecLine(OS, Width, Sec->Addr, Sec->Size, Sec->Alignment, Sec->Name);
108 StringRef PrevName = "";
109 for (InputSection *IS : Sec->Sections) {
110 writeInputSection<ELFT>(OS, IS, PrevName);
115 template <class ELFT>
116 void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
117 if (Config->MapFile.empty())
121 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
123 error("cannot open " + Config->MapFile + ": " + EC.message());
125 writeMapFile2<ELFT>(OS, OutputSections);
128 template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>);
129 template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>);
130 template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>);
131 template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>);