2017-01-14 05:05:46 +08:00
|
|
|
//===- MapFile.cpp --------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-01-14 05:05:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the -Map option. It shows lists in order and
|
|
|
|
// hierarchically the output sections, input sections, input files and
|
|
|
|
// symbol:
|
|
|
|
//
|
2017-04-29 04:38:27 +08:00
|
|
|
// Address Size Align Out In Symbol
|
|
|
|
// 00201000 00000015 4 .text
|
2017-05-01 04:58:20 +08:00
|
|
|
// 00201000 0000000e 4 test.o:(.text)
|
2017-04-29 04:38:27 +08:00
|
|
|
// 0020100e 00000000 0 local
|
|
|
|
// 00201005 00000000 0 f(int)
|
2017-01-14 05:05:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MapFile.h"
|
|
|
|
#include "InputFiles.h"
|
2017-05-19 05:30:14 +08:00
|
|
|
#include "LinkerScript.h"
|
2017-05-19 01:26:00 +08:00
|
|
|
#include "OutputSections.h"
|
2017-04-29 01:19:13 +08:00
|
|
|
#include "SymbolTable.h"
|
2017-12-10 00:56:18 +08:00
|
|
|
#include "Symbols.h"
|
2017-08-11 19:34:04 +08:00
|
|
|
#include "SyntheticSections.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-10-14 02:22:55 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2018-03-15 04:29:45 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
2017-01-15 08:41:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-14 05:05:46 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
2018-04-27 01:58:58 +08:00
|
|
|
typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy;
|
2017-04-29 01:19:13 +08:00
|
|
|
|
2018-03-08 01:15:15 +08:00
|
|
|
static const std::string Indent8 = " "; // 8 spaces
|
|
|
|
static const std::string Indent16 = " "; // 16 spaces
|
2018-03-07 06:48:46 +08:00
|
|
|
|
2017-04-29 01:19:13 +08:00
|
|
|
// Print out the first three columns of a line.
|
2018-04-05 18:51:06 +08:00
|
|
|
static void writeHeader(raw_ostream &OS, uint64_t VMA, uint64_t LMA,
|
|
|
|
uint64_t Size, uint64_t Align) {
|
2018-04-06 00:45:37 +08:00
|
|
|
if (Config->Is64)
|
2018-04-06 01:20:18 +08:00
|
|
|
OS << format("%16llx %16llx %8llx %5lld ", VMA, LMA, Size, Align);
|
2018-04-06 00:45:37 +08:00
|
|
|
else
|
2018-04-06 01:20:18 +08:00
|
|
|
OS << format("%8llx %8llx %8llx %5lld ", VMA, LMA, Size, Align);
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Returns a list of all symbols that we want to print out.
|
2018-04-27 01:58:58 +08:00
|
|
|
static std::vector<Defined *> getSymbols() {
|
|
|
|
std::vector<Defined *> V;
|
|
|
|
for (InputFile *File : ObjectFiles)
|
|
|
|
for (Symbol *B : File->getSymbols())
|
2017-11-06 12:35:31 +08:00
|
|
|
if (auto *DR = dyn_cast<Defined>(B))
|
2018-04-27 01:58:58 +08:00
|
|
|
if (!DR->isSection() && DR->Section && DR->Section->Live &&
|
|
|
|
(DR->File == File || DR->NeedsPltAddr || DR->Section->Bss))
|
2017-08-05 02:42:04 +08:00
|
|
|
V.push_back(DR);
|
2017-04-29 07:29:15 +08:00
|
|
|
return V;
|
|
|
|
}
|
2017-04-29 01:19:13 +08:00
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Returns a map from sections to their symbols.
|
2018-04-27 01:58:58 +08:00
|
|
|
static SymbolMapTy getSectionSyms(ArrayRef<Defined *> Syms) {
|
2017-04-29 07:29:15 +08:00
|
|
|
SymbolMapTy Ret;
|
2018-04-27 01:58:58 +08:00
|
|
|
for (Defined *DR : Syms)
|
|
|
|
Ret[DR->Section].push_back(DR);
|
2017-04-29 01:19:13 +08:00
|
|
|
|
|
|
|
// Sort symbols by address. We want to print out symbols in the
|
|
|
|
// order in the output file rather than the order they appeared
|
|
|
|
// in the input files.
|
2017-04-29 07:29:15 +08:00
|
|
|
for (auto &It : Ret) {
|
2018-04-27 01:58:58 +08:00
|
|
|
SmallVectorImpl<Defined *> &V = It.second;
|
|
|
|
std::stable_sort(V.begin(), V.end(), [](Defined *A, Defined *B) {
|
2018-04-24 17:55:39 +08:00
|
|
|
return A->getVA() < B->getVA();
|
|
|
|
});
|
2017-04-29 01:19:13 +08:00
|
|
|
}
|
2017-04-29 07:29:15 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
2017-01-14 05:05:46 +08:00
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Construct a map from symbols to their stringified representations.
|
|
|
|
// Demangling symbols (which is what toString() does) is slow, so
|
|
|
|
// we do that in batch using parallel-for.
|
2017-12-05 22:23:18 +08:00
|
|
|
static DenseMap<Symbol *, std::string>
|
2018-04-27 01:58:58 +08:00
|
|
|
getSymbolStrings(ArrayRef<Defined *> Syms) {
|
2017-04-29 01:19:13 +08:00
|
|
|
std::vector<std::string> Str(Syms.size());
|
2017-05-11 04:02:19 +08:00
|
|
|
parallelForEachN(0, Syms.size(), [&](size_t I) {
|
2017-04-29 01:19:13 +08:00
|
|
|
raw_string_ostream OS(Str[I]);
|
2018-04-05 18:51:06 +08:00
|
|
|
OutputSection *OSec = Syms[I]->getOutputSection();
|
|
|
|
uint64_t VMA = Syms[I]->getVA();
|
|
|
|
uint64_t LMA = OSec ? OSec->getLMA() + VMA - OSec->getVA(0) : 0;
|
|
|
|
writeHeader(OS, VMA, LMA, Syms[I]->getSize(), 1);
|
2018-03-08 01:15:15 +08:00
|
|
|
OS << Indent16 << toString(*Syms[I]);
|
2017-04-29 01:19:13 +08:00
|
|
|
});
|
2017-01-14 08:37:28 +08:00
|
|
|
|
2017-12-05 22:23:18 +08:00
|
|
|
DenseMap<Symbol *, std::string> Ret;
|
2017-04-29 07:29:15 +08:00
|
|
|
for (size_t I = 0, E = Syms.size(); I < E; ++I)
|
|
|
|
Ret[Syms[I]] = std::move(Str[I]);
|
|
|
|
return Ret;
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 05:18:18 +08:00
|
|
|
// Print .eh_frame contents. Since the section consists of EhSectionPieces,
|
|
|
|
// we need a specialized printer for that section.
|
|
|
|
//
|
|
|
|
// .eh_frame tend to contain a lot of section pieces that are contiguous
|
|
|
|
// both in input file and output file. Such pieces are squashed before
|
|
|
|
// being displayed to make output compact.
|
|
|
|
static void printEhFrame(raw_ostream &OS, OutputSection *OSec) {
|
|
|
|
std::vector<EhSectionPiece> Pieces;
|
|
|
|
|
|
|
|
auto Add = [&](const EhSectionPiece &P) {
|
|
|
|
// If P is adjacent to Last, squash the two.
|
|
|
|
if (!Pieces.empty()) {
|
|
|
|
EhSectionPiece &Last = Pieces.back();
|
|
|
|
if (Last.Sec == P.Sec && Last.InputOff + Last.Size == P.InputOff &&
|
|
|
|
Last.OutputOff + Last.Size == P.OutputOff) {
|
|
|
|
Last.Size += P.Size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Pieces.push_back(P);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Gather section pieces.
|
2018-09-26 03:26:58 +08:00
|
|
|
for (const CieRecord *Rec : In.EhFrame->getCieRecords()) {
|
2018-03-15 05:18:18 +08:00
|
|
|
Add(*Rec->Cie);
|
|
|
|
for (const EhSectionPiece *Fde : Rec->Fdes)
|
|
|
|
Add(*Fde);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out section pieces.
|
|
|
|
for (EhSectionPiece &P : Pieces) {
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(OS, OSec->Addr + P.OutputOff, OSec->getLMA() + P.OutputOff,
|
|
|
|
P.Size, 1);
|
2018-03-15 05:18:18 +08:00
|
|
|
OS << Indent8 << toString(P.Sec->File) << ":(" << P.Sec->Name << "+0x"
|
|
|
|
<< Twine::utohexstr(P.InputOff) + ")\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 04:15:56 +08:00
|
|
|
void elf::writeMapFile() {
|
2017-01-15 08:41:21 +08:00
|
|
|
if (Config->MapFile.empty())
|
2017-01-14 05:05:46 +08:00
|
|
|
return;
|
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Open a map file for writing.
|
2017-01-15 08:41:21 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
|
2017-04-29 07:29:15 +08:00
|
|
|
if (EC) {
|
2017-01-16 09:07:19 +08:00
|
|
|
error("cannot open " + Config->MapFile + ": " + EC.message());
|
2017-04-29 07:29:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect symbol info that we want to print out.
|
2018-04-27 01:58:58 +08:00
|
|
|
std::vector<Defined *> Syms = getSymbols();
|
2017-07-28 19:13:21 +08:00
|
|
|
SymbolMapTy SectionSyms = getSectionSyms(Syms);
|
2017-12-05 22:23:18 +08:00
|
|
|
DenseMap<Symbol *, std::string> SymStr = getSymbolStrings(Syms);
|
2017-04-29 07:29:15 +08:00
|
|
|
|
|
|
|
// Print out the header line.
|
2017-10-29 04:15:56 +08:00
|
|
|
int W = Config->Is64 ? 16 : 8;
|
2018-04-06 01:20:18 +08:00
|
|
|
OS << right_justify("VMA", W) << ' ' << right_justify("LMA", W)
|
|
|
|
<< " Size Align Out In Symbol\n";
|
2017-04-29 07:29:15 +08:00
|
|
|
|
2018-12-06 17:04:52 +08:00
|
|
|
OutputSection* OSec = nullptr;
|
2018-04-05 19:25:58 +08:00
|
|
|
for (BaseCommand *Base : Script->SectionCommands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
|
|
|
|
if (Cmd->Provide && !Cmd->Sym)
|
|
|
|
continue;
|
2018-12-06 17:04:52 +08:00
|
|
|
uint64_t LMA = OSec ? OSec->getLMA() + Cmd->Addr - OSec->getVA(0) : 0;
|
|
|
|
writeHeader(OS, Cmd->Addr, LMA, Cmd->Size, 1);
|
2018-04-05 19:25:58 +08:00
|
|
|
OS << Cmd->CommandString << '\n';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:04:52 +08:00
|
|
|
OSec = cast<OutputSection>(Base);
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment);
|
2017-04-29 07:29:15 +08:00
|
|
|
OS << OSec->Name << '\n';
|
|
|
|
|
|
|
|
// Dump symbols for each input section.
|
2018-03-15 17:16:40 +08:00
|
|
|
for (BaseCommand *Base : OSec->SectionCommands) {
|
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
|
|
|
|
for (InputSection *IS : ISD->Sections) {
|
2018-09-26 03:26:58 +08:00
|
|
|
if (IS == In.EhFrame) {
|
2018-03-15 17:16:40 +08:00
|
|
|
printEhFrame(OS, OSec);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(OS, IS->getVA(0), OSec->getLMA() + IS->getOffset(0),
|
|
|
|
IS->getSize(), IS->Alignment);
|
2018-03-15 17:16:40 +08:00
|
|
|
OS << Indent8 << toString(IS) << '\n';
|
|
|
|
for (Symbol *Sym : SectionSyms[IS])
|
|
|
|
OS << SymStr[Sym] << '\n';
|
|
|
|
}
|
2018-03-15 05:18:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:16:40 +08:00
|
|
|
if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(OS, OSec->Addr + Cmd->Offset, OSec->getLMA() + Cmd->Offset,
|
|
|
|
Cmd->Size, 1);
|
2018-03-15 17:16:40 +08:00
|
|
|
OS << Indent8 << Cmd->CommandString << '\n';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
|
2018-04-05 19:25:58 +08:00
|
|
|
if (Cmd->Provide && !Cmd->Sym)
|
|
|
|
continue;
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(OS, Cmd->Addr, OSec->getLMA() + Cmd->Addr - OSec->getVA(0),
|
|
|
|
Cmd->Size, 1);
|
2018-03-15 17:16:40 +08:00
|
|
|
OS << Indent8 << Cmd->CommandString << '\n';
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-29 07:29:15 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
2018-03-15 04:29:45 +08:00
|
|
|
|
|
|
|
static void print(StringRef A, StringRef B) {
|
|
|
|
outs() << left_justify(A, 49) << " " << B << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output a cross reference table to stdout. This is for --cref.
|
|
|
|
//
|
|
|
|
// For each global symbol, we print out a file that defines the symbol
|
|
|
|
// followed by files that uses that symbol. Here is an example.
|
|
|
|
//
|
|
|
|
// strlen /lib/x86_64-linux-gnu/libc.so.6
|
|
|
|
// tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
|
|
|
|
// lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
|
|
|
|
//
|
|
|
|
// In this case, strlen is defined by libc.so.6 and used by other two
|
|
|
|
// files.
|
|
|
|
void elf::writeCrossReferenceTable() {
|
|
|
|
if (!Config->Cref)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Collect symbols and files.
|
|
|
|
MapVector<Symbol *, SetVector<InputFile *>> Map;
|
|
|
|
for (InputFile *File : ObjectFiles) {
|
|
|
|
for (Symbol *Sym : File->getSymbols()) {
|
|
|
|
if (isa<SharedSymbol>(Sym))
|
|
|
|
Map[Sym].insert(File);
|
|
|
|
if (auto *D = dyn_cast<Defined>(Sym))
|
|
|
|
if (!D->isLocal() && (!D->Section || D->Section->Live))
|
|
|
|
Map[D].insert(File);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out a header.
|
|
|
|
outs() << "Cross Reference Table\n\n";
|
|
|
|
print("Symbol", "File");
|
|
|
|
|
|
|
|
// Print out a table.
|
|
|
|
for (auto KV : Map) {
|
|
|
|
Symbol *Sym = KV.first;
|
|
|
|
SetVector<InputFile *> &Files = KV.second;
|
|
|
|
|
|
|
|
print(toString(*Sym), toString(Sym->File));
|
|
|
|
for (InputFile *File : Files)
|
|
|
|
if (File != Sym->File)
|
|
|
|
print("", toString(File));
|
|
|
|
}
|
|
|
|
}
|