MCELFStreamer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCELFStreamer.h - MCStreamer ELF Object File Interface ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCELFSTREAMER_H
  14. #define LLVM_MC_MCELFSTREAMER_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/BinaryFormat/ELF.h"
  17. #include "llvm/MC/MCDirectives.h"
  18. #include "llvm/MC/MCObjectStreamer.h"
  19. namespace llvm {
  20. class MCAsmBackend;
  21. class MCCodeEmitter;
  22. class MCExpr;
  23. class MCInst;
  24. class MCELFStreamer : public MCObjectStreamer {
  25. public:
  26. MCELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
  27. std::unique_ptr<MCObjectWriter> OW,
  28. std::unique_ptr<MCCodeEmitter> Emitter);
  29. ~MCELFStreamer() override = default;
  30. /// state management
  31. void reset() override {
  32. SeenIdent = false;
  33. BundleGroups.clear();
  34. MCObjectStreamer::reset();
  35. }
  36. /// \name MCStreamer Interface
  37. /// @{
  38. void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
  39. void changeSection(MCSection *Section, const MCExpr *Subsection) override;
  40. void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
  41. void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F,
  42. uint64_t Offset) override;
  43. void emitAssemblerFlag(MCAssemblerFlag Flag) override;
  44. void emitThumbFunc(MCSymbol *Func) override;
  45. void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
  46. bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
  47. void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
  48. void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  49. unsigned ByteAlignment) override;
  50. void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
  51. void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
  52. bool KeepOriginalSym) override;
  53. void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  54. unsigned ByteAlignment) override;
  55. void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
  56. uint64_t Size = 0, unsigned ByteAlignment = 0,
  57. SMLoc L = SMLoc()) override;
  58. void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
  59. unsigned ByteAlignment = 0) override;
  60. void emitValueImpl(const MCExpr *Value, unsigned Size,
  61. SMLoc Loc = SMLoc()) override;
  62. void emitIdent(StringRef IdentString) override;
  63. void emitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override;
  64. void emitCGProfileEntry(const MCSymbolRefExpr *From,
  65. const MCSymbolRefExpr *To, uint64_t Count) override;
  66. void finishImpl() override;
  67. void emitBundleAlignMode(unsigned AlignPow2) override;
  68. void emitBundleLock(bool AlignToEnd) override;
  69. void emitBundleUnlock() override;
  70. /// ELF object attributes section emission support
  71. struct AttributeItem {
  72. // This structure holds all attributes, accounting for their string /
  73. // numeric value, so we can later emit them in declaration order, keeping
  74. // all in the same vector.
  75. enum {
  76. HiddenAttribute = 0,
  77. NumericAttribute,
  78. TextAttribute,
  79. NumericAndTextAttributes
  80. } Type;
  81. unsigned Tag;
  82. unsigned IntValue;
  83. std::string StringValue;
  84. };
  85. // Attributes that are added and managed entirely by target.
  86. SmallVector<AttributeItem, 64> Contents;
  87. void setAttributeItem(unsigned Attribute, unsigned Value,
  88. bool OverwriteExisting);
  89. void setAttributeItem(unsigned Attribute, StringRef Value,
  90. bool OverwriteExisting);
  91. void setAttributeItems(unsigned Attribute, unsigned IntValue,
  92. StringRef StringValue, bool OverwriteExisting);
  93. void emitAttributesSection(StringRef Vendor, const Twine &Section,
  94. unsigned Type, MCSection *&AttributeSection) {
  95. createAttributesSection(Vendor, Section, Type, AttributeSection, Contents);
  96. }
  97. private:
  98. AttributeItem *getAttributeItem(unsigned Attribute);
  99. size_t calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec);
  100. void createAttributesSection(StringRef Vendor, const Twine &Section,
  101. unsigned Type, MCSection *&AttributeSection,
  102. SmallVector<AttributeItem, 64> &AttrsVec);
  103. // GNU attributes that will get emitted at the end of the asm file.
  104. SmallVector<AttributeItem, 64> GNUAttributes;
  105. public:
  106. void emitGNUAttribute(unsigned Tag, unsigned Value) override {
  107. AttributeItem Item = {AttributeItem::NumericAttribute, Tag, Value,
  108. std::string(StringRef(""))};
  109. GNUAttributes.push_back(Item);
  110. }
  111. private:
  112. bool isBundleLocked() const;
  113. void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &) override;
  114. void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;
  115. void fixSymbolsInTLSFixups(const MCExpr *expr);
  116. void finalizeCGProfileEntry(const MCSymbolRefExpr *&S, uint64_t Offset);
  117. void finalizeCGProfile();
  118. /// Merge the content of the fragment \p EF into the fragment \p DF.
  119. void mergeFragment(MCDataFragment *, MCDataFragment *);
  120. bool SeenIdent = false;
  121. /// BundleGroups - The stack of fragments holding the bundle-locked
  122. /// instructions.
  123. SmallVector<MCDataFragment *, 4> BundleGroups;
  124. };
  125. MCELFStreamer *createARMELFStreamer(MCContext &Context,
  126. std::unique_ptr<MCAsmBackend> TAB,
  127. std::unique_ptr<MCObjectWriter> OW,
  128. std::unique_ptr<MCCodeEmitter> Emitter,
  129. bool RelaxAll, bool IsThumb, bool IsAndroid);
  130. } // end namespace llvm
  131. #endif // LLVM_MC_MCELFSTREAMER_H
  132. #ifdef __GNUC__
  133. #pragma GCC diagnostic pop
  134. #endif