1 //===- lib/MC/MCWin64EH.cpp - MCWin64EH implementation --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
19 
20 namespace llvm {
21 
22 // NOTE: All relocations generated here are 4-byte image-relative.
23 
CountOfUnwindCodes(std::vector<WinEH::Instruction> & Insns)24 static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
25   uint8_t Count = 0;
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:
32       Count += 1;
33       break;
34     case Win64EH::UOP_SaveNonVol:
35     case Win64EH::UOP_SaveXMM128:
36       Count += 2;
37       break;
38     case Win64EH::UOP_SaveNonVolBig:
39     case Win64EH::UOP_SaveXMM128Big:
40       Count += 3;
41       break;
42     case Win64EH::UOP_AllocLarge:
43       Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
44       break;
45     }
46   }
47   return Count;
48 }
49 
EmitAbsDifference(MCStreamer & Streamer,const MCSymbol * LHS,const MCSymbol * RHS)50 static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
51                               const MCSymbol *RHS) {
52   MCContext &Context = Streamer.getContext();
53   const MCExpr *Diff =
54       MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LHS, Context),
55                               MCSymbolRefExpr::Create(RHS, Context), Context);
56   Streamer.EmitValue(Diff, 1);
57 }
58 
EmitUnwindCode(MCStreamer & streamer,const MCSymbol * begin,WinEH::Instruction & inst)59 static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
60                            WinEH::Instruction &inst) {
61   uint8_t b2;
62   uint16_t w;
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);
69     break;
70   case Win64EH::UOP_AllocLarge:
71     EmitAbsDifference(streamer, inst.Label, begin);
72     if (inst.Offset > 512 * 1024 - 8) {
73       b2 |= 0x10;
74       streamer.EmitIntValue(b2, 1);
75       w = inst.Offset & 0xFFF8;
76       streamer.EmitIntValue(w, 2);
77       w = inst.Offset >> 16;
78     } else {
79       streamer.EmitIntValue(b2, 1);
80       w = inst.Offset >> 3;
81     }
82     streamer.EmitIntValue(w, 2);
83     break;
84   case Win64EH::UOP_AllocSmall:
85     b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
86     EmitAbsDifference(streamer, inst.Label, begin);
87     streamer.EmitIntValue(b2, 1);
88     break;
89   case Win64EH::UOP_SetFPReg:
90     EmitAbsDifference(streamer, inst.Label, begin);
91     streamer.EmitIntValue(b2, 1);
92     break;
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);
98     w = inst.Offset >> 3;
99     if (inst.Operation == Win64EH::UOP_SaveXMM128)
100       w >>= 1;
101     streamer.EmitIntValue(w, 2);
102     break;
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;
110     else
111       w = inst.Offset & 0xFFF8;
112     streamer.EmitIntValue(w, 2);
113     w = inst.Offset >> 16;
114     streamer.EmitIntValue(w, 2);
115     break;
116   case Win64EH::UOP_PushMachFrame:
117     if (inst.Offset == 1)
118       b2 |= 0x10;
119     EmitAbsDifference(streamer, inst.Label, begin);
120     streamer.EmitIntValue(b2, 1);
121     break;
122   }
123 }
124 
EmitSymbolRefWithOfs(MCStreamer & streamer,const MCSymbol * Base,const MCSymbol * Other)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,
134                                               Context);
135   streamer.EmitValue(MCBinaryExpr::CreateAdd(BaseRefRel, Ofs, Context), 4);
136 }
137 
EmitRuntimeFunction(MCStreamer & streamer,const WinEH::FrameInfo * info)138 static void EmitRuntimeFunction(MCStreamer &streamer,
139                                 const WinEH::FrameInfo *info) {
140   MCContext &context = streamer.getContext();
141 
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,
147                                              context), 4);
148 }
149 
EmitUnwindInfo(MCStreamer & streamer,WinEH::FrameInfo * info)150 static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
151   // If this UNWIND_INFO already has a symbol, it's already been emitted.
152   if (info->Symbol)
153     return;
154 
155   MCContext &context = streamer.getContext();
156   MCSymbol *Label = context.CreateTempSymbol();
157 
158   streamer.EmitValueToAlignment(4);
159   streamer.EmitLabel(Label);
160   info->Symbol = Label;
161 
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;
166   else {
167     if (info->HandlesUnwind)
168       flags |= Win64EH::UNW_TerminateHandler << 3;
169     if (info->HandlesExceptions)
170       flags |= Win64EH::UNW_ExceptionHandler << 3;
171   }
172   streamer.EmitIntValue(flags, 1);
173 
174   if (info->PrologEnd)
175     EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
176   else
177     streamer.EmitIntValue(0, 1);
178 
179   uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
180   streamer.EmitIntValue(numCodes, 1);
181 
182   uint8_t frame = 0;
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);
187   }
188   streamer.EmitIntValue(frame, 1);
189 
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);
196   }
197 
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
201   // field).
202   if (numCodes & 1) {
203     streamer.EmitIntValue(0, 2);
204   }
205 
206   if (flags & (Win64EH::UNW_ChainInfo << 3))
207     EmitRuntimeFunction(streamer, info->ChainedParent);
208   else if (flags &
209            ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
210     streamer.EmitValue(MCSymbolRefExpr::Create(info->ExceptionHandler,
211                                               MCSymbolRefExpr::VK_COFF_IMGREL32,
212                                               context), 4);
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);
218   }
219 }
220 
221 namespace Win64EH {
Emit(MCStreamer & Streamer) const222 void UnwindEmitter::Emit(MCStreamer &Streamer) const {
223   MCContext &Context = Streamer.getContext();
224 
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);
231   }
232 
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);
239   }
240 }
241 
EmitUnwindInfo(MCStreamer & Streamer,WinEH::FrameInfo * info) const242 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);
250 
251   llvm::EmitUnwindInfo(Streamer, info);
252 }
253 }
254 } // End of namespace llvm
255 
256