Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 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" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 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" |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | // NOTE: All relocations generated here are 4-byte image-relative. |
| 22 | |
| 23 | static 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 Davis | ef60724 | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 33 | break; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 34 | case Win64EH::UOP_SaveNonVol: |
| 35 | case Win64EH::UOP_SaveXMM128: |
| 36 | count += 2; |
Charles Davis | ef60724 | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 37 | break; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 38 | case Win64EH::UOP_SaveNonVolBig: |
| 39 | case Win64EH::UOP_SaveXMM128Big: |
| 40 | count += 3; |
Charles Davis | ef60724 | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 41 | break; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 42 | case Win64EH::UOP_AllocLarge: |
| 43 | if (I->getSize() > 512*1024-8) |
| 44 | count += 3; |
| 45 | else |
| 46 | count += 2; |
Charles Davis | ef60724 | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 47 | break; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | return count; |
| 51 | } |
| 52 | |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 53 | static 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 | |
| 65 | static void EmitUnwindCode(MCStreamer &streamer, MCSymbol *begin, |
| 66 | MCWin64EHInstruction &inst) { |
Kai Nacke | ea434e4 | 2013-07-06 17:15:36 +0000 | [diff] [blame^] | 67 | uint8_t b2; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 68 | uint16_t w; |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 69 | b2 = (inst.getOperation() & 0x0F); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 70 | switch (inst.getOperation()) { |
| 71 | case Win64EH::UOP_PushNonVol: |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 72 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 73 | b2 |= (inst.getRegister() & 0x0F) << 4; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 74 | streamer.EmitIntValue(b2, 1); |
| 75 | break; |
| 76 | case Win64EH::UOP_AllocLarge: |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 77 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 78 | if (inst.getSize() > 512*1024-8) { |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 79 | b2 |= 0x10; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 80 | 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 Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 91 | b2 |= (((inst.getSize()-8) >> 3) & 0x0F) << 4; |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 92 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 93 | streamer.EmitIntValue(b2, 1); |
| 94 | break; |
| 95 | case Win64EH::UOP_SetFPReg: |
Kai Nacke | ea434e4 | 2013-07-06 17:15:36 +0000 | [diff] [blame^] | 96 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 97 | streamer.EmitIntValue(b2, 1); |
| 98 | break; |
| 99 | case Win64EH::UOP_SaveNonVol: |
| 100 | case Win64EH::UOP_SaveXMM128: |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 101 | b2 |= (inst.getRegister() & 0x0F) << 4; |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 102 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 103 | 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 Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 111 | b2 |= (inst.getRegister() & 0x0F) << 4; |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 112 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 113 | 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 Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 124 | b2 |= 0x10; |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 125 | EmitAbsDifference(streamer, inst.getLabel(), begin); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 126 | streamer.EmitIntValue(b2, 1); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void EmitRuntimeFunction(MCStreamer &streamer, |
Charles Davis | 38ea9ee | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 132 | const MCWin64EHUnwindInfo *info) { |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 133 | MCContext &context = streamer.getContext(); |
| 134 | |
Charles Davis | ef60724 | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 135 | streamer.EmitValueToAlignment(4); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 136 | streamer.EmitValue(MCSymbolRefExpr::Create(info->Begin, context), 4); |
| 137 | streamer.EmitValue(MCSymbolRefExpr::Create(info->End, context), 4); |
| 138 | streamer.EmitValue(MCSymbolRefExpr::Create(info->Symbol, context), 4); |
| 139 | } |
| 140 | |
| 141 | static void EmitUnwindInfo(MCStreamer &streamer, MCWin64EHUnwindInfo *info) { |
| 142 | // If this UNWIND_INFO already has a symbol, it's already been emitted. |
| 143 | if (info->Symbol) return; |
| 144 | |
| 145 | MCContext &context = streamer.getContext(); |
Charles Davis | ef60724 | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 146 | streamer.EmitValueToAlignment(4); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 147 | // Upper 3 bits are the version number (currently 1). |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 148 | uint8_t flags = 0x01; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 149 | info->Symbol = context.CreateTempSymbol(); |
| 150 | streamer.EmitLabel(info->Symbol); |
| 151 | |
| 152 | if (info->ChainedParent) |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 153 | flags |= Win64EH::UNW_ChainInfo << 3; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 154 | else { |
| 155 | if (info->HandlesUnwind) |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 156 | flags |= Win64EH::UNW_TerminateHandler << 3; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 157 | if (info->HandlesExceptions) |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 158 | flags |= Win64EH::UNW_ExceptionHandler << 3; |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 159 | } |
| 160 | streamer.EmitIntValue(flags, 1); |
| 161 | |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 162 | if (info->PrologEnd) |
| 163 | EmitAbsDifference(streamer, info->PrologEnd, info->Begin); |
| 164 | else |
| 165 | streamer.EmitIntValue(0, 1); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 166 | |
| 167 | uint8_t numCodes = CountOfUnwindCodes(info->Instructions); |
| 168 | streamer.EmitIntValue(numCodes, 1); |
| 169 | |
| 170 | uint8_t frame = 0; |
| 171 | if (info->LastFrameInst >= 0) { |
| 172 | MCWin64EHInstruction &frameInst = info->Instructions[info->LastFrameInst]; |
| 173 | assert(frameInst.getOperation() == Win64EH::UOP_SetFPReg); |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 174 | frame = (frameInst.getRegister() & 0x0F) | |
| 175 | (frameInst.getOffset() & 0xF0); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 176 | } |
| 177 | streamer.EmitIntValue(frame, 1); |
| 178 | |
| 179 | // Emit unwind instructions (in reverse order). |
| 180 | uint8_t numInst = info->Instructions.size(); |
| 181 | for (uint8_t c = 0; c < numInst; ++c) { |
| 182 | MCWin64EHInstruction inst = info->Instructions.back(); |
| 183 | info->Instructions.pop_back(); |
Charles Davis | c4cbf9b | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 184 | EmitUnwindCode(streamer, info->Begin, inst); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 187 | if (flags & (Win64EH::UNW_ChainInfo << 3)) |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 188 | EmitRuntimeFunction(streamer, info->ChainedParent); |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 189 | else if (flags & |
| 190 | ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3)) |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 191 | streamer.EmitValue(MCSymbolRefExpr::Create(info->ExceptionHandler, context), |
| 192 | 4); |
Charles Davis | 07cbe23 | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 193 | else if (numCodes < 2) { |
| 194 | // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not |
| 195 | // a chained unwind info, if there is no handler, and if there are fewer |
| 196 | // than 2 slots used in the unwind code array, we have to pad to 8 bytes. |
| 197 | if (numCodes == 1) |
| 198 | streamer.EmitIntValue(0, 2); |
| 199 | else |
| 200 | streamer.EmitIntValue(0, 4); |
| 201 | } |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Charles Davis | 7b06b73 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 204 | StringRef MCWin64EHUnwindEmitter::GetSectionSuffix(const MCSymbol *func) { |
| 205 | if (!func || !func->isInSection()) return ""; |
| 206 | const MCSection *section = &func->getSection(); |
| 207 | const MCSectionCOFF *COFFSection; |
| 208 | if ((COFFSection = dyn_cast<MCSectionCOFF>(section))) { |
| 209 | StringRef name = COFFSection->getSectionName(); |
| 210 | size_t dollar = name.find('$'); |
| 211 | size_t dot = name.find('.', 1); |
| 212 | if (dollar == StringRef::npos && dot == StringRef::npos) |
| 213 | return ""; |
| 214 | if (dot == StringRef::npos) |
| 215 | return name.substr(dollar); |
| 216 | if (dollar == StringRef::npos || dot < dollar) |
| 217 | return name.substr(dot); |
| 218 | return name.substr(dollar); |
| 219 | } |
| 220 | return ""; |
| 221 | } |
| 222 | |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 223 | static const MCSection *getWin64EHTableSection(StringRef suffix, |
| 224 | MCContext &context) { |
| 225 | if (suffix == "") |
| 226 | return context.getObjectFileInfo()->getXDataSection(); |
| 227 | |
| 228 | return context.getCOFFSection((".xdata"+suffix).str(), |
| 229 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
Anton Korobeynikov | 0a0e50c | 2012-08-08 12:46:46 +0000 | [diff] [blame] | 230 | COFF::IMAGE_SCN_MEM_READ, |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 231 | SectionKind::getDataRel()); |
| 232 | } |
| 233 | |
| 234 | static const MCSection *getWin64EHFuncTableSection(StringRef suffix, |
| 235 | MCContext &context) { |
| 236 | if (suffix == "") |
| 237 | return context.getObjectFileInfo()->getPDataSection(); |
| 238 | return context.getCOFFSection((".pdata"+suffix).str(), |
| 239 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
Anton Korobeynikov | 0a0e50c | 2012-08-08 12:46:46 +0000 | [diff] [blame] | 240 | COFF::IMAGE_SCN_MEM_READ, |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 241 | SectionKind::getDataRel()); |
| 242 | } |
| 243 | |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 244 | void MCWin64EHUnwindEmitter::EmitUnwindInfo(MCStreamer &streamer, |
| 245 | MCWin64EHUnwindInfo *info) { |
| 246 | // Switch sections (the static function above is meant to be called from |
| 247 | // here and from Emit(). |
| 248 | MCContext &context = streamer.getContext(); |
Charles Davis | 7b06b73 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 249 | const MCSection *xdataSect = |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 250 | getWin64EHTableSection(GetSectionSuffix(info->Function), context); |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 251 | streamer.SwitchSection(xdataSect); |
| 252 | |
| 253 | llvm::EmitUnwindInfo(streamer, info); |
| 254 | } |
| 255 | |
Charles Davis | 38ea9ee | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 256 | void MCWin64EHUnwindEmitter::Emit(MCStreamer &streamer) { |
| 257 | MCContext &context = streamer.getContext(); |
| 258 | // Emit the unwind info structs first. |
Charles Davis | 7b06b73 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 259 | for (unsigned i = 0; i < streamer.getNumW64UnwindInfos(); ++i) { |
| 260 | MCWin64EHUnwindInfo &info = streamer.getW64UnwindInfo(i); |
| 261 | const MCSection *xdataSect = |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 262 | getWin64EHTableSection(GetSectionSuffix(info.Function), context); |
Charles Davis | 7b06b73 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 263 | streamer.SwitchSection(xdataSect); |
| 264 | llvm::EmitUnwindInfo(streamer, &info); |
| 265 | } |
Charles Davis | 38ea9ee | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 266 | // Now emit RUNTIME_FUNCTION entries. |
Charles Davis | 7b06b73 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 267 | for (unsigned i = 0; i < streamer.getNumW64UnwindInfos(); ++i) { |
| 268 | MCWin64EHUnwindInfo &info = streamer.getW64UnwindInfo(i); |
| 269 | const MCSection *pdataSect = |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 270 | getWin64EHFuncTableSection(GetSectionSuffix(info.Function), context); |
Charles Davis | 7b06b73 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 271 | streamer.SwitchSection(pdataSect); |
| 272 | EmitRuntimeFunction(streamer, &info); |
| 273 | } |
Charles Davis | 38ea9ee | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Charles Davis | 3185f5c | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 276 | } // End of namespace llvm |
| 277 | |