1 //===- llvm/MC/MCWinCOFFStreamer.cpp --------------------------------------===//
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/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
25 #include "llvm/MC/MCFragment.h"
26 #include "llvm/MC/MCObjectFileInfo.h"
27 #include "llvm/MC/MCObjectStreamer.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSymbolCOFF.h"
30 #include "llvm/MC/MCWinCOFFStreamer.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/SMLoc.h"
35 #include "llvm/Support/raw_ostream.h"
42 #define DEBUG_TYPE "WinCOFFStreamer"
44 MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
45 MCCodeEmitter &CE, raw_pwrite_stream &OS)
46 : MCObjectStreamer(Context, MAB, OS, &CE), CurSymbol(nullptr) {}
48 void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
49 const MCSubtargetInfo &STI) {
50 MCDataFragment *DF = getOrCreateDataFragment();
52 SmallVector<MCFixup, 4> Fixups;
53 SmallString<256> Code;
54 raw_svector_ostream VecOS(Code);
55 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
57 // Add the fixups and data.
58 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
59 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
60 DF->getFixups().push_back(Fixups[i]);
63 DF->getContents().append(Code.begin(), Code.end());
66 void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
67 // FIXME: this is identical to the ELF one.
68 // This emulates the same behavior of GNU as. This makes it easier
69 // to compare the output as the major sections are in the same order.
70 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
73 SwitchSection(getContext().getObjectFileInfo()->getDataSection());
76 SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
79 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
82 void MCWinCOFFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
83 auto *Symbol = cast<MCSymbolCOFF>(S);
84 MCObjectStreamer::EmitLabel(Symbol, Loc);
87 void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
88 llvm_unreachable("not implemented");
91 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
92 llvm_unreachable("not implemented");
95 bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *S,
96 MCSymbolAttr Attribute) {
97 auto *Symbol = cast<MCSymbolCOFF>(S);
98 getAssembler().registerSymbol(*Symbol);
101 default: return false;
102 case MCSA_WeakReference:
104 Symbol->setIsWeakExternal();
105 Symbol->setExternal(true);
108 Symbol->setExternal(true);
111 llvm_unreachable("COFF doesn't support the .alt_entry attribute");
117 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
118 llvm_unreachable("not implemented");
121 void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *S) {
122 auto *Symbol = cast<MCSymbolCOFF>(S);
124 Error("starting a new symbol definition without completing the "
129 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
131 Error("storage class specified outside of symbol definition");
135 if (StorageClass & ~COFF::SSC_Invalid) {
136 Error("storage class value '" + Twine(StorageClass) +
141 getAssembler().registerSymbol(*CurSymbol);
142 cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass);
145 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
147 Error("symbol type specified outside of a symbol definition");
151 if (Type & ~0xffff) {
152 Error("type value '" + Twine(Type) + "' out of range");
156 getAssembler().registerSymbol(*CurSymbol);
157 cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type);
160 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
162 Error("ending symbol definition without starting one");
166 void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
167 // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is
168 // unnecessary) on all platforms which use table-based exception dispatch.
169 if (getContext().getObjectFileInfo()->getTargetTriple().getArch() !=
173 const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol);
174 if (CSymbol->isSafeSEH())
177 MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
178 getAssembler().registerSection(*SXData);
179 if (SXData->getAlignment() < 4)
180 SXData->setAlignment(4);
182 new MCSafeSEHFragment(Symbol, SXData);
184 getAssembler().registerSymbol(*Symbol);
185 CSymbol->setIsSafeSEH();
187 // The Microsoft linker requires that the symbol type of a handler be
188 // function. Go ahead and oblige it here.
189 CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
190 << COFF::SCT_COMPLEX_TYPE_SHIFT);
193 void MCWinCOFFStreamer::EmitCOFFSectionIndex(const MCSymbol *Symbol) {
194 visitUsedSymbol(*Symbol);
195 MCDataFragment *DF = getOrCreateDataFragment();
196 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
197 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
198 DF->getFixups().push_back(Fixup);
199 DF->getContents().resize(DF->getContents().size() + 2, 0);
202 void MCWinCOFFStreamer::EmitCOFFSecRel32(const MCSymbol *Symbol,
204 visitUsedSymbol(*Symbol);
205 MCDataFragment *DF = getOrCreateDataFragment();
206 // Create Symbol A for the relocation relative reference.
207 const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext());
208 // Add the constant offset, if given.
210 MCE = MCBinaryExpr::createAdd(
211 MCE, MCConstantExpr::create(Offset, getContext()), getContext());
212 // Build the secrel32 relocation.
213 MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4);
214 // Record the relocation.
215 DF->getFixups().push_back(Fixup);
216 // Emit 4 bytes (zeros) to the object file.
217 DF->getContents().resize(DF->getContents().size() + 4, 0);
220 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
221 unsigned ByteAlignment) {
222 auto *Symbol = cast<MCSymbolCOFF>(S);
224 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
225 if (T.isKnownWindowsMSVCEnvironment()) {
226 if (ByteAlignment > 32)
227 report_fatal_error("alignment is limited to 32-bytes");
229 // Round size up to alignment so that we will honor the alignment request.
230 Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
233 getAssembler().registerSymbol(*Symbol);
234 Symbol->setExternal(true);
235 Symbol->setCommon(Size, ByteAlignment);
237 if (!T.isKnownWindowsMSVCEnvironment() && ByteAlignment > 1) {
238 SmallString<128> Directive;
239 raw_svector_ostream OS(Directive);
240 const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
242 OS << " -aligncomm:\"" << Symbol->getName() << "\","
243 << Log2_32_Ceil(ByteAlignment);
246 SwitchSection(MFI->getDrectveSection());
247 EmitBytes(Directive);
252 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
253 unsigned ByteAlignment) {
254 auto *Symbol = cast<MCSymbolCOFF>(S);
256 MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
257 getAssembler().registerSection(*Section);
258 if (Section->getAlignment() < ByteAlignment)
259 Section->setAlignment(ByteAlignment);
261 getAssembler().registerSymbol(*Symbol);
262 Symbol->setExternal(false);
264 if (ByteAlignment != 1)
265 new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0,
266 ByteAlignment, Section);
268 MCFillFragment *Fragment = new MCFillFragment(
269 /*Value=*/0, Size, Section);
270 Symbol->setFragment(Fragment);
273 void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
274 uint64_t Size, unsigned ByteAlignment) {
275 llvm_unreachable("not implemented");
278 void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
279 uint64_t Size, unsigned ByteAlignment) {
280 llvm_unreachable("not implemented");
283 // TODO: Implement this if you want to emit .comment section in COFF obj files.
284 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
285 llvm_unreachable("not implemented");
288 void MCWinCOFFStreamer::EmitWinEHHandlerData() {
289 llvm_unreachable("not implemented");
292 void MCWinCOFFStreamer::FinishImpl() {
293 MCObjectStreamer::FinishImpl();
296 void MCWinCOFFStreamer::Error(const Twine &Msg) const {
297 getContext().reportError(SMLoc(), Msg);