Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCWin64EH.cpp - MCWin64EH implementation --------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/MC/MCWin64EH.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCContext.h" |
| 12 | #include "llvm/MC/MCExpr.h" |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCObjectFileInfo.h" |
| 14 | #include "llvm/MC/MCObjectStreamer.h" |
| 15 | #include "llvm/MC/MCSectionCOFF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCStreamer.h" |
| 17 | #include "llvm/MC/MCSymbol.h" |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Win64EH.h" |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 19 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 21 | |
| 22 | // NOTE: All relocations generated here are 4-byte image-relative. |
| 23 | |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 24 | static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) { |
Saleem Abdulrasool | 1e76cbd | 2014-07-10 04:50:09 +0000 | [diff] [blame] | 25 | uint8_t Count = 0; |
| 26 | for (const auto &I : Insns) { |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 27 | switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) { |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 28 | default: |
| 29 | llvm_unreachable("Unsupported unwind code"); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 30 | case Win64EH::UOP_PushNonVol: |
| 31 | case Win64EH::UOP_AllocSmall: |
| 32 | case Win64EH::UOP_SetFPReg: |
| 33 | case Win64EH::UOP_PushMachFrame: |
Saleem Abdulrasool | 1e76cbd | 2014-07-10 04:50:09 +0000 | [diff] [blame] | 34 | Count += 1; |
Charles Davis | 761313b | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 35 | break; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 36 | case Win64EH::UOP_SaveNonVol: |
| 37 | case Win64EH::UOP_SaveXMM128: |
Saleem Abdulrasool | 1e76cbd | 2014-07-10 04:50:09 +0000 | [diff] [blame] | 38 | Count += 2; |
Charles Davis | 761313b | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 39 | break; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 40 | case Win64EH::UOP_SaveNonVolBig: |
| 41 | case Win64EH::UOP_SaveXMM128Big: |
Saleem Abdulrasool | 1e76cbd | 2014-07-10 04:50:09 +0000 | [diff] [blame] | 42 | Count += 3; |
Charles Davis | 761313b | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 43 | break; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 44 | case Win64EH::UOP_AllocLarge: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 45 | Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2; |
Charles Davis | 761313b | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 46 | break; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
Saleem Abdulrasool | 1e76cbd | 2014-07-10 04:50:09 +0000 | [diff] [blame] | 49 | return Count; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Saleem Abdulrasool | 5705cd8 | 2014-07-13 19:03:40 +0000 | [diff] [blame] | 52 | static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, |
| 53 | const MCSymbol *RHS) { |
| 54 | MCContext &Context = Streamer.getContext(); |
| 55 | const MCExpr *Diff = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 56 | MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context), |
| 57 | MCSymbolRefExpr::create(RHS, Context), Context); |
Rafael Espindola | 91f6621 | 2014-08-15 02:51:31 +0000 | [diff] [blame] | 58 | Streamer.EmitValue(Diff, 1); |
Charles Davis | 6d1c4c7 | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Saleem Abdulrasool | b3be737 | 2014-08-03 18:51:17 +0000 | [diff] [blame] | 61 | static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin, |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 62 | WinEH::Instruction &inst) { |
Kai Nacke | 1b7e486 | 2013-08-27 04:16:16 +0000 | [diff] [blame] | 63 | uint8_t b2; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 64 | uint16_t w; |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 65 | b2 = (inst.Operation & 0x0F); |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 66 | switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) { |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 67 | default: |
| 68 | llvm_unreachable("Unsupported unwind code"); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 69 | case Win64EH::UOP_PushNonVol: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 70 | EmitAbsDifference(streamer, inst.Label, begin); |
| 71 | b2 |= (inst.Register & 0x0F) << 4; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 72 | streamer.EmitIntValue(b2, 1); |
| 73 | break; |
| 74 | case Win64EH::UOP_AllocLarge: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 75 | EmitAbsDifference(streamer, inst.Label, begin); |
| 76 | if (inst.Offset > 512 * 1024 - 8) { |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 77 | b2 |= 0x10; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 78 | streamer.EmitIntValue(b2, 1); |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 79 | w = inst.Offset & 0xFFF8; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 80 | streamer.EmitIntValue(w, 2); |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 81 | w = inst.Offset >> 16; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 82 | } else { |
| 83 | streamer.EmitIntValue(b2, 1); |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 84 | w = inst.Offset >> 3; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 85 | } |
| 86 | streamer.EmitIntValue(w, 2); |
| 87 | break; |
| 88 | case Win64EH::UOP_AllocSmall: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 89 | b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4; |
| 90 | EmitAbsDifference(streamer, inst.Label, begin); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 91 | streamer.EmitIntValue(b2, 1); |
| 92 | break; |
| 93 | case Win64EH::UOP_SetFPReg: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 94 | EmitAbsDifference(streamer, inst.Label, begin); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 95 | streamer.EmitIntValue(b2, 1); |
| 96 | break; |
| 97 | case Win64EH::UOP_SaveNonVol: |
| 98 | case Win64EH::UOP_SaveXMM128: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 99 | b2 |= (inst.Register & 0x0F) << 4; |
| 100 | EmitAbsDifference(streamer, inst.Label, begin); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 101 | streamer.EmitIntValue(b2, 1); |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 102 | w = inst.Offset >> 3; |
| 103 | if (inst.Operation == Win64EH::UOP_SaveXMM128) |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 104 | w >>= 1; |
| 105 | streamer.EmitIntValue(w, 2); |
| 106 | break; |
| 107 | case Win64EH::UOP_SaveNonVolBig: |
| 108 | case Win64EH::UOP_SaveXMM128Big: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 109 | b2 |= (inst.Register & 0x0F) << 4; |
| 110 | EmitAbsDifference(streamer, inst.Label, begin); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 111 | streamer.EmitIntValue(b2, 1); |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 112 | if (inst.Operation == Win64EH::UOP_SaveXMM128Big) |
| 113 | w = inst.Offset & 0xFFF0; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 114 | else |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 115 | w = inst.Offset & 0xFFF8; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 116 | streamer.EmitIntValue(w, 2); |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 117 | w = inst.Offset >> 16; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 118 | streamer.EmitIntValue(w, 2); |
| 119 | break; |
| 120 | case Win64EH::UOP_PushMachFrame: |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 121 | if (inst.Offset == 1) |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 122 | b2 |= 0x10; |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 123 | EmitAbsDifference(streamer, inst.Label, begin); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 124 | streamer.EmitIntValue(b2, 1); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
Kai Nacke | 74adc8a | 2013-09-15 17:46:46 +0000 | [diff] [blame] | 129 | static void EmitSymbolRefWithOfs(MCStreamer &streamer, |
| 130 | const MCSymbol *Base, |
| 131 | const MCSymbol *Other) { |
| 132 | MCContext &Context = streamer.getContext(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 133 | const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context); |
| 134 | const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context); |
| 135 | const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context); |
| 136 | const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base, |
Kai Nacke | 74adc8a | 2013-09-15 17:46:46 +0000 | [diff] [blame] | 137 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 138 | Context); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 139 | streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4); |
Kai Nacke | 74adc8a | 2013-09-15 17:46:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 142 | static void EmitRuntimeFunction(MCStreamer &streamer, |
Saleem Abdulrasool | b3be737 | 2014-08-03 18:51:17 +0000 | [diff] [blame] | 143 | const WinEH::FrameInfo *info) { |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 144 | MCContext &context = streamer.getContext(); |
| 145 | |
Charles Davis | 761313b | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 146 | streamer.EmitValueToAlignment(4); |
Kai Nacke | 74adc8a | 2013-09-15 17:46:46 +0000 | [diff] [blame] | 147 | EmitSymbolRefWithOfs(streamer, info->Function, info->Begin); |
| 148 | EmitSymbolRefWithOfs(streamer, info->Function, info->End); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 149 | streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol, |
Kai Nacke | 74adc8a | 2013-09-15 17:46:46 +0000 | [diff] [blame] | 150 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 151 | context), 4); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Saleem Abdulrasool | b3be737 | 2014-08-03 18:51:17 +0000 | [diff] [blame] | 154 | static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) { |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 155 | // If this UNWIND_INFO already has a symbol, it's already been emitted. |
Saleem Abdulrasool | b3be737 | 2014-08-03 18:51:17 +0000 | [diff] [blame] | 156 | if (info->Symbol) |
| 157 | return; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 158 | |
| 159 | MCContext &context = streamer.getContext(); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 160 | MCSymbol *Label = context.createTempSymbol(); |
Saleem Abdulrasool | b3be737 | 2014-08-03 18:51:17 +0000 | [diff] [blame] | 161 | |
Charles Davis | 761313b | 2011-05-27 02:43:19 +0000 | [diff] [blame] | 162 | streamer.EmitValueToAlignment(4); |
Saleem Abdulrasool | b3be737 | 2014-08-03 18:51:17 +0000 | [diff] [blame] | 163 | streamer.EmitLabel(Label); |
| 164 | info->Symbol = Label; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 165 | |
Kai Nacke | 8539b46 | 2013-09-15 18:01:09 +0000 | [diff] [blame] | 166 | // Upper 3 bits are the version number (currently 1). |
| 167 | uint8_t flags = 0x01; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 168 | if (info->ChainedParent) |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 169 | flags |= Win64EH::UNW_ChainInfo << 3; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 170 | else { |
| 171 | if (info->HandlesUnwind) |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 172 | flags |= Win64EH::UNW_TerminateHandler << 3; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 173 | if (info->HandlesExceptions) |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 174 | flags |= Win64EH::UNW_ExceptionHandler << 3; |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 175 | } |
| 176 | streamer.EmitIntValue(flags, 1); |
| 177 | |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 178 | if (info->PrologEnd) |
| 179 | EmitAbsDifference(streamer, info->PrologEnd, info->Begin); |
| 180 | else |
| 181 | streamer.EmitIntValue(0, 1); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 182 | |
| 183 | uint8_t numCodes = CountOfUnwindCodes(info->Instructions); |
| 184 | streamer.EmitIntValue(numCodes, 1); |
| 185 | |
| 186 | uint8_t frame = 0; |
| 187 | if (info->LastFrameInst >= 0) { |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 188 | WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst]; |
Saleem Abdulrasool | c7c3cb1 | 2014-07-13 19:03:45 +0000 | [diff] [blame] | 189 | assert(frameInst.Operation == Win64EH::UOP_SetFPReg); |
| 190 | frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 191 | } |
| 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) { |
Saleem Abdulrasool | ab82086 | 2014-07-17 03:08:50 +0000 | [diff] [blame] | 197 | WinEH::Instruction inst = info->Instructions.back(); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 198 | info->Instructions.pop_back(); |
Charles Davis | 6d1c4c7 | 2011-05-27 03:25:01 +0000 | [diff] [blame] | 199 | EmitUnwindCode(streamer, info->Begin, inst); |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Kai Nacke | 8539b46 | 2013-09-15 18:01:09 +0000 | [diff] [blame] | 202 | // 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 Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 210 | if (flags & (Win64EH::UNW_ChainInfo << 3)) |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 211 | EmitRuntimeFunction(streamer, info->ChainedParent); |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 212 | else if (flags & |
| 213 | ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3)) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 214 | streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler, |
Kai Nacke | 74adc8a | 2013-09-15 17:46:46 +0000 | [diff] [blame] | 215 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 216 | context), 4); |
Kai Nacke | 8539b46 | 2013-09-15 18:01:09 +0000 | [diff] [blame] | 217 | else if (numCodes == 0) { |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 218 | // 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 Nacke | 8539b46 | 2013-09-15 18:01:09 +0000 | [diff] [blame] | 221 | streamer.EmitIntValue(0, 4); |
Charles Davis | ea5dc3a | 2011-05-27 15:10:25 +0000 | [diff] [blame] | 222 | } |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 225 | void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const { |
Charles Davis | bc2daa0 | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 226 | // Emit the unwind info structs first. |
Reid Kleckner | 4c4422f | 2017-10-06 17:21:49 +0000 | [diff] [blame] | 227 | for (const auto &CFI : Streamer.getWinFrameInfos()) { |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 228 | MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection); |
Saleem Abdulrasool | 427c08d | 2014-07-10 04:50:06 +0000 | [diff] [blame] | 229 | Streamer.SwitchSection(XData); |
Reid Kleckner | 4c4422f | 2017-10-06 17:21:49 +0000 | [diff] [blame] | 230 | ::EmitUnwindInfo(Streamer, CFI.get()); |
Charles Davis | 03eef62 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 231 | } |
Saleem Abdulrasool | 427c08d | 2014-07-10 04:50:06 +0000 | [diff] [blame] | 232 | |
Charles Davis | bc2daa0 | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 233 | // Now emit RUNTIME_FUNCTION entries. |
Reid Kleckner | 4c4422f | 2017-10-06 17:21:49 +0000 | [diff] [blame] | 234 | for (const auto &CFI : Streamer.getWinFrameInfos()) { |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 235 | MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection); |
Saleem Abdulrasool | 427c08d | 2014-07-10 04:50:06 +0000 | [diff] [blame] | 236 | Streamer.SwitchSection(PData); |
Reid Kleckner | 4c4422f | 2017-10-06 17:21:49 +0000 | [diff] [blame] | 237 | EmitRuntimeFunction(Streamer, CFI.get()); |
Charles Davis | 03eef62 | 2011-05-27 19:09:24 +0000 | [diff] [blame] | 238 | } |
Charles Davis | bc2daa0 | 2011-05-22 04:15:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 241 | void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo( |
| 242 | MCStreamer &Streamer, WinEH::FrameInfo *info) const { |
Saleem Abdulrasool | 64a8cc7 | 2014-08-07 02:59:41 +0000 | [diff] [blame] | 243 | // Switch sections (the static function above is meant to be called from |
| 244 | // here and from Emit(). |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 245 | MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection); |
| 246 | Streamer.SwitchSection(XData); |
Saleem Abdulrasool | 64a8cc7 | 2014-08-07 02:59:41 +0000 | [diff] [blame] | 247 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 248 | ::EmitUnwindInfo(Streamer, info); |
Saleem Abdulrasool | 64a8cc7 | 2014-08-07 02:59:41 +0000 | [diff] [blame] | 249 | } |
Charles Davis | 1c8bd5a | 2011-05-22 03:01:05 +0000 | [diff] [blame] | 250 | |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 251 | static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, |
| 252 | const MCSymbol *RHS) { |
| 253 | MCContext &Context = Streamer.getContext(); |
| 254 | const MCExpr *Diff = |
| 255 | MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context), |
| 256 | MCSymbolRefExpr::create(RHS, Context), Context); |
| 257 | MCObjectStreamer *OS = (MCObjectStreamer *)(&Streamer); |
Eli Friedman | 0b61d22 | 2019-05-03 00:10:45 +0000 | [diff] [blame] | 258 | // It should normally be possible to calculate the length of a function |
| 259 | // at this point, but it might not be possible in the presence of certain |
| 260 | // unusual constructs, like an inline asm with an alignment directive. |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 261 | int64_t value; |
Eli Friedman | 0b61d22 | 2019-05-03 00:10:45 +0000 | [diff] [blame] | 262 | if (!Diff->evaluateAsAbsolute(value, OS->getAssembler())) |
| 263 | report_fatal_error("Failed to evaluate function length in SEH unwind info"); |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 264 | return value; |
| 265 | } |
| 266 | |
| 267 | static uint32_t |
| 268 | ARM64CountOfUnwindCodes(const std::vector<WinEH::Instruction> &Insns) { |
| 269 | uint32_t Count = 0; |
| 270 | for (const auto &I : Insns) { |
| 271 | switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) { |
| 272 | default: |
| 273 | llvm_unreachable("Unsupported ARM64 unwind code"); |
| 274 | case Win64EH::UOP_AllocSmall: |
| 275 | Count += 1; |
| 276 | break; |
| 277 | case Win64EH::UOP_AllocMedium: |
| 278 | Count += 2; |
| 279 | break; |
| 280 | case Win64EH::UOP_AllocLarge: |
| 281 | Count += 4; |
| 282 | break; |
| 283 | case Win64EH::UOP_SaveFPLRX: |
| 284 | Count += 1; |
| 285 | break; |
| 286 | case Win64EH::UOP_SaveFPLR: |
| 287 | Count += 1; |
| 288 | break; |
| 289 | case Win64EH::UOP_SaveReg: |
| 290 | Count += 2; |
| 291 | break; |
| 292 | case Win64EH::UOP_SaveRegP: |
| 293 | Count += 2; |
| 294 | break; |
| 295 | case Win64EH::UOP_SaveRegPX: |
| 296 | Count += 2; |
| 297 | break; |
| 298 | case Win64EH::UOP_SaveRegX: |
| 299 | Count += 2; |
| 300 | break; |
| 301 | case Win64EH::UOP_SaveFReg: |
| 302 | Count += 2; |
| 303 | break; |
| 304 | case Win64EH::UOP_SaveFRegP: |
| 305 | Count += 2; |
| 306 | break; |
| 307 | case Win64EH::UOP_SaveFRegX: |
| 308 | Count += 2; |
| 309 | break; |
| 310 | case Win64EH::UOP_SaveFRegPX: |
| 311 | Count += 2; |
| 312 | break; |
| 313 | case Win64EH::UOP_SetFP: |
| 314 | Count += 1; |
| 315 | break; |
| 316 | case Win64EH::UOP_AddFP: |
| 317 | Count += 2; |
| 318 | break; |
| 319 | case Win64EH::UOP_Nop: |
| 320 | Count += 1; |
| 321 | break; |
| 322 | case Win64EH::UOP_End: |
| 323 | Count += 1; |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | return Count; |
| 328 | } |
| 329 | |
| 330 | // Unwind opcode encodings and restrictions are documented at |
| 331 | // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling |
| 332 | static void ARM64EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin, |
| 333 | WinEH::Instruction &inst) { |
| 334 | uint8_t b, reg; |
| 335 | switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) { |
| 336 | default: |
| 337 | llvm_unreachable("Unsupported ARM64 unwind code"); |
| 338 | case Win64EH::UOP_AllocSmall: |
| 339 | b = (inst.Offset >> 4) & 0x1F; |
| 340 | streamer.EmitIntValue(b, 1); |
| 341 | break; |
| 342 | case Win64EH::UOP_AllocMedium: { |
| 343 | uint16_t hw = (inst.Offset >> 4) & 0x7FF; |
| 344 | b = 0xC0; |
| 345 | b |= (hw >> 8); |
| 346 | streamer.EmitIntValue(b, 1); |
| 347 | b = hw & 0xFF; |
| 348 | streamer.EmitIntValue(b, 1); |
| 349 | break; |
| 350 | } |
| 351 | case Win64EH::UOP_AllocLarge: { |
| 352 | uint32_t w; |
| 353 | b = 0xE0; |
| 354 | streamer.EmitIntValue(b, 1); |
| 355 | w = inst.Offset >> 4; |
| 356 | b = (w & 0x00FF0000) >> 16; |
| 357 | streamer.EmitIntValue(b, 1); |
| 358 | b = (w & 0x0000FF00) >> 8; |
| 359 | streamer.EmitIntValue(b, 1); |
| 360 | b = w & 0x000000FF; |
| 361 | streamer.EmitIntValue(b, 1); |
| 362 | break; |
| 363 | } |
| 364 | case Win64EH::UOP_SetFP: |
| 365 | b = 0xE1; |
| 366 | streamer.EmitIntValue(b, 1); |
| 367 | break; |
| 368 | case Win64EH::UOP_AddFP: |
| 369 | b = 0xE2; |
| 370 | streamer.EmitIntValue(b, 1); |
| 371 | b = (inst.Offset >> 3); |
| 372 | streamer.EmitIntValue(b, 1); |
| 373 | break; |
| 374 | case Win64EH::UOP_Nop: |
| 375 | b = 0xE3; |
| 376 | streamer.EmitIntValue(b, 1); |
| 377 | break; |
| 378 | case Win64EH::UOP_SaveFPLRX: |
| 379 | b = 0x80; |
| 380 | b |= ((inst.Offset - 1) >> 3) & 0x3F; |
| 381 | streamer.EmitIntValue(b, 1); |
| 382 | break; |
| 383 | case Win64EH::UOP_SaveFPLR: |
| 384 | b = 0x40; |
| 385 | b |= (inst.Offset >> 3) & 0x3F; |
| 386 | streamer.EmitIntValue(b, 1); |
| 387 | break; |
| 388 | case Win64EH::UOP_SaveReg: |
| 389 | assert(inst.Register >= 19 && "Saved reg must be >= 19"); |
| 390 | reg = inst.Register - 19; |
| 391 | b = 0xD0 | ((reg & 0xC) >> 2); |
| 392 | streamer.EmitIntValue(b, 1); |
| 393 | b = ((reg & 0x3) << 6) | (inst.Offset >> 3); |
| 394 | streamer.EmitIntValue(b, 1); |
| 395 | break; |
| 396 | case Win64EH::UOP_SaveRegX: |
| 397 | assert(inst.Register >= 19 && "Saved reg must be >= 19"); |
| 398 | reg = inst.Register - 19; |
| 399 | b = 0xD4 | ((reg & 0x8) >> 3); |
| 400 | streamer.EmitIntValue(b, 1); |
| 401 | b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1); |
| 402 | streamer.EmitIntValue(b, 1); |
| 403 | break; |
| 404 | case Win64EH::UOP_SaveRegP: |
| 405 | assert(inst.Register >= 19 && "Saved registers must be >= 19"); |
| 406 | reg = inst.Register - 19; |
| 407 | b = 0xC8 | ((reg & 0xC) >> 2); |
| 408 | streamer.EmitIntValue(b, 1); |
| 409 | b = ((reg & 0x3) << 6) | (inst.Offset >> 3); |
| 410 | streamer.EmitIntValue(b, 1); |
| 411 | break; |
| 412 | case Win64EH::UOP_SaveRegPX: |
| 413 | assert(inst.Register >= 19 && "Saved registers must be >= 19"); |
| 414 | reg = inst.Register - 19; |
| 415 | b = 0xCC | ((reg & 0xC) >> 2); |
| 416 | streamer.EmitIntValue(b, 1); |
| 417 | b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1); |
| 418 | streamer.EmitIntValue(b, 1); |
| 419 | break; |
| 420 | case Win64EH::UOP_SaveFReg: |
| 421 | assert(inst.Register >= 8 && "Saved dreg must be >= 8"); |
| 422 | reg = inst.Register - 8; |
| 423 | b = 0xDC | ((reg & 0x4) >> 2); |
| 424 | streamer.EmitIntValue(b, 1); |
| 425 | b = ((reg & 0x3) << 6) | (inst.Offset >> 3); |
| 426 | streamer.EmitIntValue(b, 1); |
| 427 | break; |
| 428 | case Win64EH::UOP_SaveFRegX: |
| 429 | assert(inst.Register >= 8 && "Saved dreg must be >= 8"); |
| 430 | reg = inst.Register - 8; |
| 431 | b = 0xDE; |
| 432 | streamer.EmitIntValue(b, 1); |
| 433 | b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1); |
| 434 | streamer.EmitIntValue(b, 1); |
| 435 | break; |
| 436 | case Win64EH::UOP_SaveFRegP: |
| 437 | assert(inst.Register >= 8 && "Saved dregs must be >= 8"); |
| 438 | reg = inst.Register - 8; |
| 439 | b = 0xD8 | ((reg & 0x4) >> 2); |
| 440 | streamer.EmitIntValue(b, 1); |
| 441 | b = ((reg & 0x3) << 6) | (inst.Offset >> 3); |
| 442 | streamer.EmitIntValue(b, 1); |
| 443 | break; |
| 444 | case Win64EH::UOP_SaveFRegPX: |
| 445 | assert(inst.Register >= 8 && "Saved dregs must be >= 8"); |
| 446 | reg = inst.Register - 8; |
| 447 | b = 0xDA | ((reg & 0x4) >> 2); |
| 448 | streamer.EmitIntValue(b, 1); |
| 449 | b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1); |
| 450 | streamer.EmitIntValue(b, 1); |
| 451 | break; |
| 452 | case Win64EH::UOP_End: |
| 453 | b = 0xE4; |
| 454 | streamer.EmitIntValue(b, 1); |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
Sanjin Sijaric | b694030 | 2019-01-17 09:45:17 +0000 | [diff] [blame] | 459 | // Returns the epilog symbol of an epilog with the exact same unwind code |
| 460 | // sequence, if it exists. Otherwise, returns nulltpr. |
| 461 | // EpilogInstrs - Unwind codes for the current epilog. |
| 462 | // Epilogs - Epilogs that potentialy match the current epilog. |
| 463 | static MCSymbol* |
| 464 | FindMatchingEpilog(const std::vector<WinEH::Instruction>& EpilogInstrs, |
| 465 | const std::vector<MCSymbol *>& Epilogs, |
| 466 | const WinEH::FrameInfo *info) { |
| 467 | for (auto *EpilogStart : Epilogs) { |
| 468 | auto InstrsIter = info->EpilogMap.find(EpilogStart); |
| 469 | assert(InstrsIter != info->EpilogMap.end() && |
| 470 | "Epilog not found in EpilogMap"); |
| 471 | const auto &Instrs = InstrsIter->second; |
| 472 | |
| 473 | if (Instrs.size() != EpilogInstrs.size()) |
| 474 | continue; |
| 475 | |
| 476 | bool Match = true; |
| 477 | for (unsigned i = 0; i < Instrs.size(); ++i) |
| 478 | if (Instrs[i].Operation != EpilogInstrs[i].Operation || |
| 479 | Instrs[i].Offset != EpilogInstrs[i].Offset || |
| 480 | Instrs[i].Register != EpilogInstrs[i].Register) { |
| 481 | Match = false; |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | if (Match) |
| 486 | return EpilogStart; |
| 487 | } |
| 488 | return nullptr; |
| 489 | } |
| 490 | |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 491 | // Populate the .xdata section. The format of .xdata on ARM64 is documented at |
| 492 | // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling |
| 493 | static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) { |
| 494 | // If this UNWIND_INFO already has a symbol, it's already been emitted. |
| 495 | if (info->Symbol) |
| 496 | return; |
| 497 | |
| 498 | MCContext &context = streamer.getContext(); |
| 499 | MCSymbol *Label = context.createTempSymbol(); |
| 500 | |
| 501 | streamer.EmitValueToAlignment(4); |
| 502 | streamer.EmitLabel(Label); |
| 503 | info->Symbol = Label; |
| 504 | |
Eli Friedman | 0b61d22 | 2019-05-03 00:10:45 +0000 | [diff] [blame] | 505 | int64_t RawFuncLength; |
| 506 | if (!info->FuncletOrFuncEnd) { |
| 507 | // FIXME: This is very wrong; we emit SEH data which covers zero bytes |
| 508 | // of code. But otherwise test/MC/AArch64/seh.s crashes. |
| 509 | RawFuncLength = 0; |
| 510 | } else { |
| 511 | // FIXME: GetAbsDifference tries to compute the length of the function |
| 512 | // immediately, before the whole file is emitted, but in general |
| 513 | // that's impossible: the size in bytes of certain assembler directives |
| 514 | // like .align and .fill is not known until the whole file is parsed and |
| 515 | // relaxations are applied. Currently, GetAbsDifference fails with a fatal |
| 516 | // error in that case. (We mostly don't hit this because inline assembly |
| 517 | // specifying those directives is rare, and we don't normally try to |
| 518 | // align loops on AArch64.) |
| 519 | // |
| 520 | // There are two potential approaches to delaying the computation. One, |
| 521 | // we could emit something like ".word (endfunc-beginfunc)/4+0x10800000", |
| 522 | // as long as we have some conservative estimate we could use to prove |
| 523 | // that we don't need to split the unwind data. Emitting the constant |
| 524 | // is straightforward, but there's no existing code for estimating the |
| 525 | // size of the function. |
| 526 | // |
| 527 | // The other approach would be to use a dedicated, relaxable fragment, |
| 528 | // which could grow to accommodate splitting the unwind data if |
| 529 | // necessary. This is more straightforward, since it automatically works |
| 530 | // without any new infrastructure, and it's consistent with how we handle |
| 531 | // relaxation in other contexts. But it would require some refactoring |
| 532 | // to move parts of the pdata/xdata emission into the implementation of |
| 533 | // a fragment. We could probably continue to encode the unwind codes |
| 534 | // here, but we'd have to emit the pdata, the xdata header, and the |
| 535 | // epilogue scopes later, since they depend on whether the we need to |
| 536 | // split the unwind data. |
| 537 | RawFuncLength = GetAbsDifference(streamer, info->FuncletOrFuncEnd, |
| 538 | info->Begin); |
| 539 | } |
| 540 | if (RawFuncLength > 0xFFFFF) |
| 541 | report_fatal_error("SEH unwind data splitting not yet implemented"); |
| 542 | uint32_t FuncLength = (uint32_t)RawFuncLength / 4; |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 543 | uint32_t PrologCodeBytes = ARM64CountOfUnwindCodes(info->Instructions); |
| 544 | uint32_t TotalCodeBytes = PrologCodeBytes; |
| 545 | |
| 546 | // Process epilogs. |
| 547 | MapVector<MCSymbol *, uint32_t> EpilogInfo; |
Sanjin Sijaric | b694030 | 2019-01-17 09:45:17 +0000 | [diff] [blame] | 548 | // Epilogs processed so far. |
| 549 | std::vector<MCSymbol *> AddedEpilogs; |
| 550 | |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 551 | for (auto &I : info->EpilogMap) { |
| 552 | MCSymbol *EpilogStart = I.first; |
| 553 | auto &EpilogInstrs = I.second; |
| 554 | uint32_t CodeBytes = ARM64CountOfUnwindCodes(EpilogInstrs); |
Sanjin Sijaric | b694030 | 2019-01-17 09:45:17 +0000 | [diff] [blame] | 555 | |
Sanjin Sijaric | b694030 | 2019-01-17 09:45:17 +0000 | [diff] [blame] | 556 | MCSymbol* MatchingEpilog = |
| 557 | FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info); |
| 558 | if (MatchingEpilog) { |
| 559 | assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() && |
| 560 | "Duplicate epilog not found"); |
Sanjin Sijaric | eaa421d | 2019-01-18 19:34:20 +0000 | [diff] [blame] | 561 | EpilogInfo[EpilogStart] = EpilogInfo.lookup(MatchingEpilog); |
Sanjin Sijaric | b694030 | 2019-01-17 09:45:17 +0000 | [diff] [blame] | 562 | // Clear the unwind codes in the EpilogMap, so that they don't get output |
| 563 | // in the logic below. |
| 564 | EpilogInstrs.clear(); |
| 565 | } else { |
| 566 | EpilogInfo[EpilogStart] = TotalCodeBytes; |
| 567 | TotalCodeBytes += CodeBytes; |
| 568 | AddedEpilogs.push_back(EpilogStart); |
| 569 | } |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // Code Words, Epilog count, E, X, Vers, Function Length |
| 573 | uint32_t row1 = 0x0; |
Eli Friedman | ab29667 | 2018-11-08 21:20:52 +0000 | [diff] [blame] | 574 | uint32_t CodeWords = TotalCodeBytes / 4; |
| 575 | uint32_t CodeWordsMod = TotalCodeBytes % 4; |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 576 | if (CodeWordsMod) |
| 577 | CodeWords++; |
| 578 | uint32_t EpilogCount = info->EpilogMap.size(); |
| 579 | bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124; |
| 580 | if (!ExtensionWord) { |
| 581 | row1 |= (EpilogCount & 0x1F) << 22; |
| 582 | row1 |= (CodeWords & 0x1F) << 27; |
| 583 | } |
| 584 | // E is always 0 right now, TODO: packed epilog setup |
| 585 | if (info->HandlesExceptions) // X |
| 586 | row1 |= 1 << 20; |
| 587 | row1 |= FuncLength & 0x3FFFF; |
| 588 | streamer.EmitIntValue(row1, 4); |
| 589 | |
| 590 | // Extended Code Words, Extended Epilog Count |
| 591 | if (ExtensionWord) { |
Eli Friedman | ab29667 | 2018-11-08 21:20:52 +0000 | [diff] [blame] | 592 | // FIXME: We should be able to split unwind info into multiple sections. |
| 593 | // FIXME: We should share epilog codes across epilogs, where possible, |
| 594 | // which would make this issue show up less frequently. |
| 595 | if (CodeWords > 0xFF || EpilogCount > 0xFFFF) |
| 596 | report_fatal_error("SEH unwind data splitting not yet implemented"); |
Sanjin Sijaric | 96f2ea3 | 2018-10-27 06:13:06 +0000 | [diff] [blame] | 597 | uint32_t row2 = 0x0; |
| 598 | row2 |= (CodeWords & 0xFF) << 16; |
| 599 | row2 |= (EpilogCount & 0xFFFF); |
| 600 | streamer.EmitIntValue(row2, 4); |
| 601 | } |
| 602 | |
| 603 | // Epilog Start Index, Epilog Start Offset |
| 604 | for (auto &I : EpilogInfo) { |
| 605 | MCSymbol *EpilogStart = I.first; |
| 606 | uint32_t EpilogIndex = I.second; |
| 607 | uint32_t EpilogOffset = |
| 608 | (uint32_t)GetAbsDifference(streamer, EpilogStart, info->Begin); |
| 609 | if (EpilogOffset) |
| 610 | EpilogOffset /= 4; |
| 611 | uint32_t row3 = EpilogOffset; |
| 612 | row3 |= (EpilogIndex & 0x3FF) << 22; |
| 613 | streamer.EmitIntValue(row3, 4); |
| 614 | } |
| 615 | |
| 616 | // Emit prolog unwind instructions (in reverse order). |
| 617 | uint8_t numInst = info->Instructions.size(); |
| 618 | for (uint8_t c = 0; c < numInst; ++c) { |
| 619 | WinEH::Instruction inst = info->Instructions.back(); |
| 620 | info->Instructions.pop_back(); |
| 621 | ARM64EmitUnwindCode(streamer, info->Begin, inst); |
| 622 | } |
| 623 | |
| 624 | // Emit epilog unwind instructions |
| 625 | for (auto &I : info->EpilogMap) { |
| 626 | auto &EpilogInstrs = I.second; |
| 627 | for (uint32_t i = 0; i < EpilogInstrs.size(); i++) { |
| 628 | WinEH::Instruction inst = EpilogInstrs[i]; |
| 629 | ARM64EmitUnwindCode(streamer, info->Begin, inst); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | int32_t BytesMod = CodeWords * 4 - TotalCodeBytes; |
| 634 | assert(BytesMod >= 0); |
| 635 | for (int i = 0; i < BytesMod; i++) |
| 636 | streamer.EmitIntValue(0xE3, 1); |
| 637 | |
| 638 | if (info->HandlesExceptions) |
| 639 | streamer.EmitValue( |
| 640 | MCSymbolRefExpr::create(info->ExceptionHandler, |
| 641 | MCSymbolRefExpr::VK_COFF_IMGREL32, context), |
| 642 | 4); |
| 643 | } |
| 644 | |
| 645 | static void ARM64EmitRuntimeFunction(MCStreamer &streamer, |
| 646 | const WinEH::FrameInfo *info) { |
| 647 | MCContext &context = streamer.getContext(); |
| 648 | |
| 649 | streamer.EmitValueToAlignment(4); |
| 650 | EmitSymbolRefWithOfs(streamer, info->Function, info->Begin); |
| 651 | streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol, |
| 652 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 653 | context), |
| 654 | 4); |
| 655 | } |
| 656 | |
| 657 | void llvm::Win64EH::ARM64UnwindEmitter::Emit(MCStreamer &Streamer) const { |
| 658 | // Emit the unwind info structs first. |
| 659 | for (const auto &CFI : Streamer.getWinFrameInfos()) { |
| 660 | MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection); |
| 661 | Streamer.SwitchSection(XData); |
| 662 | ARM64EmitUnwindInfo(Streamer, CFI.get()); |
| 663 | } |
| 664 | |
| 665 | // Now emit RUNTIME_FUNCTION entries. |
| 666 | for (const auto &CFI : Streamer.getWinFrameInfos()) { |
| 667 | MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection); |
| 668 | Streamer.SwitchSection(PData); |
| 669 | ARM64EmitRuntimeFunction(Streamer, CFI.get()); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void llvm::Win64EH::ARM64UnwindEmitter::EmitUnwindInfo( |
| 674 | MCStreamer &Streamer, WinEH::FrameInfo *info) const { |
| 675 | // Switch sections (the static function above is meant to be called from |
| 676 | // here and from Emit(). |
| 677 | MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection); |
| 678 | Streamer.SwitchSection(XData); |
| 679 | ARM64EmitUnwindInfo(Streamer, info); |
| 680 | } |