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-06-17 00:07:59 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          92 : static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
      23             :   uint8_t Count = 0;
      24         230 :   for (const auto &I : Insns) {
      25         138 :     switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
      26         127 :     case Win64EH::UOP_PushNonVol:
      27             :     case Win64EH::UOP_AllocSmall:
      28             :     case Win64EH::UOP_SetFPReg:
      29             :     case Win64EH::UOP_PushMachFrame:
      30         127 :       Count += 1;
      31         127 :       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          92 :   return Count;
      46             : }
      47             : 
      48         225 : static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
      49             :                               const MCSymbol *RHS) {
      50         225 :   MCContext &Context = Streamer.getContext();
      51             :   const MCExpr *Diff =
      52             :       MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
      53             :                               MCSymbolRefExpr::create(RHS, Context), Context);
      54         225 :   Streamer.EmitValue(Diff, 1);
      55         225 : }
      56             : 
      57         138 : static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
      58             :                            WinEH::Instruction &inst) {
      59             :   uint8_t b2;
      60             :   uint16_t w;
      61         138 :   b2 = (inst.Operation & 0x0F);
      62         138 :   switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
      63          47 :   case Win64EH::UOP_PushNonVol:
      64          47 :     EmitAbsDifference(streamer, inst.Label, begin);
      65          47 :     b2 |= (inst.Register & 0x0F) << 4;
      66          47 :     streamer.EmitIntValue(b2, 1);
      67          47 :     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          77 :   case Win64EH::UOP_AllocSmall:
      83          77 :     b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
      84          77 :     EmitAbsDifference(streamer, inst.Label, begin);
      85          77 :     streamer.EmitIntValue(b2, 1);
      86          77 :     break;
      87           2 :   case Win64EH::UOP_SetFPReg:
      88           2 :     EmitAbsDifference(streamer, inst.Label, begin);
      89           2 :     streamer.EmitIntValue(b2, 1);
      90           2 :     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         138 : }
     122             : 
     123         180 : static void EmitSymbolRefWithOfs(MCStreamer &streamer,
     124             :                                  const MCSymbol *Base,
     125             :                                  const MCSymbol *Other) {
     126         180 :   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         180 :   const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
     131             :                                               MCSymbolRefExpr::VK_COFF_IMGREL32,
     132         180 :                                               Context);
     133         360 :   streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
     134         180 : }
     135             : 
     136          90 : static void EmitRuntimeFunction(MCStreamer &streamer,
     137             :                                 const WinEH::FrameInfo *info) {
     138          90 :   MCContext &context = streamer.getContext();
     139             : 
     140          90 :   streamer.EmitValueToAlignment(4);
     141          90 :   EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
     142          90 :   EmitSymbolRefWithOfs(streamer, info->Function, info->End);
     143          90 :   streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
     144             :                                              MCSymbolRefExpr::VK_COFF_IMGREL32,
     145          90 :                                              context), 4);
     146          90 : }
     147             : 
     148         172 : static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
     149             :   // If this UNWIND_INFO already has a symbol, it's already been emitted.
     150         172 :   if (info->Symbol)
     151             :     return;
     152             : 
     153          92 :   MCContext &context = streamer.getContext();
     154          92 :   MCSymbol *Label = context.createTempSymbol();
     155             : 
     156          92 :   streamer.EmitValueToAlignment(4);
     157          92 :   streamer.EmitLabel(Label);
     158          92 :   info->Symbol = Label;
     159             : 
     160             :   // Upper 3 bits are the version number (currently 1).
     161             :   uint8_t flags = 0x01;
     162          92 :   if (info->ChainedParent)
     163             :     flags |= Win64EH::UNW_ChainInfo << 3;
     164             :   else {
     165          91 :     if (info->HandlesUnwind)
     166             :       flags |= Win64EH::UNW_TerminateHandler << 3;
     167          91 :     if (info->HandlesExceptions)
     168           3 :       flags |= Win64EH::UNW_ExceptionHandler << 3;
     169             :   }
     170          92 :   streamer.EmitIntValue(flags, 1);
     171             : 
     172          92 :   if (info->PrologEnd)
     173          87 :     EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
     174             :   else
     175           5 :     streamer.EmitIntValue(0, 1);
     176             : 
     177          92 :   uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
     178          92 :   streamer.EmitIntValue(numCodes, 1);
     179             : 
     180             :   uint8_t frame = 0;
     181          92 :   if (info->LastFrameInst >= 0) {
     182           2 :     WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
     183             :     assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
     184           2 :     frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
     185             :   }
     186          92 :   streamer.EmitIntValue(frame, 1);
     187             : 
     188             :   // Emit unwind instructions (in reverse order).
     189         184 :   uint8_t numInst = info->Instructions.size();
     190         368 :   for (uint8_t c = 0; c < numInst; ++c) {
     191         138 :     WinEH::Instruction inst = info->Instructions.back();
     192             :     info->Instructions.pop_back();
     193         138 :     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          92 :   if (numCodes & 1) {
     201          61 :     streamer.EmitIntValue(0, 2);
     202             :   }
     203             : 
     204          92 :   if (flags & (Win64EH::UNW_ChainInfo << 3))
     205           1 :     EmitRuntimeFunction(streamer, info->ChainedParent);
     206          91 :   else if (flags &
     207             :            ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
     208           3 :     streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
     209             :                                               MCSymbolRefExpr::VK_COFF_IMGREL32,
     210           3 :                                               context), 4);
     211          88 :   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          49 : void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
     220             :   // Emit the unwind info structs first.
     221         227 :   for (const auto &CFI : Streamer.getWinFrameInfos()) {
     222          89 :     MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
     223          89 :     Streamer.SwitchSection(XData);
     224          89 :     ::EmitUnwindInfo(Streamer, CFI.get());
     225             :   }
     226             : 
     227             :   // Now emit RUNTIME_FUNCTION entries.
     228         227 :   for (const auto &CFI : Streamer.getWinFrameInfos()) {
     229          89 :     MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
     230          89 :     Streamer.SwitchSection(PData);
     231          89 :     EmitRuntimeFunction(Streamer, CFI.get());
     232             :   }
     233          49 : }
     234             : 
     235          83 : 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          83 :   MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
     240          83 :   Streamer.SwitchSection(XData);
     241             : 
     242          83 :   ::EmitUnwindInfo(Streamer, info);
     243          83 : }
     244             : 

Generated by: LCOV version 1.13