]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/MC/MCWin64EH.cpp
Upgrade Unbound to 1.6.1. More to follow.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / MC / MCWin64EH.cpp
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/MCStreamer.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/Win64EH.h"
17
18 using namespace llvm;
19
20 // NOTE: All relocations generated here are 4-byte image-relative.
21
22 static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
23   uint8_t Count = 0;
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:
30       Count += 1;
31       break;
32     case Win64EH::UOP_SaveNonVol:
33     case Win64EH::UOP_SaveXMM128:
34       Count += 2;
35       break;
36     case Win64EH::UOP_SaveNonVolBig:
37     case Win64EH::UOP_SaveXMM128Big:
38       Count += 3;
39       break;
40     case Win64EH::UOP_AllocLarge:
41       Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
42       break;
43     }
44   }
45   return Count;
46 }
47
48 static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
49                               const MCSymbol *RHS) {
50   MCContext &Context = Streamer.getContext();
51   const MCExpr *Diff =
52       MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
53                               MCSymbolRefExpr::create(RHS, Context), Context);
54   Streamer.EmitValue(Diff, 1);
55 }
56
57 static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
58                            WinEH::Instruction &inst) {
59   uint8_t b2;
60   uint16_t w;
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);
67     break;
68   case Win64EH::UOP_AllocLarge:
69     EmitAbsDifference(streamer, inst.Label, begin);
70     if (inst.Offset > 512 * 1024 - 8) {
71       b2 |= 0x10;
72       streamer.EmitIntValue(b2, 1);
73       w = inst.Offset & 0xFFF8;
74       streamer.EmitIntValue(w, 2);
75       w = inst.Offset >> 16;
76     } else {
77       streamer.EmitIntValue(b2, 1);
78       w = inst.Offset >> 3;
79     }
80     streamer.EmitIntValue(w, 2);
81     break;
82   case Win64EH::UOP_AllocSmall:
83     b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
84     EmitAbsDifference(streamer, inst.Label, begin);
85     streamer.EmitIntValue(b2, 1);
86     break;
87   case Win64EH::UOP_SetFPReg:
88     EmitAbsDifference(streamer, inst.Label, begin);
89     streamer.EmitIntValue(b2, 1);
90     break;
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);
96     w = inst.Offset >> 3;
97     if (inst.Operation == Win64EH::UOP_SaveXMM128)
98       w >>= 1;
99     streamer.EmitIntValue(w, 2);
100     break;
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;
108     else
109       w = inst.Offset & 0xFFF8;
110     streamer.EmitIntValue(w, 2);
111     w = inst.Offset >> 16;
112     streamer.EmitIntValue(w, 2);
113     break;
114   case Win64EH::UOP_PushMachFrame:
115     if (inst.Offset == 1)
116       b2 |= 0x10;
117     EmitAbsDifference(streamer, inst.Label, begin);
118     streamer.EmitIntValue(b2, 1);
119     break;
120   }
121 }
122
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,
132                                               Context);
133   streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
134 }
135
136 static void EmitRuntimeFunction(MCStreamer &streamer,
137                                 const WinEH::FrameInfo *info) {
138   MCContext &context = streamer.getContext();
139
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,
145                                              context), 4);
146 }
147
148 static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
149   // If this UNWIND_INFO already has a symbol, it's already been emitted.
150   if (info->Symbol)
151     return;
152
153   MCContext &context = streamer.getContext();
154   MCSymbol *Label = context.createTempSymbol();
155
156   streamer.EmitValueToAlignment(4);
157   streamer.EmitLabel(Label);
158   info->Symbol = Label;
159
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;
164   else {
165     if (info->HandlesUnwind)
166       flags |= Win64EH::UNW_TerminateHandler << 3;
167     if (info->HandlesExceptions)
168       flags |= Win64EH::UNW_ExceptionHandler << 3;
169   }
170   streamer.EmitIntValue(flags, 1);
171
172   if (info->PrologEnd)
173     EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
174   else
175     streamer.EmitIntValue(0, 1);
176
177   uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
178   streamer.EmitIntValue(numCodes, 1);
179
180   uint8_t frame = 0;
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);
185   }
186   streamer.EmitIntValue(frame, 1);
187
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);
194   }
195
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
199   // field).
200   if (numCodes & 1) {
201     streamer.EmitIntValue(0, 2);
202   }
203
204   if (flags & (Win64EH::UNW_ChainInfo << 3))
205     EmitRuntimeFunction(streamer, info->ChainedParent);
206   else if (flags &
207            ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
208     streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
209                                               MCSymbolRefExpr::VK_COFF_IMGREL32,
210                                               context), 4);
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);
216   }
217 }
218
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());
225   }
226
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());
232   }
233 }
234
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);
241
242   ::EmitUnwindInfo(Streamer, info);
243 }
244