Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:14

0001 //===- llvm/MC/MCObjectWriter.h - Object File Writer Interface --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_MC_MCOBJECTWRITER_H
0010 #define LLVM_MC_MCOBJECTWRITER_H
0011 
0012 #include "llvm/MC/MCSymbol.h"
0013 #include "llvm/TargetParser/Triple.h"
0014 #include <cstdint>
0015 
0016 namespace llvm {
0017 
0018 class MCAssembler;
0019 class MCFixup;
0020 class MCFragment;
0021 class MCSymbol;
0022 class MCSymbolRefExpr;
0023 class MCValue;
0024 
0025 /// Defines the object file and target independent interfaces used by the
0026 /// assembler backend to write native file format object files.
0027 ///
0028 /// The object writer contains a few callbacks used by the assembler to allow
0029 /// the object writer to modify the assembler data structures at appropriate
0030 /// points. Once assembly is complete, the object writer is given the
0031 /// MCAssembler instance, which contains all the symbol and section data which
0032 /// should be emitted as part of writeObject().
0033 class MCObjectWriter {
0034 protected:
0035   /// List of declared file names
0036   SmallVector<std::pair<std::string, size_t>, 0> FileNames;
0037   // XCOFF specific: Optional compiler version.
0038   std::string CompilerVersion;
0039   std::vector<const MCSymbol *> AddrsigSyms;
0040   bool EmitAddrsigSection = false;
0041   bool SubsectionsViaSymbols = false;
0042 
0043   struct CGProfileEntry {
0044     const MCSymbolRefExpr *From;
0045     const MCSymbolRefExpr *To;
0046     uint64_t Count;
0047   };
0048   SmallVector<CGProfileEntry, 0> CGProfile;
0049 
0050   MCObjectWriter() = default;
0051 
0052 public:
0053   MCObjectWriter(const MCObjectWriter &) = delete;
0054   MCObjectWriter &operator=(const MCObjectWriter &) = delete;
0055   virtual ~MCObjectWriter();
0056 
0057   /// lifetime management
0058   virtual void reset();
0059 
0060   /// \name High-Level API
0061   /// @{
0062 
0063   /// Perform any late binding of symbols (for example, to assign symbol
0064   /// indices for use when generating relocations).
0065   ///
0066   /// This routine is called by the assembler after layout and relaxation is
0067   /// complete.
0068   virtual void executePostLayoutBinding(MCAssembler &Asm) {}
0069 
0070   /// Record a relocation entry.
0071   ///
0072   /// This routine is called by the assembler after layout and relaxation, and
0073   /// post layout binding. The implementation is responsible for storing
0074   /// information about the relocation so that it can be emitted during
0075   /// writeObject().
0076   virtual void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
0077                                 const MCFixup &Fixup, MCValue Target,
0078                                 uint64_t &FixedValue) = 0;
0079 
0080   /// Check whether the difference (A - B) between two symbol references is
0081   /// fully resolved.
0082   ///
0083   /// Clients are not required to answer precisely and may conservatively return
0084   /// false, even when a difference is fully resolved.
0085   bool isSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
0086                                           const MCSymbolRefExpr *A,
0087                                           const MCSymbolRefExpr *B,
0088                                           bool InSet) const;
0089 
0090   virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
0091                                                       const MCSymbol &SymA,
0092                                                       const MCFragment &FB,
0093                                                       bool InSet,
0094                                                       bool IsPCRel) const;
0095 
0096   MutableArrayRef<std::pair<std::string, size_t>> getFileNames() {
0097     return FileNames;
0098   }
0099   void addFileName(MCAssembler &Asm, StringRef FileName);
0100   void setCompilerVersion(StringRef CompilerVers) {
0101     CompilerVersion = CompilerVers;
0102   }
0103 
0104   /// Tell the object writer to emit an address-significance table during
0105   /// writeObject(). If this function is not called, all symbols are treated as
0106   /// address-significant.
0107   void emitAddrsigSection() { EmitAddrsigSection = true; }
0108 
0109   bool getEmitAddrsigSection() { return EmitAddrsigSection; }
0110 
0111   /// Record the given symbol in the address-significance table to be written
0112   /// diring writeObject().
0113   void addAddrsigSymbol(const MCSymbol *Sym) { AddrsigSyms.push_back(Sym); }
0114 
0115   std::vector<const MCSymbol *> &getAddrsigSyms() { return AddrsigSyms; }
0116   SmallVector<CGProfileEntry, 0> &getCGProfile() { return CGProfile; }
0117 
0118   // Mach-O specific: Whether .subsections_via_symbols is enabled.
0119   bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
0120   void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
0121 
0122   /// Write the object file and returns the number of bytes written.
0123   ///
0124   /// This routine is called by the assembler after layout and relaxation is
0125   /// complete, fixups have been evaluated and applied, and relocations
0126   /// generated.
0127   virtual uint64_t writeObject(MCAssembler &Asm) = 0;
0128 
0129   /// @}
0130 };
0131 
0132 /// Base class for classes that define behaviour that is specific to both the
0133 /// target and the object format.
0134 class MCObjectTargetWriter {
0135 public:
0136   virtual ~MCObjectTargetWriter() = default;
0137   virtual Triple::ObjectFormatType getFormat() const = 0;
0138 };
0139 
0140 } // end namespace llvm
0141 
0142 #endif // LLVM_MC_MCOBJECTWRITER_H