xref: /llvm-project-15.0.7/lld/ELF/MapFile.cpp (revision a730ed31)
1 //===- 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 //
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)
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #include "MapFile.h"
25 #include "InputFiles.h"
26 #include "Strings.h"
27 
28 #include "llvm/Support/raw_ostream.h"
29 
30 using namespace llvm;
31 using namespace llvm::object;
32 
33 using namespace lld;
34 using namespace lld::elf;
35 
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 %5llx ", Width, Address, Width, Size, Align)
39      << left_justify(Name, 7);
40 }
41 
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);
47 }
48 
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);
54 }
55 
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);
61 }
62 
63 template <class ELFT>
64 static void writeInputSection(raw_fd_ostream &OS, const InputSection<ELFT> *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(),
70                    IS->Alignment, Name);
71     OS << '\n';
72     PrevName = Name;
73   }
74 
75   elf::ObjectFile<ELFT> *File = IS->getFile();
76   if (!File)
77     return;
78   writeFileLine(OS, Width, IS->OutSec->Addr + IS->OutSecOff, IS->getSize(),
79                 IS->Alignment, toString(File));
80   OS << '\n';
81 
82   for (SymbolBody *Sym : File->getSymbols()) {
83     auto *DR = dyn_cast<DefinedRegular<ELFT>>(Sym);
84     if (!DR)
85       continue;
86     if (DR->Section != IS)
87       continue;
88     if (DR->isSection())
89       continue;
90     writeSymbolLine(OS, Width, Sym->getVA<ELFT>(), Sym->getSize<ELFT>(),
91                     toString(*Sym));
92     OS << '\n';
93   }
94 }
95 
96 template <class ELFT>
97 static void writeMapFile2(raw_fd_ostream &OS,
98                           ArrayRef<OutputSectionBase *> OutputSections) {
99   int Width = ELFT::Is64Bits ? 16 : 8;
100 
101   OS << left_justify("Address", Width) << ' ' << left_justify("Size", Width)
102      << " Align Out     In      File    Symbol\n";
103 
104   for (OutputSectionBase *Sec : OutputSections) {
105     writeOutSecLine(OS, Width, Sec->Addr, Sec->Size, Sec->Addralign,
106                     Sec->getName());
107     OS << '\n';
108 
109     StringRef PrevName = "";
110     Sec->forEachInputSection([&](InputSectionData *S) {
111       if (const auto *IS = dyn_cast<InputSection<ELFT>>(S))
112         writeInputSection(OS, IS, PrevName);
113     });
114   }
115 }
116 
117 template <class ELFT>
118 void elf::writeMapFile(ArrayRef<OutputSectionBase *> OutputSections) {
119   if (Config->MapFile.empty())
120     return;
121 
122   std::error_code EC;
123   raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
124   if (EC)
125     error("cannot open " + Config->MapFile + ": " + EC.message());
126   else
127     writeMapFile2<ELFT>(OS, OutputSections);
128 }
129 
130 template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSectionBase *>);
131 template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSectionBase *>);
132 template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSectionBase *>);
133 template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSectionBase *>);
134