xref: /llvm-project/llvm/include/llvm/MC/MCWinCOFFObjectWriter.h (revision 283dca56f8dddbf2f144730a01675c94b04f57cb)
1 //===- llvm/MC/MCWinCOFFObjectWriter.h - Win COFF Object Writer -*- 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 
9 #ifndef LLVM_MC_MCWINCOFFOBJECTWRITER_H
10 #define LLVM_MC_MCWINCOFFOBJECTWRITER_H
11 
12 #include "llvm/MC/MCObjectWriter.h"
13 #include <memory>
14 
15 namespace llvm {
16 
17 class MCAsmBackend;
18 class MCContext;
19 class MCFixup;
20 class MCValue;
21 class raw_pwrite_stream;
22 
23 class MCWinCOFFObjectTargetWriter : public MCObjectTargetWriter {
24   virtual void anchor();
25 
26   const unsigned Machine;
27 
28 protected:
29   MCWinCOFFObjectTargetWriter(unsigned Machine_);
30 
31 public:
32   virtual ~MCWinCOFFObjectTargetWriter() = default;
33 
34   Triple::ObjectFormatType getFormat() const override { return Triple::COFF; }
35   static bool classof(const MCObjectTargetWriter *W) {
36     return W->getFormat() == Triple::COFF;
37   }
38 
39   unsigned getMachine() const { return Machine; }
40   virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
41                                 const MCFixup &Fixup, bool IsCrossSection,
42                                 const MCAsmBackend &MAB) const = 0;
43   virtual bool recordRelocation(const MCFixup &) const { return true; }
44 };
45 
46 class WinCOFFWriter;
47 
48 class WinCOFFObjectWriter final : public MCObjectWriter {
49   friend class WinCOFFWriter;
50 
51   std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
52   std::unique_ptr<WinCOFFWriter> ObjWriter, DwoWriter;
53   bool IncrementalLinkerCompatible = false;
54 
55 public:
56   WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
57                       raw_pwrite_stream &OS);
58   WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
59                       raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS);
60 
61   // MCObjectWriter interface implementation.
62   void reset() override;
63   void setIncrementalLinkerCompatible(bool Value) {
64     IncrementalLinkerCompatible = Value;
65   }
66   void executePostLayoutBinding(MCAssembler &Asm) override;
67   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
68                                               const MCSymbol &SymA,
69                                               const MCFragment &FB, bool InSet,
70                                               bool IsPCRel) const override;
71   void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
72                         const MCFixup &Fixup, MCValue Target,
73                         uint64_t &FixedValue) override;
74   uint64_t writeObject(MCAssembler &Asm) override;
75   int getSectionNumber(const MCSection &Section) const;
76 };
77 
78 /// Construct a new Win COFF writer instance.
79 ///
80 /// \param MOTW - The target specific WinCOFF writer subclass.
81 /// \param OS - The stream to write to.
82 /// \returns The constructed object writer.
83 std::unique_ptr<MCObjectWriter>
84 createWinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
85                           raw_pwrite_stream &OS);
86 
87 std::unique_ptr<MCObjectWriter>
88 createWinCOFFDwoObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
89                              raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS);
90 } // end namespace llvm
91 
92 #endif // LLVM_MC_MCWINCOFFOBJECTWRITER_H
93