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
18using namespace llvm;
19
20unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
21 resetUsedFlag(HasBeenUsed: true);
22 auto IterBool =
23 Pool.insert(KV: std::make_pair(x&: Sym, y: AddressPoolEntry(Pool.size(), TLS)));
24 return IterBool.first->second.Number;
25}
26
27MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
28 static const uint8_t AddrSize = Asm.MAI->getCodePointerSize();
29
30 MCSymbol *EndLabel =
31 Asm.emitDwarfUnitLength(Prefix: "debug_addr", Comment: "Length of contribution");
32 Asm.OutStreamer->AddComment(T: "DWARF version number");
33 Asm.emitInt16(Value: Asm.getDwarfVersion());
34 Asm.OutStreamer->AddComment(T: "Address size");
35 Asm.emitInt8(Value: AddrSize);
36 Asm.OutStreamer->AddComment(T: "Segment selector size");
37 Asm.emitInt8(Value: 0); // TODO: Support non-zero segment_selector_size.
38
39 return EndLabel;
40}
41
42// Emit addresses into the section given.
43void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
44 if (isEmpty())
45 return;
46
47 // Start the dwarf addr section.
48 Asm.OutStreamer->switchSection(Section: AddrSection);
49
50 MCSymbol *EndLabel = nullptr;
51
52 if (Asm.getDwarfVersion() >= 5)
53 EndLabel = emitHeader(Asm, Section: 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(Symbol: 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(Sym: I.first)
66 : MCSymbolRefExpr::create(Symbol: I.first, Ctx&: Asm.OutContext);
67
68 for (const MCExpr *Entry : Entries)
69 Asm.OutStreamer->emitValue(Value: Entry, Size: Asm.MAI->getCodePointerSize());
70
71 if (EndLabel)
72 Asm.OutStreamer->emitLabel(Symbol: EndLabel);
73}
74