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