xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 //===- llvm/CodeGen/AddressPool.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 
9 #include "AddressPool.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/CodeGen/AsmPrinter.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/Target/TargetLoweringObjectFile.h"
16 #include <utility>
17 
18 using namespace llvm;
19 
20 unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
21   resetUsedFlag(true);
22   auto IterBool =
23       Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
24   return IterBool.first->second.Number;
25 }
26 
27 MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
28   static const uint8_t AddrSize = Asm.MAI->getCodePointerSize();
29 
30   MCSymbol *EndLabel =
31       Asm.emitDwarfUnitLength("debug_addr", "Length of contribution");
32   Asm.OutStreamer->AddComment("DWARF version number");
33   Asm.emitInt16(Asm.getDwarfVersion());
34   Asm.OutStreamer->AddComment("Address size");
35   Asm.emitInt8(AddrSize);
36   Asm.OutStreamer->AddComment("Segment selector size");
37   Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
38 
39   return EndLabel;
40 }
41 
42 // Emit addresses into the section given.
43 void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
44   if (isEmpty())
45     return;
46 
47   // Start the dwarf addr section.
48   Asm.OutStreamer->switchSection(AddrSection);
49 
50   MCSymbol *EndLabel = nullptr;
51 
52   if (Asm.getDwarfVersion() >= 5)
53     EndLabel = emitHeader(Asm, AddrSection);
54 
55   // Define the symbol that marks the start of the contribution.
56   // It is referenced via DW_AT_addr_base.
57   Asm.OutStreamer->emitLabel(AddressTableBaseSym);
58 
59   // Order the address pool entries by ID
60   SmallVector<const MCExpr *, 64> Entries(Pool.size());
61 
62   for (const auto &I : Pool)
63     Entries[I.second.Number] =
64         I.second.TLS
65             ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
66             : MCSymbolRefExpr::create(I.first, Asm.OutContext);
67 
68   for (const MCExpr *Entry : Entries)
69     Asm.OutStreamer->emitValue(Entry, Asm.MAI->getCodePointerSize());
70 
71   if (EndLabel)
72     Asm.OutStreamer->emitLabel(EndLabel);
73 }
74