blob: 2a76dcb1b0827658e33a8ec162dccdade22719bd [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David Blaikiedaefdbf2014-04-25 21:34:35 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "DwarfStringPool.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000010#include "llvm/ADT/SmallVector.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/ADT/Twine.h"
Duncan P. N. Exon Smith9d50e822015-05-24 16:54:59 +000013#include "llvm/CodeGen/AsmPrinter.h"
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000014#include "llvm/MC/MCAsmInfo.h"
David Blaikiedaefdbf2014-04-25 21:34:35 +000015#include "llvm/MC/MCStreamer.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000016#include <cassert>
17#include <utility>
David Blaikiedaefdbf2014-04-25 21:34:35 +000018
19using namespace llvm;
20
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000021DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
22 StringRef Prefix)
23 : Pool(A), Prefix(Prefix),
24 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
25
Pavel Labath2f088112018-08-07 09:54:52 +000026StringMapEntry<DwarfStringPool::EntryTy> &
27DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000028 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
Pavel Labath2f088112018-08-07 09:54:52 +000029 auto &Entry = I.first->second;
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000030 if (I.second) {
Pavel Labath2f088112018-08-07 09:54:52 +000031 Entry.Index = EntryTy::NotIndexed;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000032 Entry.Offset = NumBytes;
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000033 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000034
35 NumBytes += Str.size() + 1;
36 assert(NumBytes > Entry.Offset && "Unexpected overflow");
David Blaikiedaefdbf2014-04-25 21:34:35 +000037 }
Pavel Labath2f088112018-08-07 09:54:52 +000038 return *I.first;
39}
40
41DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
42 StringRef Str) {
43 auto &MapEntry = getEntryImpl(Asm, Str);
44 return EntryRef(MapEntry, false);
45}
46
47DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
48 StringRef Str) {
49 auto &MapEntry = getEntryImpl(Asm, Str);
50 if (!MapEntry.getValue().isIndexed())
51 MapEntry.getValue().Index = NumIndexedStrings++;
52 return EntryRef(MapEntry, true);
David Blaikiedaefdbf2014-04-25 21:34:35 +000053}
54
Pavel Labath7bfa5d62018-07-26 14:36:07 +000055void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
56 MCSection *Section,
57 MCSymbol *StartSym) {
Pavel Labath2f088112018-08-07 09:54:52 +000058 if (getNumIndexedStrings() == 0)
Pavel Labath7bfa5d62018-07-26 14:36:07 +000059 return;
60 Asm.OutStreamer->SwitchSection(Section);
61 unsigned EntrySize = 4;
62 // FIXME: DWARF64
63 // We are emitting the header for a contribution to the string offsets
64 // table. The header consists of an entry with the contribution's
65 // size (not including the size of the length field), the DWARF version and
66 // 2 bytes of padding.
Pavel Labath2f088112018-08-07 09:54:52 +000067 Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
Pavel Labath7bfa5d62018-07-26 14:36:07 +000068 Asm.emitInt16(Asm.getDwarfVersion());
69 Asm.emitInt16(0);
70 // Define the symbol that marks the start of the contribution. It is
71 // referenced by most unit headers via DW_AT_str_offsets_base.
72 // Split units do not use the attribute.
73 if (StartSym)
74 Asm.OutStreamer->EmitLabel(StartSym);
75}
76
Rafael Espindola0709a7b2015-05-21 19:20:38 +000077void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
Wolfgang Pieb456b5552018-01-26 18:52:58 +000078 MCSection *OffsetSection, bool UseRelativeOffsets) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000079 if (Pool.empty())
80 return;
81
82 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000083 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000084
Pavel Labath2f088112018-08-07 09:54:52 +000085 // Get all of the string pool entries and sort them by their offset.
86 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
87 Entries.reserve(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000088
89 for (const auto &E : Pool)
Pavel Labath2f088112018-08-07 09:54:52 +000090 Entries.push_back(&E);
91
Fangrui Song3507c6e2018-09-30 22:31:29 +000092 llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
93 const StringMapEntry<EntryTy> *B) {
94 return A->getValue().Offset < B->getValue().Offset;
95 });
David Blaikiedaefdbf2014-04-25 21:34:35 +000096
97 for (const auto &Entry : Entries) {
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000098 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
99 "Mismatch between setting and entry");
100
David Blaikiedaefdbf2014-04-25 21:34:35 +0000101 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +0000102 if (ShouldCreateSymbols)
103 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000104
105 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +0000106 Asm.OutStreamer->AddComment("string offset=" +
107 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +0000108 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +0000109 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
110 }
111
112 // If we've got an offset section go ahead and emit that now as well.
113 if (OffsetSection) {
Pavel Labath2f088112018-08-07 09:54:52 +0000114 // Now only take the indexed entries and put them in an array by their ID so
115 // we can emit them in order.
116 Entries.resize(NumIndexedStrings);
117 for (const auto &Entry : Pool) {
118 if (Entry.getValue().isIndexed())
119 Entries[Entry.getValue().Index] = &Entry;
120 }
121
Lang Hames9ff69c82015-04-24 19:11:51 +0000122 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000123 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +0000124 for (const auto &Entry : Entries)
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000125 if (UseRelativeOffsets)
126 Asm.emitDwarfStringOffset(Entry->getValue());
127 else
128 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +0000129 }
130}