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