1 //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===//
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
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/MCAsmInfo.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/Target/TargetLoweringObjectFile.h"
20 unsigned AddressPool::getIndex(const MCSymbol
*Sym
, bool TLS
) {
23 Pool
.insert(std::make_pair(Sym
, AddressPoolEntry(Pool
.size(), TLS
)));
24 return IterBool
.first
->second
.Number
;
27 MCSymbol
*AddressPool::emitHeader(AsmPrinter
&Asm
, MCSection
*Section
) {
28 static const uint8_t AddrSize
= Asm
.MAI
->getCodePointerSize();
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.
42 // Emit addresses into the section given.
43 void AddressPool::emit(AsmPrinter
&Asm
, MCSection
*AddrSection
) {
47 // Start the dwarf addr section.
48 Asm
.OutStreamer
->switchSection(AddrSection
);
50 MCSymbol
*EndLabel
= nullptr;
52 if (Asm
.getDwarfVersion() >= 5)
53 EndLabel
= emitHeader(Asm
, AddrSection
);
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
);
59 // Order the address pool entries by ID
60 SmallVector
<const MCExpr
*, 64> Entries(Pool
.size());
62 for (const auto &I
: Pool
)
63 Entries
[I
.second
.Number
] =
65 ? Asm
.getObjFileLowering().getDebugThreadLocalSymbol(I
.first
)
66 : MCSymbolRefExpr::create(I
.first
, Asm
.OutContext
);
68 for (const MCExpr
*Entry
: Entries
)
69 Asm
.OutStreamer
->emitValue(Entry
, Asm
.MAI
->getCodePointerSize());
72 Asm
.OutStreamer
->emitLabel(EndLabel
);