DwarfStringPool.cpp 4.7 KB

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