1 //===- MapFile.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the -Map option. It shows lists in order and 10 // hierarchically the output sections, input sections, input files and 11 // symbol: 12 // 13 // Address Size Align Out In Symbol 14 // 00201000 00000015 4 .text 15 // 00201000 0000000e 4 test.o:(.text) 16 // 0020100e 00000000 0 local 17 // 00201005 00000000 0 f(int) 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "MapFile.h" 22 #include "InputFiles.h" 23 #include "LinkerScript.h" 24 #include "OutputSections.h" 25 #include "SymbolTable.h" 26 #include "Symbols.h" 27 #include "SyntheticSections.h" 28 #include "lld/Common/Strings.h" 29 #include "lld/Common/Threads.h" 30 #include "llvm/ADT/MapVector.h" 31 #include "llvm/ADT/SetVector.h" 32 #include "llvm/Support/raw_ostream.h" 33 34 using namespace llvm; 35 using namespace llvm::object; 36 37 using namespace lld; 38 using namespace lld::elf; 39 40 using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>; 41 42 static const std::string Indent8 = " "; // 8 spaces 43 static const std::string Indent16 = " "; // 16 spaces 44 45 // Print out the first three columns of a line. 46 static void writeHeader(raw_ostream &OS, uint64_t VMA, uint64_t LMA, 47 uint64_t Size, uint64_t Align) { 48 if (Config->Is64) 49 OS << format("%16llx %16llx %8llx %5lld ", VMA, LMA, Size, Align); 50 else 51 OS << format("%8llx %8llx %8llx %5lld ", VMA, LMA, Size, Align); 52 } 53 54 // Returns a list of all symbols that we want to print out. 55 static std::vector<Defined *> getSymbols() { 56 std::vector<Defined *> V; 57 for (InputFile *File : ObjectFiles) 58 for (Symbol *B : File->getSymbols()) 59 if (auto *DR = dyn_cast<Defined>(B)) 60 if (!DR->isSection() && DR->Section && DR->Section->Live && 61 (DR->File == File || DR->NeedsPltAddr || DR->Section->Bss)) 62 V.push_back(DR); 63 return V; 64 } 65 66 // Returns a map from sections to their symbols. 67 static SymbolMapTy getSectionSyms(ArrayRef<Defined *> Syms) { 68 SymbolMapTy Ret; 69 for (Defined *DR : Syms) 70 Ret[DR->Section].push_back(DR); 71 72 // Sort symbols by address. We want to print out symbols in the 73 // order in the output file rather than the order they appeared 74 // in the input files. 75 for (auto &It : Ret) 76 llvm::stable_sort(It.second, [](Defined *A, Defined *B) { 77 return A->getVA() < B->getVA(); 78 }); 79 return Ret; 80 } 81 82 // Construct a map from symbols to their stringified representations. 83 // Demangling symbols (which is what toString() does) is slow, so 84 // we do that in batch using parallel-for. 85 static DenseMap<Symbol *, std::string> 86 getSymbolStrings(ArrayRef<Defined *> Syms) { 87 std::vector<std::string> Str(Syms.size()); 88 parallelForEachN(0, Syms.size(), [&](size_t I) { 89 raw_string_ostream OS(Str[I]); 90 OutputSection *OSec = Syms[I]->getOutputSection(); 91 uint64_t VMA = Syms[I]->getVA(); 92 uint64_t LMA = OSec ? OSec->getLMA() + VMA - OSec->getVA(0) : 0; 93 writeHeader(OS, VMA, LMA, Syms[I]->getSize(), 1); 94 OS << Indent16 << toString(*Syms[I]); 95 }); 96 97 DenseMap<Symbol *, std::string> Ret; 98 for (size_t I = 0, E = Syms.size(); I < E; ++I) 99 Ret[Syms[I]] = std::move(Str[I]); 100 return Ret; 101 } 102 103 // Print .eh_frame contents. Since the section consists of EhSectionPieces, 104 // we need a specialized printer for that section. 105 // 106 // .eh_frame tend to contain a lot of section pieces that are contiguous 107 // both in input file and output file. Such pieces are squashed before 108 // being displayed to make output compact. 109 static void printEhFrame(raw_ostream &OS, OutputSection *OSec) { 110 std::vector<EhSectionPiece> Pieces; 111 112 auto Add = [&](const EhSectionPiece &P) { 113 // If P is adjacent to Last, squash the two. 114 if (!Pieces.empty()) { 115 EhSectionPiece &Last = Pieces.back(); 116 if (Last.Sec == P.Sec && Last.InputOff + Last.Size == P.InputOff && 117 Last.OutputOff + Last.Size == P.OutputOff) { 118 Last.Size += P.Size; 119 return; 120 } 121 } 122 Pieces.push_back(P); 123 }; 124 125 // Gather section pieces. 126 for (const CieRecord *Rec : In.EhFrame->getCieRecords()) { 127 Add(*Rec->Cie); 128 for (const EhSectionPiece *Fde : Rec->Fdes) 129 Add(*Fde); 130 } 131 132 // Print out section pieces. 133 for (EhSectionPiece &P : Pieces) { 134 writeHeader(OS, OSec->Addr + P.OutputOff, OSec->getLMA() + P.OutputOff, 135 P.Size, 1); 136 OS << Indent8 << toString(P.Sec->File) << ":(" << P.Sec->Name << "+0x" 137 << Twine::utohexstr(P.InputOff) + ")\n"; 138 } 139 } 140 141 void elf::writeMapFile() { 142 if (Config->MapFile.empty()) 143 return; 144 145 // Open a map file for writing. 146 std::error_code EC; 147 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 148 if (EC) { 149 error("cannot open " + Config->MapFile + ": " + EC.message()); 150 return; 151 } 152 153 // Collect symbol info that we want to print out. 154 std::vector<Defined *> Syms = getSymbols(); 155 SymbolMapTy SectionSyms = getSectionSyms(Syms); 156 DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms); 157 158 // Print out the header line. 159 int W = Config->Is64 ? 16 : 8; 160 OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W) 161 << " Size Align Out In Symbol\n"; 162 163 OutputSection* OSec = nullptr; 164 for (BaseCommand *Base : Script->SectionCommands) { 165 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 166 if (Cmd->Provide && !Cmd->Sym) 167 continue; 168 uint64_t LMA = OSec ? OSec->getLMA() + Cmd->Addr - OSec->getVA(0) : 0; 169 writeHeader(OS, Cmd->Addr, LMA, Cmd->Size, 1); 170 OS << Cmd->CommandString << '\n'; 171 continue; 172 } 173 174 OSec = cast<OutputSection>(Base); 175 writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment); 176 OS << OSec->Name << '\n'; 177 178 // Dump symbols for each input section. 179 for (BaseCommand *Base : OSec->SectionCommands) { 180 if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) { 181 for (InputSection *IS : ISD->Sections) { 182 if (IS == In.EhFrame) { 183 printEhFrame(OS, OSec); 184 continue; 185 } 186 187 writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0), 188 IS->getSize(), IS->Alignment); 189 OS << Indent8 << toString(IS) << '\n'; 190 for (Symbol *Sym : SectionSyms[IS]) 191 OS << SymStr[Sym] << '\n'; 192 } 193 continue; 194 } 195 196 if (auto *Cmd = dyn_cast<ByteCommand>(Base)) { 197 writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset, 198 Cmd->Size, 1); 199 OS << Indent8 << Cmd->CommandString << '\n'; 200 continue; 201 } 202 203 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 204 if (Cmd->Provide && !Cmd->Sym) 205 continue; 206 writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0), 207 Cmd->Size, 1); 208 OS << Indent8 << Cmd->CommandString << '\n'; 209 continue; 210 } 211 } 212 } 213 } 214 215 static void print(StringRef A, StringRef B) { 216 outs() << left_justify(A, 49) << " " << B << "\n"; 217 } 218 219 // Output a cross reference table to stdout. This is for --cref. 220 // 221 // For each global symbol, we print out a file that defines the symbol 222 // followed by files that uses that symbol. Here is an example. 223 // 224 // strlen /lib/x86_64-linux-gnu/libc.so.6 225 // tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o 226 // lib/libLLVMSupport.a(PrettyStackTrace.cpp.o) 227 // 228 // In this case, strlen is defined by libc.so.6 and used by other two 229 // files. 230 void elf::writeCrossReferenceTable() { 231 if (!Config->Cref) 232 return; 233 234 // Collect symbols and files. 235 MapVector<Symbol *, SetVector<InputFile *>> Map; 236 for (InputFile *File : ObjectFiles) { 237 for (Symbol *Sym : File->getSymbols()) { 238 if (isa<SharedSymbol>(Sym)) 239 Map[Sym].insert(File); 240 if (auto *D = dyn_cast<Defined>(Sym)) 241 if (!D->isLocal() && (!D->Section || D->Section->Live)) 242 Map[D].insert(File); 243 } 244 } 245 246 // Print out a header. 247 outs() << "Cross Reference Table\n\n"; 248 print("Symbol", "File"); 249 250 // Print out a table. 251 for (auto KV : Map) { 252 Symbol *Sym = KV.first; 253 SetVector<InputFile *> &Files = KV.second; 254 255 print(toString(*Sym), toString(Sym->File)); 256 for (InputFile *File : Files) 257 if (File != Sym->File) 258 print("", toString(File)); 259 } 260 } 261