AddressPool.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. StringRef Prefix = "debug_addr_";
  25. MCSymbol *BeginLabel = Asm.createTempSymbol(Prefix + "start");
  26. MCSymbol *EndLabel = Asm.createTempSymbol(Prefix + "end");
  27. Asm.OutStreamer->AddComment("Length of contribution");
  28. Asm.EmitLabelDifference(EndLabel, BeginLabel,
  29. 4); // TODO: Support DWARF64 format.
  30. Asm.OutStreamer->EmitLabel(BeginLabel);
  31. Asm.OutStreamer->AddComment("DWARF version number");
  32. Asm.emitInt16(Asm.getDwarfVersion());
  33. Asm.OutStreamer->AddComment("Address size");
  34. Asm.emitInt8(AddrSize);
  35. Asm.OutStreamer->AddComment("Segment selector size");
  36. Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
  37. return EndLabel;
  38. }
  39. // Emit addresses into the section given.
  40. void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
  41. if (isEmpty())
  42. return;
  43. // Start the dwarf addr section.
  44. Asm.OutStreamer->SwitchSection(AddrSection);
  45. MCSymbol *EndLabel = nullptr;
  46. if (Asm.getDwarfVersion() >= 5)
  47. EndLabel = emitHeader(Asm, AddrSection);
  48. // Define the symbol that marks the start of the contribution.
  49. // It is referenced via DW_AT_addr_base.
  50. Asm.OutStreamer->EmitLabel(AddressTableBaseSym);
  51. // Order the address pool entries by ID
  52. SmallVector<const MCExpr *, 64> Entries(Pool.size());
  53. for (const auto &I : Pool)
  54. Entries[I.second.Number] =
  55. I.second.TLS
  56. ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
  57. : MCSymbolRefExpr::create(I.first, Asm.OutContext);
  58. for (const MCExpr *Entry : Entries)
  59. Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
  60. if (EndLabel)
  61. Asm.OutStreamer->EmitLabel(EndLabel);
  62. }