#include "MapFile.h"
#include "InputElement.h"
#include "InputFiles.h"
#include "OutputSections.h"
#include "OutputSegment.h"
#include "SymbolTable.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "lld/Common/Strings.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::object;
using namespace lld;
using namespace lld::wasm;
using SymbolMapTy = DenseMap<const InputChunk *, SmallVector<Symbol *, 4>>;
static void writeHeader(raw_ostream &os, int64_t vma, uint64_t lma,
uint64_t size) {
if (vma == -1)
os << format(" - %8llx %8llx ", lma, size);
else
os << format("%8llx %8llx %8llx ", vma, lma, size);
}
static std::vector<Symbol *> getSymbols() {
std::vector<Symbol *> v;
for (InputFile *file : ctx.objectFiles)
for (Symbol *b : file->getSymbols())
if (auto *dr = dyn_cast<Symbol>(b))
if ((!isa<SectionSymbol>(dr)) && dr->isLive() &&
(dr->getFile() == file))
v.push_back(dr);
return v;
}
static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> syms) {
SymbolMapTy ret;
for (Symbol *dr : syms)
ret[dr->getChunk()].push_back(dr);
return ret;
}
static DenseMap<Symbol *, std::string>
getSymbolStrings(ArrayRef<Symbol *> syms) {
std::vector<std::string> str(syms.size());
parallelFor(0, syms.size(), [&](size_t i) {
raw_string_ostream os(str[i]);
auto *chunk = syms[i]->getChunk();
if (chunk == nullptr)
return;
uint64_t fileOffset = chunk->outputSec != nullptr
? chunk->outputSec->getOffset() + chunk->outSecOff
: 0;
uint64_t vma = -1;
uint64_t size = 0;
if (auto *DD = dyn_cast<DefinedData>(syms[i])) {
vma = DD->getVA();
size = DD->getSize();
fileOffset += DD->value;
}
if (auto *DF = dyn_cast<DefinedFunction>(syms[i])) {
size = DF->function->getSize();
}
writeHeader(os, vma, fileOffset, size);
os.indent(16) << toString(*syms[i]);
});
DenseMap<Symbol *, std::string> ret;
for (size_t i = 0, e = syms.size(); i < e; ++i)
ret[syms[i]] = std::move(str[i]);
return ret;
}
void lld::wasm::writeMapFile(ArrayRef<OutputSection *> outputSections) {
if (config->mapFile.empty())
return;
std::error_code ec;
raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
if (ec) {
error("cannot open " + config->mapFile + ": " + ec.message());
return;
}
std::vector<Symbol *> syms = getSymbols();
SymbolMapTy sectionSyms = getSectionSyms(syms);
DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
os << " Addr Off Size Out In Symbol\n";
for (OutputSection *osec : outputSections) {
writeHeader(os, -1, osec->getOffset(), osec->getSize());
os << toString(*osec) << '\n';
if (auto *code = dyn_cast<CodeSection>(osec)) {
for (auto *chunk : code->functions) {
writeHeader(os, -1, chunk->outputSec->getOffset() + chunk->outSecOff,
chunk->getSize());
os.indent(8) << toString(chunk) << '\n';
for (Symbol *sym : sectionSyms[chunk])
os << symStr[sym] << '\n';
}
} else if (auto *data = dyn_cast<DataSection>(osec)) {
for (auto *oseg : data->segments) {
writeHeader(os, oseg->startVA, data->getOffset() + oseg->sectionOffset,
oseg->size);
os << oseg->name << '\n';
for (auto *chunk : oseg->inputSegments) {
uint64_t offset =
chunk->outputSec != nullptr
? chunk->outputSec->getOffset() + chunk->outSecOff
: 0;
writeHeader(os, chunk->getVA(), offset, chunk->getSize());
os.indent(8) << toString(chunk) << '\n';
for (Symbol *sym : sectionSyms[chunk])
os << symStr[sym] << '\n';
}
}
} else if (auto *globals = dyn_cast<GlobalSection>(osec)) {
for (auto *global : globals->inputGlobals) {
writeHeader(os, global->getAssignedIndex(), 0, 0);
os.indent(8) << global->getName() << '\n';
}
}
}
}