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:
|
|
|
|
//
|
|
|
|
// Address Size Align Out In File Symbol
|
|
|
|
// =================================================================
|
|
|
|
// 00201000 00000015 4 .text
|
|
|
|
// 00201000 0000000e 4 .text
|
|
|
|
// 00201000 0000000e 4 test.o
|
|
|
|
// 0020100e 00000000 0 local
|
|
|
|
// 00201005 00000000 0 f(int)
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MapFile.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Strings.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;
|
|
|
|
|
|
|
|
static void writeOutSecLine(raw_fd_ostream &OS, int Width, uint64_t Address,
|
|
|
|
uint64_t Size, uint64_t Align, StringRef Name) {
|
2017-02-01 04:42:34 +08:00
|
|
|
OS << format("%0*llx %0*llx %5lld ", Width, Address, Width, Size, Align)
|
2017-01-14 05:05:46 +08:00
|
|
|
<< left_justify(Name, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeInSecLine(raw_fd_ostream &OS, int Width, uint64_t Address,
|
|
|
|
uint64_t Size, uint64_t Align, StringRef Name) {
|
|
|
|
// Pass an empty name to align the text to the correct column.
|
|
|
|
writeOutSecLine(OS, Width, Address, Size, Align, "");
|
|
|
|
OS << ' ' << left_justify(Name, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeFileLine(raw_fd_ostream &OS, int Width, uint64_t Address,
|
|
|
|
uint64_t Size, uint64_t Align, StringRef Name) {
|
|
|
|
// Pass an empty name to align the text to the correct column.
|
|
|
|
writeInSecLine(OS, Width, Address, Size, Align, "");
|
|
|
|
OS << ' ' << left_justify(Name, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeSymbolLine(raw_fd_ostream &OS, int Width, uint64_t Address,
|
|
|
|
uint64_t Size, StringRef Name) {
|
|
|
|
// Pass an empty name to align the text to the correct column.
|
|
|
|
writeFileLine(OS, Width, Address, Size, 0, "");
|
|
|
|
OS << ' ' << left_justify(Name, 7);
|
|
|
|
}
|
|
|
|
|
2017-01-14 08:37:28 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static void writeInputSection(raw_fd_ostream &OS, const InputSection<ELFT> *IS,
|
|
|
|
StringRef &PrevName) {
|
|
|
|
int Width = ELFT::Is64Bits ? 16 : 8;
|
|
|
|
StringRef Name = IS->Name;
|
|
|
|
if (Name != PrevName) {
|
2017-02-23 10:28:28 +08:00
|
|
|
writeInSecLine(OS, Width, IS->OutSec->Addr + IS->OutSecOff,
|
|
|
|
IS->template getSize<ELFT>(), IS->Alignment, Name);
|
2017-01-14 08:37:28 +08:00
|
|
|
OS << '\n';
|
|
|
|
PrevName = Name;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
elf::ObjectFile<ELFT> *File = IS->template getFile<ELFT>();
|
2017-01-14 08:37:28 +08:00
|
|
|
if (!File)
|
|
|
|
return;
|
2017-02-23 10:28:28 +08:00
|
|
|
writeFileLine(OS, Width, IS->OutSec->Addr + IS->OutSecOff,
|
|
|
|
IS->template getSize<ELFT>(), IS->Alignment, toString(File));
|
2017-01-14 08:37:28 +08:00
|
|
|
OS << '\n';
|
|
|
|
|
|
|
|
for (SymbolBody *Sym : File->getSymbols()) {
|
|
|
|
auto *DR = dyn_cast<DefinedRegular<ELFT>>(Sym);
|
|
|
|
if (!DR)
|
|
|
|
continue;
|
|
|
|
if (DR->Section != IS)
|
|
|
|
continue;
|
|
|
|
if (DR->isSection())
|
|
|
|
continue;
|
|
|
|
writeSymbolLine(OS, Width, Sym->getVA<ELFT>(), Sym->getSize<ELFT>(),
|
|
|
|
toString(*Sym));
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 05:05:46 +08:00
|
|
|
template <class ELFT>
|
2017-01-15 08:41:21 +08:00
|
|
|
static void writeMapFile2(raw_fd_ostream &OS,
|
2017-01-14 05:05:46 +08:00
|
|
|
ArrayRef<OutputSectionBase *> OutputSections) {
|
|
|
|
int Width = ELFT::Is64Bits ? 16 : 8;
|
2017-01-14 08:37:28 +08:00
|
|
|
|
2017-01-14 05:05:46 +08:00
|
|
|
OS << left_justify("Address", Width) << ' ' << left_justify("Size", Width)
|
2017-01-15 09:11:47 +08:00
|
|
|
<< " Align Out In File Symbol\n";
|
2017-01-14 08:37:28 +08:00
|
|
|
|
2017-01-14 05:05:46 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections) {
|
2017-01-14 08:37:28 +08:00
|
|
|
writeOutSecLine(OS, Width, Sec->Addr, Sec->Size, Sec->Addralign,
|
|
|
|
Sec->getName());
|
2017-01-14 05:05:46 +08:00
|
|
|
OS << '\n';
|
2017-01-14 08:37:28 +08:00
|
|
|
|
2017-01-14 05:05:46 +08:00
|
|
|
StringRef PrevName = "";
|
2017-02-23 10:32:18 +08:00
|
|
|
Sec->forEachInputSection([&](InputSectionBase *S) {
|
2017-01-14 08:37:28 +08:00
|
|
|
if (const auto *IS = dyn_cast<InputSection<ELFT>>(S))
|
|
|
|
writeInputSection(OS, IS, PrevName);
|
2017-01-14 05:05:46 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void elf::writeMapFile(ArrayRef<OutputSectionBase *> OutputSections) {
|
2017-01-15 08:41:21 +08:00
|
|
|
if (Config->MapFile.empty())
|
2017-01-14 05:05: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 05:05:46 +08:00
|
|
|
if (EC)
|
2017-01-16 09:07:19 +08:00
|
|
|
error("cannot open " + Config->MapFile + ": " + EC.message());
|
|
|
|
else
|
|
|
|
writeMapFile2<ELFT>(OS, OutputSections);
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template void elf::writeMapFile<ELF32LE>(ArrayRef<OutputSectionBase *>);
|
|
|
|
template void elf::writeMapFile<ELF32BE>(ArrayRef<OutputSectionBase *>);
|
|
|
|
template void elf::writeMapFile<ELF64LE>(ArrayRef<OutputSectionBase *>);
|
|
|
|
template void elf::writeMapFile<ELF64BE>(ArrayRef<OutputSectionBase *>);
|