1 //===-- DWARFExpression.cpp -----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 10 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 11 #include "llvm/MC/MCRegisterInfo.h" 12 #include "llvm/Support/Format.h" 13 #include <cassert> 14 #include <cstdint> 15 #include <vector> 16 17 using namespace llvm; 18 using namespace dwarf; 19 20 namespace llvm { 21 22 typedef std::vector<DWARFExpression::Operation::Description> DescVector; 23 24 static DescVector getDescriptions() { 25 DescVector Descriptions; 26 typedef DWARFExpression::Operation Op; 27 typedef Op::Description Desc; 28 29 Descriptions.resize(0xff); 30 Descriptions[DW_OP_addr] = Desc(Op::Dwarf2, Op::SizeAddr); 31 Descriptions[DW_OP_deref] = Desc(Op::Dwarf2); 32 Descriptions[DW_OP_const1u] = Desc(Op::Dwarf2, Op::Size1); 33 Descriptions[DW_OP_const1s] = Desc(Op::Dwarf2, Op::SignedSize1); 34 Descriptions[DW_OP_const2u] = Desc(Op::Dwarf2, Op::Size2); 35 Descriptions[DW_OP_const2s] = Desc(Op::Dwarf2, Op::SignedSize2); 36 Descriptions[DW_OP_const4u] = Desc(Op::Dwarf2, Op::Size4); 37 Descriptions[DW_OP_const4s] = Desc(Op::Dwarf2, Op::SignedSize4); 38 Descriptions[DW_OP_const8u] = Desc(Op::Dwarf2, Op::Size8); 39 Descriptions[DW_OP_const8s] = Desc(Op::Dwarf2, Op::SignedSize8); 40 Descriptions[DW_OP_constu] = Desc(Op::Dwarf2, Op::SizeLEB); 41 Descriptions[DW_OP_consts] = Desc(Op::Dwarf2, Op::SignedSizeLEB); 42 Descriptions[DW_OP_dup] = Desc(Op::Dwarf2); 43 Descriptions[DW_OP_drop] = Desc(Op::Dwarf2); 44 Descriptions[DW_OP_over] = Desc(Op::Dwarf2); 45 Descriptions[DW_OP_pick] = Desc(Op::Dwarf2, Op::Size1); 46 Descriptions[DW_OP_swap] = Desc(Op::Dwarf2); 47 Descriptions[DW_OP_rot] = Desc(Op::Dwarf2); 48 Descriptions[DW_OP_xderef] = Desc(Op::Dwarf2); 49 Descriptions[DW_OP_abs] = Desc(Op::Dwarf2); 50 Descriptions[DW_OP_and] = Desc(Op::Dwarf2); 51 Descriptions[DW_OP_div] = Desc(Op::Dwarf2); 52 Descriptions[DW_OP_minus] = Desc(Op::Dwarf2); 53 Descriptions[DW_OP_mod] = Desc(Op::Dwarf2); 54 Descriptions[DW_OP_mul] = Desc(Op::Dwarf2); 55 Descriptions[DW_OP_neg] = Desc(Op::Dwarf2); 56 Descriptions[DW_OP_not] = Desc(Op::Dwarf2); 57 Descriptions[DW_OP_or] = Desc(Op::Dwarf2); 58 Descriptions[DW_OP_plus] = Desc(Op::Dwarf2); 59 Descriptions[DW_OP_plus_uconst] = Desc(Op::Dwarf2, Op::SizeLEB); 60 Descriptions[DW_OP_shl] = Desc(Op::Dwarf2); 61 Descriptions[DW_OP_shr] = Desc(Op::Dwarf2); 62 Descriptions[DW_OP_shra] = Desc(Op::Dwarf2); 63 Descriptions[DW_OP_xor] = Desc(Op::Dwarf2); 64 Descriptions[DW_OP_skip] = Desc(Op::Dwarf2, Op::SignedSize2); 65 Descriptions[DW_OP_bra] = Desc(Op::Dwarf2, Op::SignedSize2); 66 Descriptions[DW_OP_eq] = Desc(Op::Dwarf2); 67 Descriptions[DW_OP_ge] = Desc(Op::Dwarf2); 68 Descriptions[DW_OP_gt] = Desc(Op::Dwarf2); 69 Descriptions[DW_OP_le] = Desc(Op::Dwarf2); 70 Descriptions[DW_OP_lt] = Desc(Op::Dwarf2); 71 Descriptions[DW_OP_ne] = Desc(Op::Dwarf2); 72 for (uint16_t LA = DW_OP_lit0; LA <= DW_OP_lit31; ++LA) 73 Descriptions[LA] = Desc(Op::Dwarf2); 74 for (uint16_t LA = DW_OP_reg0; LA <= DW_OP_reg31; ++LA) 75 Descriptions[LA] = Desc(Op::Dwarf2); 76 for (uint16_t LA = DW_OP_breg0; LA <= DW_OP_breg31; ++LA) 77 Descriptions[LA] = Desc(Op::Dwarf2, Op::SignedSizeLEB); 78 Descriptions[DW_OP_regx] = Desc(Op::Dwarf2, Op::SizeLEB); 79 Descriptions[DW_OP_fbreg] = Desc(Op::Dwarf2, Op::SignedSizeLEB); 80 Descriptions[DW_OP_bregx] = Desc(Op::Dwarf2, Op::SizeLEB, Op::SignedSizeLEB); 81 Descriptions[DW_OP_piece] = Desc(Op::Dwarf2, Op::SizeLEB); 82 Descriptions[DW_OP_deref_size] = Desc(Op::Dwarf2, Op::Size1); 83 Descriptions[DW_OP_xderef_size] = Desc(Op::Dwarf2, Op::Size1); 84 Descriptions[DW_OP_nop] = Desc(Op::Dwarf2); 85 Descriptions[DW_OP_push_object_address] = Desc(Op::Dwarf3); 86 Descriptions[DW_OP_call2] = Desc(Op::Dwarf3, Op::Size2); 87 Descriptions[DW_OP_call4] = Desc(Op::Dwarf3, Op::Size4); 88 Descriptions[DW_OP_call_ref] = Desc(Op::Dwarf3, Op::SizeRefAddr); 89 Descriptions[DW_OP_form_tls_address] = Desc(Op::Dwarf3); 90 Descriptions[DW_OP_call_frame_cfa] = Desc(Op::Dwarf3); 91 Descriptions[DW_OP_bit_piece] = Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeLEB); 92 Descriptions[DW_OP_implicit_value] = 93 Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeBlock); 94 Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf3); 95 Descriptions[DW_OP_WASM_location] = 96 Desc(Op::Dwarf4, Op::SizeLEB, Op::WasmLocationArg); 97 Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3); 98 Descriptions[DW_OP_addrx] = Desc(Op::Dwarf4, Op::SizeLEB); 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 Descriptions[DW_OP_GNU_entry_value] = Desc(Op::Dwarf4, Op::SizeLEB); 102 103 Descriptions[DW_OP_convert] = Desc(Op::Dwarf5, Op::BaseTypeRef); 104 Descriptions[DW_OP_entry_value] = Desc(Op::Dwarf5, Op::SizeLEB); 105 Descriptions[DW_OP_regval_type] = 106 Desc(Op::Dwarf5, Op::SizeLEB, Op::BaseTypeRef); 107 108 return Descriptions; 109 } 110 111 static DWARFExpression::Operation::Description getOpDesc(unsigned OpCode) { 112 // FIXME: Make this constexpr once all compilers are smart enough to do it. 113 static DescVector Descriptions = getDescriptions(); 114 // Handle possible corrupted or unsupported operation. 115 if (OpCode >= Descriptions.size()) 116 return {}; 117 return Descriptions[OpCode]; 118 } 119 120 bool DWARFExpression::Operation::extract(DataExtractor Data, 121 uint8_t AddressSize, uint64_t Offset, 122 Optional<DwarfFormat> Format) { 123 Opcode = Data.getU8(&Offset); 124 125 Desc = getOpDesc(Opcode); 126 if (Desc.Version == Operation::DwarfNA) { 127 EndOffset = Offset; 128 return false; 129 } 130 131 for (unsigned Operand = 0; Operand < 2; ++Operand) { 132 unsigned Size = Desc.Op[Operand]; 133 unsigned Signed = Size & Operation::SignBit; 134 135 if (Size == Operation::SizeNA) 136 break; 137 138 EndOffset = Offset; 139 switch (Size & ~Operation::SignBit) { 140 case Operation::Size1: 141 Operands[Operand] = Data.getU8(&Offset); 142 if (Signed) 143 Operands[Operand] = (int8_t)Operands[Operand]; 144 break; 145 case Operation::Size2: 146 Operands[Operand] = Data.getU16(&Offset); 147 if (Signed) 148 Operands[Operand] = (int16_t)Operands[Operand]; 149 break; 150 case Operation::Size4: 151 Operands[Operand] = Data.getU32(&Offset); 152 if (Signed) 153 Operands[Operand] = (int32_t)Operands[Operand]; 154 break; 155 case Operation::Size8: 156 Operands[Operand] = Data.getU64(&Offset); 157 break; 158 case Operation::SizeAddr: 159 Operands[Operand] = Data.getUnsigned(&Offset, AddressSize); 160 break; 161 case Operation::SizeRefAddr: 162 if (!Format) 163 return false; 164 Operands[Operand] = 165 Data.getUnsigned(&Offset, dwarf::getDwarfOffsetByteSize(*Format)); 166 break; 167 case Operation::SizeLEB: 168 if (Signed) 169 Operands[Operand] = Data.getSLEB128(&Offset); 170 else 171 Operands[Operand] = Data.getULEB128(&Offset); 172 break; 173 case Operation::BaseTypeRef: 174 Operands[Operand] = Data.getULEB128(&Offset); 175 break; 176 case Operation::WasmLocationArg: 177 assert(Operand == 1); 178 switch (Operands[0]) { 179 case 0: case 1: case 2: 180 Operands[Operand] = Data.getULEB128(&Offset); 181 break; 182 case 3: // global as uint32 183 Operands[Operand] = Data.getU32(&Offset); 184 break; 185 default: 186 return false; // Unknown Wasm location 187 } 188 break; 189 case Operation::SizeBlock: 190 // We need a size, so this cannot be the first operand 191 if (Operand == 0) 192 return false; 193 // Store the offset of the block as the value. 194 Operands[Operand] = Offset; 195 Offset += Operands[Operand - 1]; 196 break; 197 default: 198 llvm_unreachable("Unknown DWARFExpression Op size"); 199 } 200 201 OperandEndOffsets[Operand] = Offset; 202 } 203 204 EndOffset = Offset; 205 return true; 206 } 207 208 static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS, 209 uint64_t Operands[2], unsigned Operand) { 210 assert(Operand < 2 && "operand out of bounds"); 211 auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]); 212 if (Die && Die.getTag() == dwarf::DW_TAG_base_type) { 213 OS << format(" (0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]); 214 if (auto Name = Die.find(dwarf::DW_AT_name)) 215 OS << " \"" << Name->getAsCString() << "\""; 216 } else { 217 OS << format(" <invalid base_type ref: 0x%" PRIx64 ">", 218 Operands[Operand]); 219 } 220 } 221 222 static bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS, uint8_t Opcode, 223 uint64_t Operands[2], 224 const MCRegisterInfo *MRI, bool isEH) { 225 if (!MRI) 226 return false; 227 228 uint64_t DwarfRegNum; 229 unsigned OpNum = 0; 230 231 if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx || 232 Opcode == DW_OP_regval_type) 233 DwarfRegNum = Operands[OpNum++]; 234 else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx) 235 DwarfRegNum = Opcode - DW_OP_breg0; 236 else 237 DwarfRegNum = Opcode - DW_OP_reg0; 238 239 if (Optional<unsigned> LLVMRegNum = MRI->getLLVMRegNum(DwarfRegNum, isEH)) { 240 if (const char *RegName = MRI->getName(*LLVMRegNum)) { 241 if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) || 242 Opcode == DW_OP_bregx) 243 OS << format(" %s%+" PRId64, RegName, Operands[OpNum]); 244 else 245 OS << ' ' << RegName; 246 247 if (Opcode == DW_OP_regval_type) 248 prettyPrintBaseTypeRef(U, OS, Operands, 1); 249 return true; 250 } 251 } 252 253 return false; 254 } 255 256 bool DWARFExpression::Operation::print(raw_ostream &OS, 257 const DWARFExpression *Expr, 258 const MCRegisterInfo *RegInfo, 259 DWARFUnit *U, 260 bool isEH) { 261 if (Error) { 262 OS << "<decoding error>"; 263 return false; 264 } 265 266 StringRef Name = OperationEncodingString(Opcode); 267 assert(!Name.empty() && "DW_OP has no name!"); 268 OS << Name; 269 270 if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) || 271 (Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) || 272 Opcode == DW_OP_bregx || Opcode == DW_OP_regx || 273 Opcode == DW_OP_regval_type) 274 if (prettyPrintRegisterOp(U, OS, Opcode, Operands, RegInfo, isEH)) 275 return true; 276 277 for (unsigned Operand = 0; Operand < 2; ++Operand) { 278 unsigned Size = Desc.Op[Operand]; 279 unsigned Signed = Size & Operation::SignBit; 280 281 if (Size == Operation::SizeNA) 282 break; 283 284 if (Size == Operation::BaseTypeRef && U) { 285 // For DW_OP_convert the operand may be 0 to indicate that conversion to 286 // the generic type should be done. The same holds for DW_OP_reinterpret, 287 // which is currently not supported. 288 if (Opcode == DW_OP_convert && Operands[Operand] == 0) 289 OS << " 0x0"; 290 else 291 prettyPrintBaseTypeRef(U, OS, Operands, Operand); 292 } else if (Size == Operation::WasmLocationArg) { 293 assert(Operand == 1); 294 switch (Operands[0]) { 295 case 0: case 1: case 2: 296 case 3: // global as uint32 297 OS << format(" 0x%" PRIx64, Operands[Operand]); 298 break; 299 default: assert(false); 300 } 301 } else if (Size == Operation::SizeBlock) { 302 uint64_t Offset = Operands[Operand]; 303 for (unsigned i = 0; i < Operands[Operand - 1]; ++i) 304 OS << format(" 0x%02x", Expr->Data.getU8(&Offset)); 305 } else { 306 if (Signed) 307 OS << format(" %+" PRId64, (int64_t)Operands[Operand]); 308 else if (Opcode != DW_OP_entry_value && 309 Opcode != DW_OP_GNU_entry_value) 310 OS << format(" 0x%" PRIx64, Operands[Operand]); 311 } 312 } 313 return true; 314 } 315 316 void DWARFExpression::print(raw_ostream &OS, const MCRegisterInfo *RegInfo, 317 DWARFUnit *U, bool IsEH) const { 318 uint32_t EntryValExprSize = 0; 319 for (auto &Op : *this) { 320 if (!Op.print(OS, this, RegInfo, U, IsEH)) { 321 uint64_t FailOffset = Op.getEndOffset(); 322 while (FailOffset < Data.getData().size()) 323 OS << format(" %02x", Data.getU8(&FailOffset)); 324 return; 325 } 326 327 if (Op.getCode() == DW_OP_entry_value || 328 Op.getCode() == DW_OP_GNU_entry_value) { 329 OS << "("; 330 EntryValExprSize = Op.getRawOperand(0); 331 continue; 332 } 333 334 if (EntryValExprSize) { 335 EntryValExprSize--; 336 if (EntryValExprSize == 0) 337 OS << ")"; 338 } 339 340 if (Op.getEndOffset() < Data.getData().size()) 341 OS << ", "; 342 } 343 } 344 345 bool DWARFExpression::Operation::verify(DWARFUnit *U) { 346 347 for (unsigned Operand = 0; Operand < 2; ++Operand) { 348 unsigned Size = Desc.Op[Operand]; 349 350 if (Size == Operation::SizeNA) 351 break; 352 353 if (Size == Operation::BaseTypeRef) { 354 // For DW_OP_convert the operand may be 0 to indicate that conversion to 355 // the generic type should be done, so don't look up a base type in that 356 // case. The same holds for DW_OP_reinterpret, which is currently not 357 // supported. 358 if (Opcode == DW_OP_convert && Operands[Operand] == 0) 359 continue; 360 auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]); 361 if (!Die || Die.getTag() != dwarf::DW_TAG_base_type) { 362 Error = true; 363 return false; 364 } 365 } 366 } 367 368 return true; 369 } 370 371 bool DWARFExpression::verify(DWARFUnit *U) { 372 for (auto &Op : *this) 373 if (!Op.verify(U)) 374 return false; 375 376 return true; 377 } 378 379 } // namespace llvm 380