blob: 198c775110e9fe76da85e7ceddb2fdd0fb2a9791 [file] [log] [blame]
Charles Davis1c8bd5a2011-05-22 03:01:05 +00001//===- 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"
Evan Cheng76792992011-07-20 05:58:47 +000011#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#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"
Charles Davis1c8bd5a2011-05-22 03:01:05 +000018
19namespace llvm {
20
21// NOTE: All relocations generated here are 4-byte image-relative.
22
Saleem Abdulrasool1e76cbd2014-07-10 04:50:09 +000023static uint8_t CountOfUnwindCodes(std::vector<MCWin64EHInstruction> &Insns) {
24 uint8_t Count = 0;
25 for (const auto &I : Insns) {
26 switch (I.getOperation()) {
Charles Davis1c8bd5a2011-05-22 03:01:05 +000027 case Win64EH::UOP_PushNonVol:
28 case Win64EH::UOP_AllocSmall:
29 case Win64EH::UOP_SetFPReg:
30 case Win64EH::UOP_PushMachFrame:
Saleem Abdulrasool1e76cbd2014-07-10 04:50:09 +000031 Count += 1;
Charles Davis761313b2011-05-27 02:43:19 +000032 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000033 case Win64EH::UOP_SaveNonVol:
34 case Win64EH::UOP_SaveXMM128:
Saleem Abdulrasool1e76cbd2014-07-10 04:50:09 +000035 Count += 2;
Charles Davis761313b2011-05-27 02:43:19 +000036 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000037 case Win64EH::UOP_SaveNonVolBig:
38 case Win64EH::UOP_SaveXMM128Big:
Saleem Abdulrasool1e76cbd2014-07-10 04:50:09 +000039 Count += 3;
Charles Davis761313b2011-05-27 02:43:19 +000040 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000041 case Win64EH::UOP_AllocLarge:
Saleem Abdulrasool1e76cbd2014-07-10 04:50:09 +000042 Count += (I.getSize() > 512 * 1024 - 8) ? 3 : 2;
Charles Davis761313b2011-05-27 02:43:19 +000043 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000044 }
45 }
Saleem Abdulrasool1e76cbd2014-07-10 04:50:09 +000046 return Count;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000047}
48
Saleem Abdulrasool5705cd82014-07-13 19:03:40 +000049static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
50 const MCSymbol *RHS) {
51 MCContext &Context = Streamer.getContext();
52 const MCExpr *Diff =
53 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LHS, Context),
54 MCSymbolRefExpr::Create(RHS, Context), Context);
55 Streamer.EmitAbsValue(Diff, 1);
Charles Davis6d1c4c72011-05-27 03:25:01 +000056}
57
58static void EmitUnwindCode(MCStreamer &streamer, MCSymbol *begin,
59 MCWin64EHInstruction &inst) {
Kai Nacke1b7e4862013-08-27 04:16:16 +000060 uint8_t b2;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000061 uint16_t w;
Charles Davisea5dc3a2011-05-27 15:10:25 +000062 b2 = (inst.getOperation() & 0x0F);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000063 switch (inst.getOperation()) {
64 case Win64EH::UOP_PushNonVol:
Charles Davis6d1c4c72011-05-27 03:25:01 +000065 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davisea5dc3a2011-05-27 15:10:25 +000066 b2 |= (inst.getRegister() & 0x0F) << 4;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000067 streamer.EmitIntValue(b2, 1);
68 break;
69 case Win64EH::UOP_AllocLarge:
Charles Davis6d1c4c72011-05-27 03:25:01 +000070 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000071 if (inst.getSize() > 512*1024-8) {
Charles Davisea5dc3a2011-05-27 15:10:25 +000072 b2 |= 0x10;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000073 streamer.EmitIntValue(b2, 1);
74 w = inst.getSize() & 0xFFF8;
75 streamer.EmitIntValue(w, 2);
76 w = inst.getSize() >> 16;
77 } else {
78 streamer.EmitIntValue(b2, 1);
79 w = inst.getSize() >> 3;
80 }
81 streamer.EmitIntValue(w, 2);
82 break;
83 case Win64EH::UOP_AllocSmall:
Charles Davisea5dc3a2011-05-27 15:10:25 +000084 b2 |= (((inst.getSize()-8) >> 3) & 0x0F) << 4;
Charles Davis6d1c4c72011-05-27 03:25:01 +000085 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000086 streamer.EmitIntValue(b2, 1);
87 break;
88 case Win64EH::UOP_SetFPReg:
Kai Nacke1b7e4862013-08-27 04:16:16 +000089 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000090 streamer.EmitIntValue(b2, 1);
91 break;
92 case Win64EH::UOP_SaveNonVol:
93 case Win64EH::UOP_SaveXMM128:
Charles Davisea5dc3a2011-05-27 15:10:25 +000094 b2 |= (inst.getRegister() & 0x0F) << 4;
Charles Davis6d1c4c72011-05-27 03:25:01 +000095 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000096 streamer.EmitIntValue(b2, 1);
97 w = inst.getOffset() >> 3;
98 if (inst.getOperation() == Win64EH::UOP_SaveXMM128)
99 w >>= 1;
100 streamer.EmitIntValue(w, 2);
101 break;
102 case Win64EH::UOP_SaveNonVolBig:
103 case Win64EH::UOP_SaveXMM128Big:
Charles Davisea5dc3a2011-05-27 15:10:25 +0000104 b2 |= (inst.getRegister() & 0x0F) << 4;
Charles Davis6d1c4c72011-05-27 03:25:01 +0000105 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000106 streamer.EmitIntValue(b2, 1);
107 if (inst.getOperation() == Win64EH::UOP_SaveXMM128Big)
108 w = inst.getOffset() & 0xFFF0;
109 else
110 w = inst.getOffset() & 0xFFF8;
111 streamer.EmitIntValue(w, 2);
112 w = inst.getOffset() >> 16;
113 streamer.EmitIntValue(w, 2);
114 break;
115 case Win64EH::UOP_PushMachFrame:
116 if (inst.isPushCodeFrame())
Charles Davisea5dc3a2011-05-27 15:10:25 +0000117 b2 |= 0x10;
Charles Davis6d1c4c72011-05-27 03:25:01 +0000118 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000119 streamer.EmitIntValue(b2, 1);
120 break;
121 }
122}
123
Kai Nacke74adc8a2013-09-15 17:46:46 +0000124static void EmitSymbolRefWithOfs(MCStreamer &streamer,
125 const MCSymbol *Base,
126 const MCSymbol *Other) {
127 MCContext &Context = streamer.getContext();
128 const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::Create(Base, Context);
129 const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::Create(Other, Context);
130 const MCExpr *Ofs = MCBinaryExpr::CreateSub(OtherRef, BaseRef, Context);
131 const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::Create(Base,
132 MCSymbolRefExpr::VK_COFF_IMGREL32,
133 Context);
134 streamer.EmitValue(MCBinaryExpr::CreateAdd(BaseRefRel, Ofs, Context), 4);
135}
136
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000137static void EmitRuntimeFunction(MCStreamer &streamer,
Saleem Abdulrasool4a1a2f72014-07-12 20:49:09 +0000138 const MCWinFrameInfo *info) {
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000139 MCContext &context = streamer.getContext();
140
Charles Davis761313b2011-05-27 02:43:19 +0000141 streamer.EmitValueToAlignment(4);
Kai Nacke74adc8a2013-09-15 17:46:46 +0000142 EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
143 EmitSymbolRefWithOfs(streamer, info->Function, info->End);
144 streamer.EmitValue(MCSymbolRefExpr::Create(info->Symbol,
145 MCSymbolRefExpr::VK_COFF_IMGREL32,
146 context), 4);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000147}
148
Saleem Abdulrasool4a1a2f72014-07-12 20:49:09 +0000149static void EmitUnwindInfo(MCStreamer &streamer, MCWinFrameInfo *info) {
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000150 // If this UNWIND_INFO already has a symbol, it's already been emitted.
151 if (info->Symbol) return;
152
153 MCContext &context = streamer.getContext();
Charles Davis761313b2011-05-27 02:43:19 +0000154 streamer.EmitValueToAlignment(4);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000155 info->Symbol = context.CreateTempSymbol();
156 streamer.EmitLabel(info->Symbol);
157
Kai Nacke8539b462013-09-15 18:01:09 +0000158 // Upper 3 bits are the version number (currently 1).
159 uint8_t flags = 0x01;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000160 if (info->ChainedParent)
Charles Davisea5dc3a2011-05-27 15:10:25 +0000161 flags |= Win64EH::UNW_ChainInfo << 3;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000162 else {
163 if (info->HandlesUnwind)
Charles Davisea5dc3a2011-05-27 15:10:25 +0000164 flags |= Win64EH::UNW_TerminateHandler << 3;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000165 if (info->HandlesExceptions)
Charles Davisea5dc3a2011-05-27 15:10:25 +0000166 flags |= Win64EH::UNW_ExceptionHandler << 3;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000167 }
168 streamer.EmitIntValue(flags, 1);
169
Charles Davisea5dc3a2011-05-27 15:10:25 +0000170 if (info->PrologEnd)
171 EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
172 else
173 streamer.EmitIntValue(0, 1);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000174
175 uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
176 streamer.EmitIntValue(numCodes, 1);
177
178 uint8_t frame = 0;
179 if (info->LastFrameInst >= 0) {
180 MCWin64EHInstruction &frameInst = info->Instructions[info->LastFrameInst];
181 assert(frameInst.getOperation() == Win64EH::UOP_SetFPReg);
Charles Davisea5dc3a2011-05-27 15:10:25 +0000182 frame = (frameInst.getRegister() & 0x0F) |
183 (frameInst.getOffset() & 0xF0);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000184 }
185 streamer.EmitIntValue(frame, 1);
186
187 // Emit unwind instructions (in reverse order).
188 uint8_t numInst = info->Instructions.size();
189 for (uint8_t c = 0; c < numInst; ++c) {
190 MCWin64EHInstruction inst = info->Instructions.back();
191 info->Instructions.pop_back();
Charles Davis6d1c4c72011-05-27 03:25:01 +0000192 EmitUnwindCode(streamer, info->Begin, inst);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000193 }
194
Kai Nacke8539b462013-09-15 18:01:09 +0000195 // For alignment purposes, the instruction array will always have an even
196 // number of entries, with the final entry potentially unused (in which case
197 // the array will be one longer than indicated by the count of unwind codes
198 // field).
199 if (numCodes & 1) {
200 streamer.EmitIntValue(0, 2);
201 }
202
Charles Davisea5dc3a2011-05-27 15:10:25 +0000203 if (flags & (Win64EH::UNW_ChainInfo << 3))
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000204 EmitRuntimeFunction(streamer, info->ChainedParent);
Charles Davisea5dc3a2011-05-27 15:10:25 +0000205 else if (flags &
206 ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
Kai Nacke74adc8a2013-09-15 17:46:46 +0000207 streamer.EmitValue(MCSymbolRefExpr::Create(info->ExceptionHandler,
208 MCSymbolRefExpr::VK_COFF_IMGREL32,
209 context), 4);
Kai Nacke8539b462013-09-15 18:01:09 +0000210 else if (numCodes == 0) {
Charles Davisea5dc3a2011-05-27 15:10:25 +0000211 // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
212 // a chained unwind info, if there is no handler, and if there are fewer
213 // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
Kai Nacke8539b462013-09-15 18:01:09 +0000214 streamer.EmitIntValue(0, 4);
Charles Davisea5dc3a2011-05-27 15:10:25 +0000215 }
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000216}
217
Charles Davis03eef622011-05-27 19:09:24 +0000218StringRef MCWin64EHUnwindEmitter::GetSectionSuffix(const MCSymbol *func) {
219 if (!func || !func->isInSection()) return "";
220 const MCSection *section = &func->getSection();
221 const MCSectionCOFF *COFFSection;
222 if ((COFFSection = dyn_cast<MCSectionCOFF>(section))) {
223 StringRef name = COFFSection->getSectionName();
224 size_t dollar = name.find('$');
225 size_t dot = name.find('.', 1);
226 if (dollar == StringRef::npos && dot == StringRef::npos)
227 return "";
228 if (dot == StringRef::npos)
229 return name.substr(dollar);
230 if (dollar == StringRef::npos || dot < dollar)
231 return name.substr(dot);
232 return name.substr(dollar);
233 }
234 return "";
235}
236
Evan Cheng76792992011-07-20 05:58:47 +0000237static const MCSection *getWin64EHTableSection(StringRef suffix,
238 MCContext &context) {
239 if (suffix == "")
240 return context.getObjectFileInfo()->getXDataSection();
241
242 return context.getCOFFSection((".xdata"+suffix).str(),
243 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
Anton Korobeynikovcc8c5392012-08-08 12:46:46 +0000244 COFF::IMAGE_SCN_MEM_READ,
Evan Cheng76792992011-07-20 05:58:47 +0000245 SectionKind::getDataRel());
246}
247
248static const MCSection *getWin64EHFuncTableSection(StringRef suffix,
249 MCContext &context) {
250 if (suffix == "")
251 return context.getObjectFileInfo()->getPDataSection();
252 return context.getCOFFSection((".pdata"+suffix).str(),
253 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
Anton Korobeynikovcc8c5392012-08-08 12:46:46 +0000254 COFF::IMAGE_SCN_MEM_READ,
Evan Cheng76792992011-07-20 05:58:47 +0000255 SectionKind::getDataRel());
256}
257
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000258void MCWin64EHUnwindEmitter::EmitUnwindInfo(MCStreamer &streamer,
Saleem Abdulrasool4a1a2f72014-07-12 20:49:09 +0000259 MCWinFrameInfo *info) {
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000260 // Switch sections (the static function above is meant to be called from
261 // here and from Emit().
262 MCContext &context = streamer.getContext();
Charles Davis03eef622011-05-27 19:09:24 +0000263 const MCSection *xdataSect =
Evan Cheng76792992011-07-20 05:58:47 +0000264 getWin64EHTableSection(GetSectionSuffix(info->Function), context);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000265 streamer.SwitchSection(xdataSect);
266
267 llvm::EmitUnwindInfo(streamer, info);
268}
269
Saleem Abdulrasool427c08d2014-07-10 04:50:06 +0000270void MCWin64EHUnwindEmitter::Emit(MCStreamer &Streamer) {
271 MCContext &Context = Streamer.getContext();
272
Charles Davisbc2daa02011-05-22 04:15:07 +0000273 // Emit the unwind info structs first.
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +0000274 for (const auto &CFI : Streamer.getWinFrameInfos()) {
Saleem Abdulrasool427c08d2014-07-10 04:50:06 +0000275 const MCSection *XData =
276 getWin64EHTableSection(GetSectionSuffix(CFI->Function), Context);
277 Streamer.SwitchSection(XData);
278 EmitUnwindInfo(Streamer, CFI);
Charles Davis03eef622011-05-27 19:09:24 +0000279 }
Saleem Abdulrasool427c08d2014-07-10 04:50:06 +0000280
Charles Davisbc2daa02011-05-22 04:15:07 +0000281 // Now emit RUNTIME_FUNCTION entries.
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +0000282 for (const auto &CFI : Streamer.getWinFrameInfos()) {
Saleem Abdulrasool427c08d2014-07-10 04:50:06 +0000283 const MCSection *PData =
284 getWin64EHFuncTableSection(GetSectionSuffix(CFI->Function), Context);
285 Streamer.SwitchSection(PData);
286 EmitRuntimeFunction(Streamer, CFI);
Charles Davis03eef622011-05-27 19:09:24 +0000287 }
Charles Davisbc2daa02011-05-22 04:15:07 +0000288}
289
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000290} // End of namespace llvm
291