/src/llvm-project/llvm/lib/MC/MCWinCOFFStreamer.cpp
Line | Count | Source (jump to first uncovered line) |
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. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "llvm/MC/MCWinCOFFStreamer.h" |
14 | | #include "llvm/ADT/SmallString.h" |
15 | | #include "llvm/ADT/SmallVector.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/Support/Casting.h" |
31 | | #include "llvm/Support/ErrorHandling.h" |
32 | | #include "llvm/Support/MathExtras.h" |
33 | | #include "llvm/Support/SMLoc.h" |
34 | | #include "llvm/Support/raw_ostream.h" |
35 | | #include "llvm/TargetParser/Triple.h" |
36 | | #include <algorithm> |
37 | | #include <cstdint> |
38 | | |
39 | | using namespace llvm; |
40 | | |
41 | | #define DEBUG_TYPE "WinCOFFStreamer" |
42 | | |
43 | | MCWinCOFFStreamer::MCWinCOFFStreamer(MCContext &Context, |
44 | | std::unique_ptr<MCAsmBackend> MAB, |
45 | | std::unique_ptr<MCCodeEmitter> CE, |
46 | | std::unique_ptr<MCObjectWriter> OW) |
47 | | : MCObjectStreamer(Context, std::move(MAB), std::move(OW), std::move(CE)), |
48 | 0 | CurSymbol(nullptr) {} |
49 | | |
50 | | void MCWinCOFFStreamer::emitInstToData(const MCInst &Inst, |
51 | 0 | const MCSubtargetInfo &STI) { |
52 | 0 | MCDataFragment *DF = getOrCreateDataFragment(); |
53 | |
|
54 | 0 | SmallVector<MCFixup, 4> Fixups; |
55 | 0 | SmallString<256> Code; |
56 | 0 | getAssembler().getEmitter().encodeInstruction(Inst, Code, Fixups, STI); |
57 | | |
58 | | // Add the fixups and data. |
59 | 0 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
60 | 0 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
61 | 0 | DF->getFixups().push_back(Fixups[i]); |
62 | 0 | } |
63 | 0 | DF->setHasInstructions(STI); |
64 | 0 | DF->getContents().append(Code.begin(), Code.end()); |
65 | 0 | } |
66 | | |
67 | | void MCWinCOFFStreamer::initSections(bool NoExecStack, |
68 | 0 | const MCSubtargetInfo &STI) { |
69 | | // FIXME: this is identical to the ELF one. |
70 | | // This emulates the same behavior of GNU as. This makes it easier |
71 | | // to compare the output as the major sections are in the same order. |
72 | 0 | switchSection(getContext().getObjectFileInfo()->getTextSection()); |
73 | 0 | emitCodeAlignment(Align(4), &STI); |
74 | |
|
75 | 0 | switchSection(getContext().getObjectFileInfo()->getDataSection()); |
76 | 0 | emitCodeAlignment(Align(4), &STI); |
77 | |
|
78 | 0 | switchSection(getContext().getObjectFileInfo()->getBSSSection()); |
79 | 0 | emitCodeAlignment(Align(4), &STI); |
80 | |
|
81 | 0 | switchSection(getContext().getObjectFileInfo()->getTextSection()); |
82 | 0 | } |
83 | | |
84 | 0 | void MCWinCOFFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { |
85 | 0 | auto *Symbol = cast<MCSymbolCOFF>(S); |
86 | 0 | MCObjectStreamer::emitLabel(Symbol, Loc); |
87 | 0 | } |
88 | | |
89 | 0 | void MCWinCOFFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) { |
90 | | // Let the target do whatever target specific stuff it needs to do. |
91 | 0 | getAssembler().getBackend().handleAssemblerFlag(Flag); |
92 | |
|
93 | 0 | switch (Flag) { |
94 | | // None of these require COFF specific handling. |
95 | 0 | case MCAF_SyntaxUnified: |
96 | 0 | case MCAF_Code16: |
97 | 0 | case MCAF_Code32: |
98 | 0 | case MCAF_Code64: |
99 | 0 | break; |
100 | 0 | case MCAF_SubsectionsViaSymbols: |
101 | 0 | llvm_unreachable("COFF doesn't support .subsections_via_symbols"); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | void MCWinCOFFStreamer::emitThumbFunc(MCSymbol *Func) { |
106 | 0 | llvm_unreachable("not implemented"); |
107 | 0 | } |
108 | | |
109 | | bool MCWinCOFFStreamer::emitSymbolAttribute(MCSymbol *S, |
110 | 0 | MCSymbolAttr Attribute) { |
111 | 0 | auto *Symbol = cast<MCSymbolCOFF>(S); |
112 | 0 | getAssembler().registerSymbol(*Symbol); |
113 | |
|
114 | 0 | switch (Attribute) { |
115 | 0 | default: return false; |
116 | 0 | case MCSA_WeakReference: |
117 | 0 | case MCSA_Weak: |
118 | 0 | Symbol->setWeakExternalCharacteristics(COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS); |
119 | 0 | Symbol->setExternal(true); |
120 | 0 | break; |
121 | 0 | case MCSA_WeakAntiDep: |
122 | 0 | Symbol->setWeakExternalCharacteristics(COFF::IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY); |
123 | 0 | Symbol->setExternal(true); |
124 | 0 | Symbol->setIsWeakExternal(true); |
125 | 0 | break; |
126 | 0 | case MCSA_Global: |
127 | 0 | Symbol->setExternal(true); |
128 | 0 | break; |
129 | 0 | case MCSA_AltEntry: |
130 | 0 | llvm_unreachable("COFF doesn't support the .alt_entry attribute"); |
131 | 0 | } |
132 | | |
133 | 0 | return true; |
134 | 0 | } |
135 | | |
136 | 0 | void MCWinCOFFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
137 | 0 | llvm_unreachable("not implemented"); |
138 | 0 | } |
139 | | |
140 | 0 | void MCWinCOFFStreamer::beginCOFFSymbolDef(MCSymbol const *S) { |
141 | 0 | auto *Symbol = cast<MCSymbolCOFF>(S); |
142 | 0 | if (CurSymbol) |
143 | 0 | Error("starting a new symbol definition without completing the " |
144 | 0 | "previous one"); |
145 | 0 | CurSymbol = Symbol; |
146 | 0 | } |
147 | | |
148 | 0 | void MCWinCOFFStreamer::emitCOFFSymbolStorageClass(int StorageClass) { |
149 | 0 | if (!CurSymbol) { |
150 | 0 | Error("storage class specified outside of symbol definition"); |
151 | 0 | return; |
152 | 0 | } |
153 | | |
154 | 0 | if (StorageClass & ~COFF::SSC_Invalid) { |
155 | 0 | Error("storage class value '" + Twine(StorageClass) + |
156 | 0 | "' out of range"); |
157 | 0 | return; |
158 | 0 | } |
159 | | |
160 | 0 | getAssembler().registerSymbol(*CurSymbol); |
161 | 0 | cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass); |
162 | 0 | } |
163 | | |
164 | 0 | void MCWinCOFFStreamer::emitCOFFSymbolType(int Type) { |
165 | 0 | if (!CurSymbol) { |
166 | 0 | Error("symbol type specified outside of a symbol definition"); |
167 | 0 | return; |
168 | 0 | } |
169 | | |
170 | 0 | if (Type & ~0xffff) { |
171 | 0 | Error("type value '" + Twine(Type) + "' out of range"); |
172 | 0 | return; |
173 | 0 | } |
174 | | |
175 | 0 | getAssembler().registerSymbol(*CurSymbol); |
176 | 0 | cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type); |
177 | 0 | } |
178 | | |
179 | 0 | void MCWinCOFFStreamer::endCOFFSymbolDef() { |
180 | 0 | if (!CurSymbol) |
181 | 0 | Error("ending symbol definition without starting one"); |
182 | 0 | CurSymbol = nullptr; |
183 | 0 | } |
184 | | |
185 | 0 | void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) { |
186 | | // SafeSEH is a feature specific to 32-bit x86. It does not exist (and is |
187 | | // unnecessary) on all platforms which use table-based exception dispatch. |
188 | 0 | if (getContext().getTargetTriple().getArch() != Triple::x86) |
189 | 0 | return; |
190 | | |
191 | 0 | const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol); |
192 | 0 | if (CSymbol->isSafeSEH()) |
193 | 0 | return; |
194 | | |
195 | 0 | MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection(); |
196 | 0 | getAssembler().registerSection(*SXData); |
197 | 0 | SXData->ensureMinAlignment(Align(4)); |
198 | |
|
199 | 0 | new MCSymbolIdFragment(Symbol, SXData); |
200 | |
|
201 | 0 | getAssembler().registerSymbol(*Symbol); |
202 | 0 | CSymbol->setIsSafeSEH(); |
203 | | |
204 | | // The Microsoft linker requires that the symbol type of a handler be |
205 | | // function. Go ahead and oblige it here. |
206 | 0 | CSymbol->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION |
207 | 0 | << COFF::SCT_COMPLEX_TYPE_SHIFT); |
208 | 0 | } |
209 | | |
210 | 0 | void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) { |
211 | 0 | MCSection *Sec = getCurrentSectionOnly(); |
212 | 0 | getAssembler().registerSection(*Sec); |
213 | 0 | Sec->ensureMinAlignment(Align(4)); |
214 | |
|
215 | 0 | new MCSymbolIdFragment(Symbol, getCurrentSectionOnly()); |
216 | |
|
217 | 0 | getAssembler().registerSymbol(*Symbol); |
218 | 0 | } |
219 | | |
220 | 0 | void MCWinCOFFStreamer::emitCOFFSectionIndex(const MCSymbol *Symbol) { |
221 | 0 | visitUsedSymbol(*Symbol); |
222 | 0 | MCDataFragment *DF = getOrCreateDataFragment(); |
223 | 0 | const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext()); |
224 | 0 | MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2); |
225 | 0 | DF->getFixups().push_back(Fixup); |
226 | 0 | DF->getContents().resize(DF->getContents().size() + 2, 0); |
227 | 0 | } |
228 | | |
229 | | void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol, |
230 | 0 | uint64_t Offset) { |
231 | 0 | visitUsedSymbol(*Symbol); |
232 | 0 | MCDataFragment *DF = getOrCreateDataFragment(); |
233 | | // Create Symbol A for the relocation relative reference. |
234 | 0 | const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext()); |
235 | | // Add the constant offset, if given. |
236 | 0 | if (Offset) |
237 | 0 | MCE = MCBinaryExpr::createAdd( |
238 | 0 | MCE, MCConstantExpr::create(Offset, getContext()), getContext()); |
239 | | // Build the secrel32 relocation. |
240 | 0 | MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4); |
241 | | // Record the relocation. |
242 | 0 | DF->getFixups().push_back(Fixup); |
243 | | // Emit 4 bytes (zeros) to the object file. |
244 | 0 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
245 | 0 | } |
246 | | |
247 | | void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol, |
248 | 0 | int64_t Offset) { |
249 | 0 | visitUsedSymbol(*Symbol); |
250 | 0 | MCDataFragment *DF = getOrCreateDataFragment(); |
251 | | // Create Symbol A for the relocation relative reference. |
252 | 0 | const MCExpr *MCE = MCSymbolRefExpr::create( |
253 | 0 | Symbol, MCSymbolRefExpr::VK_COFF_IMGREL32, getContext()); |
254 | | // Add the constant offset, if given. |
255 | 0 | if (Offset) |
256 | 0 | MCE = MCBinaryExpr::createAdd( |
257 | 0 | MCE, MCConstantExpr::create(Offset, getContext()), getContext()); |
258 | | // Build the imgrel relocation. |
259 | 0 | MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_4); |
260 | | // Record the relocation. |
261 | 0 | DF->getFixups().push_back(Fixup); |
262 | | // Emit 4 bytes (zeros) to the object file. |
263 | 0 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
264 | 0 | } |
265 | | |
266 | | void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, |
267 | 0 | Align ByteAlignment) { |
268 | 0 | auto *Symbol = cast<MCSymbolCOFF>(S); |
269 | |
|
270 | 0 | const Triple &T = getContext().getTargetTriple(); |
271 | 0 | if (T.isWindowsMSVCEnvironment()) { |
272 | 0 | if (ByteAlignment > 32) |
273 | 0 | report_fatal_error("alignment is limited to 32-bytes"); |
274 | | |
275 | | // Round size up to alignment so that we will honor the alignment request. |
276 | 0 | Size = std::max(Size, ByteAlignment.value()); |
277 | 0 | } |
278 | |
|
279 | 0 | getAssembler().registerSymbol(*Symbol); |
280 | 0 | Symbol->setExternal(true); |
281 | 0 | Symbol->setCommon(Size, ByteAlignment); |
282 | |
|
283 | 0 | if (!T.isWindowsMSVCEnvironment() && ByteAlignment > 1) { |
284 | 0 | SmallString<128> Directive; |
285 | 0 | raw_svector_ostream OS(Directive); |
286 | 0 | const MCObjectFileInfo *MFI = getContext().getObjectFileInfo(); |
287 | |
|
288 | 0 | OS << " -aligncomm:\"" << Symbol->getName() << "\"," |
289 | 0 | << Log2_32_Ceil(ByteAlignment.value()); |
290 | |
|
291 | 0 | pushSection(); |
292 | 0 | switchSection(MFI->getDrectveSection()); |
293 | 0 | emitBytes(Directive); |
294 | 0 | popSection(); |
295 | 0 | } |
296 | 0 | } |
297 | | |
298 | | void MCWinCOFFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size, |
299 | 0 | Align ByteAlignment) { |
300 | 0 | auto *Symbol = cast<MCSymbolCOFF>(S); |
301 | |
|
302 | 0 | MCSection *Section = getContext().getObjectFileInfo()->getBSSSection(); |
303 | 0 | pushSection(); |
304 | 0 | switchSection(Section); |
305 | 0 | emitValueToAlignment(ByteAlignment, 0, 1, 0); |
306 | 0 | emitLabel(Symbol); |
307 | 0 | Symbol->setExternal(false); |
308 | 0 | emitZeros(Size); |
309 | 0 | popSection(); |
310 | 0 | } |
311 | | |
312 | | void MCWinCOFFStreamer::emitWeakReference(MCSymbol *AliasS, |
313 | 0 | const MCSymbol *Symbol) { |
314 | 0 | auto *Alias = cast<MCSymbolCOFF>(AliasS); |
315 | 0 | emitSymbolAttribute(Alias, MCSA_Weak); |
316 | |
|
317 | 0 | getAssembler().registerSymbol(*Symbol); |
318 | 0 | Alias->setVariableValue(MCSymbolRefExpr::create( |
319 | 0 | Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext())); |
320 | 0 | } |
321 | | |
322 | | void MCWinCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol, |
323 | | uint64_t Size, Align ByteAlignment, |
324 | 0 | SMLoc Loc) { |
325 | 0 | llvm_unreachable("not implemented"); |
326 | 0 | } |
327 | | |
328 | | void MCWinCOFFStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, |
329 | 0 | uint64_t Size, Align ByteAlignment) { |
330 | 0 | llvm_unreachable("not implemented"); |
331 | 0 | } |
332 | | |
333 | | // TODO: Implement this if you want to emit .comment section in COFF obj files. |
334 | 0 | void MCWinCOFFStreamer::emitIdent(StringRef IdentString) { |
335 | 0 | llvm_unreachable("not implemented"); |
336 | 0 | } |
337 | | |
338 | 0 | void MCWinCOFFStreamer::emitWinEHHandlerData(SMLoc Loc) { |
339 | 0 | llvm_unreachable("not implemented"); |
340 | 0 | } |
341 | | |
342 | | void MCWinCOFFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From, |
343 | | const MCSymbolRefExpr *To, |
344 | 0 | uint64_t Count) { |
345 | | // Ignore temporary symbols for now. |
346 | 0 | if (!From->getSymbol().isTemporary() && !To->getSymbol().isTemporary()) |
347 | 0 | getAssembler().CGProfile.push_back({From, To, Count}); |
348 | 0 | } |
349 | | |
350 | 0 | void MCWinCOFFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) { |
351 | 0 | const MCSymbol *S = &SRE->getSymbol(); |
352 | 0 | if (getAssembler().registerSymbol(*S)) |
353 | 0 | cast<MCSymbolCOFF>(S)->setExternal(true); |
354 | 0 | } |
355 | | |
356 | 0 | void MCWinCOFFStreamer::finalizeCGProfile() { |
357 | 0 | for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) { |
358 | 0 | finalizeCGProfileEntry(E.From); |
359 | 0 | finalizeCGProfileEntry(E.To); |
360 | 0 | } |
361 | 0 | } |
362 | | |
363 | 0 | void MCWinCOFFStreamer::finishImpl() { |
364 | 0 | finalizeCGProfile(); |
365 | |
|
366 | 0 | MCObjectStreamer::finishImpl(); |
367 | 0 | } |
368 | | |
369 | 0 | void MCWinCOFFStreamer::Error(const Twine &Msg) const { |
370 | 0 | getContext().reportError(SMLoc(), Msg); |
371 | 0 | } |