MCObjectWriter.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCObjectWriter.h - Object File Writer 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_MCOBJECTWRITER_H
  14. #define LLVM_MC_MCOBJECTWRITER_H
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/MC/MCSymbol.h"
  17. #include <cstdint>
  18. namespace llvm {
  19. class MCAsmLayout;
  20. class MCAssembler;
  21. class MCFixup;
  22. class MCFragment;
  23. class MCSymbol;
  24. class MCSymbolRefExpr;
  25. class MCValue;
  26. /// Defines the object file and target independent interfaces used by the
  27. /// assembler backend to write native file format object files.
  28. ///
  29. /// The object writer contains a few callbacks used by the assembler to allow
  30. /// the object writer to modify the assembler data structures at appropriate
  31. /// points. Once assembly is complete, the object writer is given the
  32. /// MCAssembler instance, which contains all the symbol and section data which
  33. /// should be emitted as part of writeObject().
  34. class MCObjectWriter {
  35. protected:
  36. std::vector<const MCSymbol *> AddrsigSyms;
  37. bool EmitAddrsigSection = false;
  38. MCObjectWriter() = default;
  39. public:
  40. MCObjectWriter(const MCObjectWriter &) = delete;
  41. MCObjectWriter &operator=(const MCObjectWriter &) = delete;
  42. virtual ~MCObjectWriter();
  43. /// lifetime management
  44. virtual void reset() {}
  45. /// \name High-Level API
  46. /// @{
  47. /// Perform any late binding of symbols (for example, to assign symbol
  48. /// indices for use when generating relocations).
  49. ///
  50. /// This routine is called by the assembler after layout and relaxation is
  51. /// complete.
  52. virtual void executePostLayoutBinding(MCAssembler &Asm,
  53. const MCAsmLayout &Layout) = 0;
  54. /// Record a relocation entry.
  55. ///
  56. /// This routine is called by the assembler after layout and relaxation, and
  57. /// post layout binding. The implementation is responsible for storing
  58. /// information about the relocation so that it can be emitted during
  59. /// writeObject().
  60. virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
  61. const MCFragment *Fragment,
  62. const MCFixup &Fixup, MCValue Target,
  63. uint64_t &FixedValue) = 0;
  64. /// Check whether the difference (A - B) between two symbol references is
  65. /// fully resolved.
  66. ///
  67. /// Clients are not required to answer precisely and may conservatively return
  68. /// false, even when a difference is fully resolved.
  69. bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
  70. const MCSymbolRefExpr *A,
  71. const MCSymbolRefExpr *B,
  72. bool InSet) const;
  73. virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
  74. const MCSymbol &A,
  75. const MCSymbol &B,
  76. bool InSet) const;
  77. virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
  78. const MCSymbol &SymA,
  79. const MCFragment &FB,
  80. bool InSet,
  81. bool IsPCRel) const;
  82. /// ELF only. Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN).
  83. virtual void markGnuAbi() {}
  84. /// Tell the object writer to emit an address-significance table during
  85. /// writeObject(). If this function is not called, all symbols are treated as
  86. /// address-significant.
  87. void emitAddrsigSection() { EmitAddrsigSection = true; }
  88. bool getEmitAddrsigSection() { return EmitAddrsigSection; }
  89. /// Record the given symbol in the address-significance table to be written
  90. /// diring writeObject().
  91. void addAddrsigSymbol(const MCSymbol *Sym) { AddrsigSyms.push_back(Sym); }
  92. std::vector<const MCSymbol *> &getAddrsigSyms() { return AddrsigSyms; }
  93. virtual void addExceptionEntry(const MCSymbol *Symbol, const MCSymbol *Trap,
  94. unsigned LanguageCode, unsigned ReasonCode,
  95. unsigned FunctionSize, bool hasDebug) {
  96. report_fatal_error("addExceptionEntry is only supported on XCOFF targets");
  97. }
  98. /// Write the object file and returns the number of bytes written.
  99. ///
  100. /// This routine is called by the assembler after layout and relaxation is
  101. /// complete, fixups have been evaluated and applied, and relocations
  102. /// generated.
  103. virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) = 0;
  104. /// @}
  105. };
  106. /// Base class for classes that define behaviour that is specific to both the
  107. /// target and the object format.
  108. class MCObjectTargetWriter {
  109. public:
  110. virtual ~MCObjectTargetWriter() = default;
  111. virtual Triple::ObjectFormatType getFormat() const = 0;
  112. };
  113. } // end namespace llvm
  114. #endif // LLVM_MC_MCOBJECTWRITER_H
  115. #ifdef __GNUC__
  116. #pragma GCC diagnostic pop
  117. #endif