blob: 02016534a774d21f6f12e9e0d51e2d5b13ef6045 [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
Pavel Labath2f088112018-08-07 09:54:52 +000027StringMapEntry<DwarfStringPool::EntryTy> &
28DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000029 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
Pavel Labath2f088112018-08-07 09:54:52 +000030 auto &Entry = I.first->second;
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000031 if (I.second) {
Pavel Labath2f088112018-08-07 09:54:52 +000032 Entry.Index = EntryTy::NotIndexed;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000033 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 }
Pavel Labath2f088112018-08-07 09:54:52 +000039 return *I.first;
40}
41
42DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
43 StringRef Str) {
44 auto &MapEntry = getEntryImpl(Asm, Str);
45 return EntryRef(MapEntry, false);
46}
47
48DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
49 StringRef Str) {
50 auto &MapEntry = getEntryImpl(Asm, Str);
51 if (!MapEntry.getValue().isIndexed())
52 MapEntry.getValue().Index = NumIndexedStrings++;
53 return EntryRef(MapEntry, true);
David Blaikiedaefdbf2014-04-25 21:34:35 +000054}
55
Pavel Labath7bfa5d62018-07-26 14:36:07 +000056void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
57 MCSection *Section,
58 MCSymbol *StartSym) {
Pavel Labath2f088112018-08-07 09:54:52 +000059 if (getNumIndexedStrings() == 0)
Pavel Labath7bfa5d62018-07-26 14:36:07 +000060 return;
61 Asm.OutStreamer->SwitchSection(Section);
62 unsigned EntrySize = 4;
63 // FIXME: DWARF64
64 // We are emitting the header for a contribution to the string offsets
65 // table. The header consists of an entry with the contribution's
66 // size (not including the size of the length field), the DWARF version and
67 // 2 bytes of padding.
Pavel Labath2f088112018-08-07 09:54:52 +000068 Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
Pavel Labath7bfa5d62018-07-26 14:36:07 +000069 Asm.emitInt16(Asm.getDwarfVersion());
70 Asm.emitInt16(0);
71 // Define the symbol that marks the start of the contribution. It is
72 // referenced by most unit headers via DW_AT_str_offsets_base.
73 // Split units do not use the attribute.
74 if (StartSym)
75 Asm.OutStreamer->EmitLabel(StartSym);
76}
77
Rafael Espindola0709a7b2015-05-21 19:20:38 +000078void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
Wolfgang Pieb456b5552018-01-26 18:52:58 +000079 MCSection *OffsetSection, bool UseRelativeOffsets) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000080 if (Pool.empty())
81 return;
82
83 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000084 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000085
Pavel Labath2f088112018-08-07 09:54:52 +000086 // Get all of the string pool entries and sort them by their offset.
87 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
88 Entries.reserve(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000089
90 for (const auto &E : Pool)
Pavel Labath2f088112018-08-07 09:54:52 +000091 Entries.push_back(&E);
92
Fangrui Song3507c6e2018-09-30 22:31:29 +000093 llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
94 const StringMapEntry<EntryTy> *B) {
95 return A->getValue().Offset < B->getValue().Offset;
96 });
David Blaikiedaefdbf2014-04-25 21:34:35 +000097
98 for (const auto &Entry : Entries) {
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000099 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
100 "Mismatch between setting and entry");
101
David Blaikiedaefdbf2014-04-25 21:34:35 +0000102 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +0000103 if (ShouldCreateSymbols)
104 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000105
106 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +0000107 Asm.OutStreamer->AddComment("string offset=" +
108 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +0000109 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +0000110 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
111 }
112
113 // If we've got an offset section go ahead and emit that now as well.
114 if (OffsetSection) {
Pavel Labath2f088112018-08-07 09:54:52 +0000115 // Now only take the indexed entries and put them in an array by their ID so
116 // we can emit them in order.
117 Entries.resize(NumIndexedStrings);
118 for (const auto &Entry : Pool) {
119 if (Entry.getValue().isIndexed())
120 Entries[Entry.getValue().Index] = &Entry;
121 }
122
Lang Hames9ff69c82015-04-24 19:11:51 +0000123 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000124 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +0000125 for (const auto &Entry : Entries)
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000126 if (UseRelativeOffsets)
127 Asm.emitDwarfStringOffset(Entry->getValue());
128 else
129 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000130 }
131}