blob: b8b07d3a180875f6bfd101c0bf576fedb353ffc2 [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
23static uint8_t CountOfUnwindCodes(std::vector<MCWin64EHInstruction> &instArray){
24 uint8_t count = 0;
25 for (std::vector<MCWin64EHInstruction>::const_iterator I = instArray.begin(),
26 E = instArray.end(); I != E; ++I) {
27 switch (I->getOperation()) {
28 case Win64EH::UOP_PushNonVol:
29 case Win64EH::UOP_AllocSmall:
30 case Win64EH::UOP_SetFPReg:
31 case Win64EH::UOP_PushMachFrame:
32 count += 1;
Charles Davis761313b2011-05-27 02:43:19 +000033 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000034 case Win64EH::UOP_SaveNonVol:
35 case Win64EH::UOP_SaveXMM128:
36 count += 2;
Charles Davis761313b2011-05-27 02:43:19 +000037 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000038 case Win64EH::UOP_SaveNonVolBig:
39 case Win64EH::UOP_SaveXMM128Big:
40 count += 3;
Charles Davis761313b2011-05-27 02:43:19 +000041 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000042 case Win64EH::UOP_AllocLarge:
43 if (I->getSize() > 512*1024-8)
44 count += 3;
45 else
46 count += 2;
Charles Davis761313b2011-05-27 02:43:19 +000047 break;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000048 }
49 }
50 return count;
51}
52
Charles Davis6d1c4c72011-05-27 03:25:01 +000053static void EmitAbsDifference(MCStreamer &streamer, MCSymbol *lhs,
54 MCSymbol *rhs) {
55 MCContext &context = streamer.getContext();
56 const MCExpr *diff = MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(
57 lhs, context),
58 MCSymbolRefExpr::Create(
59 rhs, context),
60 context);
61 streamer.EmitAbsValue(diff, 1);
62
63}
64
65static void EmitUnwindCode(MCStreamer &streamer, MCSymbol *begin,
66 MCWin64EHInstruction &inst) {
Kai Nacke66bfdb82013-07-06 17:15:36 +000067 uint8_t b2;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000068 uint16_t w;
Charles Davisea5dc3a2011-05-27 15:10:25 +000069 b2 = (inst.getOperation() & 0x0F);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000070 switch (inst.getOperation()) {
71 case Win64EH::UOP_PushNonVol:
Charles Davis6d1c4c72011-05-27 03:25:01 +000072 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davisea5dc3a2011-05-27 15:10:25 +000073 b2 |= (inst.getRegister() & 0x0F) << 4;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000074 streamer.EmitIntValue(b2, 1);
75 break;
76 case Win64EH::UOP_AllocLarge:
Charles Davis6d1c4c72011-05-27 03:25:01 +000077 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000078 if (inst.getSize() > 512*1024-8) {
Charles Davisea5dc3a2011-05-27 15:10:25 +000079 b2 |= 0x10;
Charles Davis1c8bd5a2011-05-22 03:01:05 +000080 streamer.EmitIntValue(b2, 1);
81 w = inst.getSize() & 0xFFF8;
82 streamer.EmitIntValue(w, 2);
83 w = inst.getSize() >> 16;
84 } else {
85 streamer.EmitIntValue(b2, 1);
86 w = inst.getSize() >> 3;
87 }
88 streamer.EmitIntValue(w, 2);
89 break;
90 case Win64EH::UOP_AllocSmall:
Charles Davisea5dc3a2011-05-27 15:10:25 +000091 b2 |= (((inst.getSize()-8) >> 3) & 0x0F) << 4;
Charles Davis6d1c4c72011-05-27 03:25:01 +000092 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000093 streamer.EmitIntValue(b2, 1);
94 break;
95 case Win64EH::UOP_SetFPReg:
Kai Nacke66bfdb82013-07-06 17:15:36 +000096 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +000097 streamer.EmitIntValue(b2, 1);
98 break;
99 case Win64EH::UOP_SaveNonVol:
100 case Win64EH::UOP_SaveXMM128:
Charles Davisea5dc3a2011-05-27 15:10:25 +0000101 b2 |= (inst.getRegister() & 0x0F) << 4;
Charles Davis6d1c4c72011-05-27 03:25:01 +0000102 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000103 streamer.EmitIntValue(b2, 1);
104 w = inst.getOffset() >> 3;
105 if (inst.getOperation() == Win64EH::UOP_SaveXMM128)
106 w >>= 1;
107 streamer.EmitIntValue(w, 2);
108 break;
109 case Win64EH::UOP_SaveNonVolBig:
110 case Win64EH::UOP_SaveXMM128Big:
Charles Davisea5dc3a2011-05-27 15:10:25 +0000111 b2 |= (inst.getRegister() & 0x0F) << 4;
Charles Davis6d1c4c72011-05-27 03:25:01 +0000112 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000113 streamer.EmitIntValue(b2, 1);
114 if (inst.getOperation() == Win64EH::UOP_SaveXMM128Big)
115 w = inst.getOffset() & 0xFFF0;
116 else
117 w = inst.getOffset() & 0xFFF8;
118 streamer.EmitIntValue(w, 2);
119 w = inst.getOffset() >> 16;
120 streamer.EmitIntValue(w, 2);
121 break;
122 case Win64EH::UOP_PushMachFrame:
123 if (inst.isPushCodeFrame())
Charles Davisea5dc3a2011-05-27 15:10:25 +0000124 b2 |= 0x10;
Charles Davis6d1c4c72011-05-27 03:25:01 +0000125 EmitAbsDifference(streamer, inst.getLabel(), begin);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000126 streamer.EmitIntValue(b2, 1);
127 break;
128 }
129}
130
Kai Nacke2a933a62013-07-06 17:16:12 +0000131static void EmitSymbolRefWithOfs(MCStreamer &streamer,
132 const MCSymbol *Base,
133 const MCSymbol *Other) {
134 MCContext &Context = streamer.getContext();
135 const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::Create(Base, Context);
136 const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::Create(Other, Context);
137 const MCExpr *Ofs = MCBinaryExpr::CreateSub(OtherRef, BaseRef, Context);
138 const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::Create(Base,
139 MCSymbolRefExpr::VK_COFF_IMGREL32,
140 Context);
141 streamer.EmitValue(MCBinaryExpr::CreateAdd(BaseRefRel, Ofs, Context), 4);
142}
143
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000144static void EmitRuntimeFunction(MCStreamer &streamer,
Charles Davisbc2daa02011-05-22 04:15:07 +0000145 const MCWin64EHUnwindInfo *info) {
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000146 MCContext &context = streamer.getContext();
147
Charles Davis761313b2011-05-27 02:43:19 +0000148 streamer.EmitValueToAlignment(4);
Kai Nacke2a933a62013-07-06 17:16:12 +0000149 EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
150 EmitSymbolRefWithOfs(streamer, info->Function, info->End);
151 streamer.EmitValue(MCSymbolRefExpr::Create(info->Symbol,
152 MCSymbolRefExpr::VK_COFF_IMGREL32,
153 context), 4);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000154}
155
156static void EmitUnwindInfo(MCStreamer &streamer, MCWin64EHUnwindInfo *info) {
157 // If this UNWIND_INFO already has a symbol, it's already been emitted.
158 if (info->Symbol) return;
159
160 MCContext &context = streamer.getContext();
Charles Davis761313b2011-05-27 02:43:19 +0000161 streamer.EmitValueToAlignment(4);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000162 info->Symbol = context.CreateTempSymbol();
163 streamer.EmitLabel(info->Symbol);
164
Kai Nacke4417ccc2013-07-06 17:16:50 +0000165 // Upper 3 bits are the version number (currently 1).
166 uint8_t flags = 0x01;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000167 if (info->ChainedParent)
Charles Davisea5dc3a2011-05-27 15:10:25 +0000168 flags |= Win64EH::UNW_ChainInfo << 3;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000169 else {
170 if (info->HandlesUnwind)
Charles Davisea5dc3a2011-05-27 15:10:25 +0000171 flags |= Win64EH::UNW_TerminateHandler << 3;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000172 if (info->HandlesExceptions)
Charles Davisea5dc3a2011-05-27 15:10:25 +0000173 flags |= Win64EH::UNW_ExceptionHandler << 3;
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000174 }
175 streamer.EmitIntValue(flags, 1);
176
Charles Davisea5dc3a2011-05-27 15:10:25 +0000177 if (info->PrologEnd)
178 EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
179 else
180 streamer.EmitIntValue(0, 1);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000181
182 uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
183 streamer.EmitIntValue(numCodes, 1);
184
185 uint8_t frame = 0;
186 if (info->LastFrameInst >= 0) {
187 MCWin64EHInstruction &frameInst = info->Instructions[info->LastFrameInst];
188 assert(frameInst.getOperation() == Win64EH::UOP_SetFPReg);
Charles Davisea5dc3a2011-05-27 15:10:25 +0000189 frame = (frameInst.getRegister() & 0x0F) |
190 (frameInst.getOffset() & 0xF0);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000191 }
192 streamer.EmitIntValue(frame, 1);
193
194 // Emit unwind instructions (in reverse order).
195 uint8_t numInst = info->Instructions.size();
196 for (uint8_t c = 0; c < numInst; ++c) {
197 MCWin64EHInstruction inst = info->Instructions.back();
198 info->Instructions.pop_back();
Charles Davis6d1c4c72011-05-27 03:25:01 +0000199 EmitUnwindCode(streamer, info->Begin, inst);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000200 }
201
Kai Nacke4417ccc2013-07-06 17:16:50 +0000202 // For alignment purposes, the instruction array will always have an even
203 // number of entries, with the final entry potentially unused (in which case
204 // the array will be one longer than indicated by the count of unwind codes
205 // field).
206 if (numCodes & 1) {
207 streamer.EmitIntValue(0, 2);
208 }
209
Charles Davisea5dc3a2011-05-27 15:10:25 +0000210 if (flags & (Win64EH::UNW_ChainInfo << 3))
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000211 EmitRuntimeFunction(streamer, info->ChainedParent);
Charles Davisea5dc3a2011-05-27 15:10:25 +0000212 else if (flags &
213 ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
Kai Nacke2a933a62013-07-06 17:16:12 +0000214 streamer.EmitValue(MCSymbolRefExpr::Create(info->ExceptionHandler,
215 MCSymbolRefExpr::VK_COFF_IMGREL32,
216 context), 4);
Kai Nacke4417ccc2013-07-06 17:16:50 +0000217 else if (numCodes == 0) {
Charles Davisea5dc3a2011-05-27 15:10:25 +0000218 // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
219 // a chained unwind info, if there is no handler, and if there are fewer
220 // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
Kai Nacke4417ccc2013-07-06 17:16:50 +0000221 streamer.EmitIntValue(0, 4);
Charles Davisea5dc3a2011-05-27 15:10:25 +0000222 }
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000223}
224
Charles Davis03eef622011-05-27 19:09:24 +0000225StringRef MCWin64EHUnwindEmitter::GetSectionSuffix(const MCSymbol *func) {
226 if (!func || !func->isInSection()) return "";
227 const MCSection *section = &func->getSection();
228 const MCSectionCOFF *COFFSection;
229 if ((COFFSection = dyn_cast<MCSectionCOFF>(section))) {
230 StringRef name = COFFSection->getSectionName();
231 size_t dollar = name.find('$');
232 size_t dot = name.find('.', 1);
233 if (dollar == StringRef::npos && dot == StringRef::npos)
234 return "";
235 if (dot == StringRef::npos)
236 return name.substr(dollar);
237 if (dollar == StringRef::npos || dot < dollar)
238 return name.substr(dot);
239 return name.substr(dollar);
240 }
241 return "";
242}
243
Evan Cheng76792992011-07-20 05:58:47 +0000244static const MCSection *getWin64EHTableSection(StringRef suffix,
245 MCContext &context) {
246 if (suffix == "")
247 return context.getObjectFileInfo()->getXDataSection();
248
249 return context.getCOFFSection((".xdata"+suffix).str(),
250 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
Anton Korobeynikovcc8c5392012-08-08 12:46:46 +0000251 COFF::IMAGE_SCN_MEM_READ,
Evan Cheng76792992011-07-20 05:58:47 +0000252 SectionKind::getDataRel());
253}
254
255static const MCSection *getWin64EHFuncTableSection(StringRef suffix,
256 MCContext &context) {
257 if (suffix == "")
258 return context.getObjectFileInfo()->getPDataSection();
259 return context.getCOFFSection((".pdata"+suffix).str(),
260 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
Anton Korobeynikovcc8c5392012-08-08 12:46:46 +0000261 COFF::IMAGE_SCN_MEM_READ,
Evan Cheng76792992011-07-20 05:58:47 +0000262 SectionKind::getDataRel());
263}
264
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000265void MCWin64EHUnwindEmitter::EmitUnwindInfo(MCStreamer &streamer,
266 MCWin64EHUnwindInfo *info) {
267 // Switch sections (the static function above is meant to be called from
268 // here and from Emit().
269 MCContext &context = streamer.getContext();
Charles Davis03eef622011-05-27 19:09:24 +0000270 const MCSection *xdataSect =
Evan Cheng76792992011-07-20 05:58:47 +0000271 getWin64EHTableSection(GetSectionSuffix(info->Function), context);
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000272 streamer.SwitchSection(xdataSect);
273
274 llvm::EmitUnwindInfo(streamer, info);
275}
276
Charles Davisbc2daa02011-05-22 04:15:07 +0000277void MCWin64EHUnwindEmitter::Emit(MCStreamer &streamer) {
278 MCContext &context = streamer.getContext();
279 // Emit the unwind info structs first.
Charles Davis03eef622011-05-27 19:09:24 +0000280 for (unsigned i = 0; i < streamer.getNumW64UnwindInfos(); ++i) {
281 MCWin64EHUnwindInfo &info = streamer.getW64UnwindInfo(i);
282 const MCSection *xdataSect =
Evan Cheng76792992011-07-20 05:58:47 +0000283 getWin64EHTableSection(GetSectionSuffix(info.Function), context);
Charles Davis03eef622011-05-27 19:09:24 +0000284 streamer.SwitchSection(xdataSect);
285 llvm::EmitUnwindInfo(streamer, &info);
286 }
Charles Davisbc2daa02011-05-22 04:15:07 +0000287 // Now emit RUNTIME_FUNCTION entries.
Charles Davis03eef622011-05-27 19:09:24 +0000288 for (unsigned i = 0; i < streamer.getNumW64UnwindInfos(); ++i) {
289 MCWin64EHUnwindInfo &info = streamer.getW64UnwindInfo(i);
290 const MCSection *pdataSect =
Evan Cheng76792992011-07-20 05:58:47 +0000291 getWin64EHFuncTableSection(GetSectionSuffix(info.Function), context);
Charles Davis03eef622011-05-27 19:09:24 +0000292 streamer.SwitchSection(pdataSect);
293 EmitRuntimeFunction(streamer, &info);
294 }
Charles Davisbc2daa02011-05-22 04:15:07 +0000295}
296
Charles Davis1c8bd5a2011-05-22 03:01:05 +0000297} // End of namespace llvm
298