2017-01-14 11:14: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 /lldmap option. It shows lists in order and
|
|
|
|
// hierarchically the output sections, input sections, input files and
|
|
|
|
// symbol:
|
|
|
|
//
|
2017-05-01 04:58:20 +08:00
|
|
|
// Address Size Align Out File Symbol
|
|
|
|
// 00201000 00000015 4 .text
|
|
|
|
// 00201000 0000000e 4 test.o:(.text)
|
|
|
|
// 0020100e 00000000 0 local
|
|
|
|
// 00201005 00000000 0 f(int)
|
2017-01-14 11:14:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MapFile.h"
|
|
|
|
#include "Error.h"
|
2017-04-30 02:50:39 +08:00
|
|
|
#include "SymbolTable.h"
|
2017-01-14 11:14:46 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
#include "Writer.h"
|
|
|
|
|
2017-05-11 08:03:52 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2017-01-15 08:41:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-14 11:14:46 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
|
|
|
|
2017-04-30 02:50:39 +08:00
|
|
|
typedef DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>
|
|
|
|
SymbolMapTy;
|
2017-01-14 11:14:46 +08:00
|
|
|
|
2017-04-30 02:50:39 +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) {
|
|
|
|
OS << format("%08llx %08llx %5lld ", Addr, Size, Align);
|
2017-01-14 11:14:46 +08:00
|
|
|
}
|
|
|
|
|
2017-04-30 02:50:39 +08:00
|
|
|
static std::string indent(int Depth) { return std::string(Depth * 8, ' '); }
|
2017-01-14 11:14:46 +08:00
|
|
|
|
2017-04-30 02:50:39 +08:00
|
|
|
// Returns a list of all symbols that we want to print out.
|
|
|
|
static std::vector<DefinedRegular *> getSymbols() {
|
|
|
|
std::vector<DefinedRegular *> V;
|
|
|
|
for (coff::ObjectFile *File : Symtab->ObjectFiles)
|
|
|
|
for (SymbolBody *B : File->getSymbols())
|
|
|
|
if (auto *Sym = dyn_cast<DefinedRegular>(B))
|
|
|
|
if (Sym && !Sym->getCOFFSymbol().isSectionDefinition())
|
|
|
|
V.push_back(Sym);
|
|
|
|
return V;
|
2017-01-14 11:14:46 +08:00
|
|
|
}
|
|
|
|
|
2017-04-30 02:50:39 +08:00
|
|
|
// Returns a map from sections to their symbols.
|
|
|
|
static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
|
|
|
|
SymbolMapTy Ret;
|
|
|
|
for (DefinedRegular *S : Syms)
|
|
|
|
Ret[S->getChunk()].push_back(S);
|
|
|
|
|
|
|
|
// Sort symbols by address.
|
|
|
|
for (auto &It : Ret) {
|
|
|
|
SmallVectorImpl<DefinedRegular *> &V = It.second;
|
|
|
|
std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
|
|
|
|
return A->getRVA() < B->getRVA();
|
|
|
|
});
|
2017-01-14 11:14:46 +08:00
|
|
|
}
|
2017-04-30 02:50:39 +08:00
|
|
|
return Ret;
|
2017-01-14 11:14:46 +08:00
|
|
|
}
|
|
|
|
|
2017-04-30 02:50:39 +08:00
|
|
|
// Construct a map from symbols to their stringified representations.
|
|
|
|
static DenseMap<DefinedRegular *, std::string>
|
|
|
|
getSymbolStrings(ArrayRef<DefinedRegular *> Syms) {
|
|
|
|
std::vector<std::string> Str(Syms.size());
|
2017-05-10 09:16:22 +08:00
|
|
|
for_each_n(parallel::par, (size_t)0, Syms.size(), [&](size_t I) {
|
2017-04-30 02:50:39 +08:00
|
|
|
raw_string_ostream OS(Str[I]);
|
|
|
|
writeHeader(OS, Syms[I]->getRVA(), 0, 0);
|
|
|
|
OS << indent(2) << toString(*Syms[I]);
|
|
|
|
});
|
2017-01-15 08:41:21 +08:00
|
|
|
|
2017-04-30 02:50:39 +08:00
|
|
|
DenseMap<DefinedRegular *, std::string> Ret;
|
|
|
|
for (size_t I = 0, E = Syms.size(); I < E; ++I)
|
|
|
|
Ret[Syms[I]] = std::move(Str[I]);
|
|
|
|
return Ret;
|
2017-01-14 11:14:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void coff::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
|
2017-01-15 08:41:21 +08:00
|
|
|
if (Config->MapFile.empty())
|
2017-01-14 11:14:46 +08:00
|
|
|
return;
|
|
|
|
|
2017-01-15 08:41:21 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Config->MapFile, EC, sys::fs::F_None);
|
2017-01-14 11:14:46 +08:00
|
|
|
if (EC)
|
2017-01-15 08:41:21 +08:00
|
|
|
fatal("cannot open " + Config->MapFile + ": " + EC.message());
|
2017-04-30 02:50:39 +08:00
|
|
|
|
|
|
|
// Collect symbol info that we want to print out.
|
|
|
|
std::vector<DefinedRegular *> Syms = getSymbols();
|
|
|
|
SymbolMapTy SectionSyms = getSectionSyms(Syms);
|
|
|
|
DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings(Syms);
|
|
|
|
|
|
|
|
// Print out the header line.
|
|
|
|
OS << "Address Size Align Out In Symbol\n";
|
|
|
|
|
|
|
|
// Print out file contents.
|
|
|
|
for (OutputSection *Sec : OutputSections) {
|
|
|
|
writeHeader(OS, Sec->getRVA(), Sec->getVirtualSize(), /*Align=*/PageSize);
|
|
|
|
OS << Sec->getName() << '\n';
|
|
|
|
|
|
|
|
for (Chunk *C : Sec->getChunks()) {
|
|
|
|
auto *SC = dyn_cast<SectionChunk>(C);
|
|
|
|
if (!SC)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
writeHeader(OS, SC->getRVA(), SC->getSize(), SC->getAlign());
|
|
|
|
OS << indent(1) << SC->File->getName() << ":(" << SC->getSectionName()
|
|
|
|
<< ")\n";
|
|
|
|
for (DefinedRegular *Sym : SectionSyms[SC])
|
|
|
|
OS << SymStr[Sym] << '\n';
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 11:14:46 +08:00
|
|
|
}
|