1 //===- MCELFStreamer.h - MCStreamer ELF Object File Interface ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCELFSTREAMER_H
11 #define LLVM_MC_MCELFSTREAMER_H
12 
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/MC/MCDirectives.h"
15 #include "llvm/MC/MCObjectStreamer.h"
16 #include "llvm/MC/SectionKind.h"
17 #include "llvm/Support/DataTypes.h"
18 #include <vector>
19 
20 namespace llvm {
21 class MCAsmBackend;
22 class MCAssembler;
23 class MCCodeEmitter;
24 class MCExpr;
25 class MCInst;
26 class MCSymbol;
27 class MCSymbolData;
28 class raw_ostream;
29 
30 class MCELFStreamer : public MCObjectStreamer {
31 public:
MCELFStreamer(MCContext & Context,MCAsmBackend & TAB,raw_pwrite_stream & OS,MCCodeEmitter * Emitter)32   MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
33                 MCCodeEmitter *Emitter)
34       : MCObjectStreamer(Context, TAB, OS, Emitter), SeenIdent(false) {}
35 
36   ~MCELFStreamer() override;
37 
38   /// state management
reset()39   void reset() override {
40     SeenIdent = false;
41     LocalCommons.clear();
42     BindingExplicitlySet.clear();
43     BundleGroups.clear();
44     MCObjectStreamer::reset();
45   }
46 
47   /// @name MCStreamer Interface
48   /// @{
49 
50   void InitSections(bool NoExecStack) override;
51   void ChangeSection(const MCSection *Section,
52                      const MCExpr *Subsection) override;
53   void EmitLabel(MCSymbol *Symbol) override;
54   void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
55   void EmitThumbFunc(MCSymbol *Func) override;
56   void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
57   bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
58   void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
59   void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
60                         unsigned ByteAlignment) override;
61   void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
62   void EmitCOFFSymbolStorageClass(int StorageClass) override;
63   void EmitCOFFSymbolType(int Type) override;
64   void EndCOFFSymbolDef() override;
65 
66   void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
67 
68   void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69                              unsigned ByteAlignment) override;
70 
71   void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = nullptr,
72                     uint64_t Size = 0, unsigned ByteAlignment = 0) override;
73   void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
74                       uint64_t Size, unsigned ByteAlignment = 0) override;
75   void EmitValueImpl(const MCExpr *Value, unsigned Size,
76                      const SMLoc &Loc = SMLoc()) override;
77 
78   void EmitFileDirective(StringRef Filename) override;
79 
80   void EmitIdent(StringRef IdentString) override;
81 
82   void EmitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override;
83 
84   void Flush() override;
85 
86   void FinishImpl() override;
87 
88   void EmitBundleAlignMode(unsigned AlignPow2) override;
89   void EmitBundleLock(bool AlignToEnd) override;
90   void EmitBundleUnlock() override;
91 
92 private:
93   void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &) override;
94   void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;
95 
96   void fixSymbolsInTLSFixups(const MCExpr *expr);
97 
98   /// \brief Merge the content of the fragment \p EF into the fragment \p DF.
99   void mergeFragment(MCDataFragment *, MCEncodedFragmentWithFixups *);
100 
101   bool SeenIdent;
102 
103   struct LocalCommon {
104     MCSymbolData *SD;
105     uint64_t Size;
106     unsigned ByteAlignment;
107   };
108 
109   std::vector<LocalCommon> LocalCommons;
110 
111   SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
112 
113   /// BundleGroups - The stack of fragments holding the bundle-locked
114   /// instructions.
115   llvm::SmallVector<MCDataFragment *, 4> BundleGroups;
116 };
117 
118 MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
119                                     raw_pwrite_stream &OS,
120                                     MCCodeEmitter *Emitter, bool RelaxAll,
121                                     bool IsThumb);
122 
123 } // end namespace llvm
124 
125 #endif
126