• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "AddressPool.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/CodeGen/AsmPrinter.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/Target/TargetLoweringObjectFile.h"
16 #include <utility>
17 
18 using namespace llvm;
19 
getIndex(const MCSymbol * Sym,bool TLS)20 unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
21   HasBeenUsed = true;
22   auto IterBool =
23       Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
24   return IterBool.first->second.Number;
25 }
26 
27 
emitHeader(AsmPrinter & Asm,MCSection * Section)28 void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
29   static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
30   Asm.OutStreamer->SwitchSection(Section);
31 
32   uint64_t Length = sizeof(uint16_t) // version
33                   + sizeof(uint8_t)  // address_size
34                   + sizeof(uint8_t)  // segment_selector_size
35                   + AddrSize * Pool.size(); // entries
36   Asm.emitInt32(Length); // TODO: Support DWARF64 format.
37   Asm.emitInt16(Asm.getDwarfVersion());
38   Asm.emitInt8(AddrSize);
39   Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
40 }
41 
42 // Emit addresses into the section given.
emit(AsmPrinter & Asm,MCSection * AddrSection)43 void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
44   if (Asm.getDwarfVersion() >= 5)
45     emitHeader(Asm, AddrSection);
46 
47   if (Pool.empty())
48     return;
49 
50   // Start the dwarf addr section.
51   Asm.OutStreamer->SwitchSection(AddrSection);
52 
53   // Order the address pool entries by ID
54   SmallVector<const MCExpr *, 64> Entries(Pool.size());
55 
56   for (const auto &I : Pool)
57     Entries[I.second.Number] =
58         I.second.TLS
59             ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
60             : MCSymbolRefExpr::create(I.first, Asm.OutContext);
61 
62   for (const MCExpr *Entry : Entries)
63     Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
64 }
65