1 //===- lib/MC/MCWin64EH.cpp - MCWin64EH implementation --------------------===//
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 #include "llvm/MC/MCWin64EH.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/Win64EH.h"
20 // NOTE: All relocations generated here are 4-byte image-relative.
22 static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
24 for (const auto &I : Insns) {
25 switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
26 case Win64EH::UOP_PushNonVol:
27 case Win64EH::UOP_AllocSmall:
28 case Win64EH::UOP_SetFPReg:
29 case Win64EH::UOP_PushMachFrame:
32 case Win64EH::UOP_SaveNonVol:
33 case Win64EH::UOP_SaveXMM128:
36 case Win64EH::UOP_SaveNonVolBig:
37 case Win64EH::UOP_SaveXMM128Big:
40 case Win64EH::UOP_AllocLarge:
41 Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
48 static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
49 const MCSymbol *RHS) {
50 MCContext &Context = Streamer.getContext();
52 MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
53 MCSymbolRefExpr::create(RHS, Context), Context);
54 Streamer.EmitValue(Diff, 1);
57 static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
58 WinEH::Instruction &inst) {
61 b2 = (inst.Operation & 0x0F);
62 switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
63 case Win64EH::UOP_PushNonVol:
64 EmitAbsDifference(streamer, inst.Label, begin);
65 b2 |= (inst.Register & 0x0F) << 4;
66 streamer.EmitIntValue(b2, 1);
68 case Win64EH::UOP_AllocLarge:
69 EmitAbsDifference(streamer, inst.Label, begin);
70 if (inst.Offset > 512 * 1024 - 8) {
72 streamer.EmitIntValue(b2, 1);
73 w = inst.Offset & 0xFFF8;
74 streamer.EmitIntValue(w, 2);
75 w = inst.Offset >> 16;
77 streamer.EmitIntValue(b2, 1);
80 streamer.EmitIntValue(w, 2);
82 case Win64EH::UOP_AllocSmall:
83 b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
84 EmitAbsDifference(streamer, inst.Label, begin);
85 streamer.EmitIntValue(b2, 1);
87 case Win64EH::UOP_SetFPReg:
88 EmitAbsDifference(streamer, inst.Label, begin);
89 streamer.EmitIntValue(b2, 1);
91 case Win64EH::UOP_SaveNonVol:
92 case Win64EH::UOP_SaveXMM128:
93 b2 |= (inst.Register & 0x0F) << 4;
94 EmitAbsDifference(streamer, inst.Label, begin);
95 streamer.EmitIntValue(b2, 1);
97 if (inst.Operation == Win64EH::UOP_SaveXMM128)
99 streamer.EmitIntValue(w, 2);
101 case Win64EH::UOP_SaveNonVolBig:
102 case Win64EH::UOP_SaveXMM128Big:
103 b2 |= (inst.Register & 0x0F) << 4;
104 EmitAbsDifference(streamer, inst.Label, begin);
105 streamer.EmitIntValue(b2, 1);
106 if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
107 w = inst.Offset & 0xFFF0;
109 w = inst.Offset & 0xFFF8;
110 streamer.EmitIntValue(w, 2);
111 w = inst.Offset >> 16;
112 streamer.EmitIntValue(w, 2);
114 case Win64EH::UOP_PushMachFrame:
115 if (inst.Offset == 1)
117 EmitAbsDifference(streamer, inst.Label, begin);
118 streamer.EmitIntValue(b2, 1);
123 static void EmitSymbolRefWithOfs(MCStreamer &streamer,
124 const MCSymbol *Base,
125 const MCSymbol *Other) {
126 MCContext &Context = streamer.getContext();
127 const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
128 const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
129 const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
130 const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
131 MCSymbolRefExpr::VK_COFF_IMGREL32,
133 streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
136 static void EmitRuntimeFunction(MCStreamer &streamer,
137 const WinEH::FrameInfo *info) {
138 MCContext &context = streamer.getContext();
140 streamer.EmitValueToAlignment(4);
141 EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
142 EmitSymbolRefWithOfs(streamer, info->Function, info->End);
143 streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
144 MCSymbolRefExpr::VK_COFF_IMGREL32,
148 static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
149 // If this UNWIND_INFO already has a symbol, it's already been emitted.
153 MCContext &context = streamer.getContext();
154 MCSymbol *Label = context.createTempSymbol();
156 streamer.EmitValueToAlignment(4);
157 streamer.EmitLabel(Label);
158 info->Symbol = Label;
160 // Upper 3 bits are the version number (currently 1).
161 uint8_t flags = 0x01;
162 if (info->ChainedParent)
163 flags |= Win64EH::UNW_ChainInfo << 3;
165 if (info->HandlesUnwind)
166 flags |= Win64EH::UNW_TerminateHandler << 3;
167 if (info->HandlesExceptions)
168 flags |= Win64EH::UNW_ExceptionHandler << 3;
170 streamer.EmitIntValue(flags, 1);
173 EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
175 streamer.EmitIntValue(0, 1);
177 uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
178 streamer.EmitIntValue(numCodes, 1);
181 if (info->LastFrameInst >= 0) {
182 WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
183 assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
184 frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
186 streamer.EmitIntValue(frame, 1);
188 // Emit unwind instructions (in reverse order).
189 uint8_t numInst = info->Instructions.size();
190 for (uint8_t c = 0; c < numInst; ++c) {
191 WinEH::Instruction inst = info->Instructions.back();
192 info->Instructions.pop_back();
193 EmitUnwindCode(streamer, info->Begin, inst);
196 // For alignment purposes, the instruction array will always have an even
197 // number of entries, with the final entry potentially unused (in which case
198 // the array will be one longer than indicated by the count of unwind codes
201 streamer.EmitIntValue(0, 2);
204 if (flags & (Win64EH::UNW_ChainInfo << 3))
205 EmitRuntimeFunction(streamer, info->ChainedParent);
207 ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
208 streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
209 MCSymbolRefExpr::VK_COFF_IMGREL32,
211 else if (numCodes == 0) {
212 // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
213 // a chained unwind info, if there is no handler, and if there are fewer
214 // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
215 streamer.EmitIntValue(0, 4);
219 void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
220 // Emit the unwind info structs first.
221 for (const auto &CFI : Streamer.getWinFrameInfos()) {
222 MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
223 Streamer.SwitchSection(XData);
224 ::EmitUnwindInfo(Streamer, CFI.get());
227 // Now emit RUNTIME_FUNCTION entries.
228 for (const auto &CFI : Streamer.getWinFrameInfos()) {
229 MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
230 Streamer.SwitchSection(PData);
231 EmitRuntimeFunction(Streamer, CFI.get());
235 void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo(
236 MCStreamer &Streamer, WinEH::FrameInfo *info) const {
237 // Switch sections (the static function above is meant to be called from
238 // here and from Emit().
239 MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
240 Streamer.SwitchSection(XData);
242 ::EmitUnwindInfo(Streamer, info);