1 /* Return location expression list.
2 Copyright (C) 2000-2010, 2013-2015 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
43 attr_ok (Dwarf_Attribute *attr)
48 /* Must be one of the attributes listed below. */
52 case DW_AT_data_member_location:
53 case DW_AT_vtable_elem_location:
54 case DW_AT_string_length:
55 case DW_AT_use_location:
56 case DW_AT_frame_base:
57 case DW_AT_return_addr:
58 case DW_AT_static_link:
60 case DW_AT_GNU_call_site_value:
61 case DW_AT_GNU_call_site_data_value:
62 case DW_AT_GNU_call_site_target:
63 case DW_AT_GNU_call_site_target_clobbered:
67 __libdw_seterrno (DWARF_E_NO_LOCLIST);
86 loc_compare (const void *p1, const void *p2)
88 const struct loc_s *l1 = (const struct loc_s *) p1;
89 const struct loc_s *l2 = (const struct loc_s *) p2;
91 if ((uintptr_t) l1->addr < (uintptr_t) l2->addr)
93 if ((uintptr_t) l1->addr > (uintptr_t) l2->addr)
99 /* For each DW_OP_implicit_value, we store a special entry in the cache.
100 This points us directly to the block data for later fetching. */
102 store_implicit_value (Dwarf *dbg, void **cache, Dwarf_Op *op)
104 struct loc_block_s *block = libdw_alloc (dbg, struct loc_block_s,
105 sizeof (struct loc_block_s), 1);
106 const unsigned char *data = (const unsigned char *) (uintptr_t) op->number2;
107 // Ignored, equal to op->number. And data length already checked.
108 (void) __libdw_get_uleb128 (&data, data + len_leb128 (Dwarf_Word));
110 block->data = (unsigned char *) data;
111 block->length = op->number;
112 (void) tsearch (block, cache, loc_compare);
116 dwarf_getlocation_implicit_value (Dwarf_Attribute *attr, const Dwarf_Op *op,
117 Dwarf_Block *return_block)
122 struct loc_block_s fake = { .addr = (void *) op };
123 struct loc_block_s **found = tfind (&fake, &attr->cu->locs, loc_compare);
124 if (unlikely (found == NULL))
126 __libdw_seterrno (DWARF_E_NO_BLOCK);
130 return_block->length = (*found)->length;
131 return_block->data = (*found)->data;
135 /* DW_AT_data_member_location can be a constant as well as a loclistptr.
136 Only data[48] indicate a loclistptr. */
138 check_constant_offset (Dwarf_Attribute *attr,
139 Dwarf_Op **llbuf, size_t *listlen)
141 if (attr->code != DW_AT_data_member_location)
146 /* Punt for any non-constant form. */
159 /* Check whether we already cached this location. */
160 struct loc_s fake = { .addr = attr->valp };
161 struct loc_s **found = tfind (&fake, &attr->cu->locs, loc_compare);
166 if (INTUSE(dwarf_formudata) (attr, &offset) != 0)
169 Dwarf_Op *result = libdw_alloc (attr->cu->dbg,
170 Dwarf_Op, sizeof (Dwarf_Op), 1);
172 result->atom = DW_OP_plus_uconst;
173 result->number = offset;
177 /* Insert a record in the search tree so we can find it again later. */
178 struct loc_s *newp = libdw_alloc (attr->cu->dbg,
179 struct loc_s, sizeof (struct loc_s),
181 newp->addr = attr->valp;
185 found = tsearch (newp, &attr->cu->locs, loc_compare);
188 assert ((*found)->nloc == 1);
192 *llbuf = (*found)->loc;
201 __libdw_intern_expression (Dwarf *dbg, bool other_byte_order,
202 unsigned int address_size, unsigned int ref_size,
203 void **cache, const Dwarf_Block *block,
204 bool cfap, bool valuep,
205 Dwarf_Op **llbuf, size_t *listlen, int sec_index)
207 /* Empty location expressions don't have any ops to intern. */
208 if (block->length == 0)
214 /* Check whether we already looked at this list. */
215 struct loc_s fake = { .addr = block->data };
216 struct loc_s **found = tfind (&fake, cache, loc_compare);
219 /* We already saw it. */
220 *llbuf = (*found)->loc;
221 *listlen = (*found)->nloc;
225 assert (*listlen > 1);
226 assert ((*llbuf)[*listlen - 1].atom == DW_OP_stack_value);
232 const unsigned char *data = block->data;
233 const unsigned char *const end_data = data + block->length;
235 const struct { bool other_byte_order; } bo = { other_byte_order };
237 struct loclist *loclist = NULL;
240 /* Stack allocate at most this many locs. */
241 #define MAX_STACK_LOCS 256
242 struct loclist stack_locs[MAX_STACK_LOCS];
243 #define NEW_LOC() ({ struct loclist *ll; \
244 ll = (likely (n < MAX_STACK_LOCS) \
246 : malloc (sizeof (struct loclist))); \
247 if (unlikely (ll == NULL)) \
250 ll->next = loclist; \
256 /* Synthesize the operation to push the CFA before the expression. */
257 struct loclist *newloc = NEW_LOC ();
258 newloc->atom = DW_OP_call_frame_cfa;
264 /* Decode the opcodes. It is possible in some situations to have a
265 block of size zero. */
266 while (data < end_data)
268 struct loclist *newloc;
272 newloc->offset = data - block->data;
274 switch ((newloc->atom = *data++))
277 /* Address, depends on address size of CU. */
281 if (address_size == 4)
283 if (unlikely (data + 4 > end_data))
286 newloc->number = read_4ubyte_unaligned_inc (&bo, data);
290 if (unlikely (data + 8 > end_data))
293 newloc->number = read_8ubyte_unaligned_inc (&bo, data);
296 else if (__libdw_read_address_inc (dbg, sec_index, &data,
297 address_size, &newloc->number))
302 /* DW_FORM_ref_addr, depends on offset size of CU. */
303 if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data,
337 case DW_OP_lit0 ... DW_OP_lit31:
338 case DW_OP_reg0 ... DW_OP_reg31:
340 case DW_OP_push_object_address:
341 case DW_OP_call_frame_cfa:
342 case DW_OP_form_tls_address:
343 case DW_OP_GNU_push_tls_address:
344 case DW_OP_stack_value:
350 case DW_OP_deref_size:
351 case DW_OP_xderef_size:
352 if (unlikely (data >= end_data))
355 __libdw_seterrno (DWARF_E_INVALID_DWARF);
357 /* Free any dynamicly allocated loclists, if any. */
358 while (n > MAX_STACK_LOCS)
360 struct loclist *loc = loclist;
368 newloc->number = *data++;
372 if (unlikely (data >= end_data))
375 newloc->number = *((int8_t *) data);
380 if (unlikely (data + 2 > end_data))
383 newloc->number = read_2ubyte_unaligned_inc (&bo, data);
390 if (unlikely (data + 2 > end_data))
393 newloc->number = read_2sbyte_unaligned_inc (&bo, data);
397 if (unlikely (data + 4 > end_data))
400 newloc->number = read_4ubyte_unaligned_inc (&bo, data);
405 case DW_OP_GNU_parameter_ref:
406 if (unlikely (data + 4 > end_data))
409 newloc->number = read_4sbyte_unaligned_inc (&bo, data);
413 if (unlikely (data + 8 > end_data))
416 newloc->number = read_8ubyte_unaligned_inc (&bo, data);
420 if (unlikely (data + 8 > end_data))
423 newloc->number = read_8sbyte_unaligned_inc (&bo, data);
427 case DW_OP_plus_uconst:
430 case DW_OP_GNU_convert:
431 case DW_OP_GNU_reinterpret:
432 get_uleb128 (newloc->number, data, end_data);
436 case DW_OP_breg0 ... DW_OP_breg31:
438 get_sleb128 (newloc->number, data, end_data);
442 get_uleb128 (newloc->number, data, end_data);
443 if (unlikely (data >= end_data))
445 get_sleb128 (newloc->number2, data, end_data);
448 case DW_OP_bit_piece:
449 case DW_OP_GNU_regval_type:
450 get_uleb128 (newloc->number, data, end_data);
451 if (unlikely (data >= end_data))
453 get_uleb128 (newloc->number2, data, end_data);
456 case DW_OP_implicit_value:
457 case DW_OP_GNU_entry_value:
458 /* This cannot be used in a CFI expression. */
459 if (unlikely (dbg == NULL))
462 /* start of block inc. len. */
463 newloc->number2 = (Dwarf_Word) (uintptr_t) data;
464 get_uleb128 (newloc->number, data, end_data); /* Block length. */
465 if (unlikely ((Dwarf_Word) (end_data - data) < newloc->number))
467 data += newloc->number; /* Skip the block. */
470 case DW_OP_GNU_implicit_pointer:
471 /* DW_FORM_ref_addr, depends on offset size of CU. */
472 if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data,
477 if (unlikely (data >= end_data))
479 get_uleb128 (newloc->number2, data, end_data); /* Byte offset. */
482 case DW_OP_GNU_deref_type:
483 if (unlikely (data + 1 >= end_data))
485 newloc->number = *data++;
486 get_uleb128 (newloc->number2, data, end_data);
489 case DW_OP_GNU_const_type:
492 get_uleb128 (newloc->number, data, end_data);
493 if (unlikely (data >= end_data))
496 /* start of block inc. len. */
497 newloc->number2 = (Dwarf_Word) (uintptr_t) data;
499 if (unlikely ((Dwarf_Word) (end_data - data) < size))
501 data += size; /* Skip the block. */
510 if (unlikely (n == 0))
512 /* This is not allowed.
513 It would mean an empty location expression, which we handled
514 already as a special case above. */
520 struct loclist *newloc = NEW_LOC ();
521 newloc->atom = DW_OP_stack_value;
524 newloc->offset = data - block->data;
527 /* Allocate the array. */
530 result = libdw_alloc (dbg, Dwarf_Op, sizeof (Dwarf_Op), n);
533 result = malloc (sizeof *result * n);
537 __libdw_seterrno (DWARF_E_NOMEM);
542 /* Store the result. */
548 /* We populate the array from the back since the list is backwards. */
550 result[n].atom = loclist->atom;
551 result[n].number = loclist->number;
552 result[n].number2 = loclist->number2;
553 result[n].offset = loclist->offset;
555 if (result[n].atom == DW_OP_implicit_value)
556 store_implicit_value (dbg, cache, &result[n]);
558 struct loclist *loc = loclist;
559 loclist = loclist->next;
560 if (unlikely (n + 1 > MAX_STACK_LOCS))
565 /* Insert a record in the search tree so that we can find it again later. */
568 newp = libdw_alloc (dbg, struct loc_s, sizeof (struct loc_s), 1);
571 newp = malloc (sizeof *newp);
579 newp->addr = block->data;
581 newp->nloc = *listlen;
582 (void) tsearch (newp, cache, loc_compare);
589 getlocation (struct Dwarf_CU *cu, const Dwarf_Block *block,
590 Dwarf_Op **llbuf, size_t *listlen, int sec_index)
592 /* Empty location expressions don't have any ops to intern.
593 Note that synthetic empty_cu doesn't have an associated DWARF dbg. */
594 if (block->length == 0)
600 return __libdw_intern_expression (cu->dbg, cu->dbg->other_byte_order,
601 cu->address_size, (cu->version == 2
606 llbuf, listlen, sec_index);
610 dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **llbuf, size_t *listlen)
612 if (! attr_ok (attr))
615 int result = check_constant_offset (attr, llbuf, listlen);
619 /* If it has a block form, it's a single location expression. */
621 if (INTUSE(dwarf_formblock) (attr, &block) != 0)
624 return getlocation (attr->cu, &block, llbuf, listlen, cu_sec_idx (attr->cu));
628 attr_base_address (Dwarf_Attribute *attr, Dwarf_Addr *basep)
630 /* Fetch the CU's base address. */
631 Dwarf_Die cudie = CUDIE (attr->cu);
633 /* Find the base address of the compilation unit. It will
634 normally be specified by DW_AT_low_pc. In DWARF-3 draft 4,
635 the base address could be overridden by DW_AT_entry_pc. It's
636 been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc
637 for compilation units with discontinuous ranges. */
638 Dwarf_Attribute attr_mem;
639 if (unlikely (INTUSE(dwarf_lowpc) (&cudie, basep) != 0)
640 && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie,
645 if (INTUSE(dwarf_errno) () != 0)
648 /* The compiler provided no base address when it should
649 have. Buggy GCC does this when it used absolute
650 addresses in the location list and no DW_AT_ranges. */
657 initial_offset_base (Dwarf_Attribute *attr, ptrdiff_t *offset,
660 if (attr_base_address (attr, basep) != 0)
663 Dwarf_Word start_offset;
664 if (__libdw_formptr (attr, IDX_debug_loc,
666 NULL, &start_offset) == NULL)
669 *offset = start_offset;
674 getlocations_addr (Dwarf_Attribute *attr, ptrdiff_t offset,
675 Dwarf_Addr *basep, Dwarf_Addr *startp, Dwarf_Addr *endp,
676 Dwarf_Addr address, const Elf_Data *locs, Dwarf_Op **expr,
679 unsigned char *readp = locs->d_buf + offset;
680 unsigned char *readendp = locs->d_buf + locs->d_size;
683 if (readendp - readp < attr->cu->address_size * 2)
686 __libdw_seterrno (DWARF_E_INVALID_DWARF);
693 switch (__libdw_read_begin_end_pair_inc (attr->cu->dbg, IDX_debug_loc,
694 &readp, attr->cu->address_size,
695 &begin, &end, basep))
697 case 0: /* got location range. */
699 case 1: /* base address setup. */
701 case 2: /* end of loclist */
707 if (readendp - readp < 2)
710 /* We have a location expression. */
712 block.length = read_2ubyte_unaligned_inc (attr->cu->dbg, readp);
714 if (readendp - readp < (ptrdiff_t) block.length)
716 readp += block.length;
718 *startp = *basep + begin;
719 *endp = *basep + end;
721 /* If address is minus one we want them all, otherwise only matching. */
722 if (address != (Dwarf_Word) -1 && (address < *startp || address >= *endp))
725 if (getlocation (attr->cu, &block, expr, exprlen, IDX_debug_loc) != 0)
728 return readp - (unsigned char *) locs->d_buf;
732 dwarf_getlocation_addr (Dwarf_Attribute *attr, Dwarf_Addr address,
733 Dwarf_Op **llbufs, size_t *listlens, size_t maxlocs)
735 if (! attr_ok (attr))
741 /* If it has a block form, it's a single location expression. */
743 if (INTUSE(dwarf_formblock) (attr, &block) == 0)
747 if (llbufs != NULL &&
748 getlocation (attr->cu, &block, &llbufs[0], &listlens[0],
749 cu_sec_idx (attr->cu)) != 0)
751 return listlens[0] == 0 ? 0 : 1;
754 int error = INTUSE(dwarf_errno) ();
755 if (unlikely (error != DWARF_E_NO_BLOCK))
757 __libdw_seterrno (error);
761 int result = check_constant_offset (attr, &llbufs[0], &listlens[0]);
765 Dwarf_Addr base, start, end;
771 /* This is a true loclistptr, fetch the initial base address and offset. */
772 if (initial_offset_base (attr, &off, &base) != 0)
775 const Elf_Data *d = attr->cu->dbg->sectiondata[IDX_debug_loc];
778 __libdw_seterrno (DWARF_E_NO_LOCLIST);
783 && (off = getlocations_addr (attr, off, &base, &start, &end,
784 address, d, &expr, &expr_len)) > 0)
786 /* This one matches the address. */
790 listlens[got] = expr_len;
795 /* We might stop early, so off can be zero or positive on success. */
803 dwarf_getlocations (Dwarf_Attribute *attr, ptrdiff_t offset, Dwarf_Addr *basep,
804 Dwarf_Addr *startp, Dwarf_Addr *endp, Dwarf_Op **expr,
807 if (! attr_ok (attr))
810 /* 1 is an invalid offset, meaning no more locations. */
816 /* If it has a block form, it's a single location expression. */
818 if (INTUSE(dwarf_formblock) (attr, &block) == 0)
820 if (getlocation (attr->cu, &block, expr, exprlen,
821 cu_sec_idx (attr->cu)) != 0)
824 /* This is the one and only location covering everything. */
830 int error = INTUSE(dwarf_errno) ();
831 if (unlikely (error != DWARF_E_NO_BLOCK))
833 __libdw_seterrno (error);
837 int result = check_constant_offset (attr, expr, exprlen);
842 /* This is the one and only location covering everything. */
850 /* We must be looking at a true loclistptr, fetch the initial
851 base address and offset. */
852 if (initial_offset_base (attr, &offset, basep) != 0)
856 const Elf_Data *d = attr->cu->dbg->sectiondata[IDX_debug_loc];
859 __libdw_seterrno (DWARF_E_NO_LOCLIST);
863 return getlocations_addr (attr, offset, basep, startp, endp,
864 (Dwarf_Word) -1, d, expr, exprlen);