2e090a045e96cf7f9348375357c724028c57547d
[oota-llvm.git] / lib / MC / WinCOFFStreamer.cpp
1 //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- 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 // This file contains an implementation of a Win32 COFF object file streamer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "WinCOFFStreamer"
15
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCObjectStreamer.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSectionCOFF.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCValue.h"
29 #include "llvm/MC/MCWin64EH.h"
30 #include "llvm/Support/COFF.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
35
36 using namespace llvm;
37
38 namespace {
39 class WinCOFFStreamer : public MCObjectStreamer {
40 public:
41   MCSymbol const *CurSymbol;
42
43   WinCOFFStreamer(MCContext &Context,
44                   MCAsmBackend &MAB,
45                   MCCodeEmitter &CE,
46                   raw_ostream &OS);
47
48   void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
49                        unsigned ByteAlignment, bool External);
50
51   // MCStreamer interface
52
53   virtual void InitSections();
54   virtual void EmitLabel(MCSymbol *Symbol);
55   virtual void EmitDebugLabel(MCSymbol *Symbol);
56   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
57   virtual void EmitThumbFunc(MCSymbol *Func);
58   virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
59   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
60   virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
61   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
62   virtual void EmitCOFFSymbolType(int Type);
63   virtual void EndCOFFSymbolDef();
64   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
65   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
66   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
67   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68                                 unsigned ByteAlignment);
69   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
70                                      unsigned ByteAlignment);
71   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
72                             uint64_t Size,unsigned ByteAlignment);
73   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
74                               uint64_t Size, unsigned ByteAlignment);
75   virtual void EmitFileDirective(StringRef Filename);
76   virtual void EmitIdent(StringRef IdentString);
77   virtual void EmitWin64EHHandlerData();
78   virtual void FinishImpl();
79
80 private:
81   virtual void EmitInstToData(const MCInst &Inst) {
82     MCDataFragment *DF = getOrCreateDataFragment();
83
84     SmallVector<MCFixup, 4> Fixups;
85     SmallString<256> Code;
86     raw_svector_ostream VecOS(Code);
87     getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
88     VecOS.flush();
89
90     // Add the fixups and data.
91     for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
92       Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
93       DF->getFixups().push_back(Fixups[i]);
94     }
95     DF->getContents().append(Code.begin(), Code.end());
96   }
97
98   const MCSection *getSectionText() {
99     return getContext().getObjectFileInfo()->getTextSection();
100   }
101
102   const MCSection *getSectionData() {
103     return getContext().getObjectFileInfo()->getDataSection();
104   }
105
106   const MCSection *getSectionBSS() {
107     return getContext().getObjectFileInfo()->getBSSSection();
108   }
109
110   void SetSectionText() {
111     SwitchSection(getSectionText());
112     EmitCodeAlignment(4, 0);
113   }
114
115   void SetSectionData() {
116     SwitchSection(getSectionData());
117     EmitCodeAlignment(4, 0);
118   }
119
120   void SetSectionBSS() {
121     SwitchSection(getSectionBSS());
122     EmitCodeAlignment(4, 0);
123   }
124 };
125 } // end anonymous namespace.
126
127 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
128                                  MCCodeEmitter &CE, raw_ostream &OS)
129     : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
130
131 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
132                                       unsigned ByteAlignment, bool External) {
133   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
134
135   const MCSection *Section = getSectionBSS();
136   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
137   if (SectionData.getAlignment() < ByteAlignment)
138     SectionData.setAlignment(ByteAlignment);
139
140   MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
141   SymbolData.setExternal(External);
142
143   AssignSection(Symbol, Section);
144
145   if (ByteAlignment != 1)
146       new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
147
148   SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
149 }
150
151 // MCStreamer interface
152
153 void WinCOFFStreamer::InitSections() {
154   SetSectionText();
155   SetSectionData();
156   SetSectionBSS();
157   SetSectionText();
158 }
159
160 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
161   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
162   MCObjectStreamer::EmitLabel(Symbol);
163 }
164
165 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
166   EmitLabel(Symbol);
167 }
168 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
169   llvm_unreachable("not implemented");
170 }
171
172 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
173   llvm_unreachable("not implemented");
174 }
175
176 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
177                                           MCSymbolAttr Attribute) {
178   assert(Symbol && "Symbol must be non-null!");
179   assert((Symbol->isInSection()
180          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
181          : true) && "Got non-COFF section in the COFF backend!");
182   switch (Attribute) {
183   case MCSA_WeakReference:
184   case MCSA_Weak: {
185       MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
186       SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
187       SD.setExternal(true);
188     }
189     break;
190
191   case MCSA_Global:
192     getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
193     break;
194
195   default:
196     return false;
197   }
198
199   return true;
200 }
201
202 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
203   llvm_unreachable("not implemented");
204 }
205
206 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
207   assert((Symbol->isInSection()
208          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
209          : true) && "Got non-COFF section in the COFF backend!");
210   assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
211                               "to BeginCOFFSymbolDef!");
212   CurSymbol = Symbol;
213 }
214
215 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
216   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
217   assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
218                                         "the first byte!");
219
220   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
221     StorageClass << COFF::SF_ClassShift,
222     COFF::SF_ClassMask);
223 }
224
225 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
226   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
227   assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
228                                   "bytes");
229
230   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
231     Type << COFF::SF_TypeShift,
232     COFF::SF_TypeMask);
233 }
234
235 void WinCOFFStreamer::EndCOFFSymbolDef() {
236   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
237   CurSymbol = NULL;
238 }
239
240 void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
241   MCDataFragment *DF = getOrCreateDataFragment();
242   DF->getFixups().push_back(MCFixup::Create(
243       DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
244       FK_SecRel_2));
245   DF->getContents().resize(DF->getContents().size() + 4, 0);
246 }
247
248 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
249   MCDataFragment *DF = getOrCreateDataFragment();
250   DF->getFixups().push_back(MCFixup::Create(
251       DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
252       FK_SecRel_4));
253   DF->getContents().resize(DF->getContents().size() + 4, 0);
254 }
255
256 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
257   llvm_unreachable("not implemented");
258 }
259
260 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
261                                        unsigned ByteAlignment) {
262   assert((Symbol->isInSection()
263          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
264          : true) && "Got non-COFF section in the COFF backend!");
265   AddCommonSymbol(Symbol, Size, ByteAlignment, true);
266 }
267
268 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
269                                             unsigned ByteAlignment) {
270   assert((Symbol->isInSection()
271          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
272          : true) && "Got non-COFF section in the COFF backend!");
273   AddCommonSymbol(Symbol, Size, ByteAlignment, false);
274 }
275
276 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
277                                    uint64_t Size,unsigned ByteAlignment) {
278   llvm_unreachable("not implemented");
279 }
280
281 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
282                                      uint64_t Size, unsigned ByteAlignment) {
283   llvm_unreachable("not implemented");
284 }
285
286 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
287   // Ignore for now, linkers don't care, and proper debug
288   // info will be a much large effort.
289 }
290
291 // TODO: Implement this if you want to emit .comment section in COFF obj files.
292 void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
293   llvm_unreachable("unsupported directive");
294 }
295
296 void WinCOFFStreamer::EmitWin64EHHandlerData() {
297   MCStreamer::EmitWin64EHHandlerData();
298
299   // We have to emit the unwind info now, because this directive
300   // actually switches to the .xdata section!
301   MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
302 }
303
304 void WinCOFFStreamer::FinishImpl() {
305   EmitFrames(NULL, true);
306   EmitW64Tables();
307   MCObjectStreamer::FinishImpl();
308 }
309
310 namespace llvm
311 {
312   MCStreamer *createWinCOFFStreamer(MCContext &Context,
313                                     MCAsmBackend &MAB,
314                                     MCCodeEmitter &CE,
315                                     raw_ostream &OS,
316                                     bool RelaxAll) {
317     WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
318     S->getAssembler().setRelaxAll(RelaxAll);
319     return S;
320   }
321 }