LCOV - code coverage report
Current view: top level - lib/MC - MCWin64EH.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 120 139 86.3 %
Date: 2018-10-20 13:21:21 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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"
      11             : #include "llvm/ADT/Twine.h"
      12             : #include "llvm/MC/MCContext.h"
      13             : #include "llvm/MC/MCExpr.h"
      14             : #include "llvm/MC/MCStreamer.h"
      15             : #include "llvm/MC/MCSymbol.h"
      16             : #include "llvm/Support/Win64EH.h"
      17             : 
      18             : using namespace llvm;
      19             : 
      20             : // NOTE: All relocations generated here are 4-byte image-relative.
      21             : 
      22         108 : static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
      23             :   uint8_t Count = 0;
      24         266 :   for (const auto &I : Insns) {
      25         158 :     switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
      26         147 :     case Win64EH::UOP_PushNonVol:
      27             :     case Win64EH::UOP_AllocSmall:
      28             :     case Win64EH::UOP_SetFPReg:
      29             :     case Win64EH::UOP_PushMachFrame:
      30         147 :       Count += 1;
      31         147 :       break;
      32           4 :     case Win64EH::UOP_SaveNonVol:
      33             :     case Win64EH::UOP_SaveXMM128:
      34           4 :       Count += 2;
      35           4 :       break;
      36           0 :     case Win64EH::UOP_SaveNonVolBig:
      37             :     case Win64EH::UOP_SaveXMM128Big:
      38           0 :       Count += 3;
      39           0 :       break;
      40           7 :     case Win64EH::UOP_AllocLarge:
      41           7 :       Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
      42           7 :       break;
      43             :     }
      44             :   }
      45         108 :   return Count;
      46             : }
      47             : 
      48         261 : static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
      49             :                               const MCSymbol *RHS) {
      50         261 :   MCContext &Context = Streamer.getContext();
      51             :   const MCExpr *Diff =
      52             :       MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
      53             :                               MCSymbolRefExpr::create(RHS, Context), Context);
      54         261 :   Streamer.EmitValue(Diff, 1);
      55         261 : }
      56             : 
      57         158 : static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
      58             :                            WinEH::Instruction &inst) {
      59             :   uint8_t b2;
      60             :   uint16_t w;
      61         158 :   b2 = (inst.Operation & 0x0F);
      62         158 :   switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
      63          49 :   case Win64EH::UOP_PushNonVol:
      64          49 :     EmitAbsDifference(streamer, inst.Label, begin);
      65          49 :     b2 |= (inst.Register & 0x0F) << 4;
      66          49 :     streamer.EmitIntValue(b2, 1);
      67          49 :     break;
      68           7 :   case Win64EH::UOP_AllocLarge:
      69           7 :     EmitAbsDifference(streamer, inst.Label, begin);
      70           7 :     if (inst.Offset > 512 * 1024 - 8) {
      71           0 :       b2 |= 0x10;
      72           0 :       streamer.EmitIntValue(b2, 1);
      73           0 :       w = inst.Offset & 0xFFF8;
      74           0 :       streamer.EmitIntValue(w, 2);
      75           0 :       w = inst.Offset >> 16;
      76             :     } else {
      77           7 :       streamer.EmitIntValue(b2, 1);
      78           7 :       w = inst.Offset >> 3;
      79             :     }
      80           7 :     streamer.EmitIntValue(w, 2);
      81           7 :     break;
      82          93 :   case Win64EH::UOP_AllocSmall:
      83          93 :     b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
      84          93 :     EmitAbsDifference(streamer, inst.Label, begin);
      85          93 :     streamer.EmitIntValue(b2, 1);
      86          93 :     break;
      87           4 :   case Win64EH::UOP_SetFPReg:
      88           4 :     EmitAbsDifference(streamer, inst.Label, begin);
      89           4 :     streamer.EmitIntValue(b2, 1);
      90           4 :     break;
      91           4 :   case Win64EH::UOP_SaveNonVol:
      92             :   case Win64EH::UOP_SaveXMM128:
      93           4 :     b2 |= (inst.Register & 0x0F) << 4;
      94           4 :     EmitAbsDifference(streamer, inst.Label, begin);
      95           4 :     streamer.EmitIntValue(b2, 1);
      96           4 :     w = inst.Offset >> 3;
      97           4 :     if (inst.Operation == Win64EH::UOP_SaveXMM128)
      98           3 :       w >>= 1;
      99           4 :     streamer.EmitIntValue(w, 2);
     100           4 :     break;
     101           0 :   case Win64EH::UOP_SaveNonVolBig:
     102             :   case Win64EH::UOP_SaveXMM128Big:
     103           0 :     b2 |= (inst.Register & 0x0F) << 4;
     104           0 :     EmitAbsDifference(streamer, inst.Label, begin);
     105           0 :     streamer.EmitIntValue(b2, 1);
     106           0 :     if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
     107           0 :       w = inst.Offset & 0xFFF0;
     108             :     else
     109           0 :       w = inst.Offset & 0xFFF8;
     110           0 :     streamer.EmitIntValue(w, 2);
     111           0 :     w = inst.Offset >> 16;
     112           0 :     streamer.EmitIntValue(w, 2);
     113           0 :     break;
     114           1 :   case Win64EH::UOP_PushMachFrame:
     115           1 :     if (inst.Offset == 1)
     116           1 :       b2 |= 0x10;
     117           1 :     EmitAbsDifference(streamer, inst.Label, begin);
     118           1 :     streamer.EmitIntValue(b2, 1);
     119           1 :     break;
     120             :   }
     121         158 : }
     122             : 
     123         212 : static void EmitSymbolRefWithOfs(MCStreamer &streamer,
     124             :                                  const MCSymbol *Base,
     125             :                                  const MCSymbol *Other) {
     126         212 :   MCContext &Context = streamer.getContext();
     127             :   const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
     128             :   const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
     129             :   const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
     130         424 :   const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
     131             :                                               MCSymbolRefExpr::VK_COFF_IMGREL32,
     132         212 :                                               Context);
     133         212 :   streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
     134         212 : }
     135             : 
     136         106 : static void EmitRuntimeFunction(MCStreamer &streamer,
     137             :                                 const WinEH::FrameInfo *info) {
     138         106 :   MCContext &context = streamer.getContext();
     139             : 
     140         106 :   streamer.EmitValueToAlignment(4);
     141         106 :   EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
     142         106 :   EmitSymbolRefWithOfs(streamer, info->Function, info->End);
     143         106 :   streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
     144             :                                              MCSymbolRefExpr::VK_COFF_IMGREL32,
     145         106 :                                              context), 4);
     146         106 : }
     147             : 
     148         204 : static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
     149             :   // If this UNWIND_INFO already has a symbol, it's already been emitted.
     150         204 :   if (info->Symbol)
     151             :     return;
     152             : 
     153         108 :   MCContext &context = streamer.getContext();
     154         108 :   MCSymbol *Label = context.createTempSymbol();
     155             : 
     156         108 :   streamer.EmitValueToAlignment(4);
     157         108 :   streamer.EmitLabel(Label);
     158         108 :   info->Symbol = Label;
     159             : 
     160             :   // Upper 3 bits are the version number (currently 1).
     161             :   uint8_t flags = 0x01;
     162         108 :   if (info->ChainedParent)
     163             :     flags |= Win64EH::UNW_ChainInfo << 3;
     164             :   else {
     165         107 :     if (info->HandlesUnwind)
     166             :       flags |= Win64EH::UNW_TerminateHandler << 3;
     167         107 :     if (info->HandlesExceptions)
     168           4 :       flags |= Win64EH::UNW_ExceptionHandler << 3;
     169             :   }
     170         108 :   streamer.EmitIntValue(flags, 1);
     171             : 
     172         108 :   if (info->PrologEnd)
     173         103 :     EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
     174             :   else
     175           5 :     streamer.EmitIntValue(0, 1);
     176             : 
     177         108 :   uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
     178         108 :   streamer.EmitIntValue(numCodes, 1);
     179             : 
     180             :   uint8_t frame = 0;
     181         108 :   if (info->LastFrameInst >= 0) {
     182           4 :     WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
     183             :     assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
     184           4 :     frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
     185             :   }
     186         108 :   streamer.EmitIntValue(frame, 1);
     187             : 
     188             :   // Emit unwind instructions (in reverse order).
     189         216 :   uint8_t numInst = info->Instructions.size();
     190         266 :   for (uint8_t c = 0; c < numInst; ++c) {
     191         158 :     WinEH::Instruction inst = info->Instructions.back();
     192             :     info->Instructions.pop_back();
     193         158 :     EmitUnwindCode(streamer, info->Begin, inst);
     194             :   }
     195             : 
     196             :   // For alignment purposes, the instruction array will always have an even
     197             :   // number of entries, with the final entry potentially unused (in which case
     198             :   // the array will be one longer than indicated by the count of unwind codes
     199             :   // field).
     200         108 :   if (numCodes & 1) {
     201          77 :     streamer.EmitIntValue(0, 2);
     202             :   }
     203             : 
     204         108 :   if (flags & (Win64EH::UNW_ChainInfo << 3))
     205           1 :     EmitRuntimeFunction(streamer, info->ChainedParent);
     206         107 :   else if (flags &
     207             :            ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
     208           4 :     streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
     209             :                                               MCSymbolRefExpr::VK_COFF_IMGREL32,
     210           4 :                                               context), 4);
     211         103 :   else if (numCodes == 0) {
     212             :     // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
     213             :     // a chained unwind info, if there is no handler, and if there are fewer
     214             :     // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
     215           5 :     streamer.EmitIntValue(0, 4);
     216             :   }
     217             : }
     218             : 
     219          52 : void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
     220             :   // Emit the unwind info structs first.
     221         157 :   for (const auto &CFI : Streamer.getWinFrameInfos()) {
     222         105 :     MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
     223         105 :     Streamer.SwitchSection(XData);
     224         105 :     ::EmitUnwindInfo(Streamer, CFI.get());
     225             :   }
     226             : 
     227             :   // Now emit RUNTIME_FUNCTION entries.
     228         157 :   for (const auto &CFI : Streamer.getWinFrameInfos()) {
     229         105 :     MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
     230         105 :     Streamer.SwitchSection(PData);
     231         105 :     EmitRuntimeFunction(Streamer, CFI.get());
     232             :   }
     233          52 : }
     234             : 
     235          99 : void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo(
     236             :     MCStreamer &Streamer, WinEH::FrameInfo *info) const {
     237             :   // Switch sections (the static function above is meant to be called from
     238             :   // here and from Emit().
     239          99 :   MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
     240          99 :   Streamer.SwitchSection(XData);
     241             : 
     242          99 :   ::EmitUnwindInfo(Streamer, info);
     243          99 : }
     244             : 

Generated by: LCOV version 1.13