#include "MapFile.h"
#include "InputFiles.h"
#include "LinkerScript.h"
#include "OutputSections.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::object;
using namespace lld;
using namespace lld::elf;
using SymbolMapTy = DenseMap<const SectionBase *,
SmallVector<std::pair<Defined *, uint64_t>, 0>>;
static constexpr char indent8[] = " ";
static constexpr char indent16[] = " ";
static void writeHeader(Ctx &ctx, raw_ostream &os, uint64_t vma, uint64_t lma,
uint64_t size, uint64_t align) {
if (ctx.arg.is64)
os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
else
os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
}
static std::vector<Defined *> getSymbols(Ctx &ctx) {
std::vector<Defined *> v;
for (ELFFileBase *file : ctx.objectFiles)
for (Symbol *b : file->getSymbols())
if (auto *dr = dyn_cast<Defined>(b))
if (!dr->isSection() && dr->section && dr->section->isLive() &&
(dr->file == file || dr->hasFlag(NEEDS_COPY) ||
(isa<SyntheticSection>(dr->section) &&
cast<SyntheticSection>(dr->section)->bss)))
v.push_back(dr);
return v;
}
static SymbolMapTy getSectionSyms(Ctx &ctx, ArrayRef<Defined *> syms) {
SymbolMapTy ret;
for (Defined *dr : syms)
ret[dr->section].emplace_back(dr, dr->getVA(ctx));
SmallPtrSet<Defined *, 4> set;
for (auto &it : ret) {
set.clear();
llvm::erase_if(it.second, [&](std::pair<Defined *, uint64_t> a) {
return !set.insert(a.first).second;
});
llvm::stable_sort(it.second, llvm::less_second());
}
return ret;
}
static DenseMap<Symbol *, std::string>
getSymbolStrings(Ctx &ctx, ArrayRef<Defined *> syms) {
auto strs = std::make_unique<std::string[]>(syms.size());
parallelFor(0, syms.size(), [&](size_t i) {
raw_string_ostream os(strs[i]);
OutputSection *osec = syms[i]->getOutputSection();
uint64_t vma = syms[i]->getVA(ctx);
uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
writeHeader(ctx, os, vma, lma, syms[i]->getSize(), 1);
os << indent16 << toStr(ctx, *syms[i]);
});
DenseMap<Symbol *, std::string> ret;
for (size_t i = 0, e = syms.size(); i < e; ++i)
ret[syms[i]] = std::move(strs[i]);
return ret;
}
static void printEhFrame(Ctx &ctx, raw_ostream &os, const EhFrameSection *sec) {
std::vector<EhSectionPiece> pieces;
auto add = [&](const EhSectionPiece &p) {
if (!pieces.empty()) {
EhSectionPiece &last = pieces.back();
if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
last.outputOff + last.size == (unsigned)p.outputOff) {
last.size += p.size;
return;
}
}
pieces.push_back(p);
};
for (const CieRecord *rec : sec->getCieRecords()) {
add(*rec->cie);
for (const EhSectionPiece *fde : rec->fdes)
add(*fde);
}
const OutputSection *osec = sec->getOutputSection();
for (EhSectionPiece &p : pieces) {
writeHeader(ctx, os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
p.size, 1);
os << indent8 << toStr(ctx, p.sec->file) << ":(" << p.sec->name << "+0x"
<< Twine::utohexstr(p.inputOff) + ")\n";
}
}
static void writeMapFile(Ctx &ctx, raw_fd_ostream &os) {
std::vector<Defined *> syms = getSymbols(ctx);
SymbolMapTy sectionSyms = getSectionSyms(ctx, syms);
DenseMap<Symbol *, std::string> symStr = getSymbolStrings(ctx, syms);
int w = ctx.arg.is64 ? 16 : 8;
os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
<< " Size Align Out In Symbol\n";
OutputSection *osec = nullptr;
for (SectionCommand *cmd : ctx.script->sectionCommands) {
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
if (assign->provide && !assign->sym)
continue;
uint64_t lma = osec ? osec->getLMA() + assign->addr - osec->getVA(0) : 0;
writeHeader(ctx, os, assign->addr, lma, assign->size, 1);
os << assign->commandString << '\n';
continue;
}
if (isa<SectionClassDesc>(cmd))
continue;
osec = &cast<OutputDesc>(cmd)->osec;
writeHeader(ctx, os, osec->addr, osec->getLMA(), osec->size,
osec->addralign);
os << osec->name << '\n';
for (SectionCommand *subCmd : osec->commands) {
if (auto *isd = dyn_cast<InputSectionDescription>(subCmd)) {
for (InputSection *isec : isd->sections) {
if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
printEhFrame(ctx, os, ehSec);
continue;
}
writeHeader(ctx, os, isec->getVA(), osec->getLMA() + isec->outSecOff,
isec->getSize(), isec->addralign);
os << indent8 << toStr(ctx, isec) << '\n';
for (Symbol *sym : llvm::make_first_range(sectionSyms[isec]))
os << symStr[sym] << '\n';
}
continue;
}
if (auto *data = dyn_cast<ByteCommand>(subCmd)) {
writeHeader(ctx, os, osec->addr + data->offset,
osec->getLMA() + data->offset, data->size, 1);
os << indent8 << data->commandString << '\n';
continue;
}
if (auto *assign = dyn_cast<SymbolAssignment>(subCmd)) {
if (assign->provide && !assign->sym)
continue;
writeHeader(ctx, os, assign->addr,
osec->getLMA() + assign->addr - osec->getVA(0),
assign->size, 1);
os << indent8 << assign->commandString << '\n';
continue;
}
}
}
}
static void writeCref(Ctx &ctx, raw_fd_ostream &os) {
MapVector<Symbol *, SetVector<InputFile *>> map;
for (ELFFileBase *file : ctx.objectFiles) {
for (Symbol *sym : file->getSymbols()) {
if (isa<SharedSymbol>(sym))
map[sym].insert(file);
if (auto *d = dyn_cast<Defined>(sym))
if (!d->isLocal())
map[d].insert(file);
}
}
auto print = [&](StringRef a, StringRef b) {
os << left_justify(a, 49) << ' ' << b << '\n';
};
os << "\nCross Reference Table\n\n";
print("Symbol", "File");
for (auto kv : map) {
Symbol *sym = kv.first;
SetVector<InputFile *> &files = kv.second;
print(toStr(ctx, *sym), toStr(ctx, sym->file));
for (InputFile *file : files)
if (file != sym->file)
print("", toStr(ctx, file));
}
}
void elf::writeMapAndCref(Ctx &ctx) {
if (ctx.arg.mapFile.empty() && !ctx.arg.cref)
return;
llvm::TimeTraceScope timeScope("Write map file");
std::error_code ec;
StringRef mapFile = ctx.arg.mapFile.empty() ? "-" : ctx.arg.mapFile;
raw_fd_ostream os = ctx.openAuxiliaryFile(mapFile, ec);
if (ec) {
ErrAlways(ctx) << "cannot open " << mapFile << ": " << ec.message();
return;
}
if (!ctx.arg.mapFile.empty())
writeMapFile(ctx, os);
if (ctx.arg.cref)
writeCref(ctx, os);
}