[COFF] Add support for the .secidx directive
[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/MCObjectStreamer.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/MC/MCWin64EH.h"
29 #include "llvm/Support/COFF.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34
35 using namespace llvm;
36
37 namespace {
38 class WinCOFFStreamer : public MCObjectStreamer {
39 public:
40   MCSymbol const *CurSymbol;
41
42   WinCOFFStreamer(MCContext &Context,
43                   MCAsmBackend &MAB,
44                   MCCodeEmitter &CE,
45                   raw_ostream &OS);
46
47   void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48                        unsigned ByteAlignment, bool External);
49
50   // MCStreamer interface
51
52   virtual void InitSections();
53   virtual void InitToTextSection();
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 MCSectionCOFF *getSectionText() {
99     return getContext().getCOFFSection(
100         ".text", COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
101                      COFF::IMAGE_SCN_MEM_READ,
102         SectionKind::getText());
103   }
104
105   const MCSectionCOFF *getSectionData() {
106     return getContext().getCOFFSection(
107         ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
108                      COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
109         SectionKind::getDataRel());
110   }
111
112   const MCSectionCOFF *getSectionBSS() {
113     return getContext().getCOFFSection(
114         ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
115                     COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
116         SectionKind::getBSS());
117   }
118
119   void SetSectionText() {
120     SwitchSection(getSectionText());
121     EmitCodeAlignment(4, 0);
122   }
123
124   void SetSectionData() {
125     SwitchSection(getSectionData());
126     EmitCodeAlignment(4, 0);
127   }
128
129   void SetSectionBSS() {
130     SwitchSection(getSectionBSS());
131     EmitCodeAlignment(4, 0);
132   }
133 };
134 } // end anonymous namespace.
135
136 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
137                                  MCCodeEmitter &CE, raw_ostream &OS)
138     : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
139
140 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
141                                       unsigned ByteAlignment, bool External) {
142   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
143
144   const MCSectionCOFF *Section = getSectionBSS();
145   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
146   if (SectionData.getAlignment() < ByteAlignment)
147     SectionData.setAlignment(ByteAlignment);
148
149   MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
150   SymbolData.setExternal(External);
151
152   AssignSection(Symbol, Section);
153
154   if (ByteAlignment != 1)
155       new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
156
157   SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
158 }
159
160 // MCStreamer interface
161
162 void WinCOFFStreamer::InitToTextSection() {
163   SetSectionText();
164 }
165
166 void WinCOFFStreamer::InitSections() {
167   SetSectionText();
168   SetSectionData();
169   SetSectionBSS();
170   SetSectionText();
171 }
172
173 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
174   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
175   MCObjectStreamer::EmitLabel(Symbol);
176 }
177
178 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
179   EmitLabel(Symbol);
180 }
181 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
182   llvm_unreachable("not implemented");
183 }
184
185 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
186   llvm_unreachable("not implemented");
187 }
188
189 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
190                                           MCSymbolAttr Attribute) {
191   assert(Symbol && "Symbol must be non-null!");
192   assert((Symbol->isInSection()
193          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
194          : true) && "Got non-COFF section in the COFF backend!");
195   switch (Attribute) {
196   case MCSA_WeakReference:
197   case MCSA_Weak: {
198       MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
199       SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
200       SD.setExternal(true);
201     }
202     break;
203
204   case MCSA_Global:
205     getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
206     break;
207
208   default:
209     return false;
210   }
211
212   return true;
213 }
214
215 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
216   llvm_unreachable("not implemented");
217 }
218
219 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
220   assert((Symbol->isInSection()
221          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
222          : true) && "Got non-COFF section in the COFF backend!");
223   assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
224                               "to BeginCOFFSymbolDef!");
225   CurSymbol = Symbol;
226 }
227
228 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
229   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
230   assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
231                                         "the first byte!");
232
233   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
234     StorageClass << COFF::SF_ClassShift,
235     COFF::SF_ClassMask);
236 }
237
238 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
239   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
240   assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
241                                   "bytes");
242
243   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
244     Type << COFF::SF_TypeShift,
245     COFF::SF_TypeMask);
246 }
247
248 void WinCOFFStreamer::EndCOFFSymbolDef() {
249   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
250   CurSymbol = NULL;
251 }
252
253 void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol)
254 {
255   MCDataFragment *DF = getOrCreateDataFragment();
256
257   DF->getFixups().push_back(
258       MCFixup::Create(DF->getContents().size(),
259                       MCSymbolRefExpr::Create (Symbol, getContext ()),
260                       FK_SecRel_2));
261   DF->getContents().resize(DF->getContents().size() + 4, 0);
262 }
263
264 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
265 {
266   MCDataFragment *DF = getOrCreateDataFragment();
267
268   DF->getFixups().push_back(
269       MCFixup::Create(DF->getContents().size(),
270                       MCSymbolRefExpr::Create (Symbol, getContext ()),
271                       FK_SecRel_4));
272   DF->getContents().resize(DF->getContents().size() + 4, 0);
273 }
274
275 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
276   llvm_unreachable("not implemented");
277 }
278
279 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
280                                        unsigned ByteAlignment) {
281   assert((Symbol->isInSection()
282          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
283          : true) && "Got non-COFF section in the COFF backend!");
284   AddCommonSymbol(Symbol, Size, ByteAlignment, true);
285 }
286
287 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
288                                             unsigned ByteAlignment) {
289   assert((Symbol->isInSection()
290          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
291          : true) && "Got non-COFF section in the COFF backend!");
292   AddCommonSymbol(Symbol, Size, ByteAlignment, false);
293 }
294
295 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
296                                    uint64_t Size,unsigned ByteAlignment) {
297   llvm_unreachable("not implemented");
298 }
299
300 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
301                                      uint64_t Size, unsigned ByteAlignment) {
302   llvm_unreachable("not implemented");
303 }
304
305 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
306   // Ignore for now, linkers don't care, and proper debug
307   // info will be a much large effort.
308 }
309
310 // TODO: Implement this if you want to emit .comment section in COFF obj files.
311 void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
312   llvm_unreachable("unsupported directive");
313 }
314
315 void WinCOFFStreamer::EmitWin64EHHandlerData() {
316   MCStreamer::EmitWin64EHHandlerData();
317
318   // We have to emit the unwind info now, because this directive
319   // actually switches to the .xdata section!
320   MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
321 }
322
323 void WinCOFFStreamer::FinishImpl() {
324   EmitFrames(NULL, true);
325   EmitW64Tables();
326   MCObjectStreamer::FinishImpl();
327 }
328
329 namespace llvm
330 {
331   MCStreamer *createWinCOFFStreamer(MCContext &Context,
332                                     MCAsmBackend &MAB,
333                                     MCCodeEmitter &CE,
334                                     raw_ostream &OS,
335                                     bool RelaxAll) {
336     WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
337     S->getAssembler().setRelaxAll(RelaxAll);
338     return S;
339   }
340 }