1 //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "AddressPool.h" 10 #include "llvm/ADT/SmallVector.h" 11 #include "llvm/CodeGen/AsmPrinter.h" 12 #include "llvm/IR/DataLayout.h" 13 #include "llvm/MC/MCStreamer.h" 14 #include "llvm/Target/TargetLoweringObjectFile.h" 15 #include <utility> 16 17 using namespace llvm; 18 19 unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) { 20 HasBeenUsed = true; 21 auto IterBool = 22 Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS))); 23 return IterBool.first->second.Number; 24 } 25 26 27 void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) { 28 static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize(); 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 33 Asm.OutStreamer->AddComment("Length of contribution"); 34 Asm.emitInt32(Length); // TODO: Support DWARF64 format. 35 Asm.OutStreamer->AddComment("DWARF version number"); 36 Asm.emitInt16(Asm.getDwarfVersion()); 37 Asm.OutStreamer->AddComment("Address size"); 38 Asm.emitInt8(AddrSize); 39 Asm.OutStreamer->AddComment("Segment selector size"); 40 Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size. 41 } 42 43 // Emit addresses into the section given. 44 void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) { 45 if (isEmpty()) 46 return; 47 48 // Start the dwarf addr section. 49 Asm.OutStreamer->SwitchSection(AddrSection); 50 51 if (Asm.getDwarfVersion() >= 5) 52 emitHeader(Asm, AddrSection); 53 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 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) 65 : MCSymbolRefExpr::create(I.first, Asm.OutContext); 66 67 for (const MCExpr *Entry : Entries) 68 Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize()); 69 } 70