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/ADT/StringExtras.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmLayout.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCObjectFileInfo.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/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::EmitAssemblerFlag(MCAssemblerFlag Flag) {
85 llvm_unreachable("not implemented");
88 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
89 llvm_unreachable("not implemented");
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!");
99 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
102 default: return false;
103 case MCSA_WeakReference:
105 SD.modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
106 SD.setExternal(true);
109 SD.setExternal(true);
116 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
117 llvm_unreachable("not implemented");
120 void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
121 assert((!Symbol->isInSection() ||
122 Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
123 "Got non-COFF section in the COFF backend!");
126 FatalError("starting a new symbol definition without completing the "
131 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
133 FatalError("storage class specified outside of symbol definition");
135 if (StorageClass & ~0xff)
136 FatalError(Twine("storage class value '") + itostr(StorageClass) +
139 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
140 SD.modifyFlags(StorageClass << COFF::SF_ClassShift, COFF::SF_ClassMask);
143 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
145 FatalError("symbol type specified outside of a symbol definition");
148 FatalError(Twine("type value '") + itostr(Type) + "' out of range");
150 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
151 SD.modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
154 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
156 FatalError("ending symbol definition without starting one");
160 void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
161 MCDataFragment *DF = getOrCreateDataFragment();
162 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
163 MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_2);
164 DF->getFixups().push_back(Fixup);
165 DF->getContents().resize(DF->getContents().size() + 4, 0);
168 void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
169 MCDataFragment *DF = getOrCreateDataFragment();
170 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::Create(Symbol, getContext());
171 MCFixup Fixup = MCFixup::Create(DF->getContents().size(), SRE, FK_SecRel_4);
172 DF->getFixups().push_back(Fixup);
173 DF->getContents().resize(DF->getContents().size() + 4, 0);
176 void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
177 llvm_unreachable("not supported");
180 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
181 unsigned ByteAlignment) {
182 assert((!Symbol->isInSection() ||
183 Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
184 "Got non-COFF section in the COFF backend!");
186 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
187 if (T.isKnownWindowsMSVCEnvironment()) {
188 if (ByteAlignment > 32)
189 report_fatal_error("alignment is limited to 32-bytes");
191 // The bfd linker from binutils only supports alignments less than 4 bytes
192 // without inserting -aligncomm arguments into the .drectve section.
193 // TODO: Support inserting -aligncomm into the .drectve section.
194 if (ByteAlignment > 4)
195 report_fatal_error("alignment is limited to 4-bytes");
197 // Round size up to alignment so that we will honor the alignment request.
198 // TODO: We don't need to do this if we are targeting the bfd linker once we
199 // add support for adding -aligncomm into the .drectve section.
200 Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
202 AssignSection(Symbol, nullptr);
204 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
205 SD.setExternal(true);
206 SD.setCommon(Size, ByteAlignment);
209 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
210 unsigned ByteAlignment) {
211 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
213 const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
214 MCSectionData &SectionData = getAssembler().getOrCreateSectionData(*Section);
215 if (SectionData.getAlignment() < ByteAlignment)
216 SectionData.setAlignment(ByteAlignment);
218 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
219 SD.setExternal(false);
221 AssignSection(Symbol, Section);
223 if (ByteAlignment != 1)
224 new MCAlignFragment(ByteAlignment, /*_Value=*/0, /*_ValueSize=*/0,
225 ByteAlignment, &SectionData);
227 MCFillFragment *Fragment =
228 new MCFillFragment(/*_Value=*/0, /*_ValueSize=*/0, Size, &SectionData);
229 SD.setFragment(Fragment);
232 void MCWinCOFFStreamer::EmitZerofill(const MCSection *Section,
233 MCSymbol *Symbol, uint64_t Size,
234 unsigned ByteAlignment) {
235 llvm_unreachable("not implemented");
238 void MCWinCOFFStreamer::EmitTBSSSymbol(const MCSection *Section,
239 MCSymbol *Symbol, uint64_t Size,
240 unsigned ByteAlignment) {
241 llvm_unreachable("not implemented");
244 void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
245 getAssembler().addFileName(Filename);
248 // TODO: Implement this if you want to emit .comment section in COFF obj files.
249 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
250 llvm_unreachable("not implemented");
253 void MCWinCOFFStreamer::EmitWinEHHandlerData() {
254 llvm_unreachable("not implemented");
257 void MCWinCOFFStreamer::FinishImpl() {
258 MCObjectStreamer::FinishImpl();
261 LLVM_ATTRIBUTE_NORETURN
262 void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
263 getContext().FatalError(SMLoc(), Msg);