blob: f2964673a6bcbc5c21b8568f14f82410fdb268fa [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
Rafael Espindola0709a7b2015-05-21 19:20:38 +000042void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
Wolfgang Pieb456b5552018-01-26 18:52:58 +000043 MCSection *OffsetSection, bool UseRelativeOffsets) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000044 if (Pool.empty())
45 return;
46
47 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000048 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000049
50 // Get all of the string pool entries and put them in an array by their ID so
51 // we can sort them.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000052 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000053
54 for (const auto &E : Pool)
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000055 Entries[E.getValue().Index] = &E;
David Blaikiedaefdbf2014-04-25 21:34:35 +000056
57 for (const auto &Entry : Entries) {
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000058 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
59 "Mismatch between setting and entry");
60
David Blaikiedaefdbf2014-04-25 21:34:35 +000061 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith882a2b52015-05-24 16:58:59 +000062 if (ShouldCreateSymbols)
63 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +000064
65 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000066 Asm.OutStreamer->AddComment("string offset=" +
67 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +000068 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +000069 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
70 }
71
72 // If we've got an offset section go ahead and emit that now as well.
73 if (OffsetSection) {
Lang Hames9ff69c82015-04-24 19:11:51 +000074 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000075 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000076 for (const auto &Entry : Entries)
Wolfgang Pieb456b5552018-01-26 18:52:58 +000077 if (UseRelativeOffsets)
78 Asm.emitDwarfStringOffset(Entry->getValue());
79 else
80 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +000081 }
82}