blob: 97c08da38273c7aa67e3e0136bc9b763c14d5a4c [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===//
David Blaikie69d0cf02014-04-25 06:22:32 +00002//
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//===----------------------------------------------------------------------===//
David Blaikiee226b082014-04-23 21:04:59 +00009
10#include "AddressPool.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000011#include "llvm/ADT/SmallVector.h"
David Blaikiee226b082014-04-23 21:04:59 +000012#include "llvm/CodeGen/AsmPrinter.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000013#include "llvm/IR/DataLayout.h"
David Blaikiee226b082014-04-23 21:04:59 +000014#include "llvm/MC/MCStreamer.h"
David Blaikie6054e652018-03-23 23:58:19 +000015#include "llvm/Target/TargetLoweringObjectFile.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000016#include <utility>
David Blaikiee226b082014-04-23 21:04:59 +000017
18using namespace llvm;
19
David Blaikiee226b082014-04-23 21:04:59 +000020unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
David Blaikiee12b49a2014-04-26 17:27:38 +000021 HasBeenUsed = true;
David Blaikiee226b082014-04-23 21:04:59 +000022 auto IterBool =
23 Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
24 return IterBool.first->second.Number;
25}
26
Victor Leschuk64e0c562018-08-01 05:48:06 +000027
28void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
29 static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
David Blaikiee20bf9a2018-12-24 07:35:10 +000030 StringRef Prefix = "debug_addr_";
31 MCSymbol *BeginLabel = Asm.createTempSymbol(Prefix + "start");
32 EndLabel = Asm.createTempSymbol(Prefix + "end");
David Blaikied671eb72018-12-24 07:09:50 +000033 Asm.OutStreamer->AddComment("Length of contribution");
David Blaikiee20bf9a2018-12-24 07:35:10 +000034 Asm.EmitLabelDifference(EndLabel, BeginLabel,
35 4); // TODO: Support DWARF64 format.
36 Asm.OutStreamer->EmitLabel(BeginLabel);
David Blaikied671eb72018-12-24 07:09:50 +000037 Asm.OutStreamer->AddComment("DWARF version number");
Victor Leschuk64e0c562018-08-01 05:48:06 +000038 Asm.emitInt16(Asm.getDwarfVersion());
David Blaikied671eb72018-12-24 07:09:50 +000039 Asm.OutStreamer->AddComment("Address size");
Victor Leschuk64e0c562018-08-01 05:48:06 +000040 Asm.emitInt8(AddrSize);
David Blaikied671eb72018-12-24 07:09:50 +000041 Asm.OutStreamer->AddComment("Segment selector size");
Victor Leschuk64e0c562018-08-01 05:48:06 +000042 Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
43}
44
David Blaikiee226b082014-04-23 21:04:59 +000045// Emit addresses into the section given.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000046void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
David Blaikie161dd3c2018-10-20 06:02:15 +000047 if (isEmpty())
48 return;
49
George Rimar425f7512018-09-20 09:17:36 +000050 // Start the dwarf addr section.
51 Asm.OutStreamer->SwitchSection(AddrSection);
52
Victor Leschuk64e0c562018-08-01 05:48:06 +000053 if (Asm.getDwarfVersion() >= 5)
54 emitHeader(Asm, AddrSection);
55
George Rimar425f7512018-09-20 09:17:36 +000056 // Define the symbol that marks the start of the contribution.
57 // It is referenced via DW_AT_addr_base.
58 Asm.OutStreamer->EmitLabel(AddressTableBaseSym);
59
David Blaikiee226b082014-04-23 21:04:59 +000060 // Order the address pool entries by ID
61 SmallVector<const MCExpr *, 64> Entries(Pool.size());
62
63 for (const auto &I : Pool)
64 Entries[I.second.Number] =
65 I.second.TLS
66 ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
Jim Grosbach13760bd2015-05-30 01:25:56 +000067 : MCSymbolRefExpr::create(I.first, Asm.OutContext);
David Blaikiee226b082014-04-23 21:04:59 +000068
69 for (const MCExpr *Entry : Entries)
Lang Hames9ff69c82015-04-24 19:11:51 +000070 Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
David Blaikiee20bf9a2018-12-24 07:35:10 +000071
72 Asm.OutStreamer->EmitLabel(EndLabel);
David Blaikiee226b082014-04-23 21:04:59 +000073}