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/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCObjectFileInfo.h"
21 #include "llvm/MC/MCObjectStreamer.h"
22 #include "llvm/MC/MCSection.h"
23 #include "llvm/MC/MCSectionCOFF.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/MC/MCWinCOFFStreamer.h"
28 #include "llvm/Support/COFF.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.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_pwrite_stream &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(bool NoExecStack) {
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 getAssembler().registerSymbol(*Symbol);
102 default: return false;
103 case MCSA_WeakReference:
105 Symbol->modifyFlags(COFF::SF_WeakExternal, COFF::SF_WeakExternal);
106 Symbol->setExternal(true);
109 Symbol->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 & ~COFF::SSC_Invalid)
136 FatalError("storage class value '" + Twine(StorageClass) +
139 getAssembler().registerSymbol(*CurSymbol);
140 CurSymbol->modifyFlags(StorageClass << COFF::SF_ClassShift,
144 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
146 FatalError("symbol type specified outside of a symbol definition");
149 FatalError("type value '" + Twine(Type) + "' out of range");
151 getAssembler().registerSymbol(*CurSymbol);
152 CurSymbol->modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
155 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
157 FatalError("ending symbol definition without starting one");
161 void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
162 // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is
163 // unnecessary) on all platforms which use table-based exception dispatch.
164 if (getContext().getObjectFileInfo()->getTargetTriple().getArch() !=
168 if (Symbol->getFlags() & COFF::SF_SafeSEH)
171 MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
172 getAssembler().registerSection(*SXData);
173 if (SXData->getAlignment() < 4)
174 SXData->setAlignment(4);
176 new MCSafeSEHFragment(Symbol, SXData);
178 getAssembler().registerSymbol(*Symbol);
179 Symbol->modifyFlags(COFF::SF_SafeSEH, COFF::SF_SafeSEH);
182 void MCWinCOFFStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
183 MCDataFragment *DF = getOrCreateDataFragment();
184 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
185 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
186 DF->getFixups().push_back(Fixup);
187 DF->getContents().resize(DF->getContents().size() + 2, 0);
190 void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
191 MCDataFragment *DF = getOrCreateDataFragment();
192 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
193 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_4);
194 DF->getFixups().push_back(Fixup);
195 DF->getContents().resize(DF->getContents().size() + 4, 0);
198 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
199 unsigned ByteAlignment) {
200 assert((!Symbol->isInSection() ||
201 Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
202 "Got non-COFF section in the COFF backend!");
204 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
205 if (T.isKnownWindowsMSVCEnvironment()) {
206 if (ByteAlignment > 32)
207 report_fatal_error("alignment is limited to 32-bytes");
209 // Round size up to alignment so that we will honor the alignment request.
210 Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
213 AssignSection(Symbol, nullptr);
215 getAssembler().registerSymbol(*Symbol);
216 Symbol->setExternal(true);
217 Symbol->setCommon(Size, ByteAlignment);
219 if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) {
220 SmallString<128> Directive;
221 raw_svector_ostream OS(Directive);
222 const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
224 OS << " -aligncomm:\"" << Symbol->getName() << "\","
225 << Log2_32_Ceil(ByteAlignment);
229 SwitchSection(MFI->getDrectveSection());
230 EmitBytes(Directive);
235 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
236 unsigned ByteAlignment) {
237 assert(!Symbol->isInSection() && "Symbol must not already have a section!");
239 MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
240 getAssembler().registerSection(*Section);
241 if (Section->getAlignment() < ByteAlignment)
242 Section->setAlignment(ByteAlignment);
244 getAssembler().registerSymbol(*Symbol);
245 Symbol->setExternal(false);
247 AssignSection(Symbol, Section);
249 if (ByteAlignment != 1)
250 new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0,
251 ByteAlignment, Section);
253 MCFillFragment *Fragment = new MCFillFragment(
254 /*Value=*/0, /*ValueSize=*/0, Size, Section);
255 Symbol->setFragment(Fragment);
258 void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
259 uint64_t Size, unsigned ByteAlignment) {
260 llvm_unreachable("not implemented");
263 void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
264 uint64_t Size, unsigned ByteAlignment) {
265 llvm_unreachable("not implemented");
268 void MCWinCOFFStreamer::EmitFileDirective(StringRef Filename) {
269 getAssembler().addFileName(Filename);
272 // TODO: Implement this if you want to emit .comment section in COFF obj files.
273 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
274 llvm_unreachable("not implemented");
277 void MCWinCOFFStreamer::EmitWinEHHandlerData() {
278 llvm_unreachable("not implemented");
281 void MCWinCOFFStreamer::FinishImpl() {
282 MCObjectStreamer::FinishImpl();
285 LLVM_ATTRIBUTE_NORETURN
286 void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
287 getContext().reportFatalError(SMLoc(), Msg);