]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/MC/WinCOFFStreamer.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / 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/MCObjectStreamer.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCAsmLayout.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/MC/MCWin64EH.h"
27 #include "llvm/MC/MCAsmBackend.h"
28
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 EmitLabel(MCSymbol *Symbol);
54   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
55   virtual void EmitThumbFunc(MCSymbol *Func);
56   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
57   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
58   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
59   virtual void BeginCOFFSymbolDef(MCSymbol const *Symbol);
60   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
61   virtual void EmitCOFFSymbolType(int Type);
62   virtual void EndCOFFSymbolDef();
63   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
64   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
65   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
66                                 unsigned ByteAlignment);
67   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68                                      unsigned ByteAlignment);
69   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
70                             uint64_t Size,unsigned ByteAlignment);
71   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
72                               uint64_t Size, unsigned ByteAlignment);
73   virtual void EmitFileDirective(StringRef Filename);
74   virtual void EmitInstruction(const MCInst &Instruction);
75   virtual void EmitWin64EHHandlerData();
76   virtual void FinishImpl();
77
78 private:
79   virtual void EmitInstToFragment(const MCInst &Inst) {
80     llvm_unreachable("Not used by WinCOFF.");
81   }
82   virtual void EmitInstToData(const MCInst &Inst) {
83     llvm_unreachable("Not used by WinCOFF.");
84   }
85
86   void SetSection(StringRef Section,
87                   unsigned Characteristics,
88                   SectionKind Kind) {
89     SwitchSection(getContext().getCOFFSection(Section, Characteristics, Kind));
90   }
91
92   void SetSectionText() {
93     SetSection(".text",
94                COFF::IMAGE_SCN_CNT_CODE
95              | COFF::IMAGE_SCN_MEM_EXECUTE
96              | COFF::IMAGE_SCN_MEM_READ,
97                SectionKind::getText());
98     EmitCodeAlignment(4, 0);
99   }
100
101   void SetSectionData() {
102     SetSection(".data",
103                COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
104              | COFF::IMAGE_SCN_MEM_READ
105              | COFF::IMAGE_SCN_MEM_WRITE,
106                SectionKind::getDataRel());
107     EmitCodeAlignment(4, 0);
108   }
109
110   void SetSectionBSS() {
111     SetSection(".bss",
112                COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
113              | COFF::IMAGE_SCN_MEM_READ
114              | COFF::IMAGE_SCN_MEM_WRITE,
115                SectionKind::getBSS());
116     EmitCodeAlignment(4, 0);
117   }
118
119 };
120 } // end anonymous namespace.
121
122 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
123                                  MCAsmBackend &MAB,
124                                  MCCodeEmitter &CE,
125                                  raw_ostream &OS)
126     : MCObjectStreamer(Context, MAB, OS, &CE)
127     , CurSymbol(NULL) {
128 }
129
130 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
131                                       unsigned ByteAlignment, bool External) {
132   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
133
134   std::string SectionName(".bss$linkonce");
135   SectionName.append(Symbol->getName().begin(), Symbol->getName().end());
136
137   MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
138
139   unsigned Characteristics =
140     COFF::IMAGE_SCN_LNK_COMDAT |
141     COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
142     COFF::IMAGE_SCN_MEM_READ |
143     COFF::IMAGE_SCN_MEM_WRITE;
144
145   int Selection = COFF::IMAGE_COMDAT_SELECT_LARGEST;
146
147   const MCSection *Section = MCStreamer::getContext().getCOFFSection(
148     SectionName, Characteristics, Selection, SectionKind::getBSS());
149
150   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
151
152   if (SectionData.getAlignment() < ByteAlignment)
153     SectionData.setAlignment(ByteAlignment);
154
155   SymbolData.setExternal(External);
156
157   Symbol->setSection(*Section);
158
159   if (ByteAlignment != 1)
160       new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
161
162   SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
163 }
164
165 // MCStreamer interface
166
167 void WinCOFFStreamer::InitSections() {
168   SetSectionText();
169   SetSectionData();
170   SetSectionBSS();
171   SetSectionText();
172 }
173
174 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
175   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
176   MCObjectStreamer::EmitLabel(Symbol);
177 }
178
179 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
180   llvm_unreachable("not implemented");
181 }
182
183 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
184   llvm_unreachable("not implemented");
185 }
186
187 void WinCOFFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
188   assert((Symbol->isInSection()
189          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
190          : true) && "Got non COFF section in the COFF backend!");
191   // FIXME: This is all very ugly and depressing. What needs to happen here
192   // depends on quite a few things that are all part of relaxation, which we
193   // don't really even do.
194
195   if (Value->getKind() != MCExpr::SymbolRef) {
196     // TODO: This is exactly the same as MachOStreamer. Consider merging into
197     // MCObjectStreamer.
198     getAssembler().getOrCreateSymbolData(*Symbol);
199     AddValueSymbols(Value);
200     Symbol->setVariableValue(Value);
201   } else {
202     // FIXME: This is a horrible way to do this :(. This should really be
203     // handled after we are done with the MC* objects and immediately before
204     // writing out the object file when we know exactly what the symbol should
205     // look like in the coff symbol table. I'm not doing that now because the
206     // COFF object writer doesn't have a clearly defined separation between MC
207     // data structures, the object writers data structures, and the raw, POD,
208     // data structures that get written to disk.
209
210     // Copy over the aliased data.
211     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
212     const MCSymbolData &RealSD = getAssembler().getOrCreateSymbolData(
213       dyn_cast<const MCSymbolRefExpr>(Value)->getSymbol());
214
215     // FIXME: This is particularly nasty because it breaks as soon as any data
216     // members of MCSymbolData change.
217     SD.CommonAlign     = RealSD.CommonAlign;
218     SD.CommonSize      = RealSD.CommonSize;
219     SD.Flags           = RealSD.Flags;
220     SD.Fragment        = RealSD.Fragment;
221     SD.Index           = RealSD.Index;
222     SD.IsExternal      = RealSD.IsExternal;
223     SD.IsPrivateExtern = RealSD.IsPrivateExtern;
224     SD.Offset          = RealSD.Offset;
225     SD.SymbolSize      = RealSD.SymbolSize;
226   }
227 }
228
229 void WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
230                                           MCSymbolAttr Attribute) {
231   assert(Symbol && "Symbol must be non-null!");
232   assert((Symbol->isInSection()
233          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
234          : true) && "Got non COFF section in the COFF backend!");
235   switch (Attribute) {
236   case MCSA_WeakReference:
237   case MCSA_Weak: {
238       MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
239       SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
240       SD.setExternal(true);
241     }
242     break;
243
244   case MCSA_Global:
245     getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
246     break;
247
248   default:
249     llvm_unreachable("unsupported attribute");
250   }
251 }
252
253 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
254   llvm_unreachable("not implemented");
255 }
256
257 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
258   assert((Symbol->isInSection()
259          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
260          : true) && "Got non COFF section in the COFF backend!");
261   assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
262                               "to BeginCOFFSymbolDef!");
263   CurSymbol = Symbol;
264 }
265
266 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
267   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
268   assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
269                                         "the first byte!");
270
271   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
272     StorageClass << COFF::SF_ClassShift,
273     COFF::SF_ClassMask);
274 }
275
276 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
277   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
278   assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
279                                   "bytes");
280
281   getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
282     Type << COFF::SF_TypeShift,
283     COFF::SF_TypeMask);
284 }
285
286 void WinCOFFStreamer::EndCOFFSymbolDef() {
287   assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
288   CurSymbol = NULL;
289 }
290
291 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
292 {
293   MCDataFragment *DF = getOrCreateDataFragment();
294
295   DF->addFixup(MCFixup::Create(DF->getContents().size(),
296                                MCSymbolRefExpr::Create (Symbol, getContext ()),
297                                FK_SecRel_4));
298   DF->getContents().resize(DF->getContents().size() + 4, 0);
299 }
300
301 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
302   llvm_unreachable("not implemented");
303 }
304
305 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
306                                        unsigned ByteAlignment) {
307   assert((Symbol->isInSection()
308          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
309          : true) && "Got non COFF section in the COFF backend!");
310   AddCommonSymbol(Symbol, Size, ByteAlignment, true);
311 }
312
313 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
314                                             unsigned ByteAlignment) {
315   assert((Symbol->isInSection()
316          ? Symbol->getSection().getVariant() == MCSection::SV_COFF
317          : true) && "Got non COFF section in the COFF backend!");
318   AddCommonSymbol(Symbol, Size, ByteAlignment, false);
319 }
320
321 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
322                                    uint64_t Size,unsigned ByteAlignment) {
323   llvm_unreachable("not implemented");
324 }
325
326 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
327                                      uint64_t Size, unsigned ByteAlignment) {
328   llvm_unreachable("not implemented");
329 }
330
331 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
332   // Ignore for now, linkers don't care, and proper debug
333   // info will be a much large effort.
334 }
335
336 void WinCOFFStreamer::EmitInstruction(const MCInst &Instruction) {
337   for (unsigned i = 0, e = Instruction.getNumOperands(); i != e; ++i)
338     if (Instruction.getOperand(i).isExpr())
339       AddValueSymbols(Instruction.getOperand(i).getExpr());
340
341   getCurrentSectionData()->setHasInstructions(true);
342
343   MCInstFragment *Fragment =
344     new MCInstFragment(Instruction, getCurrentSectionData());
345
346   raw_svector_ostream VecOS(Fragment->getCode());
347
348   getAssembler().getEmitter().EncodeInstruction(Instruction, VecOS,
349                                                 Fragment->getFixups());
350 }
351
352 void WinCOFFStreamer::EmitWin64EHHandlerData() {
353   MCStreamer::EmitWin64EHHandlerData();
354
355   // We have to emit the unwind info now, because this directive
356   // actually switches to the .xdata section!
357   MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
358 }
359
360 void WinCOFFStreamer::FinishImpl() {
361   EmitW64Tables();
362   MCObjectStreamer::FinishImpl();
363 }
364
365 namespace llvm
366 {
367   MCStreamer *createWinCOFFStreamer(MCContext &Context,
368                                     MCAsmBackend &MAB,
369                                     MCCodeEmitter &CE,
370                                     raw_ostream &OS,
371                                     bool RelaxAll) {
372     WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
373     S->getAssembler().setRelaxAll(RelaxAll);
374     return S;
375   }
376 }