Remove the MCSymbolData typedef.
[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 Windows COFF object file streamer.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/MCObjectStreamer.h"
22 #include "llvm/MC/MCSection.h"
23 #include "llvm/MC/MCSectionCOFF.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/MC/MCWinCOFFStreamer.h"
28 #include "llvm/Support/COFF.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34
35 using namespace llvm;
36
37 #define DEBUG_TYPE "WinCOFFStreamer"
38
39 namespace llvm {
40 MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
41                                      MCCodeEmitter &CE, raw_pwrite_stream &OS)
42     : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
43
44 void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
45                                        const MCSubtargetInfo &STI) {
46   MCDataFragment *DF = getOrCreateDataFragment();
47
48   SmallVector<MCFixup, 4> Fixups;
49   SmallString<256> Code;
50   raw_svector_ostream VecOS(Code);
51   getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
52   VecOS.flush();
53
54   // Add the fixups and data.
55   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
56     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
57     DF->getFixups().push_back(Fixups[i]);
58   }
59
60   DF->getContents().append(Code.begin(), Code.end());
61 }
62
63 void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
64   // FIXME: this is identical to the ELF one.
65   // This emulates the same behavior of GNU as. This makes it easier
66   // to compare the output as the major sections are in the same order.
67   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
68   EmitCodeAlignment(4);
69
70   SwitchSection(getContext().getObjectFileInfo()->getDataSection());
71   EmitCodeAlignment(4);
72
73   SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
74   EmitCodeAlignment(4);
75
76   SwitchSection(getContext().getObjectFileInfo()->getTextSection());
77 }
78
79 void MCWinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
80   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
81   MCObjectStreamer::EmitLabel(Symbol);
82 }
83
84 void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
85   llvm_unreachable("not implemented");
86 }
87
88 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
89   llvm_unreachable("not implemented");
90 }
91
92 bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
93                                             MCSymbolAttr Attribute) {
94   assert(Symbol && "Symbol must be non-null!");
95   assert((!Symbol->isInSection() ||
96           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
97          "Got non-COFF section in the COFF backend!");
98
99   getAssembler().registerSymbol(*Symbol);
100   MCSymbol &SD = Symbol->getData();
101
102   switch (Attribute) {
103   default: return false;
104   case MCSA_WeakReference:
105   case MCSA_Weak:
106     Symbol->modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
107     SD.setExternal(true);
108     break;
109   case MCSA_Global:
110     SD.setExternal(true);
111     break;
112   }
113
114   return true;
115 }
116
117 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
118   llvm_unreachable("not implemented");
119 }
120
121 void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
122   assert((!Symbol->isInSection() ||
123           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
124          "Got non-COFF section in the COFF backend!");
125
126   if (CurSymbol)
127     FatalError("starting a new symbol definition without completing the "
128                "previous one");
129   CurSymbol = Symbol;
130 }
131
132 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
133   if (!CurSymbol)
134     FatalError("storage class specified outside of symbol definition");
135
136   if (StorageClass & ~COFF::SSC_Invalid)
137     FatalError("storage class value '" + Twine(StorageClass) +
138                "' out of range");
139
140   getAssembler().registerSymbol(*CurSymbol);
141   CurSymbol->modifyFlags(StorageClass << COFF::SF_ClassShift,
142                          COFF::SF_ClassMask);
143 }
144
145 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
146   if (!CurSymbol)
147     FatalError("symbol type specified outside of a symbol definition");
148
149   if (Type & ~0xffff)
150     FatalError("type value '" + Twine(Type) + "' out of range");
151
152   getAssembler().registerSymbol(*CurSymbol);
153   CurSymbol->modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
154 }
155
156 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
157   if (!CurSymbol)
158     FatalError("ending symbol definition without starting one");
159   CurSymbol = nullptr;
160 }
161
162 void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
163   MCDataFragment *DF = getOrCreateDataFragment();
164   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
165   MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
166   DF->getFixups().push_back(Fixup);
167   DF->getContents().resize(DF->getContents().size() + 2, 0);
168 }
169
170 void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
171   MCDataFragment *DF = getOrCreateDataFragment();
172   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
173   MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_4);
174   DF->getFixups().push_back(Fixup);
175   DF->getContents().resize(DF->getContents().size() + 4, 0);
176 }
177
178 void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
179   llvm_unreachable("not supported");
180 }
181
182 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
183                                          unsigned ByteAlignment) {
184   assert((!Symbol->isInSection() ||
185           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
186          "Got non-COFF section in the COFF backend!");
187
188   const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
189   if (T.isKnownWindowsMSVCEnvironment()) {
190     if (ByteAlignment > 32)
191       report_fatal_error("alignment is limited to 32-bytes");
192
193     // Round size up to alignment so that we will honor the alignment request.
194     Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
195   }
196
197   AssignSection(Symbol, nullptr);
198
199   getAssembler().registerSymbol(*Symbol);
200   MCSymbol &SD = Symbol->getData();
201   SD.setExternal(true);
202   Symbol->setCommon(Size, ByteAlignment);
203
204   if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) {
205     SmallString<128> Directive;
206     raw_svector_ostream OS(Directive);
207     const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
208
209     OS << " -aligncomm:\"" << Symbol->getName() << "\","
210        << Log2_32_Ceil(ByteAlignment);
211     OS.flush();
212
213     PushSection();
214     SwitchSection(MFI->getDrectveSection());
215     EmitBytes(Directive);
216     PopSection();
217   }
218 }
219
220 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
221                                               unsigned ByteAlignment) {
222   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
223
224   MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
225   getAssembler().registerSection(*Section);
226   if (Section->getAlignment() < ByteAlignment)
227     Section->setAlignment(ByteAlignment);
228
229   getAssembler().registerSymbol(*Symbol);
230   MCSymbol &SD = Symbol->getData();
231   SD.setExternal(false);
232
233   AssignSection(Symbol, Section);
234
235   if (ByteAlignment != 1)
236     new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0,
237                         ByteAlignment, Section);
238
239   MCFillFragment *Fragment = new MCFillFragment(
240       /*Value=*/0, /*ValueSize=*/0, Size, Section);
241   SD.setFragment(Fragment);
242 }
243
244 void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
245                                      uint64_t Size, unsigned ByteAlignment) {
246   llvm_unreachable("not implemented");
247 }
248
249 void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
250                                        uint64_t Size, unsigned ByteAlignment) {
251   llvm_unreachable("not implemented");
252 }
253
254 void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
255   getAssembler().addFileName(Filename);
256 }
257
258 // TODO: Implement this if you want to emit .comment section in COFF obj files.
259 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
260   llvm_unreachable("not implemented");
261 }
262
263 void MCWinCOFFStreamer::EmitWinEHHandlerData() {
264   llvm_unreachable("not implemented");
265 }
266
267 void MCWinCOFFStreamer::FinishImpl() {
268   MCObjectStreamer::FinishImpl();
269 }
270
271 LLVM_ATTRIBUTE_NORETURN
272 void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
273   getContext().reportFatalError(SMLoc(), Msg);
274 }
275 }
276