blob: 17841bb8c092a03d3da74ab35c37bbcfa053fc54 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Expression/DWARFExpression.h"
10
Virgile Bellobdae3782013-08-28 12:14:27 +000011#include <inttypes.h>
12
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include <vector>
14
Greg Clayton59a1be32018-06-21 17:58:06 +000015#include "lldb/Core/Module.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/Core/Value.h"
17#include "lldb/Core/dwarf.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000018#include "lldb/Utility/DataEncoder.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000019#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000020#include "lldb/Utility/RegisterValue.h"
21#include "lldb/Utility/Scalar.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000022#include "lldb/Utility/StreamString.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000023#include "lldb/Utility/VMRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Jim Inghamf220d592011-12-01 03:01:30 +000025#include "lldb/Host/Host.h"
Zachary Turner01c32432017-02-14 19:06:07 +000026#include "lldb/Utility/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Sean Callanan4740a732016-09-06 04:48:36 +000028#include "lldb/Symbol/Function.h"
29
Greg Claytonafacd142011-09-02 01:15:17 +000030#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000034#include "lldb/Target/StackFrame.h"
Ashok Thirumurthi6e264d32013-07-29 16:05:11 +000035#include "lldb/Target/StackID.h"
Richard Mitton0a558352013-10-17 21:14:00 +000036#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Jan Kratochvilc4d65752018-03-18 20:11:02 +000038#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
Tamas Berghammer35d9d2d2015-08-25 11:46:06 +000039
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040using namespace lldb;
41using namespace lldb_private;
42
Tamas Berghammer1f5e4482015-09-16 12:37:06 +000043static lldb::addr_t
Jan Kratochvilc4d65752018-03-18 20:11:02 +000044ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 uint32_t index) {
46 uint32_t index_size = dwarf_cu->GetAddressByteSize();
47 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
48 lldb::offset_t offset = addr_base + index * index_size;
49 return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
50 &offset, index_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053// DWARFExpression constructor
Jan Kratochvilc4d65752018-03-18 20:11:02 +000054DWARFExpression::DWARFExpression(DWARFUnit *dwarf_cu)
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
56 m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058DWARFExpression::DWARFExpression(const DWARFExpression &rhs)
59 : m_module_wp(rhs.m_module_wp), m_data(rhs.m_data),
60 m_dwarf_cu(rhs.m_dwarf_cu), m_reg_kind(rhs.m_reg_kind),
61 m_loclist_slide(rhs.m_loclist_slide) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Tamas Berghammer35d9d2d2015-08-25 11:46:06 +000063DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 const DataExtractor &data,
Jan Kratochvilc4d65752018-03-18 20:11:02 +000065 DWARFUnit *dwarf_cu,
Tamas Berghammer35d9d2d2015-08-25 11:46:06 +000066 lldb::offset_t data_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 lldb::offset_t data_length)
68 : m_module_wp(), m_data(data, data_offset, data_length),
69 m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
70 m_loclist_slide(LLDB_INVALID_ADDRESS) {
71 if (module_sp)
72 m_module_wp = module_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075// Destructor
Kate Stoneb9c1b512016-09-06 20:57:50 +000076DWARFExpression::~DWARFExpression() {}
77
78bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
79
80void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
81 m_data = data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
85 const DataExtractor &data,
86 lldb::offset_t data_offset,
87 lldb::offset_t data_length) {
88 const uint8_t *bytes = data.PeekData(data_offset, data_length);
89 if (bytes) {
Richard Mitton0a558352013-10-17 21:14:00 +000090 m_module_wp = module_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
92 m_data.SetByteOrder(data.GetByteOrder());
93 m_data.SetAddressByteSize(data.GetAddressByteSize());
94 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097void DWARFExpression::CopyOpcodeData(const void *data,
98 lldb::offset_t data_length,
99 ByteOrder byte_order,
100 uint8_t addr_byte_size) {
101 if (data && data_length) {
102 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
103 m_data.SetByteOrder(byte_order);
104 m_data.SetAddressByteSize(addr_byte_size);
105 }
106}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108void DWARFExpression::CopyOpcodeData(uint64_t const_value,
109 lldb::offset_t const_value_byte_size,
110 uint8_t addr_byte_size) {
111 if (const_value_byte_size) {
112 m_data.SetData(
113 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
114 m_data.SetByteOrder(endian::InlHostByteOrder());
115 m_data.SetAddressByteSize(addr_byte_size);
116 }
117}
Greg Claytonc982c762010-07-09 20:39:50 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
120 const DataExtractor &data,
121 lldb::offset_t data_offset,
122 lldb::offset_t data_length) {
123 m_module_wp = module_sp;
124 m_data.SetData(data, data_offset, data_length);
125}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
128 lldb::offset_t length,
129 lldb::DescriptionLevel level,
130 ABI *abi) const {
131 if (!m_data.ValidOffsetForDataOfSize(offset, length))
132 return;
133 const lldb::offset_t start_offset = offset;
134 const lldb::offset_t end_offset = offset + length;
135 while (m_data.ValidOffset(offset) && offset < end_offset) {
136 const lldb::offset_t op_offset = offset;
137 const uint8_t op = m_data.GetU8(&offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 switch (level) {
140 default:
141 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 case lldb::eDescriptionLevelBrief:
Pavel Labath67b8f572018-07-19 13:30:56 +0000144 if (op_offset > start_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 s->PutChar(' ');
146 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 case lldb::eDescriptionLevelFull:
149 case lldb::eDescriptionLevelVerbose:
Pavel Labath67b8f572018-07-19 13:30:56 +0000150 if (op_offset > start_offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 s->EOL();
152 s->Indent();
153 if (level == lldb::eDescriptionLevelFull)
154 break;
155 // Fall through for verbose and print offset and DW_OP prefix..
156 s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
157 op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
158 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 switch (op) {
162 case DW_OP_addr:
163 *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
164 break; // 0x03 1 address
165 case DW_OP_deref:
166 *s << "DW_OP_deref";
167 break; // 0x06
168 case DW_OP_const1u:
Pavel Labath67b8f572018-07-19 13:30:56 +0000169 s->Printf("DW_OP_const1u(0x%2.2x)", m_data.GetU8(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 break; // 0x08 1 1-byte constant
171 case DW_OP_const1s:
Pavel Labath67b8f572018-07-19 13:30:56 +0000172 s->Printf("DW_OP_const1s(0x%2.2x)", m_data.GetU8(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 break; // 0x09 1 1-byte constant
174 case DW_OP_const2u:
Pavel Labath67b8f572018-07-19 13:30:56 +0000175 s->Printf("DW_OP_const2u(0x%4.4x)", m_data.GetU16(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 break; // 0x0a 1 2-byte constant
177 case DW_OP_const2s:
Pavel Labath67b8f572018-07-19 13:30:56 +0000178 s->Printf("DW_OP_const2s(0x%4.4x)", m_data.GetU16(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 break; // 0x0b 1 2-byte constant
180 case DW_OP_const4u:
Pavel Labath67b8f572018-07-19 13:30:56 +0000181 s->Printf("DW_OP_const4u(0x%8.8x)", m_data.GetU32(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 break; // 0x0c 1 4-byte constant
183 case DW_OP_const4s:
Pavel Labath67b8f572018-07-19 13:30:56 +0000184 s->Printf("DW_OP_const4s(0x%8.8x)", m_data.GetU32(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 break; // 0x0d 1 4-byte constant
186 case DW_OP_const8u:
Pavel Labath67b8f572018-07-19 13:30:56 +0000187 s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 break; // 0x0e 1 8-byte constant
189 case DW_OP_const8s:
Pavel Labath67b8f572018-07-19 13:30:56 +0000190 s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 break; // 0x0f 1 8-byte constant
192 case DW_OP_constu:
Pavel Labath67b8f572018-07-19 13:30:56 +0000193 s->Printf("DW_OP_constu(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 break; // 0x10 1 ULEB128 constant
195 case DW_OP_consts:
Pavel Labath67b8f572018-07-19 13:30:56 +0000196 s->Printf("DW_OP_consts(0x%" PRId64 ")", m_data.GetSLEB128(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 break; // 0x11 1 SLEB128 constant
198 case DW_OP_dup:
199 s->PutCString("DW_OP_dup");
200 break; // 0x12
201 case DW_OP_drop:
202 s->PutCString("DW_OP_drop");
203 break; // 0x13
204 case DW_OP_over:
205 s->PutCString("DW_OP_over");
206 break; // 0x14
207 case DW_OP_pick:
Pavel Labath67b8f572018-07-19 13:30:56 +0000208 s->Printf("DW_OP_pick(0x%2.2x)", m_data.GetU8(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 break; // 0x15 1 1-byte stack index
210 case DW_OP_swap:
211 s->PutCString("DW_OP_swap");
212 break; // 0x16
213 case DW_OP_rot:
214 s->PutCString("DW_OP_rot");
215 break; // 0x17
216 case DW_OP_xderef:
217 s->PutCString("DW_OP_xderef");
218 break; // 0x18
219 case DW_OP_abs:
220 s->PutCString("DW_OP_abs");
221 break; // 0x19
222 case DW_OP_and:
223 s->PutCString("DW_OP_and");
224 break; // 0x1a
225 case DW_OP_div:
226 s->PutCString("DW_OP_div");
227 break; // 0x1b
228 case DW_OP_minus:
229 s->PutCString("DW_OP_minus");
230 break; // 0x1c
231 case DW_OP_mod:
232 s->PutCString("DW_OP_mod");
233 break; // 0x1d
234 case DW_OP_mul:
235 s->PutCString("DW_OP_mul");
236 break; // 0x1e
237 case DW_OP_neg:
238 s->PutCString("DW_OP_neg");
239 break; // 0x1f
240 case DW_OP_not:
241 s->PutCString("DW_OP_not");
242 break; // 0x20
243 case DW_OP_or:
244 s->PutCString("DW_OP_or");
245 break; // 0x21
246 case DW_OP_plus:
247 s->PutCString("DW_OP_plus");
248 break; // 0x22
249 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
Pavel Labath67b8f572018-07-19 13:30:56 +0000250 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ")",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 m_data.GetULEB128(&offset));
252 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 case DW_OP_shl:
255 s->PutCString("DW_OP_shl");
256 break; // 0x24
257 case DW_OP_shr:
258 s->PutCString("DW_OP_shr");
259 break; // 0x25
260 case DW_OP_shra:
261 s->PutCString("DW_OP_shra");
262 break; // 0x26
263 case DW_OP_xor:
264 s->PutCString("DW_OP_xor");
265 break; // 0x27
266 case DW_OP_skip:
267 s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
268 break; // 0x2f 1 signed 2-byte constant
269 case DW_OP_bra:
270 s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
271 break; // 0x28 1 signed 2-byte constant
272 case DW_OP_eq:
273 s->PutCString("DW_OP_eq");
274 break; // 0x29
275 case DW_OP_ge:
276 s->PutCString("DW_OP_ge");
277 break; // 0x2a
278 case DW_OP_gt:
279 s->PutCString("DW_OP_gt");
280 break; // 0x2b
281 case DW_OP_le:
282 s->PutCString("DW_OP_le");
283 break; // 0x2c
284 case DW_OP_lt:
285 s->PutCString("DW_OP_lt");
286 break; // 0x2d
287 case DW_OP_ne:
288 s->PutCString("DW_OP_ne");
289 break; // 0x2e
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 case DW_OP_lit0: // 0x30
292 case DW_OP_lit1: // 0x31
293 case DW_OP_lit2: // 0x32
294 case DW_OP_lit3: // 0x33
295 case DW_OP_lit4: // 0x34
296 case DW_OP_lit5: // 0x35
297 case DW_OP_lit6: // 0x36
298 case DW_OP_lit7: // 0x37
299 case DW_OP_lit8: // 0x38
300 case DW_OP_lit9: // 0x39
301 case DW_OP_lit10: // 0x3A
302 case DW_OP_lit11: // 0x3B
303 case DW_OP_lit12: // 0x3C
304 case DW_OP_lit13: // 0x3D
305 case DW_OP_lit14: // 0x3E
306 case DW_OP_lit15: // 0x3F
307 case DW_OP_lit16: // 0x40
308 case DW_OP_lit17: // 0x41
309 case DW_OP_lit18: // 0x42
310 case DW_OP_lit19: // 0x43
311 case DW_OP_lit20: // 0x44
312 case DW_OP_lit21: // 0x45
313 case DW_OP_lit22: // 0x46
314 case DW_OP_lit23: // 0x47
315 case DW_OP_lit24: // 0x48
316 case DW_OP_lit25: // 0x49
317 case DW_OP_lit26: // 0x4A
318 case DW_OP_lit27: // 0x4B
319 case DW_OP_lit28: // 0x4C
320 case DW_OP_lit29: // 0x4D
321 case DW_OP_lit30: // 0x4E
322 case DW_OP_lit31:
323 s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
324 break; // 0x4f
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 case DW_OP_reg0: // 0x50
327 case DW_OP_reg1: // 0x51
328 case DW_OP_reg2: // 0x52
329 case DW_OP_reg3: // 0x53
330 case DW_OP_reg4: // 0x54
331 case DW_OP_reg5: // 0x55
332 case DW_OP_reg6: // 0x56
333 case DW_OP_reg7: // 0x57
334 case DW_OP_reg8: // 0x58
335 case DW_OP_reg9: // 0x59
336 case DW_OP_reg10: // 0x5A
337 case DW_OP_reg11: // 0x5B
338 case DW_OP_reg12: // 0x5C
339 case DW_OP_reg13: // 0x5D
340 case DW_OP_reg14: // 0x5E
341 case DW_OP_reg15: // 0x5F
342 case DW_OP_reg16: // 0x60
343 case DW_OP_reg17: // 0x61
344 case DW_OP_reg18: // 0x62
345 case DW_OP_reg19: // 0x63
346 case DW_OP_reg20: // 0x64
347 case DW_OP_reg21: // 0x65
348 case DW_OP_reg22: // 0x66
349 case DW_OP_reg23: // 0x67
350 case DW_OP_reg24: // 0x68
351 case DW_OP_reg25: // 0x69
352 case DW_OP_reg26: // 0x6A
353 case DW_OP_reg27: // 0x6B
354 case DW_OP_reg28: // 0x6C
355 case DW_OP_reg29: // 0x6D
356 case DW_OP_reg30: // 0x6E
357 case DW_OP_reg31: // 0x6F
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 uint32_t reg_num = op - DW_OP_reg0;
360 if (abi) {
361 RegisterInfo reg_info;
362 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
363 if (reg_info.name) {
364 s->PutCString(reg_info.name);
365 break;
366 } else if (reg_info.alt_name) {
367 s->PutCString(reg_info.alt_name);
368 break;
369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 }
372 s->Printf("DW_OP_reg%u", reg_num);
373 break;
374 } break;
375
376 case DW_OP_breg0:
377 case DW_OP_breg1:
378 case DW_OP_breg2:
379 case DW_OP_breg3:
380 case DW_OP_breg4:
381 case DW_OP_breg5:
382 case DW_OP_breg6:
383 case DW_OP_breg7:
384 case DW_OP_breg8:
385 case DW_OP_breg9:
386 case DW_OP_breg10:
387 case DW_OP_breg11:
388 case DW_OP_breg12:
389 case DW_OP_breg13:
390 case DW_OP_breg14:
391 case DW_OP_breg15:
392 case DW_OP_breg16:
393 case DW_OP_breg17:
394 case DW_OP_breg18:
395 case DW_OP_breg19:
396 case DW_OP_breg20:
397 case DW_OP_breg21:
398 case DW_OP_breg22:
399 case DW_OP_breg23:
400 case DW_OP_breg24:
401 case DW_OP_breg25:
402 case DW_OP_breg26:
403 case DW_OP_breg27:
404 case DW_OP_breg28:
405 case DW_OP_breg29:
406 case DW_OP_breg30:
407 case DW_OP_breg31: {
408 uint32_t reg_num = op - DW_OP_breg0;
409 int64_t reg_offset = m_data.GetSLEB128(&offset);
410 if (abi) {
411 RegisterInfo reg_info;
412 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
413 if (reg_info.name) {
414 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
415 break;
416 } else if (reg_info.alt_name) {
417 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
418 break;
419 }
420 }
421 }
422 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
423 } break;
424
425 case DW_OP_regx: // 0x90 1 ULEB128 register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 uint32_t reg_num = m_data.GetULEB128(&offset);
428 if (abi) {
429 RegisterInfo reg_info;
430 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
431 if (reg_info.name) {
432 s->PutCString(reg_info.name);
433 break;
434 } else if (reg_info.alt_name) {
435 s->PutCString(reg_info.alt_name);
436 break;
437 }
438 }
439 }
440 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
441 break;
442 } break;
443 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
444 s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
445 break;
446 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
447 {
448 uint32_t reg_num = m_data.GetULEB128(&offset);
449 int64_t reg_offset = m_data.GetSLEB128(&offset);
450 if (abi) {
451 RegisterInfo reg_info;
452 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
453 if (reg_info.name) {
454 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
455 break;
456 } else if (reg_info.alt_name) {
457 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
458 break;
459 }
460 }
461 }
462 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
463 reg_offset);
464 } break;
465 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
466 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
467 break;
468 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
469 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
470 break;
471 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
472 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
473 break;
474 case DW_OP_nop:
475 s->PutCString("DW_OP_nop");
476 break; // 0x96
477 case DW_OP_push_object_address:
478 s->PutCString("DW_OP_push_object_address");
479 break; // 0x97 DWARF3
480 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
481 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
482 break;
483 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
484 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
485 break;
486 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
487 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
488 break;
489 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;
490 // // 0x9c DWARF3
491 // case DW_OP_bit_piece: // 0x9d DWARF3 2
492 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)",
493 // m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
494 // break;
495 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break;
496 // // 0xe0
497 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break;
498 // // 0xff
499 // case DW_OP_APPLE_extern:
500 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")",
501 // m_data.GetULEB128(&offset));
502 // break;
503 // case DW_OP_APPLE_array_ref:
504 // s->PutCString("DW_OP_APPLE_array_ref");
505 // break;
506 case DW_OP_form_tls_address:
507 s->PutCString("DW_OP_form_tls_address"); // 0x9b
508 break;
509 case DW_OP_GNU_addr_index: // 0xfb
510 s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
511 m_data.GetULEB128(&offset));
512 break;
Ali Tamurd0156252019-03-07 19:41:08 +0000513 case DW_OP_addrx:
514 s->Printf("DW_OP_addrx(0x%" PRIx64 ")",
515 m_data.GetULEB128(&offset));
516 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 case DW_OP_GNU_const_index: // 0xfc
518 s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
519 m_data.GetULEB128(&offset));
520 break;
521 case DW_OP_GNU_push_tls_address:
522 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
523 break;
524 case DW_OP_APPLE_uninit:
525 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
526 break;
527 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and
528 // assigns it to second item on stack (2nd item must have
529 // assignable context)
530 // s->PutCString("DW_OP_APPLE_assign");
531 // break;
532 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of
533 // the top stack item (top item must be a variable, or have
534 // value_type that is an address already)
535 // s->PutCString("DW_OP_APPLE_address_of");
536 // break;
537 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the
538 // stack and pushes the value of that object (top item must be a
539 // variable, or expression local)
540 // s->PutCString("DW_OP_APPLE_value_of");
541 // break;
542 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of
543 // the top stack item (top item must be a variable, or a clang
544 // type)
545 // s->PutCString("DW_OP_APPLE_deref_type");
546 // break;
547 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression
548 // local index
549 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")",
550 // m_data.GetULEB128(&offset));
551 // break;
552 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size,
553 // followed by constant float data
554 // {
555 // uint8_t float_length = m_data.GetU8(&offset);
556 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
557 // m_data.Dump(s, offset, eFormatHex, float_length, 1,
558 // UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
559 // s->PutChar(')');
560 // // Consume the float data
561 // m_data.GetData(&offset, float_length);
562 // }
563 // break;
564 // case DW_OP_APPLE_scalar_cast:
565 // s->Printf("DW_OP_APPLE_scalar_cast(%s)",
566 // Scalar::GetValueTypeAsCString
567 // ((Scalar::Type)m_data.GetU8(&offset)));
568 // break;
569 // case DW_OP_APPLE_clang_cast:
570 // {
571 // clang::Type *clang_type = (clang::Type
572 // *)m_data.GetMaxU64(&offset, sizeof(void*));
573 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
574 // }
575 // break;
576 // case DW_OP_APPLE_clear:
577 // s->PutCString("DW_OP_APPLE_clear");
578 // break;
579 // case DW_OP_APPLE_error: // 0xFF - Stops expression
580 // evaluation and returns an error (no args)
581 // s->PutCString("DW_OP_APPLE_error");
582 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587void DWARFExpression::SetLocationListSlide(addr_t slide) {
588 m_loclist_slide = slide;
589}
590
591int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
592
593void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
594 m_reg_kind = reg_kind;
595}
596
597bool DWARFExpression::IsLocationList() const {
598 return m_loclist_slide != LLDB_INVALID_ADDRESS;
599}
600
601void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
602 addr_t location_list_base_addr,
603 ABI *abi) const {
604 if (IsLocationList()) {
605 // We have a location list
606 lldb::offset_t offset = 0;
607 uint32_t count = 0;
608 addr_t curr_base_addr = location_list_base_addr;
609 while (m_data.ValidOffset(offset)) {
610 addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
611 addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
612 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
613 begin_addr_offset, end_addr_offset))
614 break;
615
616 if (begin_addr_offset == 0 && end_addr_offset == 0)
617 break;
618
619 if (begin_addr_offset < end_addr_offset) {
620 if (count > 0)
621 s->PutCString(", ");
622 VMRange addr_range(curr_base_addr + begin_addr_offset,
623 curr_base_addr + end_addr_offset);
624 addr_range.Dump(s, 0, 8);
625 s->PutChar('{');
626 lldb::offset_t location_length = m_data.GetU16(&offset);
627 DumpLocation(s, offset, location_length, level, abi);
628 s->PutChar('}');
629 offset += location_length;
630 } else {
631 if ((m_data.GetAddressByteSize() == 4 &&
632 (begin_addr_offset == UINT32_MAX)) ||
633 (m_data.GetAddressByteSize() == 8 &&
634 (begin_addr_offset == UINT64_MAX))) {
635 curr_base_addr = end_addr_offset + location_list_base_addr;
636 // We have a new base address
637 if (count > 0)
638 s->PutCString(", ");
639 *s << "base_addr = " << end_addr_offset;
640 }
641 }
642
643 count++;
644 }
645 } else {
646 // We have a normal location that contains DW_OP location opcodes
647 DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
648 }
649}
650
651static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
652 lldb::RegisterKind reg_kind,
Zachary Turner97206d52017-05-12 04:51:55 +0000653 uint32_t reg_num, Status *error_ptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 Value &value) {
655 if (reg_ctx == NULL) {
656 if (error_ptr)
657 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
658 } else {
659 uint32_t native_reg =
660 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
661 if (native_reg == LLDB_INVALID_REGNUM) {
662 if (error_ptr)
663 error_ptr->SetErrorStringWithFormat("Unable to convert register "
664 "kind=%u reg_num=%u to a native "
665 "register number.\n",
666 reg_kind, reg_num);
667 } else {
668 const RegisterInfo *reg_info =
669 reg_ctx->GetRegisterInfoAtIndex(native_reg);
670 RegisterValue reg_value;
671 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
672 if (reg_value.GetScalarValue(value.GetScalar())) {
673 value.SetValueType(Value::eValueTypeScalar);
674 value.SetContext(Value::eContextTypeRegisterInfo,
675 const_cast<RegisterInfo *>(reg_info));
676 if (error_ptr)
677 error_ptr->Clear();
678 return true;
679 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000680 // If we get this error, then we need to implement a value buffer in
681 // the dwarf expression evaluation function...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 if (error_ptr)
683 error_ptr->SetErrorStringWithFormat(
684 "register %s can't be converted to a scalar value",
685 reg_info->name);
686 }
687 } else {
Jason Molenda2d107dd2010-11-20 01:28:30 +0000688 if (error_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689 error_ptr->SetErrorStringWithFormat("register %s is not available",
690 reg_info->name);
691 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 }
694 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695}
696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697// bool
698// DWARFExpression::LocationListContainsLoadAddress (Process* process, const
699// Address &addr) const
Greg Clayton016a95e2010-09-14 02:20:48 +0000700//{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701// return LocationListContainsLoadAddress(process,
702// addr.GetLoadAddress(process));
Greg Clayton016a95e2010-09-14 02:20:48 +0000703//}
704//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000705// bool
706// DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t
707// load_addr) const
Greg Clayton016a95e2010-09-14 02:20:48 +0000708//{
709// if (load_addr == LLDB_INVALID_ADDRESS)
710// return false;
711//
712// if (IsLocationList())
713// {
Greg Claytonc7bece562013-01-25 18:06:21 +0000714// lldb::offset_t offset = 0;
Greg Clayton016a95e2010-09-14 02:20:48 +0000715//
716// addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
717//
718// if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
719// return false;
720//
721// while (m_data.ValidOffset(offset))
722// {
723// // We need to figure out what the value is for the location.
724// addr_t lo_pc = m_data.GetAddress(&offset);
725// addr_t hi_pc = m_data.GetAddress(&offset);
726// if (lo_pc == 0 && hi_pc == 0)
727// break;
728// else
729// {
730// lo_pc += loc_list_base_addr;
731// hi_pc += loc_list_base_addr;
732//
733// if (lo_pc <= load_addr && load_addr < hi_pc)
734// return true;
735//
736// offset += m_data.GetU16(&offset);
737// }
738// }
739// }
740// return false;
741//}
Greg Clayton9da7bd02010-08-24 21:05:24 +0000742
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743static offset_t GetOpcodeDataSize(const DataExtractor &data,
744 const lldb::offset_t data_offset,
745 const uint8_t op) {
746 lldb::offset_t offset = data_offset;
747 switch (op) {
748 case DW_OP_addr:
749 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
750 return data.GetAddressByteSize();
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000751
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 // Opcodes with no arguments
753 case DW_OP_deref: // 0x06
754 case DW_OP_dup: // 0x12
755 case DW_OP_drop: // 0x13
756 case DW_OP_over: // 0x14
757 case DW_OP_swap: // 0x16
758 case DW_OP_rot: // 0x17
759 case DW_OP_xderef: // 0x18
760 case DW_OP_abs: // 0x19
761 case DW_OP_and: // 0x1a
762 case DW_OP_div: // 0x1b
763 case DW_OP_minus: // 0x1c
764 case DW_OP_mod: // 0x1d
765 case DW_OP_mul: // 0x1e
766 case DW_OP_neg: // 0x1f
767 case DW_OP_not: // 0x20
768 case DW_OP_or: // 0x21
769 case DW_OP_plus: // 0x22
770 case DW_OP_shl: // 0x24
771 case DW_OP_shr: // 0x25
772 case DW_OP_shra: // 0x26
773 case DW_OP_xor: // 0x27
774 case DW_OP_eq: // 0x29
775 case DW_OP_ge: // 0x2a
776 case DW_OP_gt: // 0x2b
777 case DW_OP_le: // 0x2c
778 case DW_OP_lt: // 0x2d
779 case DW_OP_ne: // 0x2e
780 case DW_OP_lit0: // 0x30
781 case DW_OP_lit1: // 0x31
782 case DW_OP_lit2: // 0x32
783 case DW_OP_lit3: // 0x33
784 case DW_OP_lit4: // 0x34
785 case DW_OP_lit5: // 0x35
786 case DW_OP_lit6: // 0x36
787 case DW_OP_lit7: // 0x37
788 case DW_OP_lit8: // 0x38
789 case DW_OP_lit9: // 0x39
790 case DW_OP_lit10: // 0x3A
791 case DW_OP_lit11: // 0x3B
792 case DW_OP_lit12: // 0x3C
793 case DW_OP_lit13: // 0x3D
794 case DW_OP_lit14: // 0x3E
795 case DW_OP_lit15: // 0x3F
796 case DW_OP_lit16: // 0x40
797 case DW_OP_lit17: // 0x41
798 case DW_OP_lit18: // 0x42
799 case DW_OP_lit19: // 0x43
800 case DW_OP_lit20: // 0x44
801 case DW_OP_lit21: // 0x45
802 case DW_OP_lit22: // 0x46
803 case DW_OP_lit23: // 0x47
804 case DW_OP_lit24: // 0x48
805 case DW_OP_lit25: // 0x49
806 case DW_OP_lit26: // 0x4A
807 case DW_OP_lit27: // 0x4B
808 case DW_OP_lit28: // 0x4C
809 case DW_OP_lit29: // 0x4D
810 case DW_OP_lit30: // 0x4E
811 case DW_OP_lit31: // 0x4f
812 case DW_OP_reg0: // 0x50
813 case DW_OP_reg1: // 0x51
814 case DW_OP_reg2: // 0x52
815 case DW_OP_reg3: // 0x53
816 case DW_OP_reg4: // 0x54
817 case DW_OP_reg5: // 0x55
818 case DW_OP_reg6: // 0x56
819 case DW_OP_reg7: // 0x57
820 case DW_OP_reg8: // 0x58
821 case DW_OP_reg9: // 0x59
822 case DW_OP_reg10: // 0x5A
823 case DW_OP_reg11: // 0x5B
824 case DW_OP_reg12: // 0x5C
825 case DW_OP_reg13: // 0x5D
826 case DW_OP_reg14: // 0x5E
827 case DW_OP_reg15: // 0x5F
828 case DW_OP_reg16: // 0x60
829 case DW_OP_reg17: // 0x61
830 case DW_OP_reg18: // 0x62
831 case DW_OP_reg19: // 0x63
832 case DW_OP_reg20: // 0x64
833 case DW_OP_reg21: // 0x65
834 case DW_OP_reg22: // 0x66
835 case DW_OP_reg23: // 0x67
836 case DW_OP_reg24: // 0x68
837 case DW_OP_reg25: // 0x69
838 case DW_OP_reg26: // 0x6A
839 case DW_OP_reg27: // 0x6B
840 case DW_OP_reg28: // 0x6C
841 case DW_OP_reg29: // 0x6D
842 case DW_OP_reg30: // 0x6E
843 case DW_OP_reg31: // 0x6F
844 case DW_OP_nop: // 0x96
845 case DW_OP_push_object_address: // 0x97 DWARF3
846 case DW_OP_form_tls_address: // 0x9b DWARF3
847 case DW_OP_call_frame_cfa: // 0x9c DWARF3
848 case DW_OP_stack_value: // 0x9f DWARF4
849 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
Tamas Berghammer1f5e4482015-09-16 12:37:06 +0000850 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851
852 // Opcodes with a single 1 byte arguments
853 case DW_OP_const1u: // 0x08 1 1-byte constant
854 case DW_OP_const1s: // 0x09 1 1-byte constant
855 case DW_OP_pick: // 0x15 1 1-byte stack index
856 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
857 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
858 return 1;
859
860 // Opcodes with a single 2 byte arguments
861 case DW_OP_const2u: // 0x0a 1 2-byte constant
862 case DW_OP_const2s: // 0x0b 1 2-byte constant
863 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
864 case DW_OP_bra: // 0x28 1 signed 2-byte constant
865 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
866 return 2;
867
868 // Opcodes with a single 4 byte arguments
869 case DW_OP_const4u: // 0x0c 1 4-byte constant
870 case DW_OP_const4s: // 0x0d 1 4-byte constant
871 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
872 return 4;
873
874 // Opcodes with a single 8 byte arguments
875 case DW_OP_const8u: // 0x0e 1 8-byte constant
876 case DW_OP_const8s: // 0x0f 1 8-byte constant
877 return 8;
878
879 // All opcodes that have a single ULEB (signed or unsigned) argument
Ali Tamurd0156252019-03-07 19:41:08 +0000880 case DW_OP_addrx: // 0xa1 1 ULEB128 index
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881 case DW_OP_constu: // 0x10 1 ULEB128 constant
882 case DW_OP_consts: // 0x11 1 SLEB128 constant
883 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
884 case DW_OP_breg0: // 0x70 1 ULEB128 register
885 case DW_OP_breg1: // 0x71 1 ULEB128 register
886 case DW_OP_breg2: // 0x72 1 ULEB128 register
887 case DW_OP_breg3: // 0x73 1 ULEB128 register
888 case DW_OP_breg4: // 0x74 1 ULEB128 register
889 case DW_OP_breg5: // 0x75 1 ULEB128 register
890 case DW_OP_breg6: // 0x76 1 ULEB128 register
891 case DW_OP_breg7: // 0x77 1 ULEB128 register
892 case DW_OP_breg8: // 0x78 1 ULEB128 register
893 case DW_OP_breg9: // 0x79 1 ULEB128 register
894 case DW_OP_breg10: // 0x7a 1 ULEB128 register
895 case DW_OP_breg11: // 0x7b 1 ULEB128 register
896 case DW_OP_breg12: // 0x7c 1 ULEB128 register
897 case DW_OP_breg13: // 0x7d 1 ULEB128 register
898 case DW_OP_breg14: // 0x7e 1 ULEB128 register
899 case DW_OP_breg15: // 0x7f 1 ULEB128 register
900 case DW_OP_breg16: // 0x80 1 ULEB128 register
901 case DW_OP_breg17: // 0x81 1 ULEB128 register
902 case DW_OP_breg18: // 0x82 1 ULEB128 register
903 case DW_OP_breg19: // 0x83 1 ULEB128 register
904 case DW_OP_breg20: // 0x84 1 ULEB128 register
905 case DW_OP_breg21: // 0x85 1 ULEB128 register
906 case DW_OP_breg22: // 0x86 1 ULEB128 register
907 case DW_OP_breg23: // 0x87 1 ULEB128 register
908 case DW_OP_breg24: // 0x88 1 ULEB128 register
909 case DW_OP_breg25: // 0x89 1 ULEB128 register
910 case DW_OP_breg26: // 0x8a 1 ULEB128 register
911 case DW_OP_breg27: // 0x8b 1 ULEB128 register
912 case DW_OP_breg28: // 0x8c 1 ULEB128 register
913 case DW_OP_breg29: // 0x8d 1 ULEB128 register
914 case DW_OP_breg30: // 0x8e 1 ULEB128 register
915 case DW_OP_breg31: // 0x8f 1 ULEB128 register
916 case DW_OP_regx: // 0x90 1 ULEB128 register
917 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
918 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
919 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
920 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
921 data.Skip_LEB128(&offset);
922 return offset - data_offset;
923
924 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
925 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
926 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
927 data.Skip_LEB128(&offset);
928 data.Skip_LEB128(&offset);
929 return offset - data_offset;
930
931 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
932 // (DWARF4)
933 {
934 uint64_t block_len = data.Skip_LEB128(&offset);
935 offset += block_len;
936 return offset - data_offset;
937 }
938
939 default:
940 break;
941 }
942 return LLDB_INVALID_OFFSET;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +0000943}
944
Kate Stoneb9c1b512016-09-06 20:57:50 +0000945lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
946 bool &error) const {
947 error = false;
948 if (IsLocationList())
949 return LLDB_INVALID_ADDRESS;
950 lldb::offset_t offset = 0;
951 uint32_t curr_op_addr_idx = 0;
952 while (m_data.ValidOffset(offset)) {
953 const uint8_t op = m_data.GetU8(&offset);
954
955 if (op == DW_OP_addr) {
956 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
957 if (curr_op_addr_idx == op_addr_idx)
958 return op_file_addr;
959 else
960 ++curr_op_addr_idx;
Ali Tamurd0156252019-03-07 19:41:08 +0000961 } else if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 uint64_t index = m_data.GetULEB128(&offset);
963 if (curr_op_addr_idx == op_addr_idx) {
964 if (!m_dwarf_cu) {
965 error = true;
966 break;
967 }
968
969 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
970 } else
971 ++curr_op_addr_idx;
972 } else {
973 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
974 if (op_arg_size == LLDB_INVALID_OFFSET) {
975 error = true;
976 break;
977 }
978 offset += op_arg_size;
979 }
980 }
981 return LLDB_INVALID_ADDRESS;
982}
983
984bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
985 if (IsLocationList())
986 return false;
987 lldb::offset_t offset = 0;
988 while (m_data.ValidOffset(offset)) {
989 const uint8_t op = m_data.GetU8(&offset);
990
991 if (op == DW_OP_addr) {
992 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
Adrian Prantl05097242018-04-30 16:49:04 +0000993 // We have to make a copy of the data as we don't know if this data is
994 // from a read only memory mapped buffer, so we duplicate all of the data
995 // first, then modify it, and if all goes well, we then replace the data
996 // for this expression
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997
998 // So first we copy the data into a heap buffer
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000999 std::unique_ptr<DataBufferHeap> head_data_up(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1001
Adrian Prantl05097242018-04-30 16:49:04 +00001002 // Make en encoder so we can write the address into the buffer using the
1003 // correct byte order (endianness)
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001004 DataEncoder encoder(head_data_up->GetBytes(), head_data_up->GetByteSize(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 m_data.GetByteOrder(), addr_byte_size);
1006
1007 // Replace the address in the new buffer
1008 if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001009 return false;
1010
Adrian Prantl05097242018-04-30 16:49:04 +00001011 // All went well, so now we can reset the data using a shared pointer to
1012 // the heap data so "m_data" will now correctly manage the heap data.
Jonas Devlieghered5b44032019-02-13 06:25:41 +00001013 m_data.SetData(DataBufferSP(head_data_up.release()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 return true;
1015 } else {
1016 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1017 if (op_arg_size == LLDB_INVALID_OFFSET)
1018 break;
1019 offset += op_arg_size;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001020 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001021 }
1022 return false;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001023}
1024
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025bool DWARFExpression::ContainsThreadLocalStorage() const {
Adrian Prantl05097242018-04-30 16:49:04 +00001026 // We are assuming for now that any thread local variable will not have a
1027 // location list. This has been true for all thread local variables we have
1028 // seen so far produced by any compiler.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 if (IsLocationList())
1030 return false;
1031 lldb::offset_t offset = 0;
1032 while (m_data.ValidOffset(offset)) {
1033 const uint8_t op = m_data.GetU8(&offset);
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001034
Kate Stoneb9c1b512016-09-06 20:57:50 +00001035 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
1036 return true;
1037 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1038 if (op_arg_size == LLDB_INVALID_OFFSET)
1039 return false;
1040 else
1041 offset += op_arg_size;
1042 }
1043 return false;
1044}
1045bool DWARFExpression::LinkThreadLocalStorage(
1046 lldb::ModuleSP new_module_sp,
1047 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
1048 &link_address_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001049 // We are assuming for now that any thread local variable will not have a
1050 // location list. This has been true for all thread local variables we have
1051 // seen so far produced by any compiler.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 if (IsLocationList())
1053 return false;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001054
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
Adrian Prantl05097242018-04-30 16:49:04 +00001056 // We have to make a copy of the data as we don't know if this data is from a
1057 // read only memory mapped buffer, so we duplicate all of the data first,
1058 // then modify it, and if all goes well, we then replace the data for this
1059 // expression
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001060
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 // So first we copy the data into a heap buffer
1062 std::shared_ptr<DataBufferHeap> heap_data_sp(
1063 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001064
Adrian Prantl05097242018-04-30 16:49:04 +00001065 // Make en encoder so we can write the address into the buffer using the
1066 // correct byte order (endianness)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
1068 m_data.GetByteOrder(), addr_byte_size);
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001069
Kate Stoneb9c1b512016-09-06 20:57:50 +00001070 lldb::offset_t offset = 0;
1071 lldb::offset_t const_offset = 0;
1072 lldb::addr_t const_value = 0;
1073 size_t const_byte_size = 0;
1074 while (m_data.ValidOffset(offset)) {
1075 const uint8_t op = m_data.GetU8(&offset);
1076
1077 bool decoded_data = false;
1078 switch (op) {
1079 case DW_OP_const4u:
1080 // Remember the const offset in case we later have a
Adrian Prantl05097242018-04-30 16:49:04 +00001081 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
Kate Stoneb9c1b512016-09-06 20:57:50 +00001082 const_offset = offset;
1083 const_value = m_data.GetU32(&offset);
1084 decoded_data = true;
1085 const_byte_size = 4;
1086 break;
1087
1088 case DW_OP_const8u:
1089 // Remember the const offset in case we later have a
Adrian Prantl05097242018-04-30 16:49:04 +00001090 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 const_offset = offset;
1092 const_value = m_data.GetU64(&offset);
1093 decoded_data = true;
1094 const_byte_size = 8;
1095 break;
1096
1097 case DW_OP_form_tls_address:
1098 case DW_OP_GNU_push_tls_address:
1099 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
Adrian Prantl05097242018-04-30 16:49:04 +00001100 // by a file address on the stack. We assume that DW_OP_const4u or
1101 // DW_OP_const8u is used for these values, and we check that the last
1102 // opcode we got before either of these was DW_OP_const4u or
1103 // DW_OP_const8u. If so, then we can link the value accodingly. For
1104 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
1105 // address of a structure that contains a function pointer, the pthread
1106 // key and the offset into the data pointed to by the pthread key. So we
1107 // must link this address and also set the module of this expression to
1108 // the new_module_sp so we can resolve the file address correctly
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 if (const_byte_size > 0) {
1110 lldb::addr_t linked_file_addr = link_address_callback(const_value);
1111 if (linked_file_addr == LLDB_INVALID_ADDRESS)
1112 return false;
1113 // Replace the address in the new buffer
1114 if (encoder.PutMaxU64(const_offset, const_byte_size,
1115 linked_file_addr) == UINT32_MAX)
1116 return false;
1117 }
1118 break;
1119
1120 default:
1121 const_offset = 0;
1122 const_value = 0;
1123 const_byte_size = 0;
1124 break;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126
1127 if (!decoded_data) {
1128 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1129 if (op_arg_size == LLDB_INVALID_OFFSET)
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001130 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001131 else
1132 offset += op_arg_size;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001135
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136 // If we linked the TLS address correctly, update the module so that when the
Adrian Prantl05097242018-04-30 16:49:04 +00001137 // expression is evaluated it can resolve the file address to a load address
1138 // and read the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 // TLS data
1140 m_module_wp = new_module_sp;
1141 m_data.SetData(heap_data_sp);
1142 return true;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001143}
1144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145bool DWARFExpression::LocationListContainsAddress(
1146 lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
1147 if (addr == LLDB_INVALID_ADDRESS)
1148 return false;
1149
1150 if (IsLocationList()) {
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001151 lldb::offset_t offset = 0;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001152
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1154 return false;
1155
1156 while (m_data.ValidOffset(offset)) {
1157 // We need to figure out what the value is for the location.
1158 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1159 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1160 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1161 hi_pc))
1162 break;
1163
1164 if (lo_pc == 0 && hi_pc == 0)
1165 break;
1166
1167 lo_pc += loclist_base_addr - m_loclist_slide;
1168 hi_pc += loclist_base_addr - m_loclist_slide;
1169
1170 if (lo_pc <= addr && addr < hi_pc)
1171 return true;
1172
1173 offset += m_data.GetU16(&offset);
1174 }
1175 }
1176 return false;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001177}
1178
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
1180 lldb::offset_t &offset,
1181 lldb::offset_t &length) {
1182 offset = 0;
1183 if (!IsLocationList()) {
1184 length = m_data.GetByteSize();
1185 return true;
1186 }
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
1189 addr_t curr_base_addr = base_addr;
1190
1191 while (m_data.ValidOffset(offset)) {
1192 // We need to figure out what the value is for the location.
1193 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1194 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1195 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1196 hi_pc))
1197 break;
1198
1199 if (lo_pc == 0 && hi_pc == 0)
1200 break;
1201
1202 lo_pc += curr_base_addr - m_loclist_slide;
1203 hi_pc += curr_base_addr - m_loclist_slide;
1204
1205 length = m_data.GetU16(&offset);
1206
1207 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1208 return true;
1209
1210 offset += length;
1211 }
1212 }
1213 offset = LLDB_INVALID_OFFSET;
1214 length = 0;
1215 return false;
1216}
1217
1218bool DWARFExpression::DumpLocationForAddress(Stream *s,
1219 lldb::DescriptionLevel level,
1220 addr_t base_addr, addr_t address,
1221 ABI *abi) {
1222 lldb::offset_t offset = 0;
1223 lldb::offset_t length = 0;
1224
1225 if (GetLocation(base_addr, address, offset, length)) {
1226 if (length > 0) {
1227 DumpLocation(s, offset, length, level, abi);
1228 return true;
1229 }
1230 }
1231 return false;
1232}
1233
1234bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235 lldb::addr_t loclist_base_load_addr,
1236 const Value *initial_value_ptr,
1237 const Value *object_address_ptr, Value &result,
Zachary Turner97206d52017-05-12 04:51:55 +00001238 Status *error_ptr) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 ExecutionContext exe_ctx(exe_scope);
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001240 return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
1241 object_address_ptr, result, error_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242}
1243
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001244bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
1245 RegisterContext *reg_ctx,
1246 lldb::addr_t loclist_base_load_addr,
1247 const Value *initial_value_ptr,
1248 const Value *object_address_ptr, Value &result,
1249 Status *error_ptr) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 ModuleSP module_sp = m_module_wp.lock();
1251
1252 if (IsLocationList()) {
1253 lldb::offset_t offset = 0;
1254 addr_t pc;
1255 StackFrame *frame = NULL;
1256 if (reg_ctx)
1257 pc = reg_ctx->GetPC();
1258 else {
1259 frame = exe_ctx->GetFramePtr();
1260 if (!frame)
1261 return false;
1262 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1263 if (!reg_ctx_sp)
1264 return false;
1265 pc = reg_ctx_sp->GetPC();
1266 }
1267
1268 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
1269 if (pc == LLDB_INVALID_ADDRESS) {
1270 if (error_ptr)
1271 error_ptr->SetErrorString("Invalid PC in frame.");
1272 return false;
1273 }
1274
1275 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1276
1277 while (m_data.ValidOffset(offset)) {
1278 // We need to figure out what the value is for the location.
1279 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1280 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1281 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
1282 lo_pc, hi_pc))
1283 break;
1284
1285 if (lo_pc == 0 && hi_pc == 0)
1286 break;
1287
1288 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1289 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1290
1291 uint16_t length = m_data.GetU16(&offset);
1292
1293 if (length > 0 && lo_pc <= pc && pc < hi_pc) {
1294 return DWARFExpression::Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001295 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, offset, length,
1296 m_reg_kind, initial_value_ptr, object_address_ptr, result,
1297 error_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 }
1299 offset += length;
1300 }
1301 }
1302 if (error_ptr)
1303 error_ptr->SetErrorString("variable not available");
1304 return false;
1305 }
1306
1307 // Not a location list, just a single expression.
1308 return DWARFExpression::Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001309 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, 0, m_data.GetByteSize(),
1310 m_reg_kind, initial_value_ptr, object_address_ptr, result, error_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311}
1312
1313bool DWARFExpression::Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001314 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001316 DWARFUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
1318 const Value *initial_value_ptr, const Value *object_address_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +00001319 Value &result, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320
1321 if (opcodes_length == 0) {
1322 if (error_ptr)
1323 error_ptr->SetErrorString(
1324 "no location, value may have been optimized out");
1325 return false;
1326 }
1327 std::vector<Value> stack;
1328
1329 Process *process = NULL;
1330 StackFrame *frame = NULL;
1331
1332 if (exe_ctx) {
1333 process = exe_ctx->GetProcessPtr();
1334 frame = exe_ctx->GetFramePtr();
1335 }
1336 if (reg_ctx == NULL && frame)
1337 reg_ctx = frame->GetRegisterContext().get();
1338
1339 if (initial_value_ptr)
1340 stack.push_back(*initial_value_ptr);
1341
1342 lldb::offset_t offset = opcodes_offset;
1343 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1344 Value tmp;
1345 uint32_t reg_num;
1346
1347 /// Insertion point for evaluating multi-piece expression.
1348 uint64_t op_piece_offset = 0;
1349 Value pieces; // Used for DW_OP_piece
1350
1351 // Make sure all of the data is available in opcodes.
1352 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
1353 if (error_ptr)
1354 error_ptr->SetErrorString(
1355 "invalid offset and/or length for opcodes buffer.");
1356 return false;
1357 }
1358 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1359
1360 while (opcodes.ValidOffset(offset) && offset < end_offset) {
1361 const lldb::offset_t op_offset = offset;
1362 const uint8_t op = opcodes.GetU8(&offset);
1363
1364 if (log && log->GetVerbose()) {
1365 size_t count = stack.size();
1366 log->Printf("Stack before operation has %" PRIu64 " values:",
1367 (uint64_t)count);
1368 for (size_t i = 0; i < count; ++i) {
1369 StreamString new_value;
1370 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1371 stack[i].Dump(&new_value);
1372 log->Printf(" %s", new_value.GetData());
1373 }
1374 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1375 }
Adrian Prantlb51804e2018-05-03 23:32:47 +00001376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 switch (op) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 // The DW_OP_addr operation has a single operand that encodes a machine
1379 // address and whose size is the size of an address on the target machine.
Adrian Prantlb78a5772018-05-03 20:19:39 +00001380 case DW_OP_addr:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1382 stack.back().SetValueType(Value::eValueTypeFileAddress);
Adrian Prantlb51804e2018-05-03 23:32:47 +00001383 // Convert the file address to a load address, so subsequent
1384 // DWARF operators can operate on it.
1385 if (frame)
1386 stack.back().ConvertToLoadAddress(module_sp.get(),
1387 frame->CalculateTarget().get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 break;
1389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1391 // shared libraries that have a location like:
1392 // DW_OP_addr(0x1000)
Adrian Prantl05097242018-04-30 16:49:04 +00001393 // If this address resides in a shared library, then this virtual address
1394 // won't make sense when it is evaluated in the context of a running
1395 // process where shared libraries have been slid. To account for this, this
1396 // new address type where we can store the section pointer and a 4 byte
1397 // offset.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 // case DW_OP_addr_sect_offset4:
1399 // {
1400 // result_type = eResultTypeFileAddress;
1401 // lldb::Section *sect = (lldb::Section
1402 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1403 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1404 //
1405 // Address so_addr (sect, sect_offset);
1406 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1407 // if (load_addr != LLDB_INVALID_ADDRESS)
1408 // {
1409 // // We successfully resolve a file address to a load
1410 // // address.
1411 // stack.push_back(load_addr);
1412 // break;
1413 // }
1414 // else
1415 // {
1416 // // We were able
1417 // if (error_ptr)
1418 // error_ptr->SetErrorStringWithFormat ("Section %s in
1419 // %s is not currently loaded.\n",
1420 // sect->GetName().AsCString(),
1421 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1422 // return false;
1423 // }
1424 // }
1425 // break;
1426
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 // OPCODE: DW_OP_deref
1428 // OPERANDS: none
1429 // DESCRIPTION: Pops the top stack entry and treats it as an address.
Adrian Prantl05097242018-04-30 16:49:04 +00001430 // The value retrieved from that address is pushed. The size of the data
1431 // retrieved from the dereferenced address is the size of an address on the
1432 // target machine.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433 case DW_OP_deref: {
1434 if (stack.empty()) {
1435 if (error_ptr)
1436 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1437 return false;
1438 }
1439 Value::ValueType value_type = stack.back().GetValueType();
1440 switch (value_type) {
1441 case Value::eValueTypeHostAddress: {
1442 void *src = (void *)stack.back().GetScalar().ULongLong();
1443 intptr_t ptr;
1444 ::memcpy(&ptr, src, sizeof(void *));
1445 stack.back().GetScalar() = ptr;
1446 stack.back().ClearContext();
1447 } break;
Greg Clayton59a1be32018-06-21 17:58:06 +00001448 case Value::eValueTypeFileAddress: {
1449 auto file_addr = stack.back().GetScalar().ULongLong(
1450 LLDB_INVALID_ADDRESS);
1451 if (!module_sp) {
1452 if (error_ptr)
1453 error_ptr->SetErrorStringWithFormat(
1454 "need module to resolve file address for DW_OP_deref");
1455 return false;
1456 }
1457 Address so_addr;
1458 if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
1459 if (error_ptr)
1460 error_ptr->SetErrorStringWithFormat(
1461 "failed to resolve file address in module");
1462 return false;
1463 }
1464 addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
1465 if (load_Addr == LLDB_INVALID_ADDRESS) {
1466 if (error_ptr)
1467 error_ptr->SetErrorStringWithFormat(
1468 "failed to resolve load address");
1469 return false;
1470 }
1471 stack.back().GetScalar() = load_Addr;
1472 stack.back().SetValueType(Value::eValueTypeLoadAddress);
1473 // Fall through to load address code below...
1474 } LLVM_FALLTHROUGH;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 case Value::eValueTypeLoadAddress:
1476 if (exe_ctx) {
1477 if (process) {
1478 lldb::addr_t pointer_addr =
1479 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Zachary Turner97206d52017-05-12 04:51:55 +00001480 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 lldb::addr_t pointer_value =
1482 process->ReadPointerFromMemory(pointer_addr, error);
1483 if (pointer_value != LLDB_INVALID_ADDRESS) {
1484 stack.back().GetScalar() = pointer_value;
1485 stack.back().ClearContext();
1486 } else {
1487 if (error_ptr)
1488 error_ptr->SetErrorStringWithFormat(
1489 "Failed to dereference pointer from 0x%" PRIx64
1490 " for DW_OP_deref: %s\n",
1491 pointer_addr, error.AsCString());
1492 return false;
1493 }
1494 } else {
1495 if (error_ptr)
1496 error_ptr->SetErrorStringWithFormat(
1497 "NULL process for DW_OP_deref.\n");
1498 return false;
1499 }
1500 } else {
1501 if (error_ptr)
1502 error_ptr->SetErrorStringWithFormat(
1503 "NULL execution context for DW_OP_deref.\n");
1504 return false;
1505 }
1506 break;
1507
1508 default:
1509 break;
1510 }
1511
1512 } break;
1513
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514 // OPCODE: DW_OP_deref_size
1515 // OPERANDS: 1
1516 // 1 - uint8_t that specifies the size of the data to dereference.
1517 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1518 // stack entry and treats it as an address. The value retrieved from that
Adrian Prantl05097242018-04-30 16:49:04 +00001519 // address is pushed. In the DW_OP_deref_size operation, however, the size
1520 // in bytes of the data retrieved from the dereferenced address is
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521 // specified by the single operand. This operand is a 1-byte unsigned
1522 // integral constant whose value may not be larger than the size of an
Adrian Prantl05097242018-04-30 16:49:04 +00001523 // address on the target machine. The data retrieved is zero extended to
1524 // the size of an address on the target machine before being pushed on the
1525 // expression stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 case DW_OP_deref_size: {
1527 if (stack.empty()) {
1528 if (error_ptr)
1529 error_ptr->SetErrorString(
1530 "Expression stack empty for DW_OP_deref_size.");
1531 return false;
1532 }
1533 uint8_t size = opcodes.GetU8(&offset);
1534 Value::ValueType value_type = stack.back().GetValueType();
1535 switch (value_type) {
1536 case Value::eValueTypeHostAddress: {
1537 void *src = (void *)stack.back().GetScalar().ULongLong();
1538 intptr_t ptr;
1539 ::memcpy(&ptr, src, sizeof(void *));
1540 // I can't decide whether the size operand should apply to the bytes in
1541 // their
1542 // lldb-host endianness or the target endianness.. I doubt this'll ever
Adrian Prantl05097242018-04-30 16:49:04 +00001543 // come up but I'll opt for assuming big endian regardless.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 switch (size) {
1545 case 1:
1546 ptr = ptr & 0xff;
1547 break;
1548 case 2:
1549 ptr = ptr & 0xffff;
1550 break;
1551 case 3:
1552 ptr = ptr & 0xffffff;
1553 break;
1554 case 4:
1555 ptr = ptr & 0xffffffff;
1556 break;
1557 // the casts are added to work around the case where intptr_t is a 32
1558 // bit quantity;
1559 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1560 // program.
1561 case 5:
1562 ptr = (intptr_t)ptr & 0xffffffffffULL;
1563 break;
1564 case 6:
1565 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1566 break;
1567 case 7:
1568 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1569 break;
1570 default:
1571 break;
1572 }
1573 stack.back().GetScalar() = ptr;
1574 stack.back().ClearContext();
1575 } break;
1576 case Value::eValueTypeLoadAddress:
1577 if (exe_ctx) {
1578 if (process) {
1579 lldb::addr_t pointer_addr =
1580 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1581 uint8_t addr_bytes[sizeof(lldb::addr_t)];
Zachary Turner97206d52017-05-12 04:51:55 +00001582 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1584 size) {
1585 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1586 process->GetByteOrder(), size);
1587 lldb::offset_t addr_data_offset = 0;
1588 switch (size) {
1589 case 1:
1590 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1591 break;
1592 case 2:
1593 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1594 break;
1595 case 4:
1596 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1597 break;
1598 case 8:
1599 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1600 break;
1601 default:
1602 stack.back().GetScalar() =
1603 addr_data.GetPointer(&addr_data_offset);
1604 }
1605 stack.back().ClearContext();
1606 } else {
1607 if (error_ptr)
1608 error_ptr->SetErrorStringWithFormat(
1609 "Failed to dereference pointer from 0x%" PRIx64
1610 " for DW_OP_deref: %s\n",
1611 pointer_addr, error.AsCString());
1612 return false;
1613 }
1614 } else {
1615 if (error_ptr)
1616 error_ptr->SetErrorStringWithFormat(
1617 "NULL process for DW_OP_deref.\n");
1618 return false;
1619 }
1620 } else {
1621 if (error_ptr)
1622 error_ptr->SetErrorStringWithFormat(
1623 "NULL execution context for DW_OP_deref.\n");
1624 return false;
1625 }
1626 break;
1627
1628 default:
1629 break;
1630 }
1631
1632 } break;
1633
Kate Stoneb9c1b512016-09-06 20:57:50 +00001634 // OPCODE: DW_OP_xderef_size
1635 // OPERANDS: 1
1636 // 1 - uint8_t that specifies the size of the data to dereference.
1637 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
Adrian Prantl05097242018-04-30 16:49:04 +00001638 // the top of the stack is treated as an address. The second stack entry is
1639 // treated as an "address space identifier" for those architectures that
1640 // support multiple address spaces. The top two stack elements are popped,
1641 // a data item is retrieved through an implementation-defined address
1642 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1643 // operation, however, the size in bytes of the data retrieved from the
1644 // dereferenced address is specified by the single operand. This operand is
1645 // a 1-byte unsigned integral constant whose value may not be larger than
1646 // the size of an address on the target machine. The data retrieved is zero
1647 // extended to the size of an address on the target machine before being
1648 // pushed on the expression stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 case DW_OP_xderef_size:
1650 if (error_ptr)
1651 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1652 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 // OPCODE: DW_OP_xderef
1654 // OPERANDS: none
1655 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
Adrian Prantl05097242018-04-30 16:49:04 +00001656 // the top of the stack is treated as an address. The second stack entry is
1657 // treated as an "address space identifier" for those architectures that
1658 // support multiple address spaces. The top two stack elements are popped,
1659 // a data item is retrieved through an implementation-defined address
1660 // calculation and pushed as the new stack top. The size of the data
1661 // retrieved from the dereferenced address is the size of an address on the
1662 // target machine.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663 case DW_OP_xderef:
1664 if (error_ptr)
1665 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1666 return false;
1667
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668 // All DW_OP_constXXX opcodes have a single operand as noted below:
1669 //
1670 // Opcode Operand 1
Adrian Prantl05097242018-04-30 16:49:04 +00001671 // DW_OP_const1u 1-byte unsigned integer constant DW_OP_const1s
1672 // 1-byte signed integer constant DW_OP_const2u 2-byte unsigned integer
1673 // constant DW_OP_const2s 2-byte signed integer constant DW_OP_const4u
1674 // 4-byte unsigned integer constant DW_OP_const4s 4-byte signed integer
1675 // constant DW_OP_const8u 8-byte unsigned integer constant DW_OP_const8s
1676 // 8-byte signed integer constant DW_OP_constu unsigned LEB128 integer
1677 // constant DW_OP_consts signed LEB128 integer constant
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 case DW_OP_const1u:
1679 stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1680 break;
1681 case DW_OP_const1s:
1682 stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1683 break;
1684 case DW_OP_const2u:
1685 stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1686 break;
1687 case DW_OP_const2s:
1688 stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1689 break;
1690 case DW_OP_const4u:
1691 stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1692 break;
1693 case DW_OP_const4s:
1694 stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1695 break;
1696 case DW_OP_const8u:
1697 stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1698 break;
1699 case DW_OP_const8s:
1700 stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1701 break;
1702 case DW_OP_constu:
1703 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1704 break;
1705 case DW_OP_consts:
1706 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1707 break;
1708
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709 // OPCODE: DW_OP_dup
1710 // OPERANDS: none
1711 // DESCRIPTION: duplicates the value at the top of the stack
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712 case DW_OP_dup:
1713 if (stack.empty()) {
1714 if (error_ptr)
1715 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1716 return false;
1717 } else
1718 stack.push_back(stack.back());
1719 break;
1720
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721 // OPCODE: DW_OP_drop
1722 // OPERANDS: none
1723 // DESCRIPTION: pops the value at the top of the stack
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724 case DW_OP_drop:
1725 if (stack.empty()) {
1726 if (error_ptr)
1727 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1728 return false;
1729 } else
1730 stack.pop_back();
1731 break;
1732
Kate Stoneb9c1b512016-09-06 20:57:50 +00001733 // OPCODE: DW_OP_over
1734 // OPERANDS: none
1735 // DESCRIPTION: Duplicates the entry currently second in the stack at
1736 // the top of the stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737 case DW_OP_over:
1738 if (stack.size() < 2) {
1739 if (error_ptr)
1740 error_ptr->SetErrorString(
1741 "Expression stack needs at least 2 items for DW_OP_over.");
1742 return false;
1743 } else
1744 stack.push_back(stack[stack.size() - 2]);
1745 break;
1746
Kate Stoneb9c1b512016-09-06 20:57:50 +00001747 // OPCODE: DW_OP_pick
1748 // OPERANDS: uint8_t index into the current stack
1749 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1750 // inclusive) is pushed on the stack
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 case DW_OP_pick: {
1752 uint8_t pick_idx = opcodes.GetU8(&offset);
1753 if (pick_idx < stack.size())
1754 stack.push_back(stack[pick_idx]);
1755 else {
1756 if (error_ptr)
1757 error_ptr->SetErrorStringWithFormat(
1758 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1759 return false;
1760 }
1761 } break;
1762
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 // OPCODE: DW_OP_swap
1764 // OPERANDS: none
1765 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1766 // of the stack becomes the second stack entry, and the second entry
1767 // becomes the top of the stack
Kate Stoneb9c1b512016-09-06 20:57:50 +00001768 case DW_OP_swap:
1769 if (stack.size() < 2) {
1770 if (error_ptr)
1771 error_ptr->SetErrorString(
1772 "Expression stack needs at least 2 items for DW_OP_swap.");
1773 return false;
1774 } else {
1775 tmp = stack.back();
1776 stack.back() = stack[stack.size() - 2];
1777 stack[stack.size() - 2] = tmp;
1778 }
1779 break;
1780
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 // OPCODE: DW_OP_rot
1782 // OPERANDS: none
1783 // DESCRIPTION: Rotates the first three stack entries. The entry at
Adrian Prantl05097242018-04-30 16:49:04 +00001784 // the top of the stack becomes the third stack entry, the second entry
1785 // becomes the top of the stack, and the third entry becomes the second
1786 // entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787 case DW_OP_rot:
1788 if (stack.size() < 3) {
1789 if (error_ptr)
1790 error_ptr->SetErrorString(
1791 "Expression stack needs at least 3 items for DW_OP_rot.");
1792 return false;
1793 } else {
1794 size_t last_idx = stack.size() - 1;
1795 Value old_top = stack[last_idx];
1796 stack[last_idx] = stack[last_idx - 1];
1797 stack[last_idx - 1] = stack[last_idx - 2];
1798 stack[last_idx - 2] = old_top;
1799 }
1800 break;
1801
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802 // OPCODE: DW_OP_abs
1803 // OPERANDS: none
1804 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1805 // value and pushes its absolute value. If the absolute value can not be
1806 // represented, the result is undefined.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 case DW_OP_abs:
1808 if (stack.empty()) {
1809 if (error_ptr)
1810 error_ptr->SetErrorString(
1811 "Expression stack needs at least 1 item for DW_OP_abs.");
1812 return false;
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001813 } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 if (error_ptr)
1815 error_ptr->SetErrorString(
1816 "Failed to take the absolute value of the first stack item.");
1817 return false;
1818 }
1819 break;
1820
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 // OPCODE: DW_OP_and
1822 // OPERANDS: none
1823 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1824 // operation on the two, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 case DW_OP_and:
1826 if (stack.size() < 2) {
1827 if (error_ptr)
1828 error_ptr->SetErrorString(
1829 "Expression stack needs at least 2 items for DW_OP_and.");
1830 return false;
1831 } else {
1832 tmp = stack.back();
1833 stack.pop_back();
1834 stack.back().ResolveValue(exe_ctx) =
1835 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1836 }
1837 break;
1838
Kate Stoneb9c1b512016-09-06 20:57:50 +00001839 // OPCODE: DW_OP_div
1840 // OPERANDS: none
1841 // DESCRIPTION: pops the top two stack values, divides the former second
Adrian Prantl05097242018-04-30 16:49:04 +00001842 // entry by the former top of the stack using signed division, and pushes
1843 // the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 case DW_OP_div:
1845 if (stack.size() < 2) {
1846 if (error_ptr)
1847 error_ptr->SetErrorString(
1848 "Expression stack needs at least 2 items for DW_OP_div.");
1849 return false;
1850 } else {
1851 tmp = stack.back();
1852 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1853 if (error_ptr)
1854 error_ptr->SetErrorString("Divide by zero.");
1855 return false;
1856 } else {
1857 stack.pop_back();
1858 stack.back() =
1859 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1860 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1861 if (error_ptr)
1862 error_ptr->SetErrorString("Divide failed.");
1863 return false;
1864 }
1865 }
1866 }
1867 break;
1868
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 // OPCODE: DW_OP_minus
1870 // OPERANDS: none
1871 // DESCRIPTION: pops the top two stack values, subtracts the former top
1872 // of the stack from the former second entry, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001873 case DW_OP_minus:
1874 if (stack.size() < 2) {
1875 if (error_ptr)
1876 error_ptr->SetErrorString(
1877 "Expression stack needs at least 2 items for DW_OP_minus.");
1878 return false;
1879 } else {
1880 tmp = stack.back();
1881 stack.pop_back();
1882 stack.back().ResolveValue(exe_ctx) =
1883 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1884 }
1885 break;
1886
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 // OPCODE: DW_OP_mod
1888 // OPERANDS: none
1889 // DESCRIPTION: pops the top two stack values and pushes the result of
Adrian Prantl05097242018-04-30 16:49:04 +00001890 // the calculation: former second stack entry modulo the former top of the
1891 // stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001892 case DW_OP_mod:
1893 if (stack.size() < 2) {
1894 if (error_ptr)
1895 error_ptr->SetErrorString(
1896 "Expression stack needs at least 2 items for DW_OP_mod.");
1897 return false;
1898 } else {
1899 tmp = stack.back();
1900 stack.pop_back();
1901 stack.back().ResolveValue(exe_ctx) =
1902 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1903 }
1904 break;
1905
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 // OPCODE: DW_OP_mul
1907 // OPERANDS: none
1908 // DESCRIPTION: pops the top two stack entries, multiplies them
1909 // together, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001910 case DW_OP_mul:
1911 if (stack.size() < 2) {
1912 if (error_ptr)
1913 error_ptr->SetErrorString(
1914 "Expression stack needs at least 2 items for DW_OP_mul.");
1915 return false;
1916 } else {
1917 tmp = stack.back();
1918 stack.pop_back();
1919 stack.back().ResolveValue(exe_ctx) =
1920 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1921 }
1922 break;
1923
Kate Stoneb9c1b512016-09-06 20:57:50 +00001924 // OPCODE: DW_OP_neg
1925 // OPERANDS: none
1926 // DESCRIPTION: pops the top stack entry, and pushes its negation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927 case DW_OP_neg:
1928 if (stack.empty()) {
1929 if (error_ptr)
1930 error_ptr->SetErrorString(
1931 "Expression stack needs at least 1 item for DW_OP_neg.");
1932 return false;
1933 } else {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001934 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001935 if (error_ptr)
1936 error_ptr->SetErrorString("Unary negate failed.");
1937 return false;
1938 }
1939 }
1940 break;
1941
Kate Stoneb9c1b512016-09-06 20:57:50 +00001942 // OPCODE: DW_OP_not
1943 // OPERANDS: none
1944 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1945 // complement
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946 case DW_OP_not:
1947 if (stack.empty()) {
1948 if (error_ptr)
1949 error_ptr->SetErrorString(
1950 "Expression stack needs at least 1 item for DW_OP_not.");
1951 return false;
1952 } else {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001953 if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954 if (error_ptr)
1955 error_ptr->SetErrorString("Logical NOT failed.");
1956 return false;
1957 }
1958 }
1959 break;
1960
Kate Stoneb9c1b512016-09-06 20:57:50 +00001961 // OPCODE: DW_OP_or
1962 // OPERANDS: none
1963 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1964 // operation on the two, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001965 case DW_OP_or:
1966 if (stack.size() < 2) {
1967 if (error_ptr)
1968 error_ptr->SetErrorString(
1969 "Expression stack needs at least 2 items for DW_OP_or.");
1970 return false;
1971 } else {
1972 tmp = stack.back();
1973 stack.pop_back();
1974 stack.back().ResolveValue(exe_ctx) =
1975 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1976 }
1977 break;
1978
Kate Stoneb9c1b512016-09-06 20:57:50 +00001979 // OPCODE: DW_OP_plus
1980 // OPERANDS: none
1981 // DESCRIPTION: pops the top two stack entries, adds them together, and
1982 // pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001983 case DW_OP_plus:
1984 if (stack.size() < 2) {
1985 if (error_ptr)
1986 error_ptr->SetErrorString(
1987 "Expression stack needs at least 2 items for DW_OP_plus.");
1988 return false;
1989 } else {
1990 tmp = stack.back();
1991 stack.pop_back();
1992 stack.back().GetScalar() += tmp.GetScalar();
1993 }
1994 break;
1995
Kate Stoneb9c1b512016-09-06 20:57:50 +00001996 // OPCODE: DW_OP_plus_uconst
1997 // OPERANDS: none
1998 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1999 // constant operand and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002000 case DW_OP_plus_uconst:
2001 if (stack.empty()) {
2002 if (error_ptr)
2003 error_ptr->SetErrorString(
2004 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2005 return false;
2006 } else {
2007 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
2008 // Implicit conversion from a UINT to a Scalar...
2009 stack.back().GetScalar() += uconst_value;
2010 if (!stack.back().GetScalar().IsValid()) {
2011 if (error_ptr)
2012 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2013 return false;
2014 }
2015 }
2016 break;
2017
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018 // OPCODE: DW_OP_shl
2019 // OPERANDS: none
2020 // DESCRIPTION: pops the top two stack entries, shifts the former
Adrian Prantl05097242018-04-30 16:49:04 +00002021 // second entry left by the number of bits specified by the former top of
2022 // the stack, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023 case DW_OP_shl:
2024 if (stack.size() < 2) {
2025 if (error_ptr)
2026 error_ptr->SetErrorString(
2027 "Expression stack needs at least 2 items for DW_OP_shl.");
2028 return false;
2029 } else {
2030 tmp = stack.back();
2031 stack.pop_back();
2032 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
2033 }
2034 break;
2035
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036 // OPCODE: DW_OP_shr
2037 // OPERANDS: none
2038 // DESCRIPTION: pops the top two stack entries, shifts the former second
2039 // entry right logically (filling with zero bits) by the number of bits
2040 // specified by the former top of the stack, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 case DW_OP_shr:
2042 if (stack.size() < 2) {
2043 if (error_ptr)
2044 error_ptr->SetErrorString(
2045 "Expression stack needs at least 2 items for DW_OP_shr.");
2046 return false;
2047 } else {
2048 tmp = stack.back();
2049 stack.pop_back();
Jonas Devliegherea6682a42018-12-15 00:15:33 +00002050 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
2051 tmp.ResolveValue(exe_ctx))) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002052 if (error_ptr)
2053 error_ptr->SetErrorString("DW_OP_shr failed.");
2054 return false;
2055 }
2056 }
2057 break;
2058
Kate Stoneb9c1b512016-09-06 20:57:50 +00002059 // OPCODE: DW_OP_shra
2060 // OPERANDS: none
2061 // DESCRIPTION: pops the top two stack entries, shifts the former second
2062 // entry right arithmetically (divide the magnitude by 2, keep the same
Adrian Prantl05097242018-04-30 16:49:04 +00002063 // sign for the result) by the number of bits specified by the former top
2064 // of the stack, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002065 case DW_OP_shra:
2066 if (stack.size() < 2) {
2067 if (error_ptr)
2068 error_ptr->SetErrorString(
2069 "Expression stack needs at least 2 items for DW_OP_shra.");
2070 return false;
2071 } else {
2072 tmp = stack.back();
2073 stack.pop_back();
2074 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2075 }
2076 break;
2077
Kate Stoneb9c1b512016-09-06 20:57:50 +00002078 // OPCODE: DW_OP_xor
2079 // OPERANDS: none
2080 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2081 // exclusive-or operation on the two, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002082 case DW_OP_xor:
2083 if (stack.size() < 2) {
2084 if (error_ptr)
2085 error_ptr->SetErrorString(
2086 "Expression stack needs at least 2 items for DW_OP_xor.");
2087 return false;
2088 } else {
2089 tmp = stack.back();
2090 stack.pop_back();
2091 stack.back().ResolveValue(exe_ctx) =
2092 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2093 }
2094 break;
2095
Kate Stoneb9c1b512016-09-06 20:57:50 +00002096 // OPCODE: DW_OP_skip
2097 // OPERANDS: int16_t
2098 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
Adrian Prantl05097242018-04-30 16:49:04 +00002099 // signed integer constant. The 2-byte constant is the number of bytes of
2100 // the DWARF expression to skip forward or backward from the current
Kate Stoneb9c1b512016-09-06 20:57:50 +00002101 // operation, beginning after the 2-byte constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002102 case DW_OP_skip: {
2103 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2104 lldb::offset_t new_offset = offset + skip_offset;
2105 if (new_offset >= opcodes_offset && new_offset < end_offset)
2106 offset = new_offset;
2107 else {
2108 if (error_ptr)
2109 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2110 return false;
2111 }
2112 } break;
2113
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114 // OPCODE: DW_OP_bra
2115 // OPERANDS: int16_t
2116 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
Adrian Prantl05097242018-04-30 16:49:04 +00002117 // signed integer constant. This operation pops the top of stack. If the
2118 // value popped is not the constant 0, the 2-byte constant operand is the
2119 // number of bytes of the DWARF expression to skip forward or backward from
2120 // the current operation, beginning after the 2-byte constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002121 case DW_OP_bra:
2122 if (stack.empty()) {
2123 if (error_ptr)
2124 error_ptr->SetErrorString(
2125 "Expression stack needs at least 1 item for DW_OP_bra.");
2126 return false;
2127 } else {
2128 tmp = stack.back();
2129 stack.pop_back();
2130 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2131 Scalar zero(0);
2132 if (tmp.ResolveValue(exe_ctx) != zero) {
2133 lldb::offset_t new_offset = offset + bra_offset;
2134 if (new_offset >= opcodes_offset && new_offset < end_offset)
2135 offset = new_offset;
2136 else {
2137 if (error_ptr)
2138 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2139 return false;
2140 }
2141 }
2142 }
2143 break;
2144
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145 // OPCODE: DW_OP_eq
2146 // OPERANDS: none
2147 // DESCRIPTION: pops the top two stack values, compares using the
2148 // equals (==) operator.
2149 // STACK RESULT: push the constant value 1 onto the stack if the result
2150 // of the operation is true or the constant value 0 if the result of the
2151 // operation is false.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002152 case DW_OP_eq:
2153 if (stack.size() < 2) {
2154 if (error_ptr)
2155 error_ptr->SetErrorString(
2156 "Expression stack needs at least 2 items for DW_OP_eq.");
2157 return false;
2158 } else {
2159 tmp = stack.back();
2160 stack.pop_back();
2161 stack.back().ResolveValue(exe_ctx) =
2162 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2163 }
2164 break;
2165
Kate Stoneb9c1b512016-09-06 20:57:50 +00002166 // OPCODE: DW_OP_ge
2167 // OPERANDS: none
2168 // DESCRIPTION: pops the top two stack values, compares using the
2169 // greater than or equal to (>=) operator.
2170 // STACK RESULT: push the constant value 1 onto the stack if the result
2171 // of the operation is true or the constant value 0 if the result of the
2172 // operation is false.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002173 case DW_OP_ge:
2174 if (stack.size() < 2) {
2175 if (error_ptr)
2176 error_ptr->SetErrorString(
2177 "Expression stack needs at least 2 items for DW_OP_ge.");
2178 return false;
2179 } else {
2180 tmp = stack.back();
2181 stack.pop_back();
2182 stack.back().ResolveValue(exe_ctx) =
2183 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2184 }
2185 break;
2186
Kate Stoneb9c1b512016-09-06 20:57:50 +00002187 // OPCODE: DW_OP_gt
2188 // OPERANDS: none
2189 // DESCRIPTION: pops the top two stack values, compares using the
2190 // greater than (>) operator.
2191 // STACK RESULT: push the constant value 1 onto the stack if the result
2192 // of the operation is true or the constant value 0 if the result of the
2193 // operation is false.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194 case DW_OP_gt:
2195 if (stack.size() < 2) {
2196 if (error_ptr)
2197 error_ptr->SetErrorString(
2198 "Expression stack needs at least 2 items for DW_OP_gt.");
2199 return false;
2200 } else {
2201 tmp = stack.back();
2202 stack.pop_back();
2203 stack.back().ResolveValue(exe_ctx) =
2204 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2205 }
2206 break;
2207
Kate Stoneb9c1b512016-09-06 20:57:50 +00002208 // OPCODE: DW_OP_le
2209 // OPERANDS: none
2210 // DESCRIPTION: pops the top two stack values, compares using the
2211 // less than or equal to (<=) operator.
2212 // STACK RESULT: push the constant value 1 onto the stack if the result
2213 // of the operation is true or the constant value 0 if the result of the
2214 // operation is false.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 case DW_OP_le:
2216 if (stack.size() < 2) {
2217 if (error_ptr)
2218 error_ptr->SetErrorString(
2219 "Expression stack needs at least 2 items for DW_OP_le.");
2220 return false;
2221 } else {
2222 tmp = stack.back();
2223 stack.pop_back();
2224 stack.back().ResolveValue(exe_ctx) =
2225 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2226 }
2227 break;
2228
Kate Stoneb9c1b512016-09-06 20:57:50 +00002229 // OPCODE: DW_OP_lt
2230 // OPERANDS: none
2231 // DESCRIPTION: pops the top two stack values, compares using the
2232 // less than (<) operator.
2233 // STACK RESULT: push the constant value 1 onto the stack if the result
2234 // of the operation is true or the constant value 0 if the result of the
2235 // operation is false.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002236 case DW_OP_lt:
2237 if (stack.size() < 2) {
2238 if (error_ptr)
2239 error_ptr->SetErrorString(
2240 "Expression stack needs at least 2 items for DW_OP_lt.");
2241 return false;
2242 } else {
2243 tmp = stack.back();
2244 stack.pop_back();
2245 stack.back().ResolveValue(exe_ctx) =
2246 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2247 }
2248 break;
2249
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250 // OPCODE: DW_OP_ne
2251 // OPERANDS: none
2252 // DESCRIPTION: pops the top two stack values, compares using the
2253 // not equal (!=) operator.
2254 // STACK RESULT: push the constant value 1 onto the stack if the result
2255 // of the operation is true or the constant value 0 if the result of the
2256 // operation is false.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 case DW_OP_ne:
2258 if (stack.size() < 2) {
2259 if (error_ptr)
2260 error_ptr->SetErrorString(
2261 "Expression stack needs at least 2 items for DW_OP_ne.");
2262 return false;
2263 } else {
2264 tmp = stack.back();
2265 stack.pop_back();
2266 stack.back().ResolveValue(exe_ctx) =
2267 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2268 }
2269 break;
2270
Kate Stoneb9c1b512016-09-06 20:57:50 +00002271 // OPCODE: DW_OP_litn
2272 // OPERANDS: none
2273 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2274 // STACK RESULT: push the unsigned literal constant value onto the top
2275 // of the stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002276 case DW_OP_lit0:
2277 case DW_OP_lit1:
2278 case DW_OP_lit2:
2279 case DW_OP_lit3:
2280 case DW_OP_lit4:
2281 case DW_OP_lit5:
2282 case DW_OP_lit6:
2283 case DW_OP_lit7:
2284 case DW_OP_lit8:
2285 case DW_OP_lit9:
2286 case DW_OP_lit10:
2287 case DW_OP_lit11:
2288 case DW_OP_lit12:
2289 case DW_OP_lit13:
2290 case DW_OP_lit14:
2291 case DW_OP_lit15:
2292 case DW_OP_lit16:
2293 case DW_OP_lit17:
2294 case DW_OP_lit18:
2295 case DW_OP_lit19:
2296 case DW_OP_lit20:
2297 case DW_OP_lit21:
2298 case DW_OP_lit22:
2299 case DW_OP_lit23:
2300 case DW_OP_lit24:
2301 case DW_OP_lit25:
2302 case DW_OP_lit26:
2303 case DW_OP_lit27:
2304 case DW_OP_lit28:
2305 case DW_OP_lit29:
2306 case DW_OP_lit30:
2307 case DW_OP_lit31:
Jonas Devliegherebf2d1122018-09-13 15:18:39 +00002308 stack.push_back(Scalar((uint64_t)(op - DW_OP_lit0)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002309 break;
2310
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311 // OPCODE: DW_OP_regN
2312 // OPERANDS: none
2313 // DESCRIPTION: Push the value in register n on the top of the stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002314 case DW_OP_reg0:
2315 case DW_OP_reg1:
2316 case DW_OP_reg2:
2317 case DW_OP_reg3:
2318 case DW_OP_reg4:
2319 case DW_OP_reg5:
2320 case DW_OP_reg6:
2321 case DW_OP_reg7:
2322 case DW_OP_reg8:
2323 case DW_OP_reg9:
2324 case DW_OP_reg10:
2325 case DW_OP_reg11:
2326 case DW_OP_reg12:
2327 case DW_OP_reg13:
2328 case DW_OP_reg14:
2329 case DW_OP_reg15:
2330 case DW_OP_reg16:
2331 case DW_OP_reg17:
2332 case DW_OP_reg18:
2333 case DW_OP_reg19:
2334 case DW_OP_reg20:
2335 case DW_OP_reg21:
2336 case DW_OP_reg22:
2337 case DW_OP_reg23:
2338 case DW_OP_reg24:
2339 case DW_OP_reg25:
2340 case DW_OP_reg26:
2341 case DW_OP_reg27:
2342 case DW_OP_reg28:
2343 case DW_OP_reg29:
2344 case DW_OP_reg30:
2345 case DW_OP_reg31: {
2346 reg_num = op - DW_OP_reg0;
2347
2348 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2349 stack.push_back(tmp);
2350 else
2351 return false;
2352 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002353 // OPCODE: DW_OP_regx
2354 // OPERANDS:
2355 // ULEB128 literal operand that encodes the register.
2356 // DESCRIPTION: Push the value in register on the top of the stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002357 case DW_OP_regx: {
2358 reg_num = opcodes.GetULEB128(&offset);
2359 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2360 stack.push_back(tmp);
2361 else
2362 return false;
2363 } break;
2364
Kate Stoneb9c1b512016-09-06 20:57:50 +00002365 // OPCODE: DW_OP_bregN
2366 // OPERANDS:
2367 // SLEB128 offset from register N
2368 // DESCRIPTION: Value is in memory at the address specified by register
2369 // N plus an offset.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002370 case DW_OP_breg0:
2371 case DW_OP_breg1:
2372 case DW_OP_breg2:
2373 case DW_OP_breg3:
2374 case DW_OP_breg4:
2375 case DW_OP_breg5:
2376 case DW_OP_breg6:
2377 case DW_OP_breg7:
2378 case DW_OP_breg8:
2379 case DW_OP_breg9:
2380 case DW_OP_breg10:
2381 case DW_OP_breg11:
2382 case DW_OP_breg12:
2383 case DW_OP_breg13:
2384 case DW_OP_breg14:
2385 case DW_OP_breg15:
2386 case DW_OP_breg16:
2387 case DW_OP_breg17:
2388 case DW_OP_breg18:
2389 case DW_OP_breg19:
2390 case DW_OP_breg20:
2391 case DW_OP_breg21:
2392 case DW_OP_breg22:
2393 case DW_OP_breg23:
2394 case DW_OP_breg24:
2395 case DW_OP_breg25:
2396 case DW_OP_breg26:
2397 case DW_OP_breg27:
2398 case DW_OP_breg28:
2399 case DW_OP_breg29:
2400 case DW_OP_breg30:
2401 case DW_OP_breg31: {
2402 reg_num = op - DW_OP_breg0;
2403
2404 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2405 tmp)) {
2406 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2407 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2408 tmp.ClearContext();
2409 stack.push_back(tmp);
2410 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2411 } else
2412 return false;
2413 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002414 // OPCODE: DW_OP_bregx
2415 // OPERANDS: 2
2416 // ULEB128 literal operand that encodes the register.
2417 // SLEB128 offset from register N
2418 // DESCRIPTION: Value is in memory at the address specified by register
2419 // N plus an offset.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002420 case DW_OP_bregx: {
2421 reg_num = opcodes.GetULEB128(&offset);
2422
2423 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2424 tmp)) {
2425 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2426 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2427 tmp.ClearContext();
2428 stack.push_back(tmp);
2429 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2430 } else
2431 return false;
2432 } break;
2433
2434 case DW_OP_fbreg:
2435 if (exe_ctx) {
2436 if (frame) {
2437 Scalar value;
2438 if (frame->GetFrameBaseValue(value, error_ptr)) {
2439 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2440 value += fbreg_offset;
2441 stack.push_back(value);
2442 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2443 } else
2444 return false;
2445 } else {
2446 if (error_ptr)
2447 error_ptr->SetErrorString(
2448 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2449 return false;
2450 }
2451 } else {
2452 if (error_ptr)
2453 error_ptr->SetErrorStringWithFormat(
2454 "NULL execution context for DW_OP_fbreg.\n");
2455 return false;
2456 }
2457
2458 break;
2459
Kate Stoneb9c1b512016-09-06 20:57:50 +00002460 // OPCODE: DW_OP_nop
2461 // OPERANDS: none
2462 // DESCRIPTION: A place holder. It has no effect on the location stack
2463 // or any of its values.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464 case DW_OP_nop:
2465 break;
2466
Kate Stoneb9c1b512016-09-06 20:57:50 +00002467 // OPCODE: DW_OP_piece
2468 // OPERANDS: 1
2469 // ULEB128: byte size of the piece
2470 // DESCRIPTION: The operand describes the size in bytes of the piece of
Adrian Prantl05097242018-04-30 16:49:04 +00002471 // the object referenced by the DWARF expression whose result is at the top
2472 // of the stack. If the piece is located in a register, but does not occupy
2473 // the entire register, the placement of the piece within that register is
2474 // defined by the ABI.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002475 //
Adrian Prantl05097242018-04-30 16:49:04 +00002476 // Many compilers store a single variable in sets of registers, or store a
2477 // variable partially in memory and partially in registers. DW_OP_piece
2478 // provides a way of describing how large a part of a variable a particular
2479 // DWARF expression refers to.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480 case DW_OP_piece: {
2481 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2482
2483 if (piece_byte_size > 0) {
2484 Value curr_piece;
2485
2486 if (stack.empty()) {
2487 // In a multi-piece expression, this means that the current piece is
Adrian Prantl05097242018-04-30 16:49:04 +00002488 // not available. Fill with zeros for now by resizing the data and
2489 // appending it
Kate Stoneb9c1b512016-09-06 20:57:50 +00002490 curr_piece.ResizeData(piece_byte_size);
2491 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2492 pieces.AppendDataToHostBuffer(curr_piece);
2493 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00002494 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002495 // Extract the current piece into "curr_piece"
2496 Value curr_piece_source_value(stack.back());
2497 stack.pop_back();
2498
2499 const Value::ValueType curr_piece_source_value_type =
2500 curr_piece_source_value.GetValueType();
2501 switch (curr_piece_source_value_type) {
2502 case Value::eValueTypeLoadAddress:
2503 if (process) {
2504 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2505 lldb::addr_t load_addr =
2506 curr_piece_source_value.GetScalar().ULongLong(
2507 LLDB_INVALID_ADDRESS);
2508 if (process->ReadMemory(
2509 load_addr, curr_piece.GetBuffer().GetBytes(),
2510 piece_byte_size, error) != piece_byte_size) {
2511 if (error_ptr)
2512 error_ptr->SetErrorStringWithFormat(
2513 "failed to read memory DW_OP_piece(%" PRIu64
2514 ") from 0x%" PRIx64,
2515 piece_byte_size, load_addr);
2516 return false;
2517 }
2518 } else {
2519 if (error_ptr)
2520 error_ptr->SetErrorStringWithFormat(
2521 "failed to resize the piece memory buffer for "
2522 "DW_OP_piece(%" PRIu64 ")",
2523 piece_byte_size);
2524 return false;
2525 }
2526 }
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00002527 break;
2528
Kate Stoneb9c1b512016-09-06 20:57:50 +00002529 case Value::eValueTypeFileAddress:
2530 case Value::eValueTypeHostAddress:
2531 if (error_ptr) {
2532 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2533 LLDB_INVALID_ADDRESS);
2534 error_ptr->SetErrorStringWithFormat(
2535 "failed to read memory DW_OP_piece(%" PRIu64
2536 ") from %s address 0x%" PRIx64,
2537 piece_byte_size, curr_piece_source_value.GetValueType() ==
2538 Value::eValueTypeFileAddress
2539 ? "file"
2540 : "host",
2541 addr);
2542 }
Sean Callanan4740a732016-09-06 04:48:36 +00002543 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544
2545 case Value::eValueTypeScalar: {
2546 uint32_t bit_size = piece_byte_size * 8;
2547 uint32_t bit_offset = 0;
2548 if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2549 bit_size, bit_offset)) {
2550 if (error_ptr)
2551 error_ptr->SetErrorStringWithFormat(
2552 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2553 " byte scalar value.",
2554 piece_byte_size,
2555 (uint64_t)curr_piece_source_value.GetScalar()
2556 .GetByteSize());
2557 return false;
2558 }
2559 curr_piece = curr_piece_source_value;
2560 } break;
2561
2562 case Value::eValueTypeVector: {
2563 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2564 curr_piece_source_value.GetVector().length = piece_byte_size;
2565 else {
2566 if (error_ptr)
2567 error_ptr->SetErrorStringWithFormat(
2568 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2569 " byte vector value.",
2570 piece_byte_size,
2571 (uint64_t)curr_piece_source_value.GetVector().length);
2572 return false;
2573 }
2574 } break;
2575 }
2576
2577 // Check if this is the first piece?
2578 if (op_piece_offset == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00002579 // This is the first piece, we should push it back onto the stack
2580 // so subsequent pieces will be able to access this piece and add
2581 // to it
Kate Stoneb9c1b512016-09-06 20:57:50 +00002582 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2583 if (error_ptr)
2584 error_ptr->SetErrorString("failed to append piece data");
2585 return false;
2586 }
2587 } else {
2588 // If this is the second or later piece there should be a value on
2589 // the stack
2590 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2591 if (error_ptr)
2592 error_ptr->SetErrorStringWithFormat(
2593 "DW_OP_piece for offset %" PRIu64
2594 " but top of stack is of size %" PRIu64,
2595 op_piece_offset, pieces.GetBuffer().GetByteSize());
2596 return false;
2597 }
2598
2599 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2600 if (error_ptr)
2601 error_ptr->SetErrorString("failed to append piece data");
2602 return false;
2603 }
2604 }
2605 op_piece_offset += piece_byte_size;
Sean Callanan4740a732016-09-06 04:48:36 +00002606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002607 }
2608 } break;
Sean Callanan4740a732016-09-06 04:48:36 +00002609
Kate Stoneb9c1b512016-09-06 20:57:50 +00002610 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2611 if (stack.size() < 1) {
2612 if (error_ptr)
2613 error_ptr->SetErrorString(
2614 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
Sean Callanan4740a732016-09-06 04:48:36 +00002615 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002616 } else {
2617 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2618 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2619 switch (stack.back().GetValueType()) {
2620 case Value::eValueTypeScalar: {
2621 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2622 piece_bit_offset)) {
2623 if (error_ptr)
2624 error_ptr->SetErrorStringWithFormat(
2625 "unable to extract %" PRIu64 " bit value with %" PRIu64
2626 " bit offset from a %" PRIu64 " bit scalar value.",
2627 piece_bit_size, piece_bit_offset,
2628 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2629 return false;
2630 }
2631 } break;
Sean Callanan4740a732016-09-06 04:48:36 +00002632
Kate Stoneb9c1b512016-09-06 20:57:50 +00002633 case Value::eValueTypeFileAddress:
2634 case Value::eValueTypeLoadAddress:
2635 case Value::eValueTypeHostAddress:
2636 if (error_ptr) {
2637 error_ptr->SetErrorStringWithFormat(
2638 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
Bruce Mitchener4ebdee02018-05-29 09:10:46 +00002639 ", bit_offset = %" PRIu64 ") from an address value.",
Kate Stoneb9c1b512016-09-06 20:57:50 +00002640 piece_bit_size, piece_bit_offset);
2641 }
2642 return false;
2643
2644 case Value::eValueTypeVector:
2645 if (error_ptr) {
2646 error_ptr->SetErrorStringWithFormat(
2647 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2648 ", bit_offset = %" PRIu64 ") from a vector value.",
2649 piece_bit_size, piece_bit_offset);
2650 }
2651 return false;
2652 }
2653 }
2654 break;
2655
Kate Stoneb9c1b512016-09-06 20:57:50 +00002656 // OPCODE: DW_OP_push_object_address
2657 // OPERANDS: none
2658 // DESCRIPTION: Pushes the address of the object currently being
Adrian Prantl05097242018-04-30 16:49:04 +00002659 // evaluated as part of evaluation of a user presented expression. This
2660 // object may correspond to an independent variable described by its own
2661 // DIE or it may be a component of an array, structure, or class whose
2662 // address has been dynamically determined by an earlier step during user
2663 // expression evaluation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002664 case DW_OP_push_object_address:
2665 if (object_address_ptr)
2666 stack.push_back(*object_address_ptr);
2667 else {
2668 if (error_ptr)
2669 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2670 "specifying an object address");
2671 return false;
2672 }
2673 break;
2674
Kate Stoneb9c1b512016-09-06 20:57:50 +00002675 // OPCODE: DW_OP_call2
2676 // OPERANDS:
2677 // uint16_t compile unit relative offset of a DIE
2678 // DESCRIPTION: Performs subroutine calls during evaluation
Adrian Prantl05097242018-04-30 16:49:04 +00002679 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2680 // debugging information entry in the current compilation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002681 //
2682 // Operand interpretation is exactly like that for DW_FORM_ref2.
2683 //
Adrian Prantl05097242018-04-30 16:49:04 +00002684 // This operation transfers control of DWARF expression evaluation to the
2685 // DW_AT_location attribute of the referenced DIE. If there is no such
2686 // attribute, then there is no effect. Execution of the DWARF expression of
2687 // a DW_AT_location attribute may add to and/or remove from values on the
2688 // stack. Execution returns to the point following the call when the end of
2689 // the attribute is reached. Values on the stack at the time of the call
2690 // may be used as parameters by the called expression and values left on
2691 // the stack by the called expression may be used as return values by prior
2692 // agreement between the calling and called expressions.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002693 case DW_OP_call2:
2694 if (error_ptr)
2695 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2696 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002697 // OPCODE: DW_OP_call4
2698 // OPERANDS: 1
2699 // uint32_t compile unit relative offset of a DIE
2700 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
Adrian Prantl05097242018-04-30 16:49:04 +00002701 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2702 // a debugging information entry in the current compilation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002703 //
2704 // Operand interpretation DW_OP_call4 is exactly like that for
2705 // DW_FORM_ref4.
2706 //
Adrian Prantl05097242018-04-30 16:49:04 +00002707 // This operation transfers control of DWARF expression evaluation to the
2708 // DW_AT_location attribute of the referenced DIE. If there is no such
2709 // attribute, then there is no effect. Execution of the DWARF expression of
2710 // a DW_AT_location attribute may add to and/or remove from values on the
2711 // stack. Execution returns to the point following the call when the end of
2712 // the attribute is reached. Values on the stack at the time of the call
2713 // may be used as parameters by the called expression and values left on
2714 // the stack by the called expression may be used as return values by prior
2715 // agreement between the calling and called expressions.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002716 case DW_OP_call4:
2717 if (error_ptr)
2718 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2719 return false;
2720
Kate Stoneb9c1b512016-09-06 20:57:50 +00002721 // OPCODE: DW_OP_stack_value
2722 // OPERANDS: None
2723 // DESCRIPTION: Specifies that the object does not exist in memory but
Adrian Prantl05097242018-04-30 16:49:04 +00002724 // rather is a constant value. The value from the top of the stack is the
2725 // value to be used. This is the actual object value and not the location.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002726 case DW_OP_stack_value:
2727 stack.back().SetValueType(Value::eValueTypeScalar);
2728 break;
2729
Kate Stoneb9c1b512016-09-06 20:57:50 +00002730 // OPCODE: DW_OP_call_frame_cfa
2731 // OPERANDS: None
2732 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2733 // the canonical frame address consistent with the call frame information
2734 // located in .debug_frame (or in the FDEs of the eh_frame section).
Kate Stoneb9c1b512016-09-06 20:57:50 +00002735 case DW_OP_call_frame_cfa:
2736 if (frame) {
2737 // Note that we don't have to parse FDEs because this DWARF expression
2738 // is commonly evaluated with a valid stack frame.
2739 StackID id = frame->GetStackID();
2740 addr_t cfa = id.GetCallFrameAddress();
2741 if (cfa != LLDB_INVALID_ADDRESS) {
2742 stack.push_back(Scalar(cfa));
2743 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2744 } else if (error_ptr)
2745 error_ptr->SetErrorString("Stack frame does not include a canonical "
2746 "frame address for DW_OP_call_frame_cfa "
2747 "opcode.");
2748 } else {
2749 if (error_ptr)
2750 error_ptr->SetErrorString("Invalid stack frame in context for "
2751 "DW_OP_call_frame_cfa opcode.");
2752 return false;
2753 }
2754 break;
2755
Kate Stoneb9c1b512016-09-06 20:57:50 +00002756 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2757 // opcode, DW_OP_GNU_push_tls_address)
2758 // OPERANDS: none
2759 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
Adrian Prantl05097242018-04-30 16:49:04 +00002760 // an address in the current thread's thread-local storage block, and
2761 // pushes it on the stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002762 case DW_OP_form_tls_address:
2763 case DW_OP_GNU_push_tls_address: {
2764 if (stack.size() < 1) {
2765 if (error_ptr) {
2766 if (op == DW_OP_form_tls_address)
2767 error_ptr->SetErrorString(
2768 "DW_OP_form_tls_address needs an argument.");
2769 else
2770 error_ptr->SetErrorString(
2771 "DW_OP_GNU_push_tls_address needs an argument.");
2772 }
2773 return false;
2774 }
2775
2776 if (!exe_ctx || !module_sp) {
2777 if (error_ptr)
2778 error_ptr->SetErrorString("No context to evaluate TLS within.");
2779 return false;
2780 }
2781
2782 Thread *thread = exe_ctx->GetThreadPtr();
2783 if (!thread) {
2784 if (error_ptr)
2785 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2786 return false;
2787 }
2788
2789 // Lookup the TLS block address for this thread and module.
2790 const addr_t tls_file_addr =
2791 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2792 const addr_t tls_load_addr =
2793 thread->GetThreadLocalData(module_sp, tls_file_addr);
2794
2795 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2796 if (error_ptr)
2797 error_ptr->SetErrorString(
2798 "No TLS data currently exists for this thread.");
2799 return false;
2800 }
2801
2802 stack.back().GetScalar() = tls_load_addr;
2803 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2804 } break;
2805
Ali Tamurd0156252019-03-07 19:41:08 +00002806 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002807 // OPERANDS: 1
2808 // ULEB128: index to the .debug_addr section
2809 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
Adrian Prantl05097242018-04-30 16:49:04 +00002810 // section with the base address specified by the DW_AT_addr_base attribute
2811 // and the 0 based index is the ULEB128 encoded index.
Ali Tamurd0156252019-03-07 19:41:08 +00002812 case DW_OP_addrx:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002813 case DW_OP_GNU_addr_index: {
2814 if (!dwarf_cu) {
2815 if (error_ptr)
2816 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2817 "compile unit being specified");
2818 return false;
2819 }
2820 uint64_t index = opcodes.GetULEB128(&offset);
2821 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2822 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2823 lldb::offset_t offset = addr_base + index * index_size;
2824 uint64_t value =
2825 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
2826 &offset, index_size);
2827 stack.push_back(Scalar(value));
2828 stack.back().SetValueType(Value::eValueTypeFileAddress);
2829 } break;
2830
Kate Stoneb9c1b512016-09-06 20:57:50 +00002831 // OPCODE: DW_OP_GNU_const_index
2832 // OPERANDS: 1
2833 // ULEB128: index to the .debug_addr section
2834 // DESCRIPTION: Pushes an constant with the size of a machine address to
2835 // the stack from the .debug_addr section with the base address specified
2836 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2837 // encoded index.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002838 case DW_OP_GNU_const_index: {
2839 if (!dwarf_cu) {
2840 if (error_ptr)
2841 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2842 "compile unit being specified");
2843 return false;
2844 }
2845 uint64_t index = opcodes.GetULEB128(&offset);
2846 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2847 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2848 lldb::offset_t offset = addr_base + index * index_size;
2849 const DWARFDataExtractor &debug_addr =
2850 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2851 switch (index_size) {
2852 case 4:
2853 stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2854 break;
2855 case 8:
2856 stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2857 break;
2858 default:
2859 assert(false && "Unhandled index size");
2860 return false;
2861 }
2862 } break;
2863
Sean Callanan4740a732016-09-06 04:48:36 +00002864 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002865 if (log)
2866 log->Printf("Unhandled opcode %s in DWARFExpression.",
2867 DW_OP_value_to_name(op));
2868 break;
Sean Callanan4740a732016-09-06 04:48:36 +00002869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870 }
2871
2872 if (stack.empty()) {
2873 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2874 // or DW_OP_bit_piece opcodes
2875 if (pieces.GetBuffer().GetByteSize()) {
2876 result = pieces;
2877 } else {
2878 if (error_ptr)
2879 error_ptr->SetErrorString("Stack empty after evaluation.");
2880 return false;
2881 }
2882 } else {
2883 if (log && log->GetVerbose()) {
2884 size_t count = stack.size();
2885 log->Printf("Stack after operation has %" PRIu64 " values:",
2886 (uint64_t)count);
2887 for (size_t i = 0; i < count; ++i) {
2888 StreamString new_value;
2889 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2890 stack[i].Dump(&new_value);
2891 log->Printf(" %s", new_value.GetData());
2892 }
2893 }
2894 result = stack.back();
2895 }
2896 return true; // Return true on success
Sean Callanan4740a732016-09-06 04:48:36 +00002897}
2898
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002899size_t DWARFExpression::LocationListSize(const DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002900 const DataExtractor &debug_loc_data,
2901 lldb::offset_t offset) {
2902 const lldb::offset_t debug_loc_offset = offset;
2903 while (debug_loc_data.ValidOffset(offset)) {
2904 lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
2905 lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
2906 if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
2907 start_addr, end_addr))
2908 break;
2909
2910 if (start_addr == 0 && end_addr == 0)
2911 break;
2912
2913 uint16_t loc_length = debug_loc_data.GetU16(&offset);
2914 offset += loc_length;
2915 }
2916
2917 if (offset > debug_loc_offset)
2918 return offset - debug_loc_offset;
2919 return 0;
2920}
2921
2922bool DWARFExpression::AddressRangeForLocationListEntry(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002923 const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
2925 if (!debug_loc_data.ValidOffset(*offset_ptr))
2926 return false;
2927
George Rimard8719642018-10-25 09:22:26 +00002928 DWARFExpression::LocationListFormat format =
2929 dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat();
2930 switch (format) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002931 case NonLocationList:
2932 return false;
2933 case RegularLocationList:
2934 low_pc = debug_loc_data.GetAddress(offset_ptr);
2935 high_pc = debug_loc_data.GetAddress(offset_ptr);
2936 return true;
2937 case SplitDwarfLocationList:
George Rimare4dee262018-10-23 09:46:15 +00002938 case LocLists:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002939 switch (debug_loc_data.GetU8(offset_ptr)) {
Adrian Prantle7045062016-10-28 20:11:27 +00002940 case DW_LLE_end_of_list:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002941 return false;
Pavel Labath6b91e402016-10-31 11:53:13 +00002942 case DW_LLE_startx_endx: {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002943 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
2944 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2945 index = debug_loc_data.GetULEB128(offset_ptr);
2946 high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
2947 return true;
2948 }
Pavel Labath6b91e402016-10-31 11:53:13 +00002949 case DW_LLE_startx_length: {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002950 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
2951 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
George Rimard8719642018-10-25 09:22:26 +00002952 uint64_t length = (format == LocLists)
2953 ? debug_loc_data.GetULEB128(offset_ptr)
2954 : debug_loc_data.GetU32(offset_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002955 high_pc = low_pc + length;
2956 return true;
2957 }
George Rimare4dee262018-10-23 09:46:15 +00002958 case DW_LLE_start_length: {
2959 low_pc = debug_loc_data.GetAddress(offset_ptr);
2960 high_pc = low_pc + debug_loc_data.GetULEB128(offset_ptr);
2961 return true;
2962 }
2963 case DW_LLE_start_end: {
2964 low_pc = debug_loc_data.GetAddress(offset_ptr);
2965 high_pc = debug_loc_data.GetAddress(offset_ptr);
2966 return true;
2967 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002968 default:
2969 // Not supported entry type
George Rimare4dee262018-10-23 09:46:15 +00002970 lldbassert(false && "Not supported location list type");
Kate Stoneb9c1b512016-09-06 20:57:50 +00002971 return false;
2972 }
2973 }
2974 assert(false && "Not supported location list type");
2975 return false;
2976}
2977
2978static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
2979 lldb::offset_t *offset_ptr, int address_size,
2980 int dwarf_ref_size) {
2981 uint8_t opcode = data.GetU8(offset_ptr);
2982 DRC_class opcode_class;
2983 uint64_t uint;
2984 int64_t sint;
2985
2986 int size;
2987
2988 opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
2989
2990 s.Printf("%s ", DW_OP_value_to_name(opcode));
2991
2992 /* Does this take zero parameters? If so we can shortcut this function. */
2993 if (opcode_class == DRC_ZEROOPERANDS)
2994 return true;
2995
2996 if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
2997 uint = data.GetULEB128(offset_ptr);
2998 sint = data.GetSLEB128(offset_ptr);
2999 s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3000 return true;
3001 }
3002 if (opcode_class != DRC_ONEOPERAND) {
3003 s.Printf("UNKNOWN OP %u", opcode);
3004 return false;
3005 }
3006
3007 switch (opcode) {
3008 case DW_OP_addr:
3009 size = address_size;
3010 break;
3011 case DW_OP_const1u:
3012 size = 1;
3013 break;
3014 case DW_OP_const1s:
3015 size = -1;
3016 break;
3017 case DW_OP_const2u:
3018 size = 2;
3019 break;
3020 case DW_OP_const2s:
3021 size = -2;
3022 break;
3023 case DW_OP_const4u:
3024 size = 4;
3025 break;
3026 case DW_OP_const4s:
3027 size = -4;
3028 break;
3029 case DW_OP_const8u:
3030 size = 8;
3031 break;
3032 case DW_OP_const8s:
3033 size = -8;
3034 break;
3035 case DW_OP_constu:
3036 size = 128;
3037 break;
3038 case DW_OP_consts:
3039 size = -128;
3040 break;
3041 case DW_OP_fbreg:
3042 size = -128;
3043 break;
3044 case DW_OP_breg0:
3045 case DW_OP_breg1:
3046 case DW_OP_breg2:
3047 case DW_OP_breg3:
3048 case DW_OP_breg4:
3049 case DW_OP_breg5:
3050 case DW_OP_breg6:
3051 case DW_OP_breg7:
3052 case DW_OP_breg8:
3053 case DW_OP_breg9:
3054 case DW_OP_breg10:
3055 case DW_OP_breg11:
3056 case DW_OP_breg12:
3057 case DW_OP_breg13:
3058 case DW_OP_breg14:
3059 case DW_OP_breg15:
3060 case DW_OP_breg16:
3061 case DW_OP_breg17:
3062 case DW_OP_breg18:
3063 case DW_OP_breg19:
3064 case DW_OP_breg20:
3065 case DW_OP_breg21:
3066 case DW_OP_breg22:
3067 case DW_OP_breg23:
3068 case DW_OP_breg24:
3069 case DW_OP_breg25:
3070 case DW_OP_breg26:
3071 case DW_OP_breg27:
3072 case DW_OP_breg28:
3073 case DW_OP_breg29:
3074 case DW_OP_breg30:
3075 case DW_OP_breg31:
3076 size = -128;
3077 break;
3078 case DW_OP_pick:
3079 case DW_OP_deref_size:
3080 case DW_OP_xderef_size:
3081 size = 1;
3082 break;
3083 case DW_OP_skip:
3084 case DW_OP_bra:
3085 size = -2;
3086 break;
3087 case DW_OP_call2:
3088 size = 2;
3089 break;
3090 case DW_OP_call4:
3091 size = 4;
3092 break;
3093 case DW_OP_call_ref:
3094 size = dwarf_ref_size;
3095 break;
Ali Tamurd0156252019-03-07 19:41:08 +00003096 case DW_OP_addrx:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003097 case DW_OP_piece:
3098 case DW_OP_plus_uconst:
3099 case DW_OP_regx:
3100 case DW_OP_GNU_addr_index:
3101 case DW_OP_GNU_const_index:
3102 size = 128;
3103 break;
3104 default:
3105 s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
Aleksandr Urakov758657e2019-02-01 10:01:18 +00003106 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003107 }
3108
3109 switch (size) {
3110 case -1:
3111 sint = (int8_t)data.GetU8(offset_ptr);
3112 s.Printf("%+" PRIi64, sint);
3113 break;
3114 case -2:
3115 sint = (int16_t)data.GetU16(offset_ptr);
3116 s.Printf("%+" PRIi64, sint);
3117 break;
3118 case -4:
3119 sint = (int32_t)data.GetU32(offset_ptr);
3120 s.Printf("%+" PRIi64, sint);
3121 break;
3122 case -8:
3123 sint = (int64_t)data.GetU64(offset_ptr);
3124 s.Printf("%+" PRIi64, sint);
3125 break;
3126 case -128:
3127 sint = data.GetSLEB128(offset_ptr);
3128 s.Printf("%+" PRIi64, sint);
3129 break;
3130 case 1:
3131 uint = data.GetU8(offset_ptr);
3132 s.Printf("0x%2.2" PRIx64, uint);
3133 break;
3134 case 2:
3135 uint = data.GetU16(offset_ptr);
3136 s.Printf("0x%4.4" PRIx64, uint);
3137 break;
3138 case 4:
3139 uint = data.GetU32(offset_ptr);
3140 s.Printf("0x%8.8" PRIx64, uint);
3141 break;
3142 case 8:
3143 uint = data.GetU64(offset_ptr);
3144 s.Printf("0x%16.16" PRIx64, uint);
3145 break;
3146 case 128:
3147 uint = data.GetULEB128(offset_ptr);
3148 s.Printf("0x%" PRIx64, uint);
3149 break;
3150 }
3151
Aleksandr Urakov758657e2019-02-01 10:01:18 +00003152 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003153}
3154
3155bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
3156 int address_size, int dwarf_ref_size,
3157 bool location_expression) {
3158 int op_count = 0;
3159 lldb::offset_t offset = 0;
3160 while (data.ValidOffset(offset)) {
3161 if (location_expression && op_count > 0)
3162 return false;
3163 if (op_count > 0)
3164 s.PutCString(", ");
3165 if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
3166 return false;
3167 op_count++;
3168 }
3169
3170 return true;
3171}
3172
3173void DWARFExpression::PrintDWARFLocationList(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003174 Stream &s, const DWARFUnit *cu, const DataExtractor &debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003175 lldb::offset_t offset) {
3176 uint64_t start_addr, end_addr;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003177 uint32_t addr_size = DWARFUnit::GetAddressByteSize(cu);
3178 s.SetAddressByteSize(DWARFUnit::GetAddressByteSize(cu));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003179 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3180 while (debug_loc_data.ValidOffset(offset)) {
3181 start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3182 end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3183
3184 if (start_addr == 0 && end_addr == 0)
3185 break;
3186
3187 s.PutCString("\n ");
3188 s.Indent();
3189 if (cu)
3190 s.AddressRange(start_addr + base_addr, end_addr + base_addr,
3191 cu->GetAddressByteSize(), NULL, ": ");
3192 uint32_t loc_length = debug_loc_data.GetU16(&offset);
3193
3194 DataExtractor locationData(debug_loc_data, offset, loc_length);
3195 PrintDWARFExpression(s, locationData, addr_size, 4, false);
3196 offset += loc_length;
3197 }
3198}
3199
3200bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
3201 lldb::offset_t &op_offset,
3202 lldb::offset_t &end_offset) {
3203 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
3204 if (!sc.function) {
3205 return false;
3206 }
3207
3208 addr_t loclist_base_file_addr =
3209 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
3210 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
3211 return false;
3212 }
3213
3214 addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
3215 lldb::offset_t opcodes_offset, opcodes_length;
3216 if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
3217 opcodes_length)) {
3218 return false;
3219 }
3220
3221 if (opcodes_length == 0) {
3222 return false;
3223 }
3224
3225 op_offset = opcodes_offset;
3226 end_offset = opcodes_offset + opcodes_length;
3227 return true;
3228}
3229
Sean Callanan807ee2f2016-09-13 21:18:27 +00003230bool DWARFExpression::MatchesOperand(StackFrame &frame,
3231 const Instruction::Operand &operand) {
3232 using namespace OperandMatchers;
3233
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234 lldb::offset_t op_offset;
3235 lldb::offset_t end_offset;
3236 if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
3237 return false;
3238 }
3239
3240 if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
3241 return false;
3242 }
3243
3244 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
3245 if (!reg_ctx_sp) {
3246 return false;
3247 }
3248
3249 DataExtractor opcodes = m_data;
3250 uint8_t opcode = opcodes.GetU8(&op_offset);
3251
Sean Callanan807ee2f2016-09-13 21:18:27 +00003252 if (opcode == DW_OP_fbreg) {
3253 int64_t offset = opcodes.GetSLEB128(&op_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003254
Sean Callanan807ee2f2016-09-13 21:18:27 +00003255 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
3256 if (!fb_expr) {
3257 return false;
3258 }
3259
Sean Callananaa4b44c2016-09-14 20:58:31 +00003260 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3261 return fb_expr->MatchesOperand(frame, child);
3262 };
Sean Callanan807ee2f2016-09-13 21:18:27 +00003263
3264 if (!offset &&
3265 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3266 recurse)(operand)) {
3267 return true;
3268 }
3269
3270 return MatchUnaryOp(
3271 MatchOpType(Instruction::Operand::Type::Dereference),
3272 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3273 MatchImmOp(offset), recurse))(operand);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003274 }
3275
Sean Callanan807ee2f2016-09-13 21:18:27 +00003276 bool dereference = false;
3277 const RegisterInfo *reg = nullptr;
3278 int64_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003279
Sean Callanan807ee2f2016-09-13 21:18:27 +00003280 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3281 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3282 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3283 offset = opcodes.GetSLEB128(&op_offset);
3284 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3285 } else if (opcode == DW_OP_regx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003286 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
Sean Callanan807ee2f2016-09-13 21:18:27 +00003287 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3288 } else if (opcode == DW_OP_bregx) {
3289 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3290 offset = opcodes.GetSLEB128(&op_offset);
3291 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3292 } else {
3293 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003295
Sean Callanan807ee2f2016-09-13 21:18:27 +00003296 if (!reg) {
3297 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298 }
Sean Callanan807ee2f2016-09-13 21:18:27 +00003299
3300 if (dereference) {
3301 if (!offset &&
3302 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3303 MatchRegOp(*reg))(operand)) {
3304 return true;
3305 }
3306
3307 return MatchUnaryOp(
3308 MatchOpType(Instruction::Operand::Type::Dereference),
3309 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3310 MatchRegOp(*reg),
3311 MatchImmOp(offset)))(operand);
3312 } else {
3313 return MatchRegOp(*reg)(operand);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003314 }
3315}
Sean Callanan807ee2f2016-09-13 21:18:27 +00003316