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