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