2017-01-14 05:05:46 +08:00
|
|
|
//===- MapFile.cpp --------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
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;
|
|
|
|
|
2017-12-05 22:23:18 +08:00
|
|
|
typedef DenseMap<const SectionBase *, SmallVector<Symbol *, 4>> SymbolMapTy;
|
2017-04-29 01:19:13 +08:00
|
|
|
|
|
|
|
// Print out the first three columns of a line.
|
|
|
|
static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
|
|
|
|
uint64_t Align) {
|
2017-07-28 19:13:21 +08:00
|
|
|
int W = Config->Is64 ? 16 : 8;
|
2017-04-29 01:19:13 +08:00
|
|
|
OS << format("%0*llx %0*llx %5lld ", W, Addr, W, Size, Align);
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
2017-04-29 01:19:13 +08:00
|
|
|
static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
|
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Returns a list of all symbols that we want to print out.
|
2017-12-05 22:23:18 +08:00
|
|
|
static std::vector<Symbol *> getSymbols() {
|
|
|
|
std::vector<Symbol *> V;
|
|
|
|
for (InputFile *File : ObjectFiles) {
|
|
|
|
for (Symbol *B : File->getSymbols()) {
|
|
|
|
if (auto *SS = dyn_cast<SharedSymbol>(B))
|
2017-12-06 16:36:21 +08:00
|
|
|
if (SS->CopyRelSec || SS->NeedsPltAddr)
|
2017-12-05 22:23:18 +08:00
|
|
|
V.push_back(SS);
|
2017-11-06 12:35:31 +08:00
|
|
|
if (auto *DR = dyn_cast<Defined>(B))
|
2017-11-30 06:47:35 +08:00
|
|
|
if (DR->File == File && !DR->isSection() && DR->Section &&
|
2017-08-05 02:42:04 +08:00
|
|
|
DR->Section->Live)
|
|
|
|
V.push_back(DR);
|
2017-12-05 22:23:18 +08:00
|
|
|
}
|
|
|
|
}
|
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.
|
2017-12-05 22:23:18 +08:00
|
|
|
static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> Syms) {
|
2017-04-29 07:29:15 +08:00
|
|
|
SymbolMapTy Ret;
|
2017-12-05 22:23:18 +08:00
|
|
|
for (Symbol *S : Syms) {
|
2017-12-06 16:36:21 +08:00
|
|
|
if (auto *DR = dyn_cast<Defined>(S)) {
|
2017-12-05 22:23:18 +08:00
|
|
|
Ret[DR->Section].push_back(S);
|
2017-12-06 16:36:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSymbol *SS = cast<SharedSymbol>(S);
|
|
|
|
if (SS->CopyRelSec)
|
|
|
|
Ret[SS->CopyRelSec].push_back(S);
|
2017-12-05 22:23:18 +08:00
|
|
|
else
|
2017-12-06 16:36:21 +08:00
|
|
|
Ret[InX::Plt].push_back(S);
|
2017-12-05 22:23:18 +08:00
|
|
|
}
|
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) {
|
2017-12-05 22:23:18 +08:00
|
|
|
SmallVectorImpl<Symbol *> &V = It.second;
|
2017-08-11 19:34:04 +08:00
|
|
|
std::sort(V.begin(), V.end(),
|
2017-12-05 22:23:18 +08:00
|
|
|
[](Symbol *A, Symbol *B) { 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>
|
|
|
|
getSymbolStrings(ArrayRef<Symbol *> 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]);
|
2017-10-29 04:15:56 +08:00
|
|
|
writeHeader(OS, Syms[I]->getVA(), Syms[I]->getSize(), 0);
|
2017-04-29 07:29:15 +08:00
|
|
|
OS << indent(2) << 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
|
|
|
}
|
|
|
|
|
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.
|
2017-12-05 22:23:18 +08:00
|
|
|
std::vector<Symbol *> 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;
|
2017-04-29 07:29:15 +08:00
|
|
|
OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
|
|
|
|
<< " Align Out In Symbol\n";
|
|
|
|
|
|
|
|
// Print out file contents.
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *OSec : OutputSections) {
|
2017-07-28 19:13:21 +08:00
|
|
|
writeHeader(OS, OSec->Addr, OSec->Size, OSec->Alignment);
|
2017-04-29 07:29:15 +08:00
|
|
|
OS << OSec->Name << '\n';
|
|
|
|
|
|
|
|
// Dump symbols for each input section.
|
2018-02-22 17:55:28 +08:00
|
|
|
for (InputSection *IS : getInputSections(OSec)) {
|
|
|
|
writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), IS->Alignment);
|
|
|
|
OS << indent(1) << toString(IS) << '\n';
|
|
|
|
for (Symbol *Sym : SectionSyms[IS])
|
|
|
|
OS << SymStr[Sym] << '\n';
|
2017-04-29 07:29:15 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|