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/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"
38 class WinCOFFStreamer : public MCObjectStreamer {
40 MCSymbol const *CurSymbol;
42 WinCOFFStreamer(MCContext &Context,
47 void AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
48 unsigned ByteAlignment, bool External);
50 // MCStreamer interface
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 EmitCOFFSecRel32(MCSymbol const *Symbol);
65 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
66 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
67 unsigned ByteAlignment);
68 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
69 unsigned ByteAlignment);
70 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
71 uint64_t Size,unsigned ByteAlignment);
72 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
73 uint64_t Size, unsigned ByteAlignment);
74 virtual void EmitFileDirective(StringRef Filename);
75 virtual void EmitIdent(StringRef IdentString);
76 virtual void EmitWin64EHHandlerData();
77 virtual void FinishImpl();
80 virtual void EmitInstToData(const MCInst &Inst) {
81 MCDataFragment *DF = getOrCreateDataFragment();
83 SmallVector<MCFixup, 4> Fixups;
84 SmallString<256> Code;
85 raw_svector_ostream VecOS(Code);
86 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
89 // Add the fixups and data.
90 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
91 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
92 DF->getFixups().push_back(Fixups[i]);
94 DF->getContents().append(Code.begin(), Code.end());
97 const MCSectionCOFF *getSectionText() {
98 return getContext().getCOFFSection(
99 ".text", COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
100 COFF::IMAGE_SCN_MEM_READ,
101 SectionKind::getText());
104 const MCSectionCOFF *getSectionData() {
105 return getContext().getCOFFSection(
106 ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
107 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
108 SectionKind::getDataRel());
111 const MCSectionCOFF *getSectionBSS() {
112 return getContext().getCOFFSection(
113 ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
114 COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
115 SectionKind::getBSS());
118 void SetSectionText() {
119 SwitchSection(getSectionText());
120 EmitCodeAlignment(4, 0);
123 void SetSectionData() {
124 SwitchSection(getSectionData());
125 EmitCodeAlignment(4, 0);
128 void SetSectionBSS() {
129 SwitchSection(getSectionBSS());
130 EmitCodeAlignment(4, 0);
133 } // end anonymous namespace.
135 WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
136 MCCodeEmitter &CE, raw_ostream &OS)
137 : MCObjectStreamer(Context, 0, MAB, OS, &CE), CurSymbol(NULL) {}
139 void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size,
140 unsigned ByteAlignment, bool External) {
141 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
143 const MCSectionCOFF *Section = getSectionBSS();
144 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
145 if (SectionData.getAlignment() < ByteAlignment)
146 SectionData.setAlignment(ByteAlignment);
148 MCSymbolData &SymbolData = getAssembler().getOrCreateSymbolData(*Symbol);
149 SymbolData.setExternal(External);
151 AssignSection(Symbol, Section);
153 if (ByteAlignment != 1)
154 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData);
156 SymbolData.setFragment(new MCFillFragment(0, 0, Size, &SectionData));
159 // MCStreamer interface
161 void WinCOFFStreamer::InitToTextSection() {
165 void WinCOFFStreamer::InitSections() {
172 void WinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
173 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
174 MCObjectStreamer::EmitLabel(Symbol);
177 void WinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
180 void WinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
181 llvm_unreachable("not implemented");
184 void WinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
185 llvm_unreachable("not implemented");
188 bool WinCOFFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
189 MCSymbolAttr Attribute) {
190 assert(Symbol && "Symbol must be non-null!");
191 assert((Symbol->isInSection()
192 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
193 : true) && "Got non-COFF section in the COFF backend!");
195 case MCSA_WeakReference:
197 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
198 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
199 SD.setExternal(true);
204 getAssembler().getOrCreateSymbolData(*Symbol).setExternal(true);
214 void WinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
215 llvm_unreachable("not implemented");
218 void WinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
219 assert((Symbol->isInSection()
220 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
221 : true) && "Got non-COFF section in the COFF backend!");
222 assert(CurSymbol == NULL && "EndCOFFSymbolDef must be called between calls "
223 "to BeginCOFFSymbolDef!");
227 void WinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
228 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
229 assert((StorageClass & ~0xFF) == 0 && "StorageClass must only have data in "
232 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
233 StorageClass << COFF::SF_ClassShift,
237 void WinCOFFStreamer::EmitCOFFSymbolType(int Type) {
238 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
239 assert((Type & ~0xFFFF) == 0 && "Type must only have data in the first 2 "
242 getAssembler().getOrCreateSymbolData(*CurSymbol).modifyFlags(
243 Type << COFF::SF_TypeShift,
247 void WinCOFFStreamer::EndCOFFSymbolDef() {
248 assert(CurSymbol != NULL && "BeginCOFFSymbolDef must be called first!");
252 void WinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol)
254 MCDataFragment *DF = getOrCreateDataFragment();
256 DF->getFixups().push_back(
257 MCFixup::Create(DF->getContents().size(),
258 MCSymbolRefExpr::Create (Symbol, getContext ()),
260 DF->getContents().resize(DF->getContents().size() + 4, 0);
263 void WinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
264 llvm_unreachable("not implemented");
267 void WinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
268 unsigned ByteAlignment) {
269 assert((Symbol->isInSection()
270 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
271 : true) && "Got non-COFF section in the COFF backend!");
272 AddCommonSymbol(Symbol, Size, ByteAlignment, true);
275 void WinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
276 unsigned ByteAlignment) {
277 assert((Symbol->isInSection()
278 ? Symbol->getSection().getVariant() == MCSection::SV_COFF
279 : true) && "Got non-COFF section in the COFF backend!");
280 AddCommonSymbol(Symbol, Size, ByteAlignment, false);
283 void WinCOFFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
284 uint64_t Size,unsigned ByteAlignment) {
285 llvm_unreachable("not implemented");
288 void WinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
289 uint64_t Size, unsigned ByteAlignment) {
290 llvm_unreachable("not implemented");
293 void WinCOFFStreamer::EmitFileDirective(StringRef Filename) {
294 // Ignore for now, linkers don't care, and proper debug
295 // info will be a much large effort.
298 // TODO: Implement this if you want to emit .comment section in COFF obj files.
299 void WinCOFFStreamer::EmitIdent(StringRef IdentString) {
300 llvm_unreachable("unsupported directive");
303 void WinCOFFStreamer::EmitWin64EHHandlerData() {
304 MCStreamer::EmitWin64EHHandlerData();
306 // We have to emit the unwind info now, because this directive
307 // actually switches to the .xdata section!
308 MCWin64EHUnwindEmitter::EmitUnwindInfo(*this, getCurrentW64UnwindInfo());
311 void WinCOFFStreamer::FinishImpl() {
312 EmitFrames(NULL, true);
314 MCObjectStreamer::FinishImpl();
319 MCStreamer *createWinCOFFStreamer(MCContext &Context,
324 WinCOFFStreamer *S = new WinCOFFStreamer(Context, MAB, CE, OS);
325 S->getAssembler().setRelaxAll(RelaxAll);