blob: 2066f745e318b1f1861861f46ad3193adc0f298f [file] [log] [blame]
David Blaikiedaefdbf2014-04-25 21:34:35 +00001//===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DwarfStringPool.h"
Duncan P. N. Exon Smith9d50e822015-05-24 16:54:59 +000011#include "llvm/CodeGen/AsmPrinter.h"
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000012#include "llvm/MC/MCAsmInfo.h"
David Blaikiedaefdbf2014-04-25 21:34:35 +000013#include "llvm/MC/MCStreamer.h"
14
15using namespace llvm;
16
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000017DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
18 StringRef Prefix)
19 : Pool(A), Prefix(Prefix),
20 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
21
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000022DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000023 StringRef Str) {
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000024 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
25 if (I.second) {
26 auto &Entry = I.first->second;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000027 Entry.Index = Pool.size() - 1;
28 Entry.Offset = NumBytes;
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000029 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000030
31 NumBytes += Str.size() + 1;
32 assert(NumBytes > Entry.Offset && "Unexpected overflow");
David Blaikiedaefdbf2014-04-25 21:34:35 +000033 }
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000034 return EntryRef(*I.first);
David Blaikiedaefdbf2014-04-25 21:34:35 +000035}
36
Rafael Espindola0709a7b2015-05-21 19:20:38 +000037void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
38 MCSection *OffsetSection) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000039 if (Pool.empty())
40 return;
41
42 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000043 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000044
45 // Get all of the string pool entries and put them in an array by their ID so
46 // we can sort them.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000047 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000048
49 for (const auto &E : Pool)
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000050 Entries[E.getValue().Index] = &E;
David Blaikiedaefdbf2014-04-25 21:34:35 +000051
52 for (const auto &Entry : Entries) {
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000053 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
54 "Mismatch between setting and entry");
55
David Blaikiedaefdbf2014-04-25 21:34:35 +000056 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000057 if (ShouldCreateSymbols)
58 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +000059
60 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000061 Asm.OutStreamer->AddComment("string offset=" +
62 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +000063 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +000064 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
65 }
66
67 // If we've got an offset section go ahead and emit that now as well.
68 if (OffsetSection) {
Lang Hames9ff69c82015-04-24 19:11:51 +000069 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000070 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000071 for (const auto &Entry : Entries)
72 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +000073 }
74}