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 SmallVectorImpl<Defined *> &V = It.second; 77 std::stable_sort(V.begin(), V.end(), [](Defined *A, Defined *B) { 78 return A->getVA() < B->getVA(); 79 }); 80 } 81 return Ret; 82 } 83 84 // Construct a map from symbols to their stringified representations. 85 // Demangling symbols (which is what toString() does) is slow, so 86 // we do that in batch using parallel-for. 87 static DenseMap<Symbol *, std::string> 88 getSymbolStrings(ArrayRef<Defined *> Syms) { 89 std::vector<std::string> Str(Syms.size()); 90 parallelForEachN(0, Syms.size(), [&](size_t I) { 91 raw_string_ostream OS(Str[I]); 92 OutputSection *OSec = Syms[I]->getOutputSection(); 93 uint64_t VMA = Syms[I]->getVA(); 94 uint64_t LMA = OSec ? OSec->getLMA() + VMA - OSec->getVA(0) : 0; 95 writeHeader(OS, VMA, LMA, Syms[I]->getSize(), 1); 96 OS << Indent16 << toString(*Syms[I]); 97 }); 98 99 DenseMap<Symbol *, std::string> Ret; 100 for (size_t I = 0, E = Syms.size(); I < E; ++I) 101 Ret[Syms[I]] = std::move(Str[I]); 102 return Ret; 103 } 104 105 // Print .eh_frame contents. Since the section consists of EhSectionPieces, 106 // we need a specialized printer for that section. 107 // 108 // .eh_frame tend to contain a lot of section pieces that are contiguous 109 // both in input file and output file. Such pieces are squashed before 110 // being displayed to make output compact. 111 static void printEhFrame(raw_ostream &OS, OutputSection *OSec) { 112 std::vector<EhSectionPiece> Pieces; 113 114 auto Add = [&](const EhSectionPiece &P) { 115 // If P is adjacent to Last, squash the two. 116 if (!Pieces.empty()) { 117 EhSectionPiece &Last = Pieces.back(); 118 if (Last.Sec == P.Sec && Last.InputOff + Last.Size == P.InputOff && 119 Last.OutputOff + Last.Size == P.OutputOff) { 120 Last.Size += P.Size; 121 return; 122 } 123 } 124 Pieces.push_back(P); 125 }; 126 127 // Gather section pieces. 128 for (const CieRecord *Rec : In.EhFrame->getCieRecords()) { 129 Add(*Rec->Cie); 130 for (const EhSectionPiece *Fde : Rec->Fdes) 131 Add(*Fde); 132 } 133 134 // Print out section pieces. 135 for (EhSectionPiece &P : Pieces) { 136 writeHeader(OS, OSec->Addr + P.OutputOff, OSec->getLMA() + P.OutputOff, 137 P.Size, 1); 138 OS << Indent8 << toString(P.Sec->File) << ":(" << P.Sec->Name << "+0x" 139 << Twine::utohexstr(P.InputOff) + ")\n"; 140 } 141 } 142 143 void elf::writeMapFile() { 144 if (Config->MapFile.empty()) 145 return; 146 147 // Open a map file for writing. 148 std::error_code EC; 149 raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None); 150 if (EC) { 151 error("cannot open " + Config->MapFile + ": " + EC.message()); 152 return; 153 } 154 155 // Collect symbol info that we want to print out. 156 std::vector<Defined *> Syms = getSymbols(); 157 SymbolMapTy SectionSyms = getSectionSyms(Syms); 158 DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms); 159 160 // Print out the header line. 161 int W = Config->Is64 ? 16 : 8; 162 OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W) 163 << " Size Align Out In Symbol\n"; 164 165 OutputSection* OSec = nullptr; 166 for (BaseCommand *Base : Script->SectionCommands) { 167 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 168 if (Cmd->Provide && !Cmd->Sym) 169 continue; 170 uint64_t LMA = OSec ? OSec->getLMA() + Cmd->Addr - OSec->getVA(0) : 0; 171 writeHeader(OS, Cmd->Addr, LMA, Cmd->Size, 1); 172 OS << Cmd->CommandString << '\n'; 173 continue; 174 } 175 176 OSec = cast<OutputSection>(Base); 177 writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment); 178 OS << OSec->Name << '\n'; 179 180 // Dump symbols for each input section. 181 for (BaseCommand *Base : OSec->SectionCommands) { 182 if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) { 183 for (InputSection *IS : ISD->Sections) { 184 if (IS == In.EhFrame) { 185 printEhFrame(OS, OSec); 186 continue; 187 } 188 189 writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0), 190 IS->getSize(), IS->Alignment); 191 OS << Indent8 << toString(IS) << '\n'; 192 for (Symbol *Sym : SectionSyms[IS]) 193 OS << SymStr[Sym] << '\n'; 194 } 195 continue; 196 } 197 198 if (auto *Cmd = dyn_cast<ByteCommand>(Base)) { 199 writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset, 200 Cmd->Size, 1); 201 OS << Indent8 << Cmd->CommandString << '\n'; 202 continue; 203 } 204 205 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 206 if (Cmd->Provide && !Cmd->Sym) 207 continue; 208 writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0), 209 Cmd->Size, 1); 210 OS << Indent8 << Cmd->CommandString << '\n'; 211 continue; 212 } 213 } 214 } 215 } 216 217 static void print(StringRef A, StringRef B) { 218 outs() << left_justify(A, 49) << " " << B << "\n"; 219 } 220 221 // Output a cross reference table to stdout. This is for --cref. 222 // 223 // For each global symbol, we print out a file that defines the symbol 224 // followed by files that uses that symbol. Here is an example. 225 // 226 // strlen /lib/x86_64-linux-gnu/libc.so.6 227 // tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o 228 // lib/libLLVMSupport.a(PrettyStackTrace.cpp.o) 229 // 230 // In this case, strlen is defined by libc.so.6 and used by other two 231 // files. 232 void elf::writeCrossReferenceTable() { 233 if (!Config->Cref) 234 return; 235 236 // Collect symbols and files. 237 MapVector<Symbol *, SetVector<InputFile *>> Map; 238 for (InputFile *File : ObjectFiles) { 239 for (Symbol *Sym : File->getSymbols()) { 240 if (isa<SharedSymbol>(Sym)) 241 Map[Sym].insert(File); 242 if (auto *D = dyn_cast<Defined>(Sym)) 243 if (!D->isLocal() && (!D->Section || D->Section->Live)) 244 Map[D].insert(File); 245 } 246 } 247 248 // Print out a header. 249 outs() << "Cross Reference Table\n\n"; 250 print("Symbol", "File"); 251 252 // Print out a table. 253 for (auto KV : Map) { 254 Symbol *Sym = KV.first; 255 SetVector<InputFile *> &Files = KV.second; 256 257 print(toString(*Sym), toString(Sym->File)); 258 for (InputFile *File : Files) 259 if (File != Sym->File) 260 print("", toString(File)); 261 } 262 } 263