LLVM  9.0.0svn
AddressPool.cpp
Go to the documentation of this file.
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"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/MC/MCStreamer.h"
15 #include <utility>
16 
17 using namespace llvm;
18 
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;
24 }
25 
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");
31 
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.
42 
43  return EndLabel;
44 }
45 
46 // Emit addresses into the section given.
47 void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
48  if (isEmpty())
49  return;
50 
51  // Start the dwarf addr section.
52  Asm.OutStreamer->SwitchSection(AddrSection);
53 
54  MCSymbol *EndLabel = nullptr;
55 
56  if (Asm.getDwarfVersion() >= 5)
57  EndLabel = emitHeader(Asm, AddrSection);
58 
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);
62 
63  // Order the address pool entries by ID
64  SmallVector<const MCExpr *, 64> Entries(Pool.size());
65 
66  for (const auto &I : Pool)
67  Entries[I.second.Number] =
68  I.second.TLS
70  : MCSymbolRefExpr::create(I.first, Asm.OutContext);
71 
72  for (const MCExpr *Entry : Entries)
73  Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
74 
75  if (EndLabel)
76  Asm.OutStreamer->EmitLabel(EndLabel);
77 }
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:213
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:321
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:88
uint16_t getDwarfVersion() const
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
void emit(AsmPrinter &Asm, MCSection *AddrSection)
Definition: AddressPool.cpp:47
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:648
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
#define I(x, y, z)
Definition: MD5.cpp:58
void EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) const
Emit something like ".long Hi-Lo" where the size in bytes of the directive is specified by Size and H...
void emitInt8(int Value) const
Emit a byte directive and value.
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:217
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
virtual const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const
Create a symbol reference to describe the given TLS variable when emitting the address in debug info...
MCSymbol * createTempSymbol(const Twine &Name) const
void emitInt16(int Value) const
Emit a short directive and value.
unsigned getIndex(const MCSymbol *Sym, bool TLS=false)
Returns the index into the address pool with the given label/symbol.
Definition: AddressPool.cpp:19