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