LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFExpression.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 139 164 84.8 %
Date: 2018-07-13 00:08:38 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- DWARFExpression.cpp -----------------------------------------------===//
       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/DebugInfo/DWARF/DWARFExpression.h"
      11             : #include "llvm/BinaryFormat/Dwarf.h"
      12             : #include "llvm/MC/MCRegisterInfo.h"
      13             : #include "llvm/Support/Format.h"
      14             : #include <cassert>
      15             : #include <cstdint>
      16             : #include <vector>
      17             : 
      18             : using namespace llvm;
      19             : using namespace dwarf;
      20             : 
      21             : namespace llvm {
      22             : 
      23             : typedef std::vector<DWARFExpression::Operation::Description> DescVector;
      24             : 
      25         454 : static DescVector getDescriptions() {
      26             :   DescVector Descriptions;
      27             :   typedef DWARFExpression::Operation Op;
      28             :   typedef Op::Description Desc;
      29             : 
      30         454 :   Descriptions.resize(0xff);
      31         454 :   Descriptions[DW_OP_addr] = Desc(Op::Dwarf2, Op::SizeAddr);
      32         454 :   Descriptions[DW_OP_deref] = Desc(Op::Dwarf2);
      33         454 :   Descriptions[DW_OP_const1u] = Desc(Op::Dwarf2, Op::Size1);
      34         454 :   Descriptions[DW_OP_const1s] = Desc(Op::Dwarf2, Op::SignedSize1);
      35         454 :   Descriptions[DW_OP_const2u] = Desc(Op::Dwarf2, Op::Size2);
      36         454 :   Descriptions[DW_OP_const2s] = Desc(Op::Dwarf2, Op::SignedSize2);
      37         454 :   Descriptions[DW_OP_const4u] = Desc(Op::Dwarf2, Op::Size4);
      38         454 :   Descriptions[DW_OP_const4s] = Desc(Op::Dwarf2, Op::SignedSize4);
      39         454 :   Descriptions[DW_OP_const8u] = Desc(Op::Dwarf2, Op::Size8);
      40         454 :   Descriptions[DW_OP_const8s] = Desc(Op::Dwarf2, Op::SignedSize8);
      41         454 :   Descriptions[DW_OP_constu] = Desc(Op::Dwarf2, Op::SizeLEB);
      42         454 :   Descriptions[DW_OP_consts] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
      43         454 :   Descriptions[DW_OP_dup] = Desc(Op::Dwarf2);
      44         454 :   Descriptions[DW_OP_drop] = Desc(Op::Dwarf2);
      45         454 :   Descriptions[DW_OP_over] = Desc(Op::Dwarf2);
      46         454 :   Descriptions[DW_OP_pick] = Desc(Op::Dwarf2, Op::Size1);
      47         454 :   Descriptions[DW_OP_swap] = Desc(Op::Dwarf2);
      48         454 :   Descriptions[DW_OP_rot] = Desc(Op::Dwarf2);
      49         454 :   Descriptions[DW_OP_xderef] = Desc(Op::Dwarf2);
      50         454 :   Descriptions[DW_OP_abs] = Desc(Op::Dwarf2);
      51         454 :   Descriptions[DW_OP_and] = Desc(Op::Dwarf2);
      52         454 :   Descriptions[DW_OP_div] = Desc(Op::Dwarf2);
      53         454 :   Descriptions[DW_OP_minus] = Desc(Op::Dwarf2);
      54         454 :   Descriptions[DW_OP_mod] = Desc(Op::Dwarf2);
      55         454 :   Descriptions[DW_OP_mul] = Desc(Op::Dwarf2);
      56         454 :   Descriptions[DW_OP_neg] = Desc(Op::Dwarf2);
      57         454 :   Descriptions[DW_OP_not] = Desc(Op::Dwarf2);
      58         454 :   Descriptions[DW_OP_or] = Desc(Op::Dwarf2);
      59         454 :   Descriptions[DW_OP_plus] = Desc(Op::Dwarf2);
      60         454 :   Descriptions[DW_OP_plus_uconst] = Desc(Op::Dwarf2, Op::SizeLEB);
      61         454 :   Descriptions[DW_OP_shl] = Desc(Op::Dwarf2);
      62         454 :   Descriptions[DW_OP_shr] = Desc(Op::Dwarf2);
      63         454 :   Descriptions[DW_OP_shra] = Desc(Op::Dwarf2);
      64         454 :   Descriptions[DW_OP_xor] = Desc(Op::Dwarf2);
      65         454 :   Descriptions[DW_OP_skip] = Desc(Op::Dwarf2, Op::SignedSize2);
      66         454 :   Descriptions[DW_OP_bra] = Desc(Op::Dwarf2, Op::SignedSize2);
      67         454 :   Descriptions[DW_OP_eq] = Desc(Op::Dwarf2);
      68         454 :   Descriptions[DW_OP_ge] = Desc(Op::Dwarf2);
      69         454 :   Descriptions[DW_OP_gt] = Desc(Op::Dwarf2);
      70         454 :   Descriptions[DW_OP_le] = Desc(Op::Dwarf2);
      71         454 :   Descriptions[DW_OP_lt] = Desc(Op::Dwarf2);
      72         454 :   Descriptions[DW_OP_ne] = Desc(Op::Dwarf2);
      73       29510 :   for (uint16_t LA = DW_OP_lit0; LA <= DW_OP_lit31; ++LA)
      74       29056 :     Descriptions[LA] = Desc(Op::Dwarf2);
      75       29510 :   for (uint16_t LA = DW_OP_reg0; LA <= DW_OP_reg31; ++LA)
      76       29056 :     Descriptions[LA] = Desc(Op::Dwarf2);
      77       29510 :   for (uint16_t LA = DW_OP_breg0; LA <= DW_OP_breg31; ++LA)
      78       29056 :     Descriptions[LA] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
      79         454 :   Descriptions[DW_OP_regx] = Desc(Op::Dwarf2, Op::SizeLEB);
      80         454 :   Descriptions[DW_OP_fbreg] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
      81         454 :   Descriptions[DW_OP_bregx] = Desc(Op::Dwarf2, Op::SizeLEB, Op::SignedSizeLEB);
      82         454 :   Descriptions[DW_OP_piece] = Desc(Op::Dwarf2, Op::SizeLEB);
      83         454 :   Descriptions[DW_OP_deref_size] = Desc(Op::Dwarf2, Op::Size1);
      84         454 :   Descriptions[DW_OP_xderef_size] = Desc(Op::Dwarf2, Op::Size1);
      85         454 :   Descriptions[DW_OP_nop] = Desc(Op::Dwarf2);
      86         454 :   Descriptions[DW_OP_push_object_address] = Desc(Op::Dwarf3);
      87         454 :   Descriptions[DW_OP_call2] = Desc(Op::Dwarf3, Op::Size2);
      88         454 :   Descriptions[DW_OP_call4] = Desc(Op::Dwarf3, Op::Size4);
      89         454 :   Descriptions[DW_OP_call_ref] = Desc(Op::Dwarf3, Op::SizeRefAddr);
      90         454 :   Descriptions[DW_OP_form_tls_address] = Desc(Op::Dwarf3);
      91         454 :   Descriptions[DW_OP_call_frame_cfa] = Desc(Op::Dwarf3);
      92         454 :   Descriptions[DW_OP_bit_piece] = Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeLEB);
      93         454 :   Descriptions[DW_OP_implicit_value] =
      94             :       Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeBlock);
      95         454 :   Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf3);
      96         454 :   Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3);
      97         454 :   Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB);
      98         454 :   Descriptions[DW_OP_GNU_const_index] = Desc(Op::Dwarf4, Op::SizeLEB);
      99         454 :   return Descriptions;
     100             : }
     101             : 
     102        3120 : static DWARFExpression::Operation::Description getOpDesc(unsigned OpCode) {
     103             :   // FIXME: Make this constexpr once all compilers are smart enough to do it.
     104        3120 :   static DescVector Descriptions = getDescriptions();
     105             :   // Handle possible corrupted or unsupported operation.
     106        6240 :   if (OpCode >= Descriptions.size())
     107           3 :     return {};
     108        3117 :   return Descriptions[OpCode];
     109             : }
     110             : 
     111             : static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
     112           0 :   return (Version == 2) ? AddrSize : 4;
     113             : }
     114             : 
     115        3120 : bool DWARFExpression::Operation::extract(DataExtractor Data, uint16_t Version,
     116             :                                          uint8_t AddressSize, uint32_t Offset) {
     117        3120 :   Opcode = Data.getU8(&Offset);
     118             : 
     119        3120 :   Desc = getOpDesc(Opcode);
     120        3120 :   if (Desc.Version == Operation::DwarfNA) {
     121           3 :     EndOffset = Offset;
     122           3 :     return false;
     123             :   }
     124             : 
     125        6873 :   for (unsigned Operand = 0; Operand < 2; ++Operand) {
     126        4989 :     unsigned Size = Desc.Op[Operand];
     127        4989 :     unsigned Signed = Size & Operation::SignBit;
     128             : 
     129        4989 :     if (Size == Operation::SizeNA)
     130             :       break;
     131             : 
     132        1878 :     switch (Size & ~Operation::SignBit) {
     133           0 :     case Operation::Size1:
     134           0 :       Operands[Operand] = Data.getU8(&Offset);
     135           0 :       if (Signed)
     136           0 :         Operands[Operand] = (int8_t)Operands[Operand];
     137             :       break;
     138           0 :     case Operation::Size2:
     139           0 :       Operands[Operand] = Data.getU16(&Offset);
     140           0 :       if (Signed)
     141           0 :         Operands[Operand] = (int16_t)Operands[Operand];
     142             :       break;
     143           1 :     case Operation::Size4:
     144           1 :       Operands[Operand] = Data.getU32(&Offset);
     145           1 :       if (Signed)
     146           0 :         Operands[Operand] = (int32_t)Operands[Operand];
     147             :       break;
     148           8 :     case Operation::Size8:
     149           8 :       Operands[Operand] = Data.getU64(&Offset);
     150           8 :       break;
     151         635 :     case Operation::SizeAddr:
     152         635 :       if (AddressSize == 8) {
     153         606 :         Operands[Operand] = Data.getU64(&Offset);
     154             :       } else {
     155             :         assert(AddressSize == 4);
     156          29 :         Operands[Operand] = Data.getU32(&Offset);
     157             :       }
     158             :       break;
     159           0 :     case Operation::SizeRefAddr:
     160           0 :       if (getRefAddrSize(AddressSize, Version) == 8) {
     161           0 :         Operands[Operand] = Data.getU64(&Offset);
     162             :       } else {
     163             :         assert(getRefAddrSize(AddressSize, Version) == 4);
     164           0 :         Operands[Operand] = Data.getU32(&Offset);
     165             :       }
     166             :       break;
     167        1234 :     case Operation::SizeLEB:
     168        1234 :       if (Signed)
     169         844 :         Operands[Operand] = Data.getSLEB128(&Offset);
     170             :       else
     171         390 :         Operands[Operand] = Data.getULEB128(&Offset);
     172             :       break;
     173           0 :     case Operation::SizeBlock:
     174             :       // We need a size, so this cannot be the first operand
     175           0 :       if (Operand == 0)
     176             :         return false;
     177             :       // Store the offset of the block as the value.
     178           0 :       Operands[Operand] = Offset;
     179           0 :       Offset += Operands[Operand - 1];
     180           0 :       break;
     181           0 :     default:
     182           0 :       llvm_unreachable("Unknown DWARFExpression Op size");
     183             :     }
     184             :   }
     185             : 
     186        3117 :   EndOffset = Offset;
     187        3117 :   return true;
     188             : }
     189             : 
     190        1193 : static bool prettyPrintRegisterOp(raw_ostream &OS, uint8_t Opcode,
     191             :                                   uint64_t Operands[2],
     192             :                                   const MCRegisterInfo *MRI, bool isEH) {
     193        1193 :   if (!MRI)
     194             :     return false;
     195             : 
     196             :   uint64_t DwarfRegNum;
     197             :   unsigned OpNum = 0;
     198             : 
     199        1143 :   if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx)
     200          18 :     DwarfRegNum = Operands[OpNum++];
     201        1125 :   else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
     202         184 :     DwarfRegNum = Opcode - DW_OP_breg0;
     203             :   else
     204         941 :     DwarfRegNum = Opcode - DW_OP_reg0;
     205             : 
     206        1143 :   int LLVMRegNum = MRI->getLLVMRegNum(DwarfRegNum, isEH);
     207        1143 :   if (LLVMRegNum >= 0) {
     208        2286 :     if (const char *RegName = MRI->getName(LLVMRegNum)) {
     209        1143 :       if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
     210             :           Opcode == DW_OP_bregx)
     211         368 :         OS << format(" %s%+" PRId64, RegName, Operands[OpNum]);
     212             :       else
     213         959 :         OS << ' ' << RegName;
     214             :       return true;
     215             :     }
     216             :   }
     217             : 
     218             :   return false;
     219             : }
     220             : 
     221        2706 : bool DWARFExpression::Operation::print(raw_ostream &OS,
     222             :                                        const DWARFExpression *Expr,
     223             :                                        const MCRegisterInfo *RegInfo,
     224             :                                        bool isEH) {
     225        2706 :   if (Error) {
     226           2 :     OS << "<decoding error>";
     227           2 :     return false;
     228             :   }
     229             : 
     230        2704 :   StringRef Name = OperationEncodingString(Opcode);
     231             :   assert(!Name.empty() && "DW_OP has no name!");
     232        2704 :   OS << Name;
     233             : 
     234        5408 :   if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
     235        2704 :       (Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) ||
     236        1529 :       Opcode == DW_OP_bregx || Opcode == DW_OP_regx)
     237        1193 :     if (prettyPrintRegisterOp(OS, Opcode, Operands, RegInfo, isEH))
     238             :       return true;
     239             : 
     240        4143 :   for (unsigned Operand = 0; Operand < 2; ++Operand) {
     241        2846 :     unsigned Size = Desc.Op[Operand];
     242             :     unsigned Signed = Size & Operation::SignBit;
     243             : 
     244        2846 :     if (Size == Operation::SizeNA)
     245             :       break;
     246             : 
     247        1291 :     if (Size == Operation::SizeBlock) {
     248           0 :       uint32_t Offset = Operands[Operand];
     249           0 :       for (unsigned i = 0; i < Operands[Operand - 1]; ++i)
     250           0 :         OS << format(" 0x%02x", Expr->Data.getU8(&Offset));
     251             :     } else {
     252        1291 :       if (Signed)
     253        1182 :         OS << format(" %+" PRId64, (int64_t)Operands[Operand]);
     254             :       else
     255        1400 :         OS << format(" 0x%" PRIx64, Operands[Operand]);
     256             :     }
     257             :   }
     258             :   return true;
     259             : }
     260             : 
     261        2257 : void DWARFExpression::print(raw_ostream &OS, const MCRegisterInfo *RegInfo,
     262             :                             bool IsEH) const {
     263        2704 :   for (auto &Op : *this) {
     264        2706 :     if (!Op.print(OS, this, RegInfo, IsEH)) {
     265           2 :       uint32_t FailOffset = Op.getEndOffset();
     266           2 :       while (FailOffset < Data.getData().size())
     267           0 :         OS << format(" %02x", Data.getU8(&FailOffset));
     268             :       return;
     269             :     }
     270        2704 :     if (Op.getEndOffset() < Data.getData().size())
     271         450 :       OS << ", ";
     272             :   }
     273             : }
     274             : 
     275             : } // namespace llvm

Generated by: LCOV version 1.13