/src/elfutils/libdw/dwarf_getlocation.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Return location expression list. |
2 | | Copyright (C) 2000-2010, 2013-2015, 2017, 2018 Red Hat, Inc. |
3 | | This file is part of elfutils. |
4 | | |
5 | | This file is free software; you can redistribute it and/or modify |
6 | | it under the terms of either |
7 | | |
8 | | * the GNU Lesser General Public License as published by the Free |
9 | | Software Foundation; either version 3 of the License, or (at |
10 | | your option) any later version |
11 | | |
12 | | or |
13 | | |
14 | | * the GNU General Public License as published by the Free |
15 | | Software Foundation; either version 2 of the License, or (at |
16 | | your option) any later version |
17 | | |
18 | | or both in parallel, as here. |
19 | | |
20 | | elfutils is distributed in the hope that it will be useful, but |
21 | | WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
23 | | General Public License for more details. |
24 | | |
25 | | You should have received copies of the GNU General Public License and |
26 | | the GNU Lesser General Public License along with this program. If |
27 | | not, see <http://www.gnu.org/licenses/>. */ |
28 | | |
29 | | #ifdef HAVE_CONFIG_H |
30 | | # include <config.h> |
31 | | #endif |
32 | | |
33 | | #include <dwarf.h> |
34 | | #include <stdlib.h> |
35 | | #include <assert.h> |
36 | | |
37 | | #include <libdwP.h> |
38 | | #include "eu-search.h" |
39 | | |
40 | | |
41 | | static bool |
42 | | attr_ok (Dwarf_Attribute *attr) |
43 | 0 | { |
44 | 0 | if (attr == NULL) |
45 | 0 | return false; |
46 | | |
47 | | /* If it is an exprloc, it is obviously OK. */ |
48 | 0 | if (dwarf_whatform (attr) == DW_FORM_exprloc) |
49 | 0 | return true; |
50 | | |
51 | 0 | if (attr->cu->version >= 4) |
52 | 0 | { |
53 | | /* Must be an exprloc (or constant), just not any block form. */ |
54 | 0 | switch (dwarf_whatform (attr)) |
55 | 0 | { |
56 | 0 | case DW_FORM_block: |
57 | 0 | case DW_FORM_block1: |
58 | 0 | case DW_FORM_block2: |
59 | 0 | case DW_FORM_block4: |
60 | 0 | __libdw_seterrno (DWARF_E_NO_LOC_VALUE); |
61 | 0 | return false; |
62 | 0 | default: |
63 | 0 | break; |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | /* Otherwise must be one of the attributes listed below. Older |
68 | | DWARF versions might have encoded the exprloc as block, and we |
69 | | cannot easily distinguish attributes in the loclist class because |
70 | | the same forms are used for different classes. */ |
71 | 0 | switch (attr->code) |
72 | 0 | { |
73 | 0 | case DW_AT_location: |
74 | 0 | case DW_AT_byte_size: |
75 | 0 | case DW_AT_bit_offset: |
76 | 0 | case DW_AT_bit_size: |
77 | 0 | case DW_AT_lower_bound: |
78 | 0 | case DW_AT_bit_stride: |
79 | 0 | case DW_AT_upper_bound: |
80 | 0 | case DW_AT_count: |
81 | 0 | case DW_AT_allocated: |
82 | 0 | case DW_AT_associated: |
83 | 0 | case DW_AT_data_location: |
84 | 0 | case DW_AT_byte_stride: |
85 | 0 | case DW_AT_rank: |
86 | 0 | case DW_AT_call_value: |
87 | 0 | case DW_AT_call_target: |
88 | 0 | case DW_AT_call_target_clobbered: |
89 | 0 | case DW_AT_call_data_location: |
90 | 0 | case DW_AT_call_data_value: |
91 | 0 | case DW_AT_data_member_location: |
92 | 0 | case DW_AT_vtable_elem_location: |
93 | 0 | case DW_AT_string_length: |
94 | 0 | case DW_AT_use_location: |
95 | 0 | case DW_AT_frame_base: |
96 | 0 | case DW_AT_return_addr: |
97 | 0 | case DW_AT_static_link: |
98 | 0 | case DW_AT_segment: |
99 | 0 | case DW_AT_GNU_call_site_value: |
100 | 0 | case DW_AT_GNU_call_site_data_value: |
101 | 0 | case DW_AT_GNU_call_site_target: |
102 | 0 | case DW_AT_GNU_call_site_target_clobbered: |
103 | 0 | break; |
104 | | |
105 | 0 | default: |
106 | 0 | __libdw_seterrno (DWARF_E_NO_LOC_VALUE); |
107 | 0 | return false; |
108 | 0 | } |
109 | | |
110 | 0 | return true; |
111 | 0 | } |
112 | | |
113 | | |
114 | | struct loclist |
115 | | { |
116 | | uint8_t atom; |
117 | | Dwarf_Word number; |
118 | | Dwarf_Word number2; |
119 | | Dwarf_Word offset; |
120 | | struct loclist *next; |
121 | | }; |
122 | | |
123 | | |
124 | | static int |
125 | | loc_compare (const void *p1, const void *p2) |
126 | 0 | { |
127 | 0 | const struct loc_s *l1 = (const struct loc_s *) p1; |
128 | 0 | const struct loc_s *l2 = (const struct loc_s *) p2; |
129 | |
|
130 | 0 | if ((uintptr_t) l1->addr < (uintptr_t) l2->addr) |
131 | 0 | return -1; |
132 | 0 | if ((uintptr_t) l1->addr > (uintptr_t) l2->addr) |
133 | 0 | return 1; |
134 | | |
135 | 0 | return 0; |
136 | 0 | } |
137 | | |
138 | | /* For each DW_OP_implicit_value, we store a special entry in the cache. |
139 | | This points us directly to the block data for later fetching. |
140 | | Returns zero on success, -1 on bad DWARF or 1 if eu_tsearch failed. */ |
141 | | static int |
142 | | store_implicit_value (Dwarf *dbg, search_tree *cache, Dwarf_Op *op) |
143 | 0 | { |
144 | 0 | if (dbg == NULL) |
145 | 0 | return -1; |
146 | 0 | struct loc_block_s *block = libdw_alloc (dbg, struct loc_block_s, |
147 | 0 | sizeof (struct loc_block_s), 1); |
148 | 0 | const unsigned char *data = (const unsigned char *) (uintptr_t) op->number2; |
149 | | /* Skip the block length. */ |
150 | 0 | Dwarf_Word length; |
151 | 0 | get_uleb128_unchecked (length, data); |
152 | 0 | if (length != op->number) |
153 | 0 | return -1; |
154 | 0 | block->addr = op; |
155 | 0 | block->data = (unsigned char *) data; |
156 | 0 | block->length = op->number; |
157 | 0 | if (unlikely (eu_tsearch_nolock (block, cache, loc_compare) == NULL)) |
158 | 0 | return 1; |
159 | 0 | return 0; |
160 | 0 | } |
161 | | |
162 | | int |
163 | | dwarf_getlocation_implicit_value (Dwarf_Attribute *attr, const Dwarf_Op *op, |
164 | | Dwarf_Block *return_block) |
165 | 0 | { |
166 | 0 | if (attr == NULL) |
167 | 0 | return -1; |
168 | | |
169 | 0 | struct loc_block_s fake = { .addr = (void *) op }; |
170 | 0 | struct loc_block_s **found = eu_tfind (&fake, &attr->cu->locs_tree, |
171 | 0 | loc_compare); |
172 | 0 | if (unlikely (found == NULL)) |
173 | 0 | { |
174 | 0 | __libdw_seterrno (DWARF_E_NO_BLOCK); |
175 | 0 | return -1; |
176 | 0 | } |
177 | | |
178 | 0 | return_block->length = (*found)->length; |
179 | 0 | return_block->data = (*found)->data; |
180 | 0 | return 0; |
181 | 0 | } |
182 | | |
183 | | /* If the given attribute is DW_AT_data_member_location and it has constant |
184 | | form then create a fake location using DW_OP_plus_uconst and the offset |
185 | | value. On success returns zero and fills in llbuf (when not NULL) and |
186 | | sets listlen to 1. Returns 1 when this isn't a DW_AT_data_member_location |
187 | | offset. Returns -1 and sets dwarf_errno on failure (bad DWARF data). */ |
188 | | static int |
189 | | is_constant_offset (Dwarf_Attribute *attr, |
190 | | Dwarf_Op **llbuf, size_t *listlen) |
191 | 0 | { |
192 | 0 | if (attr->code != DW_AT_data_member_location) |
193 | 0 | return 1; |
194 | | |
195 | 0 | switch (attr->form) |
196 | 0 | { |
197 | | /* Punt for any non-constant form. */ |
198 | 0 | default: |
199 | 0 | return 1; |
200 | | |
201 | | /* Note, we don't regard DW_FORM_data16 as a constant form, |
202 | | even though technically it is according to the standard. */ |
203 | 0 | case DW_FORM_data1: |
204 | 0 | case DW_FORM_data2: |
205 | 0 | case DW_FORM_data4: |
206 | 0 | case DW_FORM_data8: |
207 | 0 | case DW_FORM_sdata: |
208 | 0 | case DW_FORM_udata: |
209 | 0 | case DW_FORM_implicit_const: |
210 | 0 | break; |
211 | 0 | } |
212 | | |
213 | | /* Check whether we already cached this location. */ |
214 | 0 | mutex_lock (attr->cu->intern_lock); |
215 | 0 | struct loc_s fake = { .addr = attr->valp }; |
216 | 0 | struct loc_s **found = eu_tfind_nolock (&fake, &attr->cu->locs_tree, loc_compare); |
217 | |
|
218 | 0 | if (found == NULL) |
219 | 0 | { |
220 | 0 | Dwarf_Word offset; |
221 | 0 | if (INTUSE(dwarf_formudata) (attr, &offset) != 0) |
222 | 0 | { |
223 | 0 | mutex_unlock (attr->cu->intern_lock); |
224 | 0 | return -1; |
225 | 0 | } |
226 | | |
227 | 0 | Dwarf_Op *result = libdw_alloc (attr->cu->dbg, |
228 | 0 | Dwarf_Op, sizeof (Dwarf_Op), 1); |
229 | |
|
230 | 0 | result->atom = DW_OP_plus_uconst; |
231 | 0 | result->number = offset; |
232 | 0 | result->number2 = 0; |
233 | 0 | result->offset = 0; |
234 | | |
235 | | /* Insert a record in the search tree so we can find it again later. */ |
236 | 0 | struct loc_s *newp = libdw_alloc (attr->cu->dbg, |
237 | 0 | struct loc_s, sizeof (struct loc_s), |
238 | 0 | 1); |
239 | 0 | newp->addr = attr->valp; |
240 | 0 | newp->loc = result; |
241 | 0 | newp->nloc = 1; |
242 | |
|
243 | 0 | found = eu_tsearch_nolock (newp, &attr->cu->locs_tree, loc_compare); |
244 | 0 | } |
245 | | |
246 | 0 | mutex_unlock (attr->cu->intern_lock); |
247 | 0 | assert ((*found)->nloc == 1); |
248 | | |
249 | 0 | if (llbuf != NULL) |
250 | 0 | { |
251 | 0 | *llbuf = (*found)->loc; |
252 | 0 | *listlen = 1; |
253 | 0 | } |
254 | |
|
255 | 0 | return 0; |
256 | 0 | } |
257 | | |
258 | | int |
259 | | internal_function |
260 | | __libdw_intern_expression (Dwarf *dbg, bool other_byte_order, |
261 | | unsigned int address_size, unsigned int ref_size, |
262 | | search_tree *cache, const Dwarf_Block *block, |
263 | | bool cfap, bool valuep, |
264 | | Dwarf_Op **llbuf, size_t *listlen, int sec_index) |
265 | 0 | { |
266 | | /* Empty location expressions don't have any ops to intern. */ |
267 | 0 | if (block->length == 0) |
268 | 0 | { |
269 | 0 | *listlen = 0; |
270 | 0 | return 0; |
271 | 0 | } |
272 | | |
273 | | /* Check whether we already looked at this list. */ |
274 | 0 | struct loc_s fake = { .addr = block->data }; |
275 | 0 | struct loc_s **found = eu_tfind_nolock (&fake, cache, loc_compare); |
276 | 0 | if (found != NULL) |
277 | 0 | { |
278 | | /* We already saw it. */ |
279 | 0 | *llbuf = (*found)->loc; |
280 | 0 | *listlen = (*found)->nloc; |
281 | |
|
282 | 0 | if (valuep) |
283 | 0 | { |
284 | 0 | assert (*listlen > 1); |
285 | 0 | assert ((*llbuf)[*listlen - 1].atom == DW_OP_stack_value); |
286 | 0 | } |
287 | | |
288 | 0 | return 0; |
289 | 0 | } |
290 | | |
291 | 0 | const unsigned char *data = block->data; |
292 | 0 | const unsigned char *const end_data = data + block->length; |
293 | |
|
294 | 0 | const struct { bool other_byte_order; } bo = { other_byte_order }; |
295 | |
|
296 | 0 | struct loclist *loclist = NULL; |
297 | 0 | unsigned int n = 0; |
298 | | |
299 | | /* Stack allocate at most this many locs. */ |
300 | 0 | #define MAX_STACK_LOCS 256 |
301 | 0 | struct loclist stack_locs[MAX_STACK_LOCS]; |
302 | 0 | #define NEW_LOC() ({ struct loclist *ll; \ |
303 | 0 | ll = (likely (n < MAX_STACK_LOCS) \ |
304 | 0 | ? &stack_locs[n] \ |
305 | 0 | : malloc (sizeof (struct loclist))); \ |
306 | 0 | if (unlikely (ll == NULL)) \ |
307 | 0 | goto nomem; \ |
308 | 0 | n++; \ |
309 | 0 | ll->next = loclist; \ |
310 | 0 | loclist = ll; \ |
311 | 0 | ll; }) |
312 | |
|
313 | 0 | if (cfap) |
314 | 0 | { |
315 | | /* Synthesize the operation to push the CFA before the expression. */ |
316 | 0 | struct loclist *newloc = NEW_LOC (); |
317 | 0 | newloc->atom = DW_OP_call_frame_cfa; |
318 | 0 | newloc->number = 0; |
319 | 0 | newloc->number2 = 0; |
320 | 0 | newloc->offset = -1; |
321 | 0 | } |
322 | | |
323 | | /* Decode the opcodes. It is possible in some situations to have a |
324 | | block of size zero. */ |
325 | 0 | while (data < end_data) |
326 | 0 | { |
327 | 0 | struct loclist *newloc; |
328 | 0 | newloc = NEW_LOC (); |
329 | 0 | newloc->number = 0; |
330 | 0 | newloc->number2 = 0; |
331 | 0 | newloc->offset = data - block->data; |
332 | |
|
333 | 0 | switch ((newloc->atom = *data++)) |
334 | 0 | { |
335 | 0 | case DW_OP_addr: |
336 | | /* Address, depends on address size of CU. */ |
337 | 0 | if (dbg == NULL) |
338 | 0 | { |
339 | | // XXX relocation? |
340 | 0 | if (address_size == 4) |
341 | 0 | { |
342 | 0 | if (unlikely (data + 4 > end_data)) |
343 | 0 | goto invalid; |
344 | 0 | else |
345 | 0 | newloc->number = read_4ubyte_unaligned_inc (&bo, data); |
346 | 0 | } |
347 | 0 | else |
348 | 0 | { |
349 | 0 | if (unlikely (data + 8 > end_data)) |
350 | 0 | goto invalid; |
351 | 0 | else |
352 | 0 | newloc->number = read_8ubyte_unaligned_inc (&bo, data); |
353 | 0 | } |
354 | 0 | } |
355 | 0 | else if (__libdw_read_address_inc (dbg, sec_index, &data, |
356 | 0 | address_size, &newloc->number)) |
357 | 0 | goto invalid; |
358 | 0 | break; |
359 | | |
360 | 0 | case DW_OP_call_ref: |
361 | 0 | case DW_OP_GNU_variable_value: |
362 | | /* DW_FORM_ref_addr, depends on offset size of CU. */ |
363 | 0 | if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data, |
364 | 0 | ref_size, |
365 | 0 | &newloc->number, |
366 | 0 | IDX_debug_info, 0)) |
367 | 0 | goto invalid; |
368 | 0 | break; |
369 | | |
370 | 0 | case DW_OP_deref: |
371 | 0 | case DW_OP_dup: |
372 | 0 | case DW_OP_drop: |
373 | 0 | case DW_OP_over: |
374 | 0 | case DW_OP_swap: |
375 | 0 | case DW_OP_rot: |
376 | 0 | case DW_OP_xderef: |
377 | 0 | case DW_OP_abs: |
378 | 0 | case DW_OP_and: |
379 | 0 | case DW_OP_div: |
380 | 0 | case DW_OP_minus: |
381 | 0 | case DW_OP_mod: |
382 | 0 | case DW_OP_mul: |
383 | 0 | case DW_OP_neg: |
384 | 0 | case DW_OP_not: |
385 | 0 | case DW_OP_or: |
386 | 0 | case DW_OP_plus: |
387 | 0 | case DW_OP_shl: |
388 | 0 | case DW_OP_shr: |
389 | 0 | case DW_OP_shra: |
390 | 0 | case DW_OP_xor: |
391 | 0 | case DW_OP_eq: |
392 | 0 | case DW_OP_ge: |
393 | 0 | case DW_OP_gt: |
394 | 0 | case DW_OP_le: |
395 | 0 | case DW_OP_lt: |
396 | 0 | case DW_OP_ne: |
397 | 0 | case DW_OP_lit0 ... DW_OP_lit31: |
398 | 0 | case DW_OP_reg0 ... DW_OP_reg31: |
399 | 0 | case DW_OP_nop: |
400 | 0 | case DW_OP_push_object_address: |
401 | 0 | case DW_OP_call_frame_cfa: |
402 | 0 | case DW_OP_form_tls_address: |
403 | 0 | case DW_OP_GNU_push_tls_address: |
404 | 0 | case DW_OP_stack_value: |
405 | 0 | case DW_OP_GNU_uninit: |
406 | | /* No operand. */ |
407 | 0 | break; |
408 | | |
409 | 0 | case DW_OP_const1u: |
410 | 0 | case DW_OP_pick: |
411 | 0 | case DW_OP_deref_size: |
412 | 0 | case DW_OP_xderef_size: |
413 | 0 | if (unlikely (data >= end_data)) |
414 | 0 | { |
415 | 0 | invalid: |
416 | 0 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
417 | 0 | returnmem: |
418 | | /* Free any dynamically allocated loclists, if any. */ |
419 | 0 | while (n > MAX_STACK_LOCS) |
420 | 0 | { |
421 | 0 | struct loclist *loc = loclist; |
422 | 0 | loclist = loc->next; |
423 | 0 | free (loc); |
424 | 0 | n--; |
425 | 0 | } |
426 | 0 | return -1; |
427 | 0 | } |
428 | | |
429 | 0 | newloc->number = *data++; |
430 | 0 | break; |
431 | | |
432 | 0 | case DW_OP_const1s: |
433 | 0 | if (unlikely (data >= end_data)) |
434 | 0 | goto invalid; |
435 | | |
436 | 0 | newloc->number = *((int8_t *) data); |
437 | 0 | ++data; |
438 | 0 | break; |
439 | | |
440 | 0 | case DW_OP_const2u: |
441 | 0 | if (unlikely (data + 2 > end_data)) |
442 | 0 | goto invalid; |
443 | | |
444 | 0 | newloc->number = read_2ubyte_unaligned_inc (&bo, data); |
445 | 0 | break; |
446 | | |
447 | 0 | case DW_OP_const2s: |
448 | 0 | case DW_OP_skip: |
449 | 0 | case DW_OP_bra: |
450 | 0 | case DW_OP_call2: |
451 | 0 | if (unlikely (data + 2 > end_data)) |
452 | 0 | goto invalid; |
453 | | |
454 | 0 | newloc->number = read_2sbyte_unaligned_inc (&bo, data); |
455 | 0 | break; |
456 | | |
457 | 0 | case DW_OP_const4u: |
458 | 0 | if (unlikely (data + 4 > end_data)) |
459 | 0 | goto invalid; |
460 | | |
461 | 0 | newloc->number = read_4ubyte_unaligned_inc (&bo, data); |
462 | 0 | break; |
463 | | |
464 | 0 | case DW_OP_const4s: |
465 | 0 | case DW_OP_call4: |
466 | 0 | case DW_OP_GNU_parameter_ref: |
467 | 0 | if (unlikely (data + 4 > end_data)) |
468 | 0 | goto invalid; |
469 | | |
470 | 0 | newloc->number = read_4sbyte_unaligned_inc (&bo, data); |
471 | 0 | break; |
472 | | |
473 | 0 | case DW_OP_const8u: |
474 | 0 | if (unlikely (data + 8 > end_data)) |
475 | 0 | goto invalid; |
476 | | |
477 | 0 | newloc->number = read_8ubyte_unaligned_inc (&bo, data); |
478 | 0 | break; |
479 | | |
480 | 0 | case DW_OP_const8s: |
481 | 0 | if (unlikely (data + 8 > end_data)) |
482 | 0 | goto invalid; |
483 | | |
484 | 0 | newloc->number = read_8sbyte_unaligned_inc (&bo, data); |
485 | 0 | break; |
486 | | |
487 | 0 | case DW_OP_constu: |
488 | 0 | case DW_OP_plus_uconst: |
489 | 0 | case DW_OP_regx: |
490 | 0 | case DW_OP_piece: |
491 | 0 | case DW_OP_convert: |
492 | 0 | case DW_OP_GNU_convert: |
493 | 0 | case DW_OP_reinterpret: |
494 | 0 | case DW_OP_GNU_reinterpret: |
495 | 0 | case DW_OP_addrx: |
496 | 0 | case DW_OP_GNU_addr_index: |
497 | 0 | case DW_OP_constx: |
498 | 0 | case DW_OP_GNU_const_index: |
499 | 0 | get_uleb128 (newloc->number, data, end_data); |
500 | 0 | break; |
501 | | |
502 | 0 | case DW_OP_consts: |
503 | 0 | case DW_OP_breg0 ... DW_OP_breg31: |
504 | 0 | case DW_OP_fbreg: |
505 | 0 | get_sleb128 (newloc->number, data, end_data); |
506 | 0 | break; |
507 | | |
508 | 0 | case DW_OP_bregx: |
509 | 0 | get_uleb128 (newloc->number, data, end_data); |
510 | 0 | if (unlikely (data >= end_data)) |
511 | 0 | goto invalid; |
512 | 0 | get_sleb128 (newloc->number2, data, end_data); |
513 | 0 | break; |
514 | | |
515 | 0 | case DW_OP_bit_piece: |
516 | 0 | case DW_OP_regval_type: |
517 | 0 | case DW_OP_GNU_regval_type: |
518 | 0 | get_uleb128 (newloc->number, data, end_data); |
519 | 0 | if (unlikely (data >= end_data)) |
520 | 0 | goto invalid; |
521 | 0 | get_uleb128 (newloc->number2, data, end_data); |
522 | 0 | break; |
523 | | |
524 | 0 | case DW_OP_implicit_value: |
525 | 0 | case DW_OP_entry_value: |
526 | 0 | case DW_OP_GNU_entry_value: |
527 | | /* This cannot be used in a CFI expression. */ |
528 | 0 | if (unlikely (dbg == NULL)) |
529 | 0 | goto invalid; |
530 | | |
531 | | /* start of block inc. len. */ |
532 | 0 | newloc->number2 = (Dwarf_Word) (uintptr_t) data; |
533 | 0 | get_uleb128 (newloc->number, data, end_data); /* Block length. */ |
534 | 0 | if (unlikely ((Dwarf_Word) (end_data - data) < newloc->number)) |
535 | 0 | goto invalid; |
536 | 0 | data += newloc->number; /* Skip the block. */ |
537 | 0 | break; |
538 | | |
539 | 0 | case DW_OP_implicit_pointer: |
540 | 0 | case DW_OP_GNU_implicit_pointer: |
541 | | /* DW_FORM_ref_addr, depends on offset size of CU. */ |
542 | 0 | if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data, |
543 | 0 | ref_size, |
544 | 0 | &newloc->number, |
545 | 0 | IDX_debug_info, 0)) |
546 | 0 | goto invalid; |
547 | 0 | if (unlikely (data >= end_data)) |
548 | 0 | goto invalid; |
549 | 0 | get_uleb128 (newloc->number2, data, end_data); /* Byte offset. */ |
550 | 0 | break; |
551 | | |
552 | 0 | case DW_OP_deref_type: |
553 | 0 | case DW_OP_GNU_deref_type: |
554 | 0 | case DW_OP_xderef_type: |
555 | 0 | if (unlikely (data + 1 >= end_data)) |
556 | 0 | goto invalid; |
557 | 0 | newloc->number = *data++; |
558 | 0 | get_uleb128 (newloc->number2, data, end_data); |
559 | 0 | break; |
560 | | |
561 | 0 | case DW_OP_const_type: |
562 | 0 | case DW_OP_GNU_const_type: |
563 | 0 | { |
564 | 0 | size_t size; |
565 | 0 | get_uleb128 (newloc->number, data, end_data); |
566 | 0 | if (unlikely (data >= end_data)) |
567 | 0 | goto invalid; |
568 | | |
569 | | /* start of block inc. len. */ |
570 | 0 | newloc->number2 = (Dwarf_Word) (uintptr_t) data; |
571 | 0 | size = *data++; |
572 | 0 | if (unlikely ((Dwarf_Word) (end_data - data) < size)) |
573 | 0 | goto invalid; |
574 | 0 | data += size; /* Skip the block. */ |
575 | 0 | } |
576 | 0 | break; |
577 | | |
578 | 0 | default: |
579 | 0 | goto invalid; |
580 | 0 | } |
581 | 0 | } |
582 | | |
583 | 0 | if (unlikely (n == 0)) |
584 | 0 | { |
585 | | /* This is not allowed. |
586 | | It would mean an empty location expression, which we handled |
587 | | already as a special case above. */ |
588 | 0 | goto invalid; |
589 | 0 | } |
590 | | |
591 | 0 | if (valuep) |
592 | 0 | { |
593 | 0 | struct loclist *newloc = NEW_LOC (); |
594 | 0 | newloc->atom = DW_OP_stack_value; |
595 | 0 | newloc->number = 0; |
596 | 0 | newloc->number2 = 0; |
597 | 0 | newloc->offset = data - block->data; |
598 | 0 | } |
599 | | |
600 | | /* Allocate the array. */ |
601 | 0 | Dwarf_Op *result; |
602 | 0 | if (dbg != NULL) |
603 | 0 | result = libdw_alloc (dbg, Dwarf_Op, sizeof (Dwarf_Op), n); |
604 | 0 | else |
605 | 0 | { |
606 | 0 | result = malloc (sizeof *result * n); |
607 | 0 | if (result == NULL) |
608 | 0 | { |
609 | 0 | nomem: |
610 | 0 | __libdw_seterrno (DWARF_E_NOMEM); |
611 | 0 | goto returnmem; |
612 | 0 | } |
613 | 0 | } |
614 | | |
615 | | /* Store the result. */ |
616 | 0 | *llbuf = result; |
617 | 0 | *listlen = n; |
618 | |
|
619 | 0 | do |
620 | 0 | { |
621 | | /* We populate the array from the back since the list is backwards. */ |
622 | 0 | --n; |
623 | 0 | result[n].atom = loclist->atom; |
624 | 0 | result[n].number = loclist->number; |
625 | 0 | result[n].number2 = loclist->number2; |
626 | 0 | result[n].offset = loclist->offset; |
627 | |
|
628 | 0 | if (result[n].atom == DW_OP_implicit_value) |
629 | 0 | { |
630 | 0 | int store = store_implicit_value (dbg, cache, &result[n]); |
631 | 0 | if (unlikely (store != 0)) |
632 | 0 | { |
633 | 0 | if (store < 0) |
634 | 0 | goto invalid; |
635 | 0 | else |
636 | 0 | goto nomem; |
637 | 0 | } |
638 | 0 | } |
639 | | |
640 | 0 | struct loclist *loc = loclist; |
641 | 0 | loclist = loclist->next; |
642 | 0 | if (unlikely (n + 1 > MAX_STACK_LOCS)) |
643 | 0 | free (loc); |
644 | 0 | } |
645 | 0 | while (n > 0); |
646 | | |
647 | | /* Insert a record in the search tree so that we can find it again later. */ |
648 | 0 | struct loc_s *newp; |
649 | 0 | if (dbg != NULL) |
650 | 0 | newp = libdw_alloc (dbg, struct loc_s, sizeof (struct loc_s), 1); |
651 | 0 | else |
652 | 0 | { |
653 | 0 | newp = malloc (sizeof *newp); |
654 | 0 | if (newp == NULL) |
655 | 0 | { |
656 | 0 | free (result); |
657 | 0 | goto nomem; |
658 | 0 | } |
659 | 0 | } |
660 | | |
661 | 0 | newp->addr = block->data; |
662 | 0 | newp->loc = result; |
663 | 0 | newp->nloc = *listlen; |
664 | 0 | eu_tsearch_nolock (newp, cache, loc_compare); |
665 | | |
666 | | /* We did it. */ |
667 | 0 | return 0; |
668 | 0 | } |
669 | | |
670 | | static int |
671 | | getlocation (struct Dwarf_CU *cu, const Dwarf_Block *block, |
672 | | Dwarf_Op **llbuf, size_t *listlen, int sec_index) |
673 | 0 | { |
674 | | /* Empty location expressions don't have any ops to intern. |
675 | | Note that synthetic empty_cu doesn't have an associated DWARF dbg. */ |
676 | 0 | if (block->length == 0) |
677 | 0 | { |
678 | 0 | *listlen = 0; |
679 | 0 | return 0; |
680 | 0 | } |
681 | | |
682 | 0 | mutex_lock (cu->intern_lock); |
683 | 0 | int res = __libdw_intern_expression (cu->dbg, cu->dbg->other_byte_order, |
684 | 0 | cu->address_size, (cu->version == 2 |
685 | 0 | ? cu->address_size |
686 | 0 | : cu->offset_size), |
687 | 0 | &cu->locs_tree, block, |
688 | 0 | false, false, |
689 | 0 | llbuf, listlen, sec_index); |
690 | 0 | mutex_unlock (cu->intern_lock); |
691 | |
|
692 | 0 | return res; |
693 | 0 | } |
694 | | |
695 | | int |
696 | | dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **llbuf, size_t *listlen) |
697 | 0 | { |
698 | 0 | if (! attr_ok (attr)) |
699 | 0 | return -1; |
700 | | |
701 | 0 | int result = is_constant_offset (attr, llbuf, listlen); |
702 | 0 | if (result != 1) |
703 | 0 | return result; /* Either success 0, or -1 to indicate error. */ |
704 | | |
705 | | /* If it has a block form, it's a single location expression. |
706 | | Except for DW_FORM_data16, which is a 128bit constant. */ |
707 | 0 | if (attr->form == DW_FORM_data16) |
708 | 0 | { |
709 | 0 | __libdw_seterrno (DWARF_E_NO_BLOCK); |
710 | 0 | return -1; |
711 | 0 | } |
712 | 0 | Dwarf_Block block; |
713 | 0 | if (INTUSE(dwarf_formblock) (attr, &block) != 0) |
714 | 0 | return -1; |
715 | | |
716 | 0 | return getlocation (attr->cu, &block, llbuf, listlen, cu_sec_idx (attr->cu)); |
717 | 0 | } |
718 | | |
719 | | Dwarf_Addr |
720 | | __libdw_cu_base_address (Dwarf_CU *cu) |
721 | 0 | { |
722 | 0 | if (cu->base_address == (Dwarf_Addr) -1) |
723 | 0 | { |
724 | 0 | Dwarf_Addr base; |
725 | | |
726 | | /* Fetch the CU's base address. */ |
727 | 0 | Dwarf_Die cudie = CUDIE (cu); |
728 | | |
729 | | /* Find the base address of the compilation unit. It will |
730 | | normally be specified by DW_AT_low_pc. In DWARF-3 draft 4, |
731 | | the base address could be overridden by DW_AT_entry_pc. It's |
732 | | been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc |
733 | | for compilation units with discontinuous ranges. */ |
734 | 0 | Dwarf_Attribute attr_mem; |
735 | 0 | if (INTUSE(dwarf_lowpc) (&cudie, &base) != 0 |
736 | 0 | && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie, |
737 | 0 | DW_AT_entry_pc, |
738 | 0 | &attr_mem), |
739 | 0 | &base) != 0) |
740 | 0 | { |
741 | | /* The compiler provided no base address when it should |
742 | | have. Buggy GCC does this when it used absolute |
743 | | addresses in the location list and no DW_AT_ranges. */ |
744 | 0 | base = 0; |
745 | 0 | } |
746 | 0 | cu->base_address = base; |
747 | 0 | } |
748 | |
|
749 | 0 | return cu->base_address; |
750 | 0 | } |
751 | | |
752 | | static int |
753 | | initial_offset (Dwarf_Attribute *attr, ptrdiff_t *offset) |
754 | 0 | { |
755 | 0 | size_t secidx = (attr->cu->version < 5 |
756 | 0 | ? IDX_debug_loc : IDX_debug_loclists); |
757 | |
|
758 | 0 | Dwarf_Word start_offset; |
759 | 0 | if (attr->form == DW_FORM_loclistx) |
760 | 0 | { |
761 | 0 | Dwarf_Word idx; |
762 | 0 | Dwarf_CU *cu = attr->cu; |
763 | 0 | const unsigned char *datap = attr->valp; |
764 | 0 | const unsigned char *endp = cu->endp; |
765 | 0 | if (datap >= endp) |
766 | 0 | { |
767 | 0 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
768 | 0 | return -1; |
769 | 0 | } |
770 | 0 | get_uleb128 (idx, datap, endp); |
771 | |
|
772 | 0 | Elf_Data *data = cu->dbg->sectiondata[secidx]; |
773 | 0 | if (data == NULL && cu->unit_type == DW_UT_split_compile) |
774 | 0 | { |
775 | 0 | cu = __libdw_find_split_unit (cu); |
776 | 0 | if (cu != NULL) |
777 | 0 | data = cu->dbg->sectiondata[secidx]; |
778 | 0 | } |
779 | |
|
780 | 0 | if (data == NULL) |
781 | 0 | { |
782 | 0 | __libdw_seterrno (secidx == IDX_debug_loc |
783 | 0 | ? DWARF_E_NO_DEBUG_LOC |
784 | 0 | : DWARF_E_NO_DEBUG_LOCLISTS); |
785 | 0 | return -1; |
786 | 0 | } |
787 | | |
788 | 0 | Dwarf_Off loc_base_off = __libdw_cu_locs_base (cu); |
789 | | |
790 | | /* The section should at least contain room for one offset. */ |
791 | 0 | size_t sec_size = cu->dbg->sectiondata[secidx]->d_size; |
792 | 0 | size_t offset_size = cu->offset_size; |
793 | 0 | if (offset_size > sec_size) |
794 | 0 | { |
795 | 0 | invalid_offset: |
796 | 0 | __libdw_seterrno (DWARF_E_INVALID_OFFSET); |
797 | 0 | return -1; |
798 | 0 | } |
799 | | |
800 | | /* And the base offset should be at least inside the section. */ |
801 | 0 | if (loc_base_off > (sec_size - offset_size)) |
802 | 0 | goto invalid_offset; |
803 | | |
804 | 0 | size_t max_idx = (sec_size - offset_size - loc_base_off) / offset_size; |
805 | 0 | if (idx > max_idx) |
806 | 0 | goto invalid_offset; |
807 | | |
808 | 0 | datap = (cu->dbg->sectiondata[secidx]->d_buf |
809 | 0 | + loc_base_off + (idx * offset_size)); |
810 | 0 | if (offset_size == 4) |
811 | 0 | start_offset = read_4ubyte_unaligned (cu->dbg, datap); |
812 | 0 | else |
813 | 0 | start_offset = read_8ubyte_unaligned (cu->dbg, datap); |
814 | |
|
815 | 0 | start_offset += loc_base_off; |
816 | 0 | } |
817 | 0 | else |
818 | 0 | { |
819 | 0 | if (__libdw_formptr (attr, secidx, |
820 | 0 | (secidx == IDX_debug_loc |
821 | 0 | ? DWARF_E_NO_DEBUG_LOC |
822 | 0 | : DWARF_E_NO_DEBUG_LOCLISTS), |
823 | 0 | NULL, &start_offset) == NULL) |
824 | 0 | return -1; |
825 | | |
826 | 0 | Dwarf_Off loc_off; |
827 | 0 | if (INTUSE(dwarf_cu_dwp_section_info) (attr->cu, DW_SECT_LOCLISTS, |
828 | 0 | &loc_off, NULL) != 0) |
829 | 0 | return -1; |
830 | 0 | start_offset += loc_off; |
831 | 0 | } |
832 | | |
833 | 0 | *offset = start_offset; |
834 | 0 | return 0; |
835 | 0 | } |
836 | | |
837 | | static ptrdiff_t |
838 | | getlocations_addr (Dwarf_Attribute *attr, ptrdiff_t offset, |
839 | | Dwarf_Addr *basep, Dwarf_Addr *startp, Dwarf_Addr *endp, |
840 | | Dwarf_Addr address, const Elf_Data *locs, Dwarf_Op **expr, |
841 | | size_t *exprlen) |
842 | 0 | { |
843 | 0 | Dwarf_CU *cu = attr->cu; |
844 | 0 | Dwarf *dbg = cu->dbg; |
845 | 0 | size_t secidx = cu->version < 5 ? IDX_debug_loc : IDX_debug_loclists; |
846 | 0 | const unsigned char *readp = locs->d_buf + offset; |
847 | 0 | const unsigned char *readendp = locs->d_buf + locs->d_size; |
848 | |
|
849 | 0 | Dwarf_Addr begin; |
850 | 0 | Dwarf_Addr end; |
851 | |
|
852 | 0 | next: |
853 | 0 | switch (__libdw_read_begin_end_pair_inc (cu, secidx, |
854 | 0 | &readp, readendp, |
855 | 0 | cu->address_size, |
856 | 0 | &begin, &end, basep)) |
857 | 0 | { |
858 | 0 | case 0: /* got location range. */ |
859 | 0 | break; |
860 | 0 | case 1: /* base address setup. */ |
861 | 0 | goto next; |
862 | 0 | case 2: /* end of loclist */ |
863 | 0 | return 0; |
864 | 0 | default: /* error */ |
865 | 0 | return -1; |
866 | 0 | } |
867 | | |
868 | | /* We have a location expression. */ |
869 | 0 | Dwarf_Block block; |
870 | 0 | if (secidx == IDX_debug_loc) |
871 | 0 | { |
872 | 0 | if (readendp - readp < 2) |
873 | 0 | { |
874 | 0 | invalid: |
875 | 0 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
876 | 0 | return -1; |
877 | 0 | } |
878 | 0 | block.length = read_2ubyte_unaligned_inc (dbg, readp); |
879 | 0 | } |
880 | 0 | else |
881 | 0 | { |
882 | 0 | if (readendp - readp < 1) |
883 | 0 | goto invalid; |
884 | 0 | get_uleb128 (block.length, readp, readendp); |
885 | 0 | } |
886 | 0 | block.data = (unsigned char *) readp; |
887 | 0 | if (readendp - readp < (ptrdiff_t) block.length) |
888 | 0 | goto invalid; |
889 | 0 | readp += block.length; |
890 | | |
891 | | /* Note these addresses include any base (if necessary) already. */ |
892 | 0 | *startp = begin; |
893 | 0 | *endp = end; |
894 | | |
895 | | /* If address is minus one we want them all, otherwise only matching. */ |
896 | 0 | if (address != (Dwarf_Word) -1 && (address < *startp || address >= *endp)) |
897 | 0 | goto next; |
898 | | |
899 | 0 | if (getlocation (cu, &block, expr, exprlen, secidx) != 0) |
900 | 0 | return -1; |
901 | | |
902 | 0 | return readp - (unsigned char *) locs->d_buf; |
903 | 0 | } |
904 | | |
905 | | int |
906 | | dwarf_getlocation_addr (Dwarf_Attribute *attr, Dwarf_Addr address, |
907 | | Dwarf_Op **llbufs, size_t *listlens, size_t maxlocs) |
908 | 0 | { |
909 | 0 | if (! attr_ok (attr)) |
910 | 0 | return -1; |
911 | | |
912 | 0 | if (llbufs == NULL) |
913 | 0 | maxlocs = SIZE_MAX; |
914 | | |
915 | | /* If it has a block form, it's a single location expression. |
916 | | Except for DW_FORM_data16, which is a 128bit constant. */ |
917 | 0 | Dwarf_Block block; |
918 | 0 | if (attr->form != DW_FORM_data16 |
919 | 0 | && INTUSE(dwarf_formblock) (attr, &block) == 0) |
920 | 0 | { |
921 | 0 | if (maxlocs == 0) |
922 | 0 | return 0; |
923 | 0 | if (llbufs != NULL && |
924 | 0 | getlocation (attr->cu, &block, &llbufs[0], &listlens[0], |
925 | 0 | cu_sec_idx (attr->cu)) != 0) |
926 | 0 | return -1; |
927 | 0 | return listlens[0] == 0 ? 0 : 1; |
928 | 0 | } |
929 | | |
930 | 0 | if (attr->form != DW_FORM_data16) |
931 | 0 | { |
932 | 0 | int error = INTUSE(dwarf_errno) (); |
933 | 0 | if (unlikely (error != DWARF_E_NO_BLOCK)) |
934 | 0 | { |
935 | 0 | __libdw_seterrno (error); |
936 | 0 | return -1; |
937 | 0 | } |
938 | 0 | } |
939 | | |
940 | | /* If is_constant_offset is successful, we are done with 1 result. */ |
941 | 0 | int result = is_constant_offset (attr, llbufs, listlens); |
942 | 0 | if (result != 1) |
943 | 0 | return result ?: 1; |
944 | | |
945 | 0 | Dwarf_Addr base, start, end; |
946 | 0 | Dwarf_Op *expr; |
947 | 0 | size_t expr_len; |
948 | 0 | ptrdiff_t off = 0; |
949 | 0 | size_t got = 0; |
950 | | |
951 | | /* This is a true loclistptr, fetch the initial base address and offset. */ |
952 | 0 | base = __libdw_cu_base_address (attr->cu); |
953 | 0 | if (base == (Dwarf_Addr) -1) |
954 | 0 | return -1; |
955 | | |
956 | 0 | if (initial_offset (attr, &off) != 0) |
957 | 0 | return -1; |
958 | | |
959 | 0 | size_t secidx = attr->cu->version < 5 ? IDX_debug_loc : IDX_debug_loclists; |
960 | 0 | const Elf_Data *d = attr->cu->dbg->sectiondata[secidx]; |
961 | |
|
962 | 0 | while (got < maxlocs |
963 | 0 | && (off = getlocations_addr (attr, off, &base, &start, &end, |
964 | 0 | address, d, &expr, &expr_len)) > 0) |
965 | 0 | { |
966 | | /* This one matches the address. */ |
967 | 0 | if (llbufs != NULL) |
968 | 0 | { |
969 | 0 | llbufs[got] = expr; |
970 | 0 | listlens[got] = expr_len; |
971 | 0 | } |
972 | 0 | ++got; |
973 | 0 | } |
974 | | |
975 | | /* We might stop early, so off can be zero or positive on success. */ |
976 | 0 | if (off < 0) |
977 | 0 | return -1; |
978 | | |
979 | 0 | return got; |
980 | 0 | } |
981 | | |
982 | | ptrdiff_t |
983 | | dwarf_getlocations (Dwarf_Attribute *attr, ptrdiff_t offset, Dwarf_Addr *basep, |
984 | | Dwarf_Addr *startp, Dwarf_Addr *endp, Dwarf_Op **expr, |
985 | | size_t *exprlen) |
986 | 0 | { |
987 | 0 | if (! attr_ok (attr)) |
988 | 0 | return -1; |
989 | | |
990 | | /* 1 is an invalid offset, meaning no more locations. */ |
991 | 0 | if (offset == 1) |
992 | 0 | return 0; |
993 | | |
994 | 0 | if (offset == 0) |
995 | 0 | { |
996 | | /* If it has a block form, it's a single location expression. |
997 | | Except for DW_FORM_data16, which is a 128bit constant. */ |
998 | 0 | Dwarf_Block block; |
999 | 0 | if (attr->form != DW_FORM_data16 |
1000 | 0 | && INTUSE(dwarf_formblock) (attr, &block) == 0) |
1001 | 0 | { |
1002 | 0 | if (getlocation (attr->cu, &block, expr, exprlen, |
1003 | 0 | cu_sec_idx (attr->cu)) != 0) |
1004 | 0 | return -1; |
1005 | | |
1006 | | /* This is the one and only location covering everything. */ |
1007 | 0 | *startp = 0; |
1008 | 0 | *endp = -1; |
1009 | 0 | return 1; |
1010 | 0 | } |
1011 | | |
1012 | 0 | if (attr->form != DW_FORM_data16) |
1013 | 0 | { |
1014 | 0 | int error = INTUSE(dwarf_errno) (); |
1015 | 0 | if (unlikely (error != DWARF_E_NO_BLOCK)) |
1016 | 0 | { |
1017 | 0 | __libdw_seterrno (error); |
1018 | 0 | return -1; |
1019 | 0 | } |
1020 | 0 | } |
1021 | | |
1022 | 0 | int result = is_constant_offset (attr, expr, exprlen); |
1023 | 0 | if (result != 1) |
1024 | 0 | { |
1025 | 0 | if (result == 0) |
1026 | 0 | { |
1027 | | /* This is the one and only location covering everything. */ |
1028 | 0 | *startp = 0; |
1029 | 0 | *endp = -1; |
1030 | 0 | return 1; |
1031 | 0 | } |
1032 | 0 | return result; /* Something bad, dwarf_errno has been set. */ |
1033 | 0 | } |
1034 | | |
1035 | | /* We must be looking at a true loclistptr, fetch the initial |
1036 | | base address and offset. */ |
1037 | 0 | *basep = __libdw_cu_base_address (attr->cu); |
1038 | 0 | if (*basep == (Dwarf_Addr) -1) |
1039 | 0 | return -1; |
1040 | | |
1041 | 0 | if (initial_offset (attr, &offset) != 0) |
1042 | 0 | return -1; |
1043 | 0 | } |
1044 | | |
1045 | 0 | size_t secidx = attr->cu->version < 5 ? IDX_debug_loc : IDX_debug_loclists; |
1046 | 0 | const Elf_Data *d = attr->cu->dbg->sectiondata[secidx]; |
1047 | |
|
1048 | 0 | return getlocations_addr (attr, offset, basep, startp, endp, |
1049 | 0 | (Dwarf_Word) -1, d, expr, exprlen); |
1050 | 0 | } |