DwarfStringPool.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "DwarfStringPool.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/ADT/Twine.h"
  12. #include "llvm/CodeGen/AsmPrinter.h"
  13. #include "llvm/MC/MCAsmInfo.h"
  14. #include "llvm/MC/MCStreamer.h"
  15. #include <cassert>
  16. #include <utility>
  17. using namespace llvm;
  18. DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
  19. StringRef Prefix)
  20. : Pool(A), Prefix(Prefix),
  21. ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
  22. StringMapEntry<DwarfStringPool::EntryTy> &
  23. DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
  24. auto I = Pool.insert(std::make_pair(Str, EntryTy()));
  25. auto &Entry = I.first->second;
  26. if (I.second) {
  27. Entry.Index = EntryTy::NotIndexed;
  28. Entry.Offset = NumBytes;
  29. Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
  30. NumBytes += Str.size() + 1;
  31. assert(NumBytes > Entry.Offset && "Unexpected overflow");
  32. }
  33. return *I.first;
  34. }
  35. DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
  36. StringRef Str) {
  37. auto &MapEntry = getEntryImpl(Asm, Str);
  38. return EntryRef(MapEntry, false);
  39. }
  40. DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
  41. StringRef Str) {
  42. auto &MapEntry = getEntryImpl(Asm, Str);
  43. if (!MapEntry.getValue().isIndexed())
  44. MapEntry.getValue().Index = NumIndexedStrings++;
  45. return EntryRef(MapEntry, true);
  46. }
  47. void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
  48. MCSection *Section,
  49. MCSymbol *StartSym) {
  50. if (getNumIndexedStrings() == 0)
  51. return;
  52. Asm.OutStreamer->SwitchSection(Section);
  53. unsigned EntrySize = 4;
  54. // FIXME: DWARF64
  55. // We are emitting the header for a contribution to the string offsets
  56. // table. The header consists of an entry with the contribution's
  57. // size (not including the size of the length field), the DWARF version and
  58. // 2 bytes of padding.
  59. Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
  60. Asm.emitInt16(Asm.getDwarfVersion());
  61. Asm.emitInt16(0);
  62. // Define the symbol that marks the start of the contribution. It is
  63. // referenced by most unit headers via DW_AT_str_offsets_base.
  64. // Split units do not use the attribute.
  65. if (StartSym)
  66. Asm.OutStreamer->EmitLabel(StartSym);
  67. }
  68. void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
  69. MCSection *OffsetSection, bool UseRelativeOffsets) {
  70. if (Pool.empty())
  71. return;
  72. // Start the dwarf str section.
  73. Asm.OutStreamer->SwitchSection(StrSection);
  74. // Get all of the string pool entries and sort them by their offset.
  75. SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
  76. Entries.reserve(Pool.size());
  77. for (const auto &E : Pool)
  78. Entries.push_back(&E);
  79. llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
  80. const StringMapEntry<EntryTy> *B) {
  81. return A->getValue().Offset < B->getValue().Offset;
  82. });
  83. for (const auto &Entry : Entries) {
  84. assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
  85. "Mismatch between setting and entry");
  86. // Emit a label for reference from debug information entries.
  87. if (ShouldCreateSymbols)
  88. Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
  89. // Emit the string itself with a terminating null byte.
  90. Asm.OutStreamer->AddComment("string offset=" +
  91. Twine(Entry->getValue().Offset));
  92. Asm.OutStreamer->EmitBytes(
  93. StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
  94. }
  95. // If we've got an offset section go ahead and emit that now as well.
  96. if (OffsetSection) {
  97. // Now only take the indexed entries and put them in an array by their ID so
  98. // we can emit them in order.
  99. Entries.resize(NumIndexedStrings);
  100. for (const auto &Entry : Pool) {
  101. if (Entry.getValue().isIndexed())
  102. Entries[Entry.getValue().Index] = &Entry;
  103. }
  104. Asm.OutStreamer->SwitchSection(OffsetSection);
  105. unsigned size = 4; // FIXME: DWARF64 is 8.
  106. for (const auto &Entry : Entries)
  107. if (UseRelativeOffsets)
  108. Asm.emitDwarfStringOffset(Entry->getValue());
  109. else
  110. Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
  111. }
  112. }