Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===// |
David Blaikie | 69d0cf0 | 2014-04-25 06:22:32 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 9 | |
| 10 | #include "AddressPool.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/AsmPrinter.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 13 | #include "llvm/IR/DataLayout.h" |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCStreamer.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 16 | #include <utility> |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 20 | unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) { |
David Blaikie | e12b49a | 2014-04-26 17:27:38 +0000 | [diff] [blame] | 21 | HasBeenUsed = true; |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 22 | auto IterBool = |
| 23 | Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS))); |
| 24 | return IterBool.first->second.Number; |
| 25 | } |
| 26 | |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 27 | |
| 28 | void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) { |
| 29 | static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize(); |
David Blaikie | e20bf9a | 2018-12-24 07:35:10 +0000 | [diff] [blame] | 30 | StringRef Prefix = "debug_addr_"; |
| 31 | MCSymbol *BeginLabel = Asm.createTempSymbol(Prefix + "start"); |
| 32 | EndLabel = Asm.createTempSymbol(Prefix + "end"); |
David Blaikie | d671eb7 | 2018-12-24 07:09:50 +0000 | [diff] [blame] | 33 | Asm.OutStreamer->AddComment("Length of contribution"); |
David Blaikie | e20bf9a | 2018-12-24 07:35:10 +0000 | [diff] [blame] | 34 | Asm.EmitLabelDifference(EndLabel, BeginLabel, |
| 35 | 4); // TODO: Support DWARF64 format. |
| 36 | Asm.OutStreamer->EmitLabel(BeginLabel); |
David Blaikie | d671eb7 | 2018-12-24 07:09:50 +0000 | [diff] [blame] | 37 | Asm.OutStreamer->AddComment("DWARF version number"); |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 38 | Asm.emitInt16(Asm.getDwarfVersion()); |
David Blaikie | d671eb7 | 2018-12-24 07:09:50 +0000 | [diff] [blame] | 39 | Asm.OutStreamer->AddComment("Address size"); |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 40 | Asm.emitInt8(AddrSize); |
David Blaikie | d671eb7 | 2018-12-24 07:09:50 +0000 | [diff] [blame] | 41 | Asm.OutStreamer->AddComment("Segment selector size"); |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 42 | Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size. |
| 43 | } |
| 44 | |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 45 | // Emit addresses into the section given. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 46 | void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) { |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 47 | if (isEmpty()) |
| 48 | return; |
| 49 | |
George Rimar | 425f751 | 2018-09-20 09:17:36 +0000 | [diff] [blame] | 50 | // Start the dwarf addr section. |
| 51 | Asm.OutStreamer->SwitchSection(AddrSection); |
| 52 | |
Victor Leschuk | 64e0c56 | 2018-08-01 05:48:06 +0000 | [diff] [blame] | 53 | if (Asm.getDwarfVersion() >= 5) |
| 54 | emitHeader(Asm, AddrSection); |
| 55 | |
George Rimar | 425f751 | 2018-09-20 09:17:36 +0000 | [diff] [blame] | 56 | // 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 Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 60 | // 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 Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 67 | : MCSymbolRefExpr::create(I.first, Asm.OutContext); |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 68 | |
| 69 | for (const MCExpr *Entry : Entries) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 70 | Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize()); |
David Blaikie | e20bf9a | 2018-12-24 07:35:10 +0000 | [diff] [blame] | 71 | |
| 72 | Asm.OutStreamer->EmitLabel(EndLabel); |
David Blaikie | e226b08 | 2014-04-23 21:04:59 +0000 | [diff] [blame] | 73 | } |