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 Windows COFF object file streamer.
12 //===----------------------------------------------------------------------===//
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"
37 #define DEBUG_TYPE "WinCOFFStreamer"
40 MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
41 MCCodeEmitter &CE, raw_ostream &OS)
42 : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
44 void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
45 const MCSubtargetInfo &STI) {
46 MCDataFragment *DF = getOrCreateDataFragment();
48 SmallVector<MCFixup, 4> Fixups;
49 SmallString<256> Code;
50 raw_svector_ostream VecOS(Code);
51 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
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]);
60 DF->getContents().append(Code.begin(), Code.end());
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());
70 SwitchSection(getContext().getObjectFileInfo()->getDataSection());
73 SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
76 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
79 void MCWinCOFFStreamer::EmitLabel(MCSymbol *Symbol) {
80 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
81 MCObjectStreamer::EmitLabel(Symbol);
84 void MCWinCOFFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
88 void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
89 llvm_unreachable("not implemented");
92 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
93 llvm_unreachable("not implemented");
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!");
103 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
106 default: return false;
107 case MCSA_WeakReference:
109 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
110 SD.setExternal(true);
113 SD.setExternal(true);
120 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
121 llvm_unreachable("not implemented");
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");
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!");
137 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
138 SD.modifyFlags(StorageClass << COFF::SF_ClassShift, COFF::SF_ClassMask);
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");
146 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
147 SD.modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
150 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
151 assert(CurSymbol && "ending symbol definition without beginning one");
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);
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);
171 void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
172 llvm_unreachable("not supported");
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!");
181 if (ByteAlignment > 32)
182 report_fatal_error("alignment is limited to 32-bytes");
184 AssignSection(Symbol, nullptr);
186 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
187 SD.setExternal(true);
188 SD.setCommon(Size, ByteAlignment);
191 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
192 unsigned ByteAlignment) {
193 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
195 const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
196 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
197 if (SectionData.getAlignment() < ByteAlignment)
198 SectionData.setAlignment(ByteAlignment);
200 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
201 SD.setExternal(false);
203 AssignSection(Symbol, Section);
205 if (ByteAlignment != 1)
206 new MCAlignFragment(ByteAlignment, /*_Value=*/0, /*_ValueSize=*/0,
207 ByteAlignment, &SectionData);
209 MCFillFragment *Fragment =
210 new MCFillFragment(/*_Value=*/0, /*_ValueSize=*/0, Size, &SectionData);
211 SD.setFragment(Fragment);
214 void MCWinCOFFStreamer::EmitZerofill(const MCSection *Section,
215 MCSymbol *Symbol, uint64_t Size,
216 unsigned ByteAlignment) {
217 llvm_unreachable("not implemented");
220 void MCWinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section,
221 MCSymbol *Symbol, uint64_t Size,
222 unsigned ByteAlignment) {
223 llvm_unreachable("not implemented");
226 void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
227 getAssembler().addFileName(Filename);
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");
235 void MCWinCOFFStreamer::EmitWin64EHHandlerData() {
236 llvm_unreachable("not implemented");
239 void MCWinCOFFStreamer::FinishImpl() {
240 MCObjectStreamer::FinishImpl();