blob: a61fa83cfb03dd64a8d01fc58d2eaa6716df394f [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
David Blaikiedaefdbf2014-04-25 21:34:35 +00002//
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"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/Twine.h"
Duncan P. N. Exon Smith9d50e822015-05-24 16:54:59 +000014#include "llvm/CodeGen/AsmPrinter.h"
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000015#include "llvm/MC/MCAsmInfo.h"
David Blaikiedaefdbf2014-04-25 21:34:35 +000016#include "llvm/MC/MCStreamer.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000017#include <cassert>
18#include <utility>
David Blaikiedaefdbf2014-04-25 21:34:35 +000019
20using namespace llvm;
21
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000022DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
23 StringRef Prefix)
24 : Pool(A), Prefix(Prefix),
25 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
26
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000027DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000028 StringRef Str) {
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000029 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
30 if (I.second) {
31 auto &Entry = I.first->second;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000032 Entry.Index = Pool.size() - 1;
33 Entry.Offset = NumBytes;
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000034 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000035
36 NumBytes += Str.size() + 1;
37 assert(NumBytes > Entry.Offset && "Unexpected overflow");
David Blaikiedaefdbf2014-04-25 21:34:35 +000038 }
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000039 return EntryRef(*I.first);
David Blaikiedaefdbf2014-04-25 21:34:35 +000040}
41
Pavel Labath7bfa5d62018-07-26 14:36:07 +000042void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
43 MCSection *Section,
44 MCSymbol *StartSym) {
45 if (empty())
46 return;
47 Asm.OutStreamer->SwitchSection(Section);
48 unsigned EntrySize = 4;
49 // FIXME: DWARF64
50 // We are emitting the header for a contribution to the string offsets
51 // table. The header consists of an entry with the contribution's
52 // size (not including the size of the length field), the DWARF version and
53 // 2 bytes of padding.
54 Asm.emitInt32(size() * EntrySize + 4);
55 Asm.emitInt16(Asm.getDwarfVersion());
56 Asm.emitInt16(0);
57 // Define the symbol that marks the start of the contribution. It is
58 // referenced by most unit headers via DW_AT_str_offsets_base.
59 // Split units do not use the attribute.
60 if (StartSym)
61 Asm.OutStreamer->EmitLabel(StartSym);
62}
63
Rafael Espindola0709a7b2015-05-21 19:20:38 +000064void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
Wolfgang Pieb456b5552018-01-26 18:52:58 +000065 MCSection *OffsetSection, bool UseRelativeOffsets) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000066 if (Pool.empty())
67 return;
68
69 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000070 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000071
72 // Get all of the string pool entries and put them in an array by their ID so
73 // we can sort them.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000074 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000075
76 for (const auto &E : Pool)
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000077 Entries[E.getValue().Index] = &E;
David Blaikiedaefdbf2014-04-25 21:34:35 +000078
79 for (const auto &Entry : Entries) {
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000080 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
81 "Mismatch between setting and entry");
82
David Blaikiedaefdbf2014-04-25 21:34:35 +000083 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000084 if (ShouldCreateSymbols)
85 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +000086
87 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000088 Asm.OutStreamer->AddComment("string offset=" +
89 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +000090 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +000091 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
92 }
93
94 // If we've got an offset section go ahead and emit that now as well.
95 if (OffsetSection) {
Lang Hames9ff69c82015-04-24 19:11:51 +000096 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000097 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000098 for (const auto &Entry : Entries)
Wolfgang Pieb456b5552018-01-26 18:52:58 +000099 if (UseRelativeOffsets)
100 Asm.emitDwarfStringOffset(Entry->getValue());
101 else
102 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000103 }
104}