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