146b69c69SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===//
246b69c69SDimitry Andric //
346b69c69SDimitry Andric // The LLVM Linker
446b69c69SDimitry Andric //
546b69c69SDimitry Andric // This file is distributed under the University of Illinois Open Source
646b69c69SDimitry Andric // License. See LICENSE.TXT for details.
746b69c69SDimitry Andric //
846b69c69SDimitry Andric //===----------------------------------------------------------------------===//
946b69c69SDimitry Andric //
1046b69c69SDimitry Andric // This file implements the -Map option. It shows lists in order and
1146b69c69SDimitry Andric // hierarchically the output sections, input sections, input files and
1246b69c69SDimitry Andric // symbol:
1346b69c69SDimitry Andric //
14f37b6182SDimitry Andric // Address Size Align Out In Symbol
1546b69c69SDimitry Andric // 00201000 00000015 4 .text
16f37b6182SDimitry Andric // 00201000 0000000e 4 test.o:(.text)
1746b69c69SDimitry Andric // 0020100e 00000000 0 local
1846b69c69SDimitry Andric // 00201005 00000000 0 f(int)
1946b69c69SDimitry Andric //
2046b69c69SDimitry Andric //===----------------------------------------------------------------------===//
2146b69c69SDimitry Andric
2246b69c69SDimitry Andric #include "MapFile.h"
2346b69c69SDimitry Andric #include "InputFiles.h"
24d8866befSDimitry Andric #include "LinkerScript.h"
25d8866befSDimitry Andric #include "OutputSections.h"
26f37b6182SDimitry Andric #include "SymbolTable.h"
2710dc89a5SDimitry Andric #include "Symbols.h"
2810dc89a5SDimitry Andric #include "SyntheticSections.h"
294ba319b5SDimitry Andric #include "lld/Common/Strings.h"
3010dc89a5SDimitry Andric #include "lld/Common/Threads.h"
314ba319b5SDimitry Andric #include "llvm/ADT/MapVector.h"
324ba319b5SDimitry Andric #include "llvm/ADT/SetVector.h"
3346b69c69SDimitry Andric #include "llvm/Support/raw_ostream.h"
3446b69c69SDimitry Andric
3546b69c69SDimitry Andric using namespace llvm;
3646b69c69SDimitry Andric using namespace llvm::object;
3746b69c69SDimitry Andric
3846b69c69SDimitry Andric using namespace lld;
3946b69c69SDimitry Andric using namespace lld::elf;
4046b69c69SDimitry Andric
414ba319b5SDimitry Andric typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy;
424ba319b5SDimitry Andric
434ba319b5SDimitry Andric static const std::string Indent8 = " "; // 8 spaces
444ba319b5SDimitry Andric static const std::string Indent16 = " "; // 16 spaces
4546b69c69SDimitry Andric
46f37b6182SDimitry Andric // Print out the first three columns of a line.
writeHeader(raw_ostream & OS,uint64_t VMA,uint64_t LMA,uint64_t Size,uint64_t Align)474ba319b5SDimitry Andric static void writeHeader(raw_ostream &OS, uint64_t VMA, uint64_t LMA,
484ba319b5SDimitry Andric uint64_t Size, uint64_t Align) {
494ba319b5SDimitry Andric if (Config->Is64)
504ba319b5SDimitry Andric OS << format("%16llx %16llx %8llx %5lld ", VMA, LMA, Size, Align);
514ba319b5SDimitry Andric else
524ba319b5SDimitry Andric OS << format("%8llx %8llx %8llx %5lld ", VMA, LMA, Size, Align);
5346b69c69SDimitry Andric }
5446b69c69SDimitry Andric
55f37b6182SDimitry Andric // Returns a list of all symbols that we want to print out.
getSymbols()564ba319b5SDimitry Andric static std::vector<Defined *> getSymbols() {
574ba319b5SDimitry Andric std::vector<Defined *> V;
584ba319b5SDimitry Andric for (InputFile *File : ObjectFiles)
594ba319b5SDimitry Andric for (Symbol *B : File->getSymbols())
6010dc89a5SDimitry Andric if (auto *DR = dyn_cast<Defined>(B))
614ba319b5SDimitry Andric if (!DR->isSection() && DR->Section && DR->Section->Live &&
624ba319b5SDimitry Andric (DR->File == File || DR->NeedsPltAddr || DR->Section->Bss))
6310dc89a5SDimitry Andric V.push_back(DR);
64f37b6182SDimitry Andric return V;
6546b69c69SDimitry Andric }
6646b69c69SDimitry Andric
67f37b6182SDimitry Andric // Returns a map from sections to their symbols.
getSectionSyms(ArrayRef<Defined * > Syms)684ba319b5SDimitry Andric static SymbolMapTy getSectionSyms(ArrayRef<Defined *> Syms) {
69f37b6182SDimitry Andric SymbolMapTy Ret;
704ba319b5SDimitry Andric for (Defined *DR : Syms)
714ba319b5SDimitry Andric Ret[DR->Section].push_back(DR);
7246b69c69SDimitry Andric
73f37b6182SDimitry Andric // Sort symbols by address. We want to print out symbols in the
74f37b6182SDimitry Andric // order in the output file rather than the order they appeared
75f37b6182SDimitry Andric // in the input files.
76f37b6182SDimitry Andric for (auto &It : Ret) {
774ba319b5SDimitry Andric SmallVectorImpl<Defined *> &V = It.second;
784ba319b5SDimitry Andric std::stable_sort(V.begin(), V.end(), [](Defined *A, Defined *B) {
794ba319b5SDimitry Andric return A->getVA() < B->getVA();
804ba319b5SDimitry Andric });
8146b69c69SDimitry Andric }
82f37b6182SDimitry Andric return Ret;
8346b69c69SDimitry Andric }
84f37b6182SDimitry Andric
85f37b6182SDimitry Andric // Construct a map from symbols to their stringified representations.
86f37b6182SDimitry Andric // Demangling symbols (which is what toString() does) is slow, so
87f37b6182SDimitry Andric // we do that in batch using parallel-for.
8810dc89a5SDimitry Andric static DenseMap<Symbol *, std::string>
getSymbolStrings(ArrayRef<Defined * > Syms)894ba319b5SDimitry Andric getSymbolStrings(ArrayRef<Defined *> Syms) {
90f37b6182SDimitry Andric std::vector<std::string> Str(Syms.size());
915517e702SDimitry Andric parallelForEachN(0, Syms.size(), [&](size_t I) {
92f37b6182SDimitry Andric raw_string_ostream OS(Str[I]);
934ba319b5SDimitry Andric OutputSection *OSec = Syms[I]->getOutputSection();
944ba319b5SDimitry Andric uint64_t VMA = Syms[I]->getVA();
954ba319b5SDimitry Andric uint64_t LMA = OSec ? OSec->getLMA() + VMA - OSec->getVA(0) : 0;
964ba319b5SDimitry Andric writeHeader(OS, VMA, LMA, Syms[I]->getSize(), 1);
974ba319b5SDimitry Andric OS << Indent16 << toString(*Syms[I]);
98f37b6182SDimitry Andric });
99f37b6182SDimitry Andric
10010dc89a5SDimitry Andric DenseMap<Symbol *, std::string> Ret;
101f37b6182SDimitry Andric for (size_t I = 0, E = Syms.size(); I < E; ++I)
102f37b6182SDimitry Andric Ret[Syms[I]] = std::move(Str[I]);
103f37b6182SDimitry Andric return Ret;
10446b69c69SDimitry Andric }
10546b69c69SDimitry Andric
1064ba319b5SDimitry Andric // Print .eh_frame contents. Since the section consists of EhSectionPieces,
1074ba319b5SDimitry Andric // we need a specialized printer for that section.
1084ba319b5SDimitry Andric //
1094ba319b5SDimitry Andric // .eh_frame tend to contain a lot of section pieces that are contiguous
1104ba319b5SDimitry Andric // both in input file and output file. Such pieces are squashed before
1114ba319b5SDimitry Andric // being displayed to make output compact.
printEhFrame(raw_ostream & OS,OutputSection * OSec)1124ba319b5SDimitry Andric static void printEhFrame(raw_ostream &OS, OutputSection *OSec) {
1134ba319b5SDimitry Andric std::vector<EhSectionPiece> Pieces;
1144ba319b5SDimitry Andric
1154ba319b5SDimitry Andric auto Add = [&](const EhSectionPiece &P) {
1164ba319b5SDimitry Andric // If P is adjacent to Last, squash the two.
1174ba319b5SDimitry Andric if (!Pieces.empty()) {
1184ba319b5SDimitry Andric EhSectionPiece &Last = Pieces.back();
1194ba319b5SDimitry Andric if (Last.Sec == P.Sec && Last.InputOff + Last.Size == P.InputOff &&
1204ba319b5SDimitry Andric Last.OutputOff + Last.Size == P.OutputOff) {
1214ba319b5SDimitry Andric Last.Size += P.Size;
1224ba319b5SDimitry Andric return;
1234ba319b5SDimitry Andric }
1244ba319b5SDimitry Andric }
1254ba319b5SDimitry Andric Pieces.push_back(P);
1264ba319b5SDimitry Andric };
1274ba319b5SDimitry Andric
1284ba319b5SDimitry Andric // Gather section pieces.
129*b5893f02SDimitry Andric for (const CieRecord *Rec : In.EhFrame->getCieRecords()) {
1304ba319b5SDimitry Andric Add(*Rec->Cie);
1314ba319b5SDimitry Andric for (const EhSectionPiece *Fde : Rec->Fdes)
1324ba319b5SDimitry Andric Add(*Fde);
1334ba319b5SDimitry Andric }
1344ba319b5SDimitry Andric
1354ba319b5SDimitry Andric // Print out section pieces.
1364ba319b5SDimitry Andric for (EhSectionPiece &P : Pieces) {
1374ba319b5SDimitry Andric writeHeader(OS, OSec->Addr + P.OutputOff, OSec->getLMA() + P.OutputOff,
1384ba319b5SDimitry Andric P.Size, 1);
1394ba319b5SDimitry Andric OS << Indent8 << toString(P.Sec->File) << ":(" << P.Sec->Name << "+0x"
1404ba319b5SDimitry Andric << Twine::utohexstr(P.InputOff) + ")\n";
1414ba319b5SDimitry Andric }
1424ba319b5SDimitry Andric }
1434ba319b5SDimitry Andric
writeMapFile()14410dc89a5SDimitry Andric void elf::writeMapFile() {
14546b69c69SDimitry Andric if (Config->MapFile.empty())
14646b69c69SDimitry Andric return;
14746b69c69SDimitry Andric
148f37b6182SDimitry Andric // Open a map file for writing.
14946b69c69SDimitry Andric std::error_code EC;
15046b69c69SDimitry Andric raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
151f37b6182SDimitry Andric if (EC) {
15246b69c69SDimitry Andric error("cannot open " + Config->MapFile + ": " + EC.message());
153f37b6182SDimitry Andric return;
154f37b6182SDimitry Andric }
155f37b6182SDimitry Andric
156f37b6182SDimitry Andric // Collect symbol info that we want to print out.
1574ba319b5SDimitry Andric std::vector<Defined *> Syms = getSymbols();
15810dc89a5SDimitry Andric SymbolMapTy SectionSyms = getSectionSyms(Syms);
15910dc89a5SDimitry Andric DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
160f37b6182SDimitry Andric
161f37b6182SDimitry Andric // Print out the header line.
16210dc89a5SDimitry Andric int W = Config->Is64 ? 16 : 8;
1634ba319b5SDimitry Andric OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W)
1644ba319b5SDimitry Andric << " Size Align Out In Symbol\n";
165f37b6182SDimitry Andric
166*b5893f02SDimitry Andric OutputSection* OSec = nullptr;
1674ba319b5SDimitry Andric for (BaseCommand *Base : Script->SectionCommands) {
1684ba319b5SDimitry Andric if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
1694ba319b5SDimitry Andric if (Cmd->Provide && !Cmd->Sym)
1704ba319b5SDimitry Andric continue;
171*b5893f02SDimitry Andric uint64_t LMA = OSec ? OSec->getLMA() + Cmd->Addr - OSec->getVA(0) : 0;
172*b5893f02SDimitry Andric writeHeader(OS, Cmd->Addr, LMA, Cmd->Size, 1);
1734ba319b5SDimitry Andric OS << Cmd->CommandString << '\n';
1744ba319b5SDimitry Andric continue;
1754ba319b5SDimitry Andric }
1764ba319b5SDimitry Andric
177*b5893f02SDimitry Andric OSec = cast<OutputSection>(Base);
1784ba319b5SDimitry Andric writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment);
179f37b6182SDimitry Andric OS << OSec->Name << '\n';
180f37b6182SDimitry Andric
181f37b6182SDimitry Andric // Dump symbols for each input section.
18210dc89a5SDimitry Andric for (BaseCommand *Base : OSec->SectionCommands) {
1834ba319b5SDimitry Andric if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
184302affcbSDimitry Andric for (InputSection *IS : ISD->Sections) {
185*b5893f02SDimitry Andric if (IS == In.EhFrame) {
1864ba319b5SDimitry Andric printEhFrame(OS, OSec);
1874ba319b5SDimitry Andric continue;
1884ba319b5SDimitry Andric }
1894ba319b5SDimitry Andric
1904ba319b5SDimitry Andric writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0),
1914ba319b5SDimitry Andric IS->getSize(), IS->Alignment);
1924ba319b5SDimitry Andric OS << Indent8 << toString(IS) << '\n';
19310dc89a5SDimitry Andric for (Symbol *Sym : SectionSyms[IS])
194f37b6182SDimitry Andric OS << SymStr[Sym] << '\n';
195f37b6182SDimitry Andric }
1964ba319b5SDimitry Andric continue;
197f37b6182SDimitry Andric }
1984ba319b5SDimitry Andric
1994ba319b5SDimitry Andric if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
2004ba319b5SDimitry Andric writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset,
2014ba319b5SDimitry Andric Cmd->Size, 1);
2024ba319b5SDimitry Andric OS << Indent8 << Cmd->CommandString << '\n';
2034ba319b5SDimitry Andric continue;
2044ba319b5SDimitry Andric }
2054ba319b5SDimitry Andric
2064ba319b5SDimitry Andric if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
2074ba319b5SDimitry Andric if (Cmd->Provide && !Cmd->Sym)
2084ba319b5SDimitry Andric continue;
2094ba319b5SDimitry Andric writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0),
2104ba319b5SDimitry Andric Cmd->Size, 1);
2114ba319b5SDimitry Andric OS << Indent8 << Cmd->CommandString << '\n';
2124ba319b5SDimitry Andric continue;
2134ba319b5SDimitry Andric }
2144ba319b5SDimitry Andric }
2154ba319b5SDimitry Andric }
2164ba319b5SDimitry Andric }
2174ba319b5SDimitry Andric
print(StringRef A,StringRef B)2184ba319b5SDimitry Andric static void print(StringRef A, StringRef B) {
2194ba319b5SDimitry Andric outs() << left_justify(A, 49) << " " << B << "\n";
2204ba319b5SDimitry Andric }
2214ba319b5SDimitry Andric
2224ba319b5SDimitry Andric // Output a cross reference table to stdout. This is for --cref.
2234ba319b5SDimitry Andric //
2244ba319b5SDimitry Andric // For each global symbol, we print out a file that defines the symbol
2254ba319b5SDimitry Andric // followed by files that uses that symbol. Here is an example.
2264ba319b5SDimitry Andric //
2274ba319b5SDimitry Andric // strlen /lib/x86_64-linux-gnu/libc.so.6
2284ba319b5SDimitry Andric // tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
2294ba319b5SDimitry Andric // lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
2304ba319b5SDimitry Andric //
2314ba319b5SDimitry Andric // In this case, strlen is defined by libc.so.6 and used by other two
2324ba319b5SDimitry Andric // files.
writeCrossReferenceTable()2334ba319b5SDimitry Andric void elf::writeCrossReferenceTable() {
2344ba319b5SDimitry Andric if (!Config->Cref)
2354ba319b5SDimitry Andric return;
2364ba319b5SDimitry Andric
2374ba319b5SDimitry Andric // Collect symbols and files.
2384ba319b5SDimitry Andric MapVector<Symbol *, SetVector<InputFile *>> Map;
2394ba319b5SDimitry Andric for (InputFile *File : ObjectFiles) {
2404ba319b5SDimitry Andric for (Symbol *Sym : File->getSymbols()) {
2414ba319b5SDimitry Andric if (isa<SharedSymbol>(Sym))
2424ba319b5SDimitry Andric Map[Sym].insert(File);
2434ba319b5SDimitry Andric if (auto *D = dyn_cast<Defined>(Sym))
2444ba319b5SDimitry Andric if (!D->isLocal() && (!D->Section || D->Section->Live))
2454ba319b5SDimitry Andric Map[D].insert(File);
2464ba319b5SDimitry Andric }
2474ba319b5SDimitry Andric }
2484ba319b5SDimitry Andric
2494ba319b5SDimitry Andric // Print out a header.
2504ba319b5SDimitry Andric outs() << "Cross Reference Table\n\n";
2514ba319b5SDimitry Andric print("Symbol", "File");
2524ba319b5SDimitry Andric
2534ba319b5SDimitry Andric // Print out a table.
2544ba319b5SDimitry Andric for (auto KV : Map) {
2554ba319b5SDimitry Andric Symbol *Sym = KV.first;
2564ba319b5SDimitry Andric SetVector<InputFile *> &Files = KV.second;
2574ba319b5SDimitry Andric
2584ba319b5SDimitry Andric print(toString(*Sym), toString(Sym->File));
2594ba319b5SDimitry Andric for (InputFile *File : Files)
2604ba319b5SDimitry Andric if (File != Sym->File)
2614ba319b5SDimitry Andric print("", toString(File));
26246b69c69SDimitry Andric }
263302affcbSDimitry Andric }
264