ARM: correct bundle generation for MOV32T relocations
[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/MCStreamer.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/MCObjectStreamer.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCSectionCOFF.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/MC/MCWin64EH.h"
28 #include "llvm/MC/MCWinCOFFStreamer.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 #define DEBUG_TYPE "WinCOFFStreamer"
38
39 namespace llvm {
40 MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
41                                      MCCodeEmitter &CE, raw_ostream &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() {
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::EmitDebugLabel(MCSymbol *Symbol) {
85   EmitLabel(Symbol);
86 }
87
88 void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
89   llvm_unreachable("not implemented");
90 }
91
92 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
93   llvm_unreachable("not implemented");
94 }
95
96 bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
97                                             MCSymbolAttr Attribute) {
98   assert(Symbol && "Symbol must be non-null!");
99   assert((!Symbol->isInSection() ||
100           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
101          "Got non-COFF section in the COFF backend!");
102
103   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
104
105   switch (Attribute) {
106   default: return false;
107   case MCSA_WeakReference:
108   case MCSA_Weak:
109     SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
110     SD.setExternal(true);
111     break;
112   case MCSA_Global:
113     SD.setExternal(true);
114     break;
115   }
116
117   return true;
118 }
119
120 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
121   llvm_unreachable("not implemented");
122 }
123
124 void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
125   assert((!Symbol->isInSection() ||
126           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
127          "Got non-COFF section in the COFF backend!");
128   assert(!CurSymbol && "starting new symbol definition in a symbol definition");
129   CurSymbol = Symbol;
130 }
131
132 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
133   assert(CurSymbol && "StorageClass specified outside of symbol definition");
134   assert((StorageClass & ~0xFF) == 0 &&
135          "StorageClass must only have data in the first byte!");
136
137   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
138   SD.modifyFlags(StorageClass << COFF::SF_ClassShift, COFF::SF_ClassMask);
139 }
140
141 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
142   assert(CurSymbol && "SymbolType specified outside of a symbol definition");
143   assert((Type & ~0xFFFF) == 0 &&
144          "Type must only have data in the first 2 bytes");
145
146   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
147   SD.modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
148 }
149
150 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
151   assert(CurSymbol && "ending symbol definition without beginning one");
152   CurSymbol = nullptr;
153 }
154
155 void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
156   MCDataFragment *DF = getOrCreateDataFragment();
157   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
158   MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_2);
159   DF->getFixups().push_back(Fixup);
160   DF->getContents().resize(DF->getContents().size() + 4, 0);
161 }
162
163 void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
164   MCDataFragment *DF = getOrCreateDataFragment();
165   const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
166   MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_4);
167   DF->getFixups().push_back(Fixup);
168   DF->getContents().resize(DF->getContents().size() + 4, 0);
169 }
170
171 void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
172   llvm_unreachable("not supported");
173 }
174
175 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
176                                          unsigned ByteAlignment) {
177   assert((!Symbol->isInSection() ||
178           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
179          "Got non-COFF section in the COFF backend!");
180
181   if (ByteAlignment > 32)
182     report_fatal_error("alignment is limited to 32-bytes");
183
184   AssignSection(Symbol, nullptr);
185
186   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
187   SD.setExternal(true);
188   SD.setCommon(Size, ByteAlignment);
189 }
190
191 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
192                                               unsigned ByteAlignment) {
193   assert(!Symbol->isInSection() && "Symbol must not already have a section!");
194
195   const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
196   MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
197   if (SectionData.getAlignment() < ByteAlignment)
198     SectionData.setAlignment(ByteAlignment);
199
200   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
201   SD.setExternal(false);
202
203   AssignSection(Symbol, Section);
204
205   if (ByteAlignment != 1)
206     new MCAlignFragment(ByteAlignment, /*_Value=*/0, /*_ValueSize=*/0,
207                         ByteAlignment, &SectionData);
208
209   MCFillFragment *Fragment =
210       new MCFillFragment(/*_Value=*/0, /*_ValueSize=*/0, Size, &SectionData);
211   SD.setFragment(Fragment);
212 }
213
214 void MCWinCOFFStreamer::EmitZerofill(const MCSection *Section,
215                                      MCSymbol *Symbol, uint64_t Size,
216                                      unsigned ByteAlignment) {
217   llvm_unreachable("not implemented");
218 }
219
220 void MCWinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section,
221                                        MCSymbol *Symbol, uint64_t Size,
222                                        unsigned ByteAlignment) {
223   llvm_unreachable("not implemented");
224 }
225
226 void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
227   getAssembler().addFileName(Filename);
228 }
229
230 // TODO: Implement this if you want to emit .comment section in COFF obj files.
231 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
232   llvm_unreachable("not implemented");
233 }
234
235 void MCWinCOFFStreamer::EmitWin64EHHandlerData() {
236   llvm_unreachable("not implemented");
237 }
238
239 void MCWinCOFFStreamer::FinishImpl() {
240   MCObjectStreamer::FinishImpl();
241 }
242 }
243