AddressPool.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "AddressPool.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/CodeGen/AsmPrinter.h"
  11. #include "llvm/IR/DataLayout.h"
  12. #include "llvm/MC/MCStreamer.h"
  13. #include "llvm/Target/TargetLoweringObjectFile.h"
  14. #include <utility>
  15. using namespace llvm;
  16. unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
  17. HasBeenUsed = true;
  18. auto IterBool =
  19. Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
  20. return IterBool.first->second.Number;
  21. }
  22. MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
  23. static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
  24. MCSymbol *EndLabel =
  25. Asm.emitDwarfUnitLength("debug_addr", "Length of contribution");
  26. Asm.OutStreamer->AddComment("DWARF version number");
  27. Asm.emitInt16(Asm.getDwarfVersion());
  28. Asm.OutStreamer->AddComment("Address size");
  29. Asm.emitInt8(AddrSize);
  30. Asm.OutStreamer->AddComment("Segment selector size");
  31. Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
  32. return EndLabel;
  33. }
  34. // Emit addresses into the section given.
  35. void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
  36. if (isEmpty())
  37. return;
  38. // Start the dwarf addr section.
  39. Asm.OutStreamer->SwitchSection(AddrSection);
  40. MCSymbol *EndLabel = nullptr;
  41. if (Asm.getDwarfVersion() >= 5)
  42. EndLabel = emitHeader(Asm, AddrSection);
  43. // Define the symbol that marks the start of the contribution.
  44. // It is referenced via DW_AT_addr_base.
  45. Asm.OutStreamer->emitLabel(AddressTableBaseSym);
  46. // Order the address pool entries by ID
  47. SmallVector<const MCExpr *, 64> Entries(Pool.size());
  48. for (const auto &I : Pool)
  49. Entries[I.second.Number] =
  50. I.second.TLS
  51. ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
  52. : MCSymbolRefExpr::create(I.first, Asm.OutContext);
  53. for (const MCExpr *Entry : Entries)
  54. Asm.OutStreamer->emitValue(Entry, Asm.getDataLayout().getPointerSize());
  55. if (EndLabel)
  56. Asm.OutStreamer->emitLabel(EndLabel);
  57. }