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 "Symbols.h" 26*0b57cec5SDimitry Andric #include "SyntheticSections.h" 27*0b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 28*0b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 2981ad6265SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 3081ad6265SDimitry Andric #include "llvm/Support/FileSystem.h" 315ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h" 32e8d8bef9SDimitry Andric #include "llvm/Support/TimeProfiler.h" 33*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric using namespace llvm; 36*0b57cec5SDimitry Andric using namespace llvm::object; 375ffd83dbSDimitry Andric using namespace lld; 385ffd83dbSDimitry Andric using namespace lld::elf; 39*0b57cec5SDimitry Andric 4004eeddc0SDimitry Andric using SymbolMapTy = DenseMap<const SectionBase *, 4104eeddc0SDimitry Andric SmallVector<std::pair<Defined *, uint64_t>, 0>>; 42*0b57cec5SDimitry Andric 4385868e8aSDimitry Andric static constexpr char indent8[] = " "; // 8 spaces 4485868e8aSDimitry Andric static constexpr char indent16[] = " "; // 16 spaces 45*0b57cec5SDimitry Andric 46*0b57cec5SDimitry Andric // Print out the first three columns of a line. 47*0b57cec5SDimitry Andric static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma, 48*0b57cec5SDimitry Andric uint64_t size, uint64_t align) { 49*0b57cec5SDimitry Andric if (config->is64) 50*0b57cec5SDimitry Andric os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align); 51*0b57cec5SDimitry Andric else 52*0b57cec5SDimitry Andric os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align); 53*0b57cec5SDimitry Andric } 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric // Returns a list of all symbols that we want to print out. 56*0b57cec5SDimitry Andric static std::vector<Defined *> getSymbols() { 57*0b57cec5SDimitry Andric std::vector<Defined *> v; 5881ad6265SDimitry Andric for (ELFFileBase *file : ctx->objectFiles) 59*0b57cec5SDimitry Andric for (Symbol *b : file->getSymbols()) 60*0b57cec5SDimitry Andric if (auto *dr = dyn_cast<Defined>(b)) 61*0b57cec5SDimitry Andric if (!dr->isSection() && dr->section && dr->section->isLive() && 620eae32dcSDimitry Andric (dr->file == file || dr->needsCopy || dr->section->bss)) 63*0b57cec5SDimitry Andric v.push_back(dr); 64*0b57cec5SDimitry Andric return v; 65*0b57cec5SDimitry Andric } 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric // Returns a map from sections to their symbols. 68*0b57cec5SDimitry Andric static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) { 69*0b57cec5SDimitry Andric SymbolMapTy ret; 70*0b57cec5SDimitry Andric for (Defined *dr : syms) 7104eeddc0SDimitry Andric ret[dr->section].emplace_back(dr, dr->getVA()); 72*0b57cec5SDimitry Andric 73*0b57cec5SDimitry Andric // Sort symbols by address. We want to print out symbols in the 74*0b57cec5SDimitry Andric // order in the output file rather than the order they appeared 75*0b57cec5SDimitry Andric // in the input files. 760eae32dcSDimitry Andric SmallPtrSet<Defined *, 4> set; 770eae32dcSDimitry Andric for (auto &it : ret) { 780eae32dcSDimitry Andric // Deduplicate symbols which need a canonical PLT entry/copy relocation. 790eae32dcSDimitry Andric set.clear(); 8004eeddc0SDimitry Andric llvm::erase_if(it.second, [&](std::pair<Defined *, uint64_t> a) { 8104eeddc0SDimitry Andric return !set.insert(a.first).second; 82*0b57cec5SDimitry Andric }); 8304eeddc0SDimitry Andric 8404eeddc0SDimitry Andric llvm::stable_sort(it.second, llvm::less_second()); 850eae32dcSDimitry Andric } 86*0b57cec5SDimitry Andric return ret; 87*0b57cec5SDimitry Andric } 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric // Construct a map from symbols to their stringified representations. 90*0b57cec5SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so 91*0b57cec5SDimitry Andric // we do that in batch using parallel-for. 92*0b57cec5SDimitry Andric static DenseMap<Symbol *, std::string> 93*0b57cec5SDimitry Andric getSymbolStrings(ArrayRef<Defined *> syms) { 9404eeddc0SDimitry Andric auto strs = std::make_unique<std::string[]>(syms.size()); 9581ad6265SDimitry Andric parallelFor(0, syms.size(), [&](size_t i) { 9604eeddc0SDimitry Andric raw_string_ostream os(strs[i]); 97*0b57cec5SDimitry Andric OutputSection *osec = syms[i]->getOutputSection(); 98*0b57cec5SDimitry Andric uint64_t vma = syms[i]->getVA(); 99*0b57cec5SDimitry Andric uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0; 100*0b57cec5SDimitry Andric writeHeader(os, vma, lma, syms[i]->getSize(), 1); 101*0b57cec5SDimitry Andric os << indent16 << toString(*syms[i]); 102*0b57cec5SDimitry Andric }); 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric DenseMap<Symbol *, std::string> ret; 105*0b57cec5SDimitry Andric for (size_t i = 0, e = syms.size(); i < e; ++i) 10604eeddc0SDimitry Andric ret[syms[i]] = std::move(strs[i]); 107*0b57cec5SDimitry Andric return ret; 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric // Print .eh_frame contents. Since the section consists of EhSectionPieces, 111*0b57cec5SDimitry Andric // we need a specialized printer for that section. 112*0b57cec5SDimitry Andric // 113*0b57cec5SDimitry Andric // .eh_frame tend to contain a lot of section pieces that are contiguous 114*0b57cec5SDimitry Andric // both in input file and output file. Such pieces are squashed before 115*0b57cec5SDimitry Andric // being displayed to make output compact. 116*0b57cec5SDimitry Andric static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) { 117*0b57cec5SDimitry Andric std::vector<EhSectionPiece> pieces; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric auto add = [&](const EhSectionPiece &p) { 120*0b57cec5SDimitry Andric // If P is adjacent to Last, squash the two. 121*0b57cec5SDimitry Andric if (!pieces.empty()) { 122*0b57cec5SDimitry Andric EhSectionPiece &last = pieces.back(); 123*0b57cec5SDimitry Andric if (last.sec == p.sec && last.inputOff + last.size == p.inputOff && 124*0b57cec5SDimitry Andric last.outputOff + last.size == p.outputOff) { 125*0b57cec5SDimitry Andric last.size += p.size; 126*0b57cec5SDimitry Andric return; 127*0b57cec5SDimitry Andric } 128*0b57cec5SDimitry Andric } 129*0b57cec5SDimitry Andric pieces.push_back(p); 130*0b57cec5SDimitry Andric }; 131*0b57cec5SDimitry Andric 132*0b57cec5SDimitry Andric // Gather section pieces. 133*0b57cec5SDimitry Andric for (const CieRecord *rec : sec->getCieRecords()) { 134*0b57cec5SDimitry Andric add(*rec->cie); 135*0b57cec5SDimitry Andric for (const EhSectionPiece *fde : rec->fdes) 136*0b57cec5SDimitry Andric add(*fde); 137*0b57cec5SDimitry Andric } 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric // Print out section pieces. 140*0b57cec5SDimitry Andric const OutputSection *osec = sec->getOutputSection(); 141*0b57cec5SDimitry Andric for (EhSectionPiece &p : pieces) { 142*0b57cec5SDimitry Andric writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff, 143*0b57cec5SDimitry Andric p.size, 1); 144*0b57cec5SDimitry Andric os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x" 145*0b57cec5SDimitry Andric << Twine::utohexstr(p.inputOff) + ")\n"; 146*0b57cec5SDimitry Andric } 147*0b57cec5SDimitry Andric } 148*0b57cec5SDimitry Andric 1494824e7fdSDimitry Andric static void writeMapFile(raw_fd_ostream &os) { 150*0b57cec5SDimitry Andric // Collect symbol info that we want to print out. 151*0b57cec5SDimitry Andric std::vector<Defined *> syms = getSymbols(); 152*0b57cec5SDimitry Andric SymbolMapTy sectionSyms = getSectionSyms(syms); 153*0b57cec5SDimitry Andric DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms); 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric // Print out the header line. 156*0b57cec5SDimitry Andric int w = config->is64 ? 16 : 8; 157*0b57cec5SDimitry Andric os << right_justify("VMA", w) << ' ' << right_justify("LMA", w) 158*0b57cec5SDimitry Andric << " Size Align Out In Symbol\n"; 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric OutputSection* osec = nullptr; 1614824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) { 1624824e7fdSDimitry Andric if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) { 1634824e7fdSDimitry Andric if (assign->provide && !assign->sym) 164*0b57cec5SDimitry Andric continue; 1654824e7fdSDimitry Andric uint64_t lma = osec ? osec->getLMA() + assign->addr - osec->getVA(0) : 0; 1664824e7fdSDimitry Andric writeHeader(os, assign->addr, lma, assign->size, 1); 1674824e7fdSDimitry Andric os << assign->commandString << '\n'; 168*0b57cec5SDimitry Andric continue; 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 17181ad6265SDimitry Andric osec = &cast<OutputDesc>(cmd)->osec; 172*0b57cec5SDimitry Andric writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment); 173*0b57cec5SDimitry Andric os << osec->name << '\n'; 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric // Dump symbols for each input section. 1764824e7fdSDimitry Andric for (SectionCommand *subCmd : osec->commands) { 1774824e7fdSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(subCmd)) { 178*0b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 179*0b57cec5SDimitry Andric if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) { 180*0b57cec5SDimitry Andric printEhFrame(os, ehSec); 181*0b57cec5SDimitry Andric continue; 182*0b57cec5SDimitry Andric } 183*0b57cec5SDimitry Andric 1844824e7fdSDimitry Andric writeHeader(os, isec->getVA(), osec->getLMA() + isec->outSecOff, 185*0b57cec5SDimitry Andric isec->getSize(), isec->alignment); 186*0b57cec5SDimitry Andric os << indent8 << toString(isec) << '\n'; 18704eeddc0SDimitry Andric for (Symbol *sym : llvm::make_first_range(sectionSyms[isec])) 188*0b57cec5SDimitry Andric os << symStr[sym] << '\n'; 189*0b57cec5SDimitry Andric } 190*0b57cec5SDimitry Andric continue; 191*0b57cec5SDimitry Andric } 192*0b57cec5SDimitry Andric 1934824e7fdSDimitry Andric if (auto *data = dyn_cast<ByteCommand>(subCmd)) { 1944824e7fdSDimitry Andric writeHeader(os, osec->addr + data->offset, 1954824e7fdSDimitry Andric osec->getLMA() + data->offset, data->size, 1); 1964824e7fdSDimitry Andric os << indent8 << data->commandString << '\n'; 197*0b57cec5SDimitry Andric continue; 198*0b57cec5SDimitry Andric } 199*0b57cec5SDimitry Andric 2004824e7fdSDimitry Andric if (auto *assign = dyn_cast<SymbolAssignment>(subCmd)) { 2014824e7fdSDimitry Andric if (assign->provide && !assign->sym) 202*0b57cec5SDimitry Andric continue; 2034824e7fdSDimitry Andric writeHeader(os, assign->addr, 2044824e7fdSDimitry Andric osec->getLMA() + assign->addr - osec->getVA(0), 2054824e7fdSDimitry Andric assign->size, 1); 2064824e7fdSDimitry Andric os << indent8 << assign->commandString << '\n'; 207*0b57cec5SDimitry Andric continue; 208*0b57cec5SDimitry Andric } 209*0b57cec5SDimitry Andric } 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric } 212*0b57cec5SDimitry Andric 213*0b57cec5SDimitry Andric // Output a cross reference table to stdout. This is for --cref. 214*0b57cec5SDimitry Andric // 215*0b57cec5SDimitry Andric // For each global symbol, we print out a file that defines the symbol 216*0b57cec5SDimitry Andric // followed by files that uses that symbol. Here is an example. 217*0b57cec5SDimitry Andric // 218*0b57cec5SDimitry Andric // strlen /lib/x86_64-linux-gnu/libc.so.6 219*0b57cec5SDimitry Andric // tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o 220*0b57cec5SDimitry Andric // lib/libLLVMSupport.a(PrettyStackTrace.cpp.o) 221*0b57cec5SDimitry Andric // 222*0b57cec5SDimitry Andric // In this case, strlen is defined by libc.so.6 and used by other two 223*0b57cec5SDimitry Andric // files. 2244824e7fdSDimitry Andric static void writeCref(raw_fd_ostream &os) { 225*0b57cec5SDimitry Andric // Collect symbols and files. 226*0b57cec5SDimitry Andric MapVector<Symbol *, SetVector<InputFile *>> map; 22781ad6265SDimitry Andric for (ELFFileBase *file : ctx->objectFiles) { 228*0b57cec5SDimitry Andric for (Symbol *sym : file->getSymbols()) { 229*0b57cec5SDimitry Andric if (isa<SharedSymbol>(sym)) 230*0b57cec5SDimitry Andric map[sym].insert(file); 231*0b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) 232*0b57cec5SDimitry Andric if (!d->isLocal() && (!d->section || d->section->isLive())) 233*0b57cec5SDimitry Andric map[d].insert(file); 234*0b57cec5SDimitry Andric } 235*0b57cec5SDimitry Andric } 236*0b57cec5SDimitry Andric 2374824e7fdSDimitry Andric auto print = [&](StringRef a, StringRef b) { 2384824e7fdSDimitry Andric os << left_justify(a, 49) << ' ' << b << '\n'; 2394824e7fdSDimitry Andric }; 2404824e7fdSDimitry Andric 2414824e7fdSDimitry Andric // Print a blank line and a header. The format matches GNU ld. 2424824e7fdSDimitry Andric os << "\nCross Reference Table\n\n"; 243*0b57cec5SDimitry Andric print("Symbol", "File"); 244*0b57cec5SDimitry Andric 245*0b57cec5SDimitry Andric // Print out a table. 246*0b57cec5SDimitry Andric for (auto kv : map) { 247*0b57cec5SDimitry Andric Symbol *sym = kv.first; 248*0b57cec5SDimitry Andric SetVector<InputFile *> &files = kv.second; 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric print(toString(*sym), toString(sym->file)); 251*0b57cec5SDimitry Andric for (InputFile *file : files) 252*0b57cec5SDimitry Andric if (file != sym->file) 253*0b57cec5SDimitry Andric print("", toString(file)); 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric } 25685868e8aSDimitry Andric 2574824e7fdSDimitry Andric void elf::writeMapAndCref() { 2584824e7fdSDimitry Andric if (config->mapFile.empty() && !config->cref) 2594824e7fdSDimitry Andric return; 2604824e7fdSDimitry Andric 2614824e7fdSDimitry Andric llvm::TimeTraceScope timeScope("Write map file"); 2624824e7fdSDimitry Andric 2634824e7fdSDimitry Andric // Open a map file for writing. 2644824e7fdSDimitry Andric std::error_code ec; 2654824e7fdSDimitry Andric StringRef mapFile = config->mapFile.empty() ? "-" : config->mapFile; 2664824e7fdSDimitry Andric raw_fd_ostream os(mapFile, ec, sys::fs::OF_None); 2674824e7fdSDimitry Andric if (ec) { 2684824e7fdSDimitry Andric error("cannot open " + mapFile + ": " + ec.message()); 2694824e7fdSDimitry Andric return; 2704824e7fdSDimitry Andric } 2714824e7fdSDimitry Andric 2724824e7fdSDimitry Andric if (!config->mapFile.empty()) 2734824e7fdSDimitry Andric writeMapFile(os); 2744824e7fdSDimitry Andric if (config->cref) 2754824e7fdSDimitry Andric writeCref(os); 2764824e7fdSDimitry Andric } 277