[ASan] Make insertion of version mismatch guard configurable
[llvm-core.git] / lib / MC / MCWinCOFFStreamer.cpp
blob04d5f100a2ffb1705a39dd54ec1c41b28af5913d
1 //===- llvm/MC/MCWinCOFFStreamer.cpp --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains an implementation of a Windows COFF object file streamer.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/MC/MCAsmBackend.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/MCFixup.h"
24 #include "llvm/MC/MCFragment.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCObjectStreamer.h"
27 #include "llvm/MC/MCObjectWriter.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"
36 #include <algorithm>
37 #include <cassert>
38 #include <cstdint>
40 using namespace llvm;
42 #define DEBUG_TYPE "WinCOFFStreamer"
44 MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context,
45 std::unique_ptr<MCAsmBackend> MAB,
46 std::unique_ptr<MCCodeEmitter> CE,
47 std::unique_ptr<MCObjectWriter> OW)
48 : MCObjectStreamer(Context, std::move(MAB), std::move(OW), std::move(CE)),
49 CurSymbol(nullptr) {}
51 void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst,
52 const MCSubtargetInfo &STI) {
53 MCDataFragment *DF = getOrCreateDataFragment();
55 SmallVector<MCFixup, 4> Fixups;
56 SmallString<256> Code;
57 raw_svector_ostream VecOS(Code);
58 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
60 // Add the fixups and data.
61 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
62 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
63 DF->getFixups().push_back(Fixups[i]);
65 DF->setHasInstructions(STI);
66 DF->getContents().append(Code.begin(), Code.end());
69 void MCWinCOFFStreamer::InitSections(bool NoExecStack) {
70 // FIXME: this is identical to the ELF one.
71 // This emulates the same behavior of GNU as. This makes it easier
72 // to compare the output as the major sections are in the same order.
73 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
74 EmitCodeAlignment(4);
76 SwitchSection(getContext().getObjectFileInfo()->getDataSection());
77 EmitCodeAlignment(4);
79 SwitchSection(getContext().getObjectFileInfo()->getBSSSection());
80 EmitCodeAlignment(4);
82 SwitchSection(getContext().getObjectFileInfo()->getTextSection());
85 void MCWinCOFFStreamer::EmitLabel(MCSymbol *S, SMLoc Loc) {
86 auto *Symbol = cast<MCSymbolCOFF>(S);
87 MCObjectStreamer::EmitLabel(Symbol, Loc);
90 void MCWinCOFFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
91 llvm_unreachable("not implemented");
94 void MCWinCOFFStreamer::EmitThumbFunc(MCSymbol *Func) {
95 llvm_unreachable("not implemented");
98 bool MCWinCOFFStreamer::EmitSymbolAttribute(MCSymbol *S,
99 MCSymbolAttr Attribute) {
100 auto *Symbol = cast<MCSymbolCOFF>(S);
101 getAssembler().registerSymbol(*Symbol);
103 switch (Attribute) {
104 default: return false;
105 case MCSA_WeakReference:
106 case MCSA_Weak:
107 Symbol->setIsWeakExternal();
108 Symbol->setExternal(true);
109 break;
110 case MCSA_Global:
111 Symbol->setExternal(true);
112 break;
113 case MCSA_AltEntry:
114 llvm_unreachable("COFF doesn't support the .alt_entry attribute");
117 return true;
120 void MCWinCOFFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
121 llvm_unreachable("not implemented");
124 void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *S) {
125 auto *Symbol = cast<MCSymbolCOFF>(S);
126 if (CurSymbol)
127 Error("starting a new symbol definition without completing the "
128 "previous one");
129 CurSymbol = Symbol;
132 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
133 if (!CurSymbol) {
134 Error("storage class specified outside of symbol definition");
135 return;
138 if (StorageClass & ~COFF::SSC_Invalid) {
139 Error("storage class value '" + Twine(StorageClass) +
140 "' out of range");
141 return;
144 getAssembler().registerSymbol(*CurSymbol);
145 cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass);
148 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
149 if (!CurSymbol) {
150 Error("symbol type specified outside of a symbol definition");
151 return;
154 if (Type & ~0xffff) {
155 Error("type value '" + Twine(Type) + "' out of range");
156 return;
159 getAssembler().registerSymbol(*CurSymbol);
160 cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type);
163 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
164 if (!CurSymbol)
165 Error("ending symbol definition without starting one");
166 CurSymbol = nullptr;
169 void MCWinCOFFStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
170 // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is
171 // unnecessary) on all platforms which use table-based exception dispatch.
172 if (getContext().getObjectFileInfo()->getTargetTriple().getArch() !=
173 Triple::x86)
174 return;
176 const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol);
177 if (CSymbol->isSafeSEH())
178 return;
180 MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection();
181 getAssembler().registerSection(*SXData);
182 if (SXData->getAlignment() < 4)
183 SXData->setAlignment(4);
185 new MCSymbolIdFragment(Symbol, SXData);
187 getAssembler().registerSymbol(*Symbol);
188 CSymbol->setIsSafeSEH();
190 // The Microsoft linker requires that the symbol type of a handler be
191 // function. Go ahead and oblige it here.
192 CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION
193 << COFF::SCT_COMPLEX_TYPE_SHIFT);
196 void MCWinCOFFStreamer::EmitCOFFSymbolIndex(MCSymbol const *Symbol) {
197 MCSection *Sec = getCurrentSectionOnly();
198 getAssembler().registerSection(*Sec);
199 if (Sec->getAlignment() < 4)
200 Sec->setAlignment(4);
202 new MCSymbolIdFragment(Symbol, getCurrentSectionOnly());
204 getAssembler().registerSymbol(*Symbol);
207 void MCWinCOFFStreamer::EmitCOFFSectionIndex(const MCSymbol *Symbol) {
208 visitUsedSymbol(*Symbol);
209 MCDataFragment *DF = getOrCreateDataFragment();
210 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
211 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
212 DF->getFixups().push_back(Fixup);
213 DF->getContents().resize(DF->getContents().size() + 2, 0);
216 void MCWinCOFFStreamer::EmitCOFFSecRel32(const MCSymbol *Symbol,
217 uint64_t Offset) {
218 visitUsedSymbol(*Symbol);
219 MCDataFragment *DF = getOrCreateDataFragment();
220 // Create Symbol A for the relocation relative reference.
221 const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext());
222 // Add the constant offset, if given.
223 if (Offset)
224 MCE = MCBinaryExpr::createAdd(
225 MCE, MCConstantExpr::create(Offset, getContext()), getContext());
226 // Build the secrel32 relocation.
227 MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4);
228 // Record the relocation.
229 DF->getFixups().push_back(Fixup);
230 // Emit 4 bytes (zeros) to the object file.
231 DF->getContents().resize(DF->getContents().size() + 4, 0);
234 void MCWinCOFFStreamer::EmitCOFFImgRel32(const MCSymbol *Symbol,
235 int64_t Offset) {
236 visitUsedSymbol(*Symbol);
237 MCDataFragment *DF = getOrCreateDataFragment();
238 // Create Symbol A for the relocation relative reference.
239 const MCExpr *MCE = MCSymbolRefExpr::create(
240 Symbol, MCSymbolRefExpr::VK_COFF_IMGREL32, getContext());
241 // Add the constant offset, if given.
242 if (Offset)
243 MCE = MCBinaryExpr::createAdd(
244 MCE, MCConstantExpr::create(Offset, getContext()), getContext());
245 // Build the imgrel relocation.
246 MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_4);
247 // Record the relocation.
248 DF->getFixups().push_back(Fixup);
249 // Emit 4 bytes (zeros) to the object file.
250 DF->getContents().resize(DF->getContents().size() + 4, 0);
253 void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
254 unsigned ByteAlignment) {
255 auto *Symbol = cast<MCSymbolCOFF>(S);
257 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
258 if (T.isWindowsMSVCEnvironment()) {
259 if (ByteAlignment > 32)
260 report_fatal_error("alignment is limited to 32-bytes");
262 // Round size up to alignment so that we will honor the alignment request.
263 Size = std::max(Size, static_cast<uint64_t>(ByteAlignment));
266 getAssembler().registerSymbol(*Symbol);
267 Symbol->setExternal(true);
268 Symbol->setCommon(Size, ByteAlignment);
270 if (!T.isWindowsMSVCEnvironment() && ByteAlignment > 1) {
271 SmallString<128> Directive;
272 raw_svector_ostream OS(Directive);
273 const MCObjectFileInfo *MFI = getContext().getObjectFileInfo();
275 OS << " -aligncomm:\"" << Symbol->getName() << "\","
276 << Log2_32_Ceil(ByteAlignment);
278 PushSection();
279 SwitchSection(MFI->getDrectveSection());
280 EmitBytes(Directive);
281 PopSection();
285 void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
286 unsigned ByteAlignment) {
287 auto *Symbol = cast<MCSymbolCOFF>(S);
289 MCSection *Section = getContext().getObjectFileInfo()->getBSSSection();
290 PushSection();
291 SwitchSection(Section);
292 EmitValueToAlignment(ByteAlignment, 0, 1, 0);
293 EmitLabel(Symbol);
294 Symbol->setExternal(false);
295 EmitZeros(Size);
296 PopSection();
299 void MCWinCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
300 uint64_t Size, unsigned ByteAlignment,
301 SMLoc Loc) {
302 llvm_unreachable("not implemented");
305 void MCWinCOFFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
306 uint64_t Size, unsigned ByteAlignment) {
307 llvm_unreachable("not implemented");
310 // TODO: Implement this if you want to emit .comment section in COFF obj files.
311 void MCWinCOFFStreamer::EmitIdent(StringRef IdentString) {
312 llvm_unreachable("not implemented");
315 void MCWinCOFFStreamer::EmitWinEHHandlerData(SMLoc Loc) {
316 llvm_unreachable("not implemented");
319 void MCWinCOFFStreamer::FinishImpl() {
320 MCObjectStreamer::FinishImpl();
323 void MCWinCOFFStreamer::Error(const Twine &Msg) const {
324 getContext().reportError(SMLoc(), Msg);