xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "DwarfStringPool.h"
100b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
110b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
120b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
130b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
140b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
150b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
160b57cec5SDimitry Andric #include <cassert>
170b57cec5SDimitry Andric #include <utility>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
220b57cec5SDimitry Andric                                  StringRef Prefix)
230b57cec5SDimitry Andric     : Pool(A), Prefix(Prefix),
240b57cec5SDimitry Andric       ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric StringMapEntry<DwarfStringPool::EntryTy> &
270b57cec5SDimitry Andric DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) {
280b57cec5SDimitry Andric   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
290b57cec5SDimitry Andric   auto &Entry = I.first->second;
300b57cec5SDimitry Andric   if (I.second) {
310b57cec5SDimitry Andric     Entry.Index = EntryTy::NotIndexed;
320b57cec5SDimitry Andric     Entry.Offset = NumBytes;
330b57cec5SDimitry Andric     Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric     NumBytes += Str.size() + 1;
360b57cec5SDimitry Andric     assert(NumBytes > Entry.Offset && "Unexpected overflow");
370b57cec5SDimitry Andric   }
380b57cec5SDimitry Andric   return *I.first;
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
420b57cec5SDimitry Andric                                                     StringRef Str) {
430b57cec5SDimitry Andric   auto &MapEntry = getEntryImpl(Asm, Str);
440b57cec5SDimitry Andric   return EntryRef(MapEntry, false);
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
480b57cec5SDimitry Andric                                                            StringRef Str) {
490b57cec5SDimitry Andric   auto &MapEntry = getEntryImpl(Asm, Str);
500b57cec5SDimitry Andric   if (!MapEntry.getValue().isIndexed())
510b57cec5SDimitry Andric     MapEntry.getValue().Index = NumIndexedStrings++;
520b57cec5SDimitry Andric   return EntryRef(MapEntry, true);
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
560b57cec5SDimitry Andric                                                    MCSection *Section,
570b57cec5SDimitry Andric                                                    MCSymbol *StartSym) {
580b57cec5SDimitry Andric   if (getNumIndexedStrings() == 0)
590b57cec5SDimitry Andric     return;
600b57cec5SDimitry Andric   Asm.OutStreamer->SwitchSection(Section);
610b57cec5SDimitry Andric   unsigned EntrySize = 4;
620b57cec5SDimitry Andric   // FIXME: DWARF64
630b57cec5SDimitry Andric   // We are emitting the header for a contribution to the string offsets
640b57cec5SDimitry Andric   // table. The header consists of an entry with the contribution's
650b57cec5SDimitry Andric   // size (not including the size of the length field), the DWARF version and
660b57cec5SDimitry Andric   // 2 bytes of padding.
670b57cec5SDimitry Andric   Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4);
680b57cec5SDimitry Andric   Asm.emitInt16(Asm.getDwarfVersion());
690b57cec5SDimitry Andric   Asm.emitInt16(0);
700b57cec5SDimitry Andric   // Define the symbol that marks the start of the contribution. It is
710b57cec5SDimitry Andric   // referenced by most unit headers via DW_AT_str_offsets_base.
720b57cec5SDimitry Andric   // Split units do not use the attribute.
730b57cec5SDimitry Andric   if (StartSym)
74*5ffd83dbSDimitry Andric     Asm.OutStreamer->emitLabel(StartSym);
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
780b57cec5SDimitry Andric                            MCSection *OffsetSection, bool UseRelativeOffsets) {
790b57cec5SDimitry Andric   if (Pool.empty())
800b57cec5SDimitry Andric     return;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // Start the dwarf str section.
830b57cec5SDimitry Andric   Asm.OutStreamer->SwitchSection(StrSection);
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Get all of the string pool entries and sort them by their offset.
860b57cec5SDimitry Andric   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
870b57cec5SDimitry Andric   Entries.reserve(Pool.size());
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   for (const auto &E : Pool)
900b57cec5SDimitry Andric     Entries.push_back(&E);
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   llvm::sort(Entries, [](const StringMapEntry<EntryTy> *A,
930b57cec5SDimitry Andric                          const StringMapEntry<EntryTy> *B) {
940b57cec5SDimitry Andric     return A->getValue().Offset < B->getValue().Offset;
950b57cec5SDimitry Andric   });
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   for (const auto &Entry : Entries) {
980b57cec5SDimitry Andric     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
990b57cec5SDimitry Andric            "Mismatch between setting and entry");
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric     // Emit a label for reference from debug information entries.
1020b57cec5SDimitry Andric     if (ShouldCreateSymbols)
103*5ffd83dbSDimitry Andric       Asm.OutStreamer->emitLabel(Entry->getValue().Symbol);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric     // Emit the string itself with a terminating null byte.
1060b57cec5SDimitry Andric     Asm.OutStreamer->AddComment("string offset=" +
1070b57cec5SDimitry Andric                                 Twine(Entry->getValue().Offset));
108*5ffd83dbSDimitry Andric     Asm.OutStreamer->emitBytes(
1090b57cec5SDimitry Andric         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
1100b57cec5SDimitry Andric   }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   // If we've got an offset section go ahead and emit that now as well.
1130b57cec5SDimitry Andric   if (OffsetSection) {
1140b57cec5SDimitry Andric     // Now only take the indexed entries and put them in an array by their ID so
1150b57cec5SDimitry Andric     // we can emit them in order.
1160b57cec5SDimitry Andric     Entries.resize(NumIndexedStrings);
1170b57cec5SDimitry Andric     for (const auto &Entry : Pool) {
1180b57cec5SDimitry Andric       if (Entry.getValue().isIndexed())
1190b57cec5SDimitry Andric         Entries[Entry.getValue().Index] = &Entry;
1200b57cec5SDimitry Andric     }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric     Asm.OutStreamer->SwitchSection(OffsetSection);
1230b57cec5SDimitry Andric     unsigned size = 4; // FIXME: DWARF64 is 8.
1240b57cec5SDimitry Andric     for (const auto &Entry : Entries)
1250b57cec5SDimitry Andric       if (UseRelativeOffsets)
1260b57cec5SDimitry Andric         Asm.emitDwarfStringOffset(Entry->getValue());
1270b57cec5SDimitry Andric       else
128*5ffd83dbSDimitry Andric         Asm.OutStreamer->emitIntValue(Entry->getValue().Offset, size);
1290b57cec5SDimitry Andric   }
1300b57cec5SDimitry Andric }
131