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

Generated by: LCOV version 1.13