1 //===-- llvm/MC/WinCOFFStreamer.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains an implementation of a Win32 COFF object file streamer.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "WinCOFFStreamer"
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"
39 class WinCOFFStreamer : public MCObjectStreamer {
41 MCSymbol const *CurSymbol;
43 WinCOFFStreamer(MCContext &Context,
48 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
49 unsigned ByteAlignment, bool External);
51 // MCStreamer interface
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();
81 virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) {
82 MCDataFragment *DF = getOrCreateDataFragment();
84 SmallVector<MCFixup, 4> Fixups;
85 SmallString<256> Code;
86 raw_svector_ostream VecOS(Code);
87 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
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]);
95 DF->getContents().append(Code.begin(), Code.end());
98 } // end anonymous namespace.
100 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
101 MCCodeEmitter &CE, raw_ostream &OS)
102 : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(NULL) {}
104 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
105 unsigned ByteAlignment, bool External) {
106 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
108 const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
109 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
110 if (SectionData.getAlignment() < ByteAlignment)
111 SectionData.setAlignment(ByteAlignment);
113 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
114 SymbolData.setExternal(External);
116 AssignSection(Symbol, Section);
118 if (ByteAlignment != 1)
119 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
121 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
124 // MCStreamer interface
126 void WinCOFFStreamer::InitSections() {
127 // FIXME: this is identical to the ELF one.
128 // This emulates the same behavior of GNU as. This makes it easier
129 // to compare the output as the major sections are in the same order.
130 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
131 EmitCodeAlignment(4);
133 SwitchSection(getContext().getObjectFileInfo()->getDataSection());
134 EmitCodeAlignment(4);
136 SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
137 EmitCodeAlignment(4);
139 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
142 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
143 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
144 MCObjectStreamer::EmitLabel(Symbol);
147 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
150 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
151 llvm_unreachable("not implemented");
154 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
155 llvm_unreachable("not implemented");
158 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
159 MCSymbolAttr Attribute) {
160 assert(Symbol && "Symbol must be non-null!");
161 assert((Symbol->isInSection()
162 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
163 : true) && "Got non-COFF section in the COFF backend!");
165 case MCSA_WeakReference:
167 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
168 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
169 SD.setExternal(true);
174 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
184 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
185 llvm_unreachable("not implemented");
188 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
189 assert((Symbol->isInSection()
190 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
191 : true) && "Got non-COFF section in the COFF backend!");
192 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
193 "to BeginCOFFSymbolDef!");
197 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
198 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
199 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
202 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
203 StorageClass << COFF::SF_ClassShift,
207 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
208 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
209 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
212 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
213 Type << COFF::SF_TypeShift,
217 void WinCOFFStreamer::EndCOFFSymbolDef() {
218 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
222 void WinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
223 MCDataFragment *DF = getOrCreateDataFragment();
224 DF->getFixups().push_back(MCFixup::Create(
225 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
227 DF->getContents().resize(DF->getContents().size() + 4, 0);
230 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
231 MCDataFragment *DF = getOrCreateDataFragment();
232 DF->getFixups().push_back(MCFixup::Create(
233 DF->getContents().size(), MCSymbolRefExpr::Create(Symbol, getContext()),
235 DF->getContents().resize(DF->getContents().size() + 4, 0);
238 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
239 llvm_unreachable("not implemented");
242 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
243 unsigned ByteAlignment) {
244 assert((Symbol->isInSection()
245 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
246 : true) && "Got non-COFF section in the COFF backend!");
247 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
250 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
251 unsigned ByteAlignment) {
252 assert((Symbol->isInSection()
253 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
254 : true) && "Got non-COFF section in the COFF backend!");
255 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
258 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
259 uint64_t Size,unsigned ByteAlignment) {
260 llvm_unreachable("not implemented");
263 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
264 uint64_t Size, unsigned ByteAlignment) {
265 llvm_unreachable("not implemented");
268 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
269 // Ignore for now, linkers don't care, and proper debug
270 // info will be a much large effort.
273 // TODO: Implement this if you want to emit .comment section in COFF obj files.
274 void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
275 llvm_unreachable("unsupported directive");
278 void WinCOFFStreamer::EmitWin64EHHandlerData() {
279 MCStreamer::EmitWin64EHHandlerData();
281 // We have to emit the unwind info now, because this directive
282 // actually switches to the .xdata section!
283 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
286 void WinCOFFStreamer::FinishImpl() {
287 EmitFrames(NULL, true);
289 MCObjectStreamer::FinishImpl();
294 MCStreamer *createWinCOFFStreamer(MCContext &Context,
299 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
300 S->getAssembler().setRelaxAll(RelaxAll);