MCObjectWriter.h 4.5 KB

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