Recommit r373598 "[yaml2obj/obj2yaml] - Add support for SHT_LLVM_ADDRSIG sections."
[llvm-complete.git] / lib / CodeGen / AsmPrinter / AddressPool.cpp
blobf11c7de5ed8afadce9fd03f33bf2b4c6698c5c79
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 //===----------------------------------------------------------------------===//
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/MCStreamer.h"
14 #include "llvm/Target/TargetLoweringObjectFile.h"
15 #include <utility>
17 using namespace llvm;
19 unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
20 HasBeenUsed = true;
21 auto IterBool =
22 Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
23 return IterBool.first->second.Number;
26 MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
27 static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
28 StringRef Prefix = "debug_addr_";
29 MCSymbol *BeginLabel = Asm.createTempSymbol(Prefix + "start");
30 MCSymbol *EndLabel = Asm.createTempSymbol(Prefix + "end");
32 Asm.OutStreamer->AddComment("Length of contribution");
33 Asm.EmitLabelDifference(EndLabel, BeginLabel,
34 4); // TODO: Support DWARF64 format.
35 Asm.OutStreamer->EmitLabel(BeginLabel);
36 Asm.OutStreamer->AddComment("DWARF version number");
37 Asm.emitInt16(Asm.getDwarfVersion());
38 Asm.OutStreamer->AddComment("Address size");
39 Asm.emitInt8(AddrSize);
40 Asm.OutStreamer->AddComment("Segment selector size");
41 Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
43 return EndLabel;
46 // Emit addresses into the section given.
47 void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
48 if (isEmpty())
49 return;
51 // Start the dwarf addr section.
52 Asm.OutStreamer->SwitchSection(AddrSection);
54 MCSymbol *EndLabel = nullptr;
56 if (Asm.getDwarfVersion() >= 5)
57 EndLabel = emitHeader(Asm, AddrSection);
59 // Define the symbol that marks the start of the contribution.
60 // It is referenced via DW_AT_addr_base.
61 Asm.OutStreamer->EmitLabel(AddressTableBaseSym);
63 // Order the address pool entries by ID
64 SmallVector<const MCExpr *, 64> Entries(Pool.size());
66 for (const auto &I : Pool)
67 Entries[I.second.Number] =
68 I.second.TLS
69 ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
70 : MCSymbolRefExpr::create(I.first, Asm.OutContext);
72 for (const MCExpr *Entry : Entries)
73 Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
75 if (EndLabel)
76 Asm.OutStreamer->EmitLabel(EndLabel);