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