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::SignedSizeLEB);
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 
107   return Descriptions;
108 }
109 
110 static DWARFExpression::Operation::Description getOpDesc(unsigned OpCode) {
111   // FIXME: Make this constexpr once all compilers are smart enough to do it.
112   static DescVector Descriptions = getDescriptions();
113   // Handle possible corrupted or unsupported operation.
114   if (OpCode >= Descriptions.size())
115     return {};
116   return Descriptions[OpCode];
117 }
118 
119 static uint8_t getRefAddrSize(uint8_t AddrSize, uint16_t Version) {
120   return (Version == 2) ? AddrSize : 4;
121 }
122 
123 bool DWARFExpression::Operation::extract(DataExtractor Data, uint16_t Version,
124                                          uint8_t AddressSize, uint64_t Offset) {
125   Opcode = Data.getU8(&Offset);
126 
127   Desc = getOpDesc(Opcode);
128   if (Desc.Version == Operation::DwarfNA) {
129     EndOffset = Offset;
130     return false;
131   }
132 
133   for (unsigned Operand = 0; Operand < 2; ++Operand) {
134     unsigned Size = Desc.Op[Operand];
135     unsigned Signed = Size & Operation::SignBit;
136 
137     if (Size == Operation::SizeNA)
138       break;
139 
140     switch (Size & ~Operation::SignBit) {
141     case Operation::Size1:
142       Operands[Operand] = Data.getU8(&Offset);
143       if (Signed)
144         Operands[Operand] = (int8_t)Operands[Operand];
145       break;
146     case Operation::Size2:
147       Operands[Operand] = Data.getU16(&Offset);
148       if (Signed)
149         Operands[Operand] = (int16_t)Operands[Operand];
150       break;
151     case Operation::Size4:
152       Operands[Operand] = Data.getU32(&Offset);
153       if (Signed)
154         Operands[Operand] = (int32_t)Operands[Operand];
155       break;
156     case Operation::Size8:
157       Operands[Operand] = Data.getU64(&Offset);
158       break;
159     case Operation::SizeAddr:
160       if (AddressSize == 8) {
161         Operands[Operand] = Data.getU64(&Offset);
162       } else if (AddressSize == 4) {
163         Operands[Operand] = Data.getU32(&Offset);
164       } else {
165         assert(AddressSize == 2);
166         Operands[Operand] = Data.getU16(&Offset);
167       }
168       break;
169     case Operation::SizeRefAddr:
170       if (getRefAddrSize(AddressSize, Version) == 8) {
171         Operands[Operand] = Data.getU64(&Offset);
172       } else if (getRefAddrSize(AddressSize, Version) == 4) {
173         Operands[Operand] = Data.getU32(&Offset);
174       } else {
175         assert(getRefAddrSize(AddressSize, Version) == 2);
176         Operands[Operand] = Data.getU16(&Offset);
177       }
178       break;
179     case Operation::SizeLEB:
180       if (Signed)
181         Operands[Operand] = Data.getSLEB128(&Offset);
182       else
183         Operands[Operand] = Data.getULEB128(&Offset);
184       break;
185     case Operation::BaseTypeRef:
186       Operands[Operand] = Data.getULEB128(&Offset);
187       break;
188     case Operation::SizeBlock:
189       // We need a size, so this cannot be the first operand
190       if (Operand == 0)
191         return false;
192       // Store the offset of the block as the value.
193       Operands[Operand] = Offset;
194       Offset += Operands[Operand - 1];
195       break;
196     default:
197       llvm_unreachable("Unknown DWARFExpression Op size");
198     }
199 
200     OperandEndOffsets[Operand] = Offset;
201   }
202 
203   EndOffset = Offset;
204   return true;
205 }
206 
207 static bool prettyPrintRegisterOp(raw_ostream &OS, uint8_t Opcode,
208                                   uint64_t Operands[2],
209                                   const MCRegisterInfo *MRI, bool isEH) {
210   if (!MRI)
211     return false;
212 
213   uint64_t DwarfRegNum;
214   unsigned OpNum = 0;
215 
216   if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx)
217     DwarfRegNum = Operands[OpNum++];
218   else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
219     DwarfRegNum = Opcode - DW_OP_breg0;
220   else
221     DwarfRegNum = Opcode - DW_OP_reg0;
222 
223   if (Optional<unsigned> LLVMRegNum = MRI->getLLVMRegNum(DwarfRegNum, isEH)) {
224     if (const char *RegName = MRI->getName(*LLVMRegNum)) {
225       if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
226           Opcode == DW_OP_bregx)
227         OS << format(" %s%+" PRId64, RegName, Operands[OpNum]);
228       else
229         OS << ' ' << RegName;
230       return true;
231     }
232   }
233 
234   return false;
235 }
236 
237 bool DWARFExpression::Operation::print(raw_ostream &OS,
238                                        const DWARFExpression *Expr,
239                                        const MCRegisterInfo *RegInfo,
240                                        DWARFUnit *U,
241                                        bool isEH) {
242   if (Error) {
243     OS << "<decoding error>";
244     return false;
245   }
246 
247   StringRef Name = OperationEncodingString(Opcode);
248   assert(!Name.empty() && "DW_OP has no name!");
249   OS << Name;
250 
251   if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
252       (Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) ||
253       Opcode == DW_OP_bregx || Opcode == DW_OP_regx)
254     if (prettyPrintRegisterOp(OS, Opcode, Operands, RegInfo, isEH))
255       return true;
256 
257   for (unsigned Operand = 0; Operand < 2; ++Operand) {
258     unsigned Size = Desc.Op[Operand];
259     unsigned Signed = Size & Operation::SignBit;
260 
261     if (Size == Operation::SizeNA)
262       break;
263 
264     if (Size == Operation::BaseTypeRef && U) {
265       auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
266       if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
267         OS << format(" (0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
268         if (auto Name = Die.find(dwarf::DW_AT_name))
269           OS << " \"" << Name->getAsCString() << "\"";
270       } else {
271         OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
272                      Operands[Operand]);
273       }
274     } else if (Size == Operation::SizeBlock) {
275       uint64_t Offset = Operands[Operand];
276       for (unsigned i = 0; i < Operands[Operand - 1]; ++i)
277         OS << format(" 0x%02x", Expr->Data.getU8(&Offset));
278     } else {
279       if (Signed)
280         OS << format(" %+" PRId64, (int64_t)Operands[Operand]);
281       else if (Opcode != DW_OP_entry_value &&
282                Opcode != DW_OP_GNU_entry_value)
283         OS << format(" 0x%" PRIx64, Operands[Operand]);
284     }
285   }
286   return true;
287 }
288 
289 void DWARFExpression::print(raw_ostream &OS, const MCRegisterInfo *RegInfo,
290                             DWARFUnit *U, bool IsEH) const {
291   uint32_t EntryValExprSize = 0;
292   for (auto &Op : *this) {
293     if (!Op.print(OS, this, RegInfo, U, IsEH)) {
294       uint64_t FailOffset = Op.getEndOffset();
295       while (FailOffset < Data.getData().size())
296         OS << format(" %02x", Data.getU8(&FailOffset));
297       return;
298     }
299 
300     if (Op.getCode() == DW_OP_entry_value ||
301         Op.getCode() == DW_OP_GNU_entry_value) {
302       OS << "(";
303       EntryValExprSize = Op.getRawOperand(0);
304       continue;
305     }
306 
307     if (EntryValExprSize) {
308       EntryValExprSize--;
309       if (EntryValExprSize == 0)
310         OS << ")";
311     }
312 
313     if (Op.getEndOffset() < Data.getData().size())
314       OS << ", ";
315   }
316 }
317 
318 bool DWARFExpression::Operation::verify(DWARFUnit *U) {
319 
320   for (unsigned Operand = 0; Operand < 2; ++Operand) {
321     unsigned Size = Desc.Op[Operand];
322 
323     if (Size == Operation::SizeNA)
324       break;
325 
326     if (Size == Operation::BaseTypeRef) {
327       auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
328       if (!Die || Die.getTag() != dwarf::DW_TAG_base_type) {
329         Error = true;
330         return false;
331       }
332     }
333   }
334 
335   return true;
336 }
337 
338 bool DWARFExpression::verify(DWARFUnit *U) {
339   for (auto &Op : *this)
340     if (!Op.verify(U))
341       return false;
342 
343   return true;
344 }
345 
346 } // namespace llvm
347