MCELFStreamer.h 6.2 KB

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