2014-04-25 23:34:35 +02:00
|
|
|
//===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DwarfStringPool.h"
|
2015-05-24 18:54:59 +02:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2015-05-24 18:58:59 +02:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2014-04-25 23:34:35 +02:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-24 18:58:59 +02:00
|
|
|
DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
|
|
|
|
StringRef Prefix)
|
|
|
|
: Pool(A), Prefix(Prefix),
|
|
|
|
ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
|
|
|
|
|
2015-05-24 18:33:33 +02:00
|
|
|
DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
|
2015-05-24 18:14:59 +02:00
|
|
|
StringRef Str) {
|
2015-05-24 18:33:33 +02:00
|
|
|
auto I = Pool.insert(std::make_pair(Str, EntryTy()));
|
|
|
|
if (I.second) {
|
|
|
|
auto &Entry = I.first->second;
|
2015-05-24 18:14:59 +02:00
|
|
|
Entry.Index = Pool.size() - 1;
|
|
|
|
Entry.Offset = NumBytes;
|
2015-05-24 18:58:59 +02:00
|
|
|
Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
|
2015-05-24 18:14:59 +02:00
|
|
|
|
|
|
|
NumBytes += Str.size() + 1;
|
|
|
|
assert(NumBytes > Entry.Offset && "Unexpected overflow");
|
2014-04-25 23:34:35 +02:00
|
|
|
}
|
2015-05-24 18:33:33 +02:00
|
|
|
return EntryRef(*I.first);
|
2014-04-25 23:34:35 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
|
|
|
|
MCSection *OffsetSection) {
|
2014-04-25 23:34:35 +02:00
|
|
|
if (Pool.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Start the dwarf str section.
|
2015-04-24 21:11:51 +02:00
|
|
|
Asm.OutStreamer->SwitchSection(StrSection);
|
2014-04-25 23:34:35 +02:00
|
|
|
|
|
|
|
// Get all of the string pool entries and put them in an array by their ID so
|
|
|
|
// we can sort them.
|
2015-05-24 18:14:59 +02:00
|
|
|
SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
|
2014-04-25 23:34:35 +02:00
|
|
|
|
|
|
|
for (const auto &E : Pool)
|
2015-05-24 18:14:59 +02:00
|
|
|
Entries[E.getValue().Index] = &E;
|
2014-04-25 23:34:35 +02:00
|
|
|
|
|
|
|
for (const auto &Entry : Entries) {
|
2015-05-24 18:58:59 +02:00
|
|
|
assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
|
|
|
|
"Mismatch between setting and entry");
|
|
|
|
|
2014-04-25 23:34:35 +02:00
|
|
|
// Emit a label for reference from debug information entries.
|
2015-05-24 18:58:59 +02:00
|
|
|
if (ShouldCreateSymbols)
|
|
|
|
Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
|
2014-04-25 23:34:35 +02:00
|
|
|
|
|
|
|
// Emit the string itself with a terminating null byte.
|
2015-05-24 18:14:59 +02:00
|
|
|
Asm.OutStreamer->AddComment("string offset=" +
|
|
|
|
Twine(Entry->getValue().Offset));
|
2015-04-24 21:11:51 +02:00
|
|
|
Asm.OutStreamer->EmitBytes(
|
2014-04-25 23:34:35 +02:00
|
|
|
StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've got an offset section go ahead and emit that now as well.
|
|
|
|
if (OffsetSection) {
|
2015-04-24 21:11:51 +02:00
|
|
|
Asm.OutStreamer->SwitchSection(OffsetSection);
|
2014-04-25 23:34:35 +02:00
|
|
|
unsigned size = 4; // FIXME: DWARF64 is 8.
|
2015-05-24 18:14:59 +02:00
|
|
|
for (const auto &Entry : Entries)
|
|
|
|
Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
|
2014-04-25 23:34:35 +02:00
|
|
|
}
|
|
|
|
}
|