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