xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp (revision 8c6499fa6dbe9d01fcedd9c5aa176fdf5ffca2c8)
1 //===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
2 //
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"
11 #include "llvm/MC/MCStreamer.h"
12 
13 using namespace llvm;
14 
15 std::pair<MCSymbol *, unsigned> &DwarfStringPool::getEntry(AsmPrinter &Asm,
16                                                            StringRef Str) {
17   std::pair<MCSymbol *, unsigned> &Entry = Pool[Str];
18   if (!Entry.first) {
19     Entry.second = Pool.size() - 1;
20     Entry.first = Asm.createTempSymbol(Prefix);
21   }
22   return Entry;
23 }
24 
25 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
26                            MCSection *OffsetSection) {
27   if (Pool.empty())
28     return;
29 
30   // Start the dwarf str section.
31   Asm.OutStreamer->SwitchSection(StrSection);
32 
33   // Get all of the string pool entries and put them in an array by their ID so
34   // we can sort them.
35   SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
36   Entries(Pool.size());
37 
38   for (const auto &E : Pool)
39     Entries[E.getValue().second] = &E;
40 
41   for (const auto &Entry : Entries) {
42     // Emit a label for reference from debug information entries.
43     Asm.OutStreamer->EmitLabel(Entry->getValue().first);
44 
45     // Emit the string itself with a terminating null byte.
46     Asm.OutStreamer->EmitBytes(
47         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
48   }
49 
50   // If we've got an offset section go ahead and emit that now as well.
51   if (OffsetSection) {
52     Asm.OutStreamer->SwitchSection(OffsetSection);
53     unsigned offset = 0;
54     unsigned size = 4; // FIXME: DWARF64 is 8.
55     for (const auto &Entry : Entries) {
56       Asm.OutStreamer->EmitIntValue(offset, size);
57       offset += Entry->getKeyLength() + 1;
58     }
59   }
60 }
61