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/MC/MCStreamer.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCObjectFileInfo.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MCSectionCOFF.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/ADT/Twine.h"
21 // NOTE: All relocations generated here are 4-byte image-relative.
23 static uint8_t CountOfUnwindCodes(std::vector<MCWin64EHInstruction> &instArray){
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:
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 if (I->getSize() > 512*1024-8)
53 static void EmitAbsDifference(MCStreamer &streamer, MCSymbol *lhs,
55 MCContext &context = streamer.getContext();
56 const MCExpr *diff = MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(
58 MCSymbolRefExpr::Create(
61 streamer.EmitAbsValue(diff, 1);
65 static void EmitUnwindCode(MCStreamer &streamer, MCSymbol *begin,
66 MCWin64EHInstruction &inst) {
69 b2 = (inst.getOperation() & 0x0F);
70 switch (inst.getOperation()) {
71 case Win64EH::UOP_PushNonVol:
72 EmitAbsDifference(streamer, inst.getLabel(), begin);
73 b2 |= (inst.getRegister() & 0x0F) << 4;
74 streamer.EmitIntValue(b2, 1);
76 case Win64EH::UOP_AllocLarge:
77 EmitAbsDifference(streamer, inst.getLabel(), begin);
78 if (inst.getSize() > 512*1024-8) {
80 streamer.EmitIntValue(b2, 1);
81 w = inst.getSize() & 0xFFF8;
82 streamer.EmitIntValue(w, 2);
83 w = inst.getSize() >> 16;
85 streamer.EmitIntValue(b2, 1);
86 w = inst.getSize() >> 3;
88 streamer.EmitIntValue(w, 2);
90 case Win64EH::UOP_AllocSmall:
91 b2 |= (((inst.getSize()-8) >> 3) & 0x0F) << 4;
92 EmitAbsDifference(streamer, inst.getLabel(), begin);
93 streamer.EmitIntValue(b2, 1);
95 case Win64EH::UOP_SetFPReg:
96 b1 = inst.getOffset() & 0xF0;
97 streamer.EmitIntValue(b1, 1);
98 streamer.EmitIntValue(b2, 1);
100 case Win64EH::UOP_SaveNonVol:
101 case Win64EH::UOP_SaveXMM128:
102 b2 |= (inst.getRegister() & 0x0F) << 4;
103 EmitAbsDifference(streamer, inst.getLabel(), begin);
104 streamer.EmitIntValue(b2, 1);
105 w = inst.getOffset() >> 3;
106 if (inst.getOperation() == Win64EH::UOP_SaveXMM128)
108 streamer.EmitIntValue(w, 2);
110 case Win64EH::UOP_SaveNonVolBig:
111 case Win64EH::UOP_SaveXMM128Big:
112 b2 |= (inst.getRegister() & 0x0F) << 4;
113 EmitAbsDifference(streamer, inst.getLabel(), begin);
114 streamer.EmitIntValue(b2, 1);
115 if (inst.getOperation() == Win64EH::UOP_SaveXMM128Big)
116 w = inst.getOffset() & 0xFFF0;
118 w = inst.getOffset() & 0xFFF8;
119 streamer.EmitIntValue(w, 2);
120 w = inst.getOffset() >> 16;
121 streamer.EmitIntValue(w, 2);
123 case Win64EH::UOP_PushMachFrame:
124 if (inst.isPushCodeFrame())
126 EmitAbsDifference(streamer, inst.getLabel(), begin);
127 streamer.EmitIntValue(b2, 1);
132 static void EmitRuntimeFunction(MCStreamer &streamer,
133 const MCWin64EHUnwindInfo *info) {
134 MCContext &context = streamer.getContext();
136 streamer.EmitValueToAlignment(4);
137 streamer.EmitValue(MCSymbolRefExpr::Create(info->Begin, context), 4);
138 streamer.EmitValue(MCSymbolRefExpr::Create(info->End, context), 4);
139 streamer.EmitValue(MCSymbolRefExpr::Create(info->Symbol, context), 4);
142 static void EmitUnwindInfo(MCStreamer &streamer, MCWin64EHUnwindInfo *info) {
143 // If this UNWIND_INFO already has a symbol, it's already been emitted.
144 if (info->Symbol) return;
146 MCContext &context = streamer.getContext();
147 streamer.EmitValueToAlignment(4);
148 // Upper 3 bits are the version number (currently 1).
149 uint8_t flags = 0x01;
150 info->Symbol = context.CreateTempSymbol();
151 streamer.EmitLabel(info->Symbol);
153 if (info->ChainedParent)
154 flags |= Win64EH::UNW_ChainInfo << 3;
156 if (info->HandlesUnwind)
157 flags |= Win64EH::UNW_TerminateHandler << 3;
158 if (info->HandlesExceptions)
159 flags |= Win64EH::UNW_ExceptionHandler << 3;
161 streamer.EmitIntValue(flags, 1);
164 EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
166 streamer.EmitIntValue(0, 1);
168 uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
169 streamer.EmitIntValue(numCodes, 1);
172 if (info->LastFrameInst >= 0) {
173 MCWin64EHInstruction &frameInst = info->Instructions[info->LastFrameInst];
174 assert(frameInst.getOperation() == Win64EH::UOP_SetFPReg);
175 frame = (frameInst.getRegister() & 0x0F) |
176 (frameInst.getOffset() & 0xF0);
178 streamer.EmitIntValue(frame, 1);
180 // Emit unwind instructions (in reverse order).
181 uint8_t numInst = info->Instructions.size();
182 for (uint8_t c = 0; c < numInst; ++c) {
183 MCWin64EHInstruction inst = info->Instructions.back();
184 info->Instructions.pop_back();
185 EmitUnwindCode(streamer, info->Begin, inst);
188 if (flags & (Win64EH::UNW_ChainInfo << 3))
189 EmitRuntimeFunction(streamer, info->ChainedParent);
191 ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
192 streamer.EmitValue(MCSymbolRefExpr::Create(info->ExceptionHandler, context),
194 else if (numCodes < 2) {
195 // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
196 // a chained unwind info, if there is no handler, and if there are fewer
197 // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
199 streamer.EmitIntValue(0, 2);
201 streamer.EmitIntValue(0, 4);
205 StringRef MCWin64EHUnwindEmitter::GetSectionSuffix(const MCSymbol *func) {
206 if (!func || !func->isInSection()) return "";
207 const MCSection *section = &func->getSection();
208 const MCSectionCOFF *COFFSection;
209 if ((COFFSection = dyn_cast<MCSectionCOFF>(section))) {
210 StringRef name = COFFSection->getSectionName();
211 size_t dollar = name.find('$');
212 size_t dot = name.find('.', 1);
213 if (dollar == StringRef::npos && dot == StringRef::npos)
215 if (dot == StringRef::npos)
216 return name.substr(dollar);
217 if (dollar == StringRef::npos || dot < dollar)
218 return name.substr(dot);
219 return name.substr(dollar);
224 static const MCSection *getWin64EHTableSection(StringRef suffix,
225 MCContext &context) {
227 return context.getObjectFileInfo()->getXDataSection();
229 return context.getCOFFSection((".xdata"+suffix).str(),
230 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
231 COFF::IMAGE_SCN_MEM_READ |
232 COFF::IMAGE_SCN_MEM_WRITE,
233 SectionKind::getDataRel());
236 static const MCSection *getWin64EHFuncTableSection(StringRef suffix,
237 MCContext &context) {
239 return context.getObjectFileInfo()->getPDataSection();
240 return context.getCOFFSection((".pdata"+suffix).str(),
241 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
242 COFF::IMAGE_SCN_MEM_READ |
243 COFF::IMAGE_SCN_MEM_WRITE,
244 SectionKind::getDataRel());
247 void MCWin64EHUnwindEmitter::EmitUnwindInfo(MCStreamer &streamer,
248 MCWin64EHUnwindInfo *info) {
249 // Switch sections (the static function above is meant to be called from
250 // here and from Emit().
251 MCContext &context = streamer.getContext();
252 const MCSection *xdataSect =
253 getWin64EHTableSection(GetSectionSuffix(info->Function), context);
254 streamer.SwitchSection(xdataSect);
256 llvm::EmitUnwindInfo(streamer, info);
259 void MCWin64EHUnwindEmitter::Emit(MCStreamer &streamer) {
260 MCContext &context = streamer.getContext();
261 // Emit the unwind info structs first.
262 for (unsigned i = 0; i < streamer.getNumW64UnwindInfos(); ++i) {
263 MCWin64EHUnwindInfo &info = streamer.getW64UnwindInfo(i);
264 const MCSection *xdataSect =
265 getWin64EHTableSection(GetSectionSuffix(info.Function), context);
266 streamer.SwitchSection(xdataSect);
267 llvm::EmitUnwindInfo(streamer, &info);
269 // Now emit RUNTIME_FUNCTION entries.
270 for (unsigned i = 0; i < streamer.getNumW64UnwindInfos(); ++i) {
271 MCWin64EHUnwindInfo &info = streamer.getW64UnwindInfo(i);
272 const MCSection *pdataSect =
273 getWin64EHFuncTableSection(GetSectionSuffix(info.Function), context);
274 streamer.SwitchSection(pdataSect);
275 EmitRuntimeFunction(streamer, &info);
279 } // End of namespace llvm