blob: 6591cd7a96c5fa84c9ed2501ead1bc93e1345ca9 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Blaikie69d0cf02014-04-25 06:22:32 +00006//
7//===----------------------------------------------------------------------===//
David Blaikiee226b082014-04-23 21:04:59 +00008
9#include "AddressPool.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000010#include "llvm/ADT/SmallVector.h"
David Blaikiee226b082014-04-23 21:04:59 +000011#include "llvm/CodeGen/AsmPrinter.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000012#include "llvm/IR/DataLayout.h"
David Blaikiee226b082014-04-23 21:04:59 +000013#include "llvm/MC/MCStreamer.h"
David Blaikie6054e652018-03-23 23:58:19 +000014#include "llvm/Target/TargetLoweringObjectFile.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000015#include <utility>
David Blaikiee226b082014-04-23 21:04:59 +000016
17using namespace llvm;
18
David Blaikiee226b082014-04-23 21:04:59 +000019unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
David Blaikiee12b49a2014-04-26 17:27:38 +000020 HasBeenUsed = true;
David Blaikiee226b082014-04-23 21:04:59 +000021 auto IterBool =
22 Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
23 return IterBool.first->second.Number;
24}
25
Max Kazantsev0b455c22018-12-24 10:30:04 +000026
27void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
Victor Leschuk64e0c562018-08-01 05:48:06 +000028 static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
Max Kazantsev0b455c22018-12-24 10:30:04 +000029 uint64_t Length = sizeof(uint16_t) // version
30 + sizeof(uint8_t) // address_size
31 + sizeof(uint8_t) // segment_selector_size
32 + AddrSize * Pool.size(); // entries
David Blaikied671eb72018-12-24 07:09:50 +000033 Asm.OutStreamer->AddComment("Length of contribution");
Max Kazantsev0b455c22018-12-24 10:30:04 +000034 Asm.emitInt32(Length); // TODO: Support DWARF64 format.
David Blaikied671eb72018-12-24 07:09:50 +000035 Asm.OutStreamer->AddComment("DWARF version number");
Victor Leschuk64e0c562018-08-01 05:48:06 +000036 Asm.emitInt16(Asm.getDwarfVersion());
David Blaikied671eb72018-12-24 07:09:50 +000037 Asm.OutStreamer->AddComment("Address size");
Victor Leschuk64e0c562018-08-01 05:48:06 +000038 Asm.emitInt8(AddrSize);
David Blaikied671eb72018-12-24 07:09:50 +000039 Asm.OutStreamer->AddComment("Segment selector size");
Victor Leschuk64e0c562018-08-01 05:48:06 +000040 Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
41}
42
David Blaikiee226b082014-04-23 21:04:59 +000043// Emit addresses into the section given.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000044void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
David Blaikie161dd3c2018-10-20 06:02:15 +000045 if (isEmpty())
46 return;
47
George Rimar425f7512018-09-20 09:17:36 +000048 // Start the dwarf addr section.
49 Asm.OutStreamer->SwitchSection(AddrSection);
50
Victor Leschuk64e0c562018-08-01 05:48:06 +000051 if (Asm.getDwarfVersion() >= 5)
Max Kazantsev0b455c22018-12-24 10:30:04 +000052 emitHeader(Asm, AddrSection);
Victor Leschuk64e0c562018-08-01 05:48:06 +000053
George Rimar425f7512018-09-20 09:17:36 +000054 // Define the symbol that marks the start of the contribution.
55 // It is referenced via DW_AT_addr_base.
56 Asm.OutStreamer->EmitLabel(AddressTableBaseSym);
57
David Blaikiee226b082014-04-23 21:04:59 +000058 // Order the address pool entries by ID
59 SmallVector<const MCExpr *, 64> Entries(Pool.size());
60
61 for (const auto &I : Pool)
62 Entries[I.second.Number] =
63 I.second.TLS
64 ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
Jim Grosbach13760bd2015-05-30 01:25:56 +000065 : MCSymbolRefExpr::create(I.first, Asm.OutContext);
David Blaikiee226b082014-04-23 21:04:59 +000066
67 for (const MCExpr *Entry : Entries)
Lang Hames9ff69c82015-04-24 19:11:51 +000068 Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
David Blaikiee226b082014-04-23 21:04:59 +000069}