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/MCObjectFileInfo.h"
15 #include "llvm/MC/MCSectionCOFF.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/Win64EH.h"
22 // NOTE: All relocations generated here are 4-byte image-relative.
24 static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
26 for (const auto &I : Insns) {
27 switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
28 case Win64EH::UOP_PushNonVol:
29 case Win64EH::UOP_AllocSmall:
30 case Win64EH::UOP_SetFPReg:
31 case Win64EH::UOP_PushMachFrame:
34 case Win64EH::UOP_SaveNonVol:
35 case Win64EH::UOP_SaveXMM128:
38 case Win64EH::UOP_SaveNonVolBig:
39 case Win64EH::UOP_SaveXMM128Big:
42 case Win64EH::UOP_AllocLarge:
43 Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
50 static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
51 const MCSymbol *RHS) {
52 MCContext &Context = Streamer.getContext();
54 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LHS, Context),
55 MCSymbolRefExpr::Create(RHS, Context), Context);
56 Streamer.EmitValue(Diff, 1);
59 static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
60 WinEH::Instruction &inst) {
63 b2 = (inst.Operation & 0x0F);
64 switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
65 case Win64EH::UOP_PushNonVol:
66 EmitAbsDifference(streamer, inst.Label, begin);
67 b2 |= (inst.Register & 0x0F) << 4;
68 streamer.EmitIntValue(b2, 1);
70 case Win64EH::UOP_AllocLarge:
71 EmitAbsDifference(streamer, inst.Label, begin);
72 if (inst.Offset > 512 * 1024 - 8) {
74 streamer.EmitIntValue(b2, 1);
75 w = inst.Offset & 0xFFF8;
76 streamer.EmitIntValue(w, 2);
77 w = inst.Offset >> 16;
79 streamer.EmitIntValue(b2, 1);
82 streamer.EmitIntValue(w, 2);
84 case Win64EH::UOP_AllocSmall:
85 b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
86 EmitAbsDifference(streamer, inst.Label, begin);
87 streamer.EmitIntValue(b2, 1);
89 case Win64EH::UOP_SetFPReg:
90 EmitAbsDifference(streamer, inst.Label, begin);
91 streamer.EmitIntValue(b2, 1);
93 case Win64EH::UOP_SaveNonVol:
94 case Win64EH::UOP_SaveXMM128:
95 b2 |= (inst.Register & 0x0F) << 4;
96 EmitAbsDifference(streamer, inst.Label, begin);
97 streamer.EmitIntValue(b2, 1);
99 if (inst.Operation == Win64EH::UOP_SaveXMM128)
101 streamer.EmitIntValue(w, 2);
103 case Win64EH::UOP_SaveNonVolBig:
104 case Win64EH::UOP_SaveXMM128Big:
105 b2 |= (inst.Register & 0x0F) << 4;
106 EmitAbsDifference(streamer, inst.Label, begin);
107 streamer.EmitIntValue(b2, 1);
108 if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
109 w = inst.Offset & 0xFFF0;
111 w = inst.Offset & 0xFFF8;
112 streamer.EmitIntValue(w, 2);
113 w = inst.Offset >> 16;
114 streamer.EmitIntValue(w, 2);
116 case Win64EH::UOP_PushMachFrame:
117 if (inst.Offset == 1)
119 EmitAbsDifference(streamer, inst.Label, begin);
120 streamer.EmitIntValue(b2, 1);
125 static void EmitSymbolRefWithOfs(MCStreamer &streamer,
126 const MCSymbol *Base,
127 const MCSymbol *Other) {
128 MCContext &Context = streamer.getContext();
129 const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::Create(Base, Context);
130 const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::Create(Other, Context);
131 const MCExpr *Ofs = MCBinaryExpr::CreateSub(OtherRef, BaseRef, Context);
132 const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::Create(Base,
133 MCSymbolRefExpr::VK_COFF_IMGREL32,
135 streamer.EmitValue(MCBinaryExpr::CreateAdd(BaseRefRel, Ofs, Context), 4);
138 static void EmitRuntimeFunction(MCStreamer &streamer,
139 const WinEH::FrameInfo *info) {
140 MCContext &context = streamer.getContext();
142 streamer.EmitValueToAlignment(4);
143 EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
144 EmitSymbolRefWithOfs(streamer, info->Function, info->End);
145 streamer.EmitValue(MCSymbolRefExpr::Create(info->Symbol,
146 MCSymbolRefExpr::VK_COFF_IMGREL32,
150 static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
151 // If this UNWIND_INFO already has a symbol, it's already been emitted.
155 MCContext &context = streamer.getContext();
156 MCSymbol *Label = context.CreateTempSymbol();
158 streamer.EmitValueToAlignment(4);
159 streamer.EmitLabel(Label);
160 info->Symbol = Label;
162 // Upper 3 bits are the version number (currently 1).
163 uint8_t flags = 0x01;
164 if (info->ChainedParent)
165 flags |= Win64EH::UNW_ChainInfo << 3;
167 if (info->HandlesUnwind)
168 flags |= Win64EH::UNW_TerminateHandler << 3;
169 if (info->HandlesExceptions)
170 flags |= Win64EH::UNW_ExceptionHandler << 3;
172 streamer.EmitIntValue(flags, 1);
175 EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
177 streamer.EmitIntValue(0, 1);
179 uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
180 streamer.EmitIntValue(numCodes, 1);
183 if (info->LastFrameInst >= 0) {
184 WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
185 assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
186 frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
188 streamer.EmitIntValue(frame, 1);
190 // Emit unwind instructions (in reverse order).
191 uint8_t numInst = info->Instructions.size();
192 for (uint8_t c = 0; c < numInst; ++c) {
193 WinEH::Instruction inst = info->Instructions.back();
194 info->Instructions.pop_back();
195 EmitUnwindCode(streamer, info->Begin, inst);
198 // For alignment purposes, the instruction array will always have an even
199 // number of entries, with the final entry potentially unused (in which case
200 // the array will be one longer than indicated by the count of unwind codes
203 streamer.EmitIntValue(0, 2);
206 if (flags & (Win64EH::UNW_ChainInfo << 3))
207 EmitRuntimeFunction(streamer, info->ChainedParent);
209 ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
210 streamer.EmitValue(MCSymbolRefExpr::Create(info->ExceptionHandler,
211 MCSymbolRefExpr::VK_COFF_IMGREL32,
213 else if (numCodes == 0) {
214 // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
215 // a chained unwind info, if there is no handler, and if there are fewer
216 // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
217 streamer.EmitIntValue(0, 4);
222 void UnwindEmitter::Emit(MCStreamer &Streamer) const {
223 MCContext &Context = Streamer.getContext();
225 // Emit the unwind info structs first.
226 for (const auto &CFI : Streamer.getWinFrameInfos()) {
227 const MCSection *XData =
228 getXDataSection(CFI->Function, Context);
229 Streamer.SwitchSection(XData);
230 EmitUnwindInfo(Streamer, CFI);
233 // Now emit RUNTIME_FUNCTION entries.
234 for (const auto &CFI : Streamer.getWinFrameInfos()) {
235 const MCSection *PData =
236 getPDataSection(CFI->Function, Context);
237 Streamer.SwitchSection(PData);
238 EmitRuntimeFunction(Streamer, CFI);
242 void UnwindEmitter::EmitUnwindInfo(MCStreamer &Streamer,
243 WinEH::FrameInfo *info) const {
244 // Switch sections (the static function above is meant to be called from
245 // here and from Emit().
246 MCContext &context = Streamer.getContext();
247 const MCSection *xdataSect =
248 getXDataSection(info->Function, context);
249 Streamer.SwitchSection(xdataSect);
251 llvm::EmitUnwindInfo(Streamer, info);
254 } // End of namespace llvm