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 01:26:00 +08:00
|
|
|
#include "OutputSections.h"
|
2017-01-14 05:05:46 +08:00
|
|
|
#include "Strings.h"
|
2017-04-29 01:19:13 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Threads.h"
|
2017-01-14 05:05:46 +08:00
|
|
|
|
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-04-29 07:29:15 +08:00
|
|
|
typedef DenseMap<const SectionBase *, SmallVector<DefinedRegular *, 4>>
|
|
|
|
SymbolMapTy;
|
2017-04-29 01:19:13 +08:00
|
|
|
|
|
|
|
// Print out the first three columns of a line.
|
|
|
|
template <class ELFT>
|
|
|
|
static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size,
|
|
|
|
uint64_t Align) {
|
|
|
|
int W = ELFT::Is64Bits ? 16 : 8;
|
|
|
|
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.
|
|
|
|
template <class ELFT> std::vector<DefinedRegular *> getSymbols() {
|
|
|
|
std::vector<DefinedRegular *> V;
|
2017-04-29 01:19:13 +08:00
|
|
|
for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles())
|
|
|
|
for (SymbolBody *B : File->getSymbols())
|
|
|
|
if (B->File == File && !B->isSection())
|
|
|
|
if (auto *Sym = dyn_cast<DefinedRegular>(B))
|
|
|
|
if (Sym->Section)
|
2017-04-29 07:29:15 +08:00
|
|
|
V.push_back(Sym);
|
|
|
|
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.
|
|
|
|
template <class ELFT>
|
|
|
|
SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> Syms) {
|
|
|
|
SymbolMapTy Ret;
|
|
|
|
for (DefinedRegular *S : Syms)
|
|
|
|
Ret[S->Section].push_back(S);
|
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-04-29 01:19:13 +08:00
|
|
|
SmallVectorImpl<DefinedRegular *> &V = It.second;
|
|
|
|
std::sort(V.begin(), V.end(), [](DefinedRegular *A, DefinedRegular *B) {
|
|
|
|
return A->getVA() < B->getVA();
|
|
|
|
});
|
|
|
|
}
|
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.
|
|
|
|
template <class ELFT>
|
|
|
|
DenseMap<DefinedRegular *, std::string>
|
|
|
|
getSymbolStrings(ArrayRef<DefinedRegular *> 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]);
|
|
|
|
writeHeader<ELFT>(OS, Syms[I]->getVA(), Syms[I]->template getSize<ELFT>(),
|
|
|
|
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-04-29 07:29:15 +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 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-02-24 23:07:30 +08:00
|
|
|
void elf::writeMapFile(ArrayRef<OutputSection *> OutputSections) {
|
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.
|
|
|
|
std::vector<DefinedRegular *> Syms = getSymbols<ELFT>();
|
|
|
|
SymbolMapTy SectionSyms = getSectionSyms<ELFT>(Syms);
|
|
|
|
DenseMap<DefinedRegular *, std::string> SymStr = getSymbolStrings<ELFT>(Syms);
|
|
|
|
|
|
|
|
// Print out the header line.
|
|
|
|
int W = ELFT::Is64Bits ? 16 : 8;
|
|
|
|
OS << left_justify("Address", W) << ' ' << left_justify("Size", W)
|
|
|
|
<< " Align Out In Symbol\n";
|
|
|
|
|
|
|
|
// Print out file contents.
|
|
|
|
for (OutputSection *OSec : OutputSections) {
|
|
|
|
writeHeader<ELFT>(OS, OSec->Addr, OSec->Size, OSec->Alignment);
|
|
|
|
OS << OSec->Name << '\n';
|
|
|
|
|
|
|
|
// Dump symbols for each input section.
|
|
|
|
for (InputSection *IS : OSec->Sections) {
|
|
|
|
writeHeader<ELFT>(OS, OSec->Addr + IS->OutSecOff, IS->getSize(),
|
|
|
|
IS->Alignment);
|
|
|
|
OS << indent(1) << toString(IS) << '\n';
|
|
|
|
for (DefinedRegular *Sym : SectionSyms[IS])
|
|
|
|
OS << SymStr[Sym] << '\n';
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSection *>);
|
|
|
|
template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSection *>);
|
|
|
|
template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSection *>);
|
|
|
|
template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSection *>);
|