1 //===- lib/MC/MCELFStreamer.h - ELF Object Output -------------------------===//
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 // This file assembles .s files and emits ELF .o object files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_MC_MCELFSTREAMER_H
15 #define LLVM_MC_MCELFSTREAMER_H
16 
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCObjectStreamer.h"
22 #include "llvm/MC/MCSectionELF.h"
23 
24 namespace llvm {
25 
26 class MCELFStreamer : public MCObjectStreamer {
27 public:
MCELFStreamer(MCContext & Context,MCAsmBackend & TAB,raw_ostream & OS,MCCodeEmitter * Emitter)28   MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
29                   raw_ostream &OS, MCCodeEmitter *Emitter)
30     : MCObjectStreamer(Context, TAB, OS, Emitter) {}
31 
MCELFStreamer(MCContext & Context,MCAsmBackend & TAB,raw_ostream & OS,MCCodeEmitter * Emitter,MCAssembler * Assembler)32   MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
33                 raw_ostream &OS, MCCodeEmitter *Emitter,
34                 MCAssembler *Assembler)
35     : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {}
36 
37 
~MCELFStreamer()38   ~MCELFStreamer() {}
39 
40   /// @name MCStreamer Interface
41   /// @{
42 
43   virtual void InitSections();
44   virtual void ChangeSection(const MCSection *Section);
45   virtual void EmitLabel(MCSymbol *Symbol);
46   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
47   virtual void EmitThumbFunc(MCSymbol *Func);
48   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
49   virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
50   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)51   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
52     assert(0 && "ELF doesn't support this directive");
53   }
54   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
55                                 unsigned ByteAlignment);
BeginCOFFSymbolDef(const MCSymbol * Symbol)56   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
57     assert(0 && "ELF doesn't support this directive");
58   }
59 
EmitCOFFSymbolStorageClass(int StorageClass)60   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
61     assert(0 && "ELF doesn't support this directive");
62   }
63 
EmitCOFFSymbolType(int Type)64   virtual void EmitCOFFSymbolType(int Type) {
65     assert(0 && "ELF doesn't support this directive");
66   }
67 
EndCOFFSymbolDef()68   virtual void EndCOFFSymbolDef() {
69     assert(0 && "ELF doesn't support this directive");
70   }
71 
EmitELFSize(MCSymbol * Symbol,const MCExpr * Value)72   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
73      MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
74      SD.setSize(Value);
75   }
76 
77   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
78                                      unsigned ByteAlignment);
79 
80   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
81                             unsigned Size = 0, unsigned ByteAlignment = 0) {
82     assert(0 && "ELF doesn't support this directive");
83   }
84   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
85                               uint64_t Size, unsigned ByteAlignment = 0) {
86     assert(0 && "ELF doesn't support this directive");
87   }
88   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
89   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
90                                     unsigned ValueSize = 1,
91                                     unsigned MaxBytesToEmit = 0);
92   virtual void EmitCodeAlignment(unsigned ByteAlignment,
93                                  unsigned MaxBytesToEmit = 0);
94 
95   virtual void EmitFileDirective(StringRef Filename);
96 
97   virtual void Finish();
98 
99 private:
100   virtual void EmitInstToFragment(const MCInst &Inst);
101   virtual void EmitInstToData(const MCInst &Inst);
102 
103   void fixSymbolsInTLSFixups(const MCExpr *expr);
104 
105   struct LocalCommon {
106     MCSymbolData *SD;
107     uint64_t Size;
108     unsigned ByteAlignment;
109   };
110   std::vector<LocalCommon> LocalCommons;
111 
112   SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
113   /// @}
SetSection(StringRef Section,unsigned Type,unsigned Flags,SectionKind Kind)114   void SetSection(StringRef Section, unsigned Type, unsigned Flags,
115                   SectionKind Kind) {
116     SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
117   }
118 
SetSectionData()119   void SetSectionData() {
120     SetSection(".data", ELF::SHT_PROGBITS,
121                ELF::SHF_WRITE |ELF::SHF_ALLOC,
122                SectionKind::getDataRel());
123     EmitCodeAlignment(4, 0);
124   }
SetSectionText()125   void SetSectionText() {
126     SetSection(".text", ELF::SHT_PROGBITS,
127                ELF::SHF_EXECINSTR |
128                ELF::SHF_ALLOC, SectionKind::getText());
129     EmitCodeAlignment(4, 0);
130   }
SetSectionBss()131   void SetSectionBss() {
132     SetSection(".bss", ELF::SHT_NOBITS,
133                ELF::SHF_WRITE |
134                ELF::SHF_ALLOC, SectionKind::getBSS());
135     EmitCodeAlignment(4, 0);
136   }
137 };
138 
139 } // end llvm namespace
140 
141 #endif
142