1 /* Return location expression list.
2 Copyright (C) 2000-2010, 2013-2015, 2017, 2018 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
42 attr_ok (Dwarf_Attribute *attr)
47 /* If it is an exprloc, it is obviously OK. */
48 if (dwarf_whatform (attr) == DW_FORM_exprloc)
51 /* Otherwise must be one of the attributes listed below. Older
52 DWARF versions might have encoded the exprloc as block, and we
53 cannot easily distinquish attributes in the loclist class because
54 the same forms are used for different classes. */
59 case DW_AT_bit_offset:
61 case DW_AT_lower_bound:
62 case DW_AT_bit_stride:
63 case DW_AT_upper_bound:
66 case DW_AT_associated:
67 case DW_AT_data_location:
68 case DW_AT_byte_stride:
70 case DW_AT_call_value:
71 case DW_AT_call_target:
72 case DW_AT_call_target_clobbered:
73 case DW_AT_call_data_location:
74 case DW_AT_call_data_value:
75 case DW_AT_data_member_location:
76 case DW_AT_vtable_elem_location:
77 case DW_AT_string_length:
78 case DW_AT_use_location:
79 case DW_AT_frame_base:
80 case DW_AT_return_addr:
81 case DW_AT_static_link:
83 case DW_AT_GNU_call_site_value:
84 case DW_AT_GNU_call_site_data_value:
85 case DW_AT_GNU_call_site_target:
86 case DW_AT_GNU_call_site_target_clobbered:
90 __libdw_seterrno (DWARF_E_NO_LOC_VALUE);
104 struct loclist *next;
109 loc_compare (const void *p1, const void *p2)
111 const struct loc_s *l1 = (const struct loc_s *) p1;
112 const struct loc_s *l2 = (const struct loc_s *) p2;
114 if ((uintptr_t) l1->addr < (uintptr_t) l2->addr)
116 if ((uintptr_t) l1->addr > (uintptr_t) l2->addr)
122 /* For each DW_OP_implicit_value, we store a special entry in the cache.
123 This points us directly to the block data for later fetching.
124 Returns zero on success, -1 on bad DWARF or 1 if tsearch failed. */
126 store_implicit_value (Dwarf *dbg, void **cache, Dwarf_Op *op)
128 struct loc_block_s *block = libdw_alloc (dbg, struct loc_block_s,
129 sizeof (struct loc_block_s), 1);
130 const unsigned char *data = (const unsigned char *) (uintptr_t) op->number2;
131 uint64_t len = __libdw_get_uleb128 (&data, data + len_leb128 (Dwarf_Word));
132 if (unlikely (len != op->number))
135 block->data = (unsigned char *) data;
136 block->length = op->number;
137 if (unlikely (tsearch (block, cache, loc_compare) == NULL))
143 dwarf_getlocation_implicit_value (Dwarf_Attribute *attr, const Dwarf_Op *op,
144 Dwarf_Block *return_block)
149 struct loc_block_s fake = { .addr = (void *) op };
150 struct loc_block_s **found = tfind (&fake, &attr->cu->locs, loc_compare);
151 if (unlikely (found == NULL))
153 __libdw_seterrno (DWARF_E_NO_BLOCK);
157 return_block->length = (*found)->length;
158 return_block->data = (*found)->data;
162 /* DW_AT_data_member_location can be a constant as well as a loclistptr.
163 Only data[48] indicate a loclistptr. */
165 check_constant_offset (Dwarf_Attribute *attr,
166 Dwarf_Op **llbuf, size_t *listlen)
168 if (attr->code != DW_AT_data_member_location)
173 /* Punt for any non-constant form. */
177 /* Note, we don't regard DW_FORM_data16 as a constant form,
178 even though technically it is according to the standard. */
188 /* Check whether we already cached this location. */
189 struct loc_s fake = { .addr = attr->valp };
190 struct loc_s **found = tfind (&fake, &attr->cu->locs, loc_compare);
195 if (INTUSE(dwarf_formudata) (attr, &offset) != 0)
198 Dwarf_Op *result = libdw_alloc (attr->cu->dbg,
199 Dwarf_Op, sizeof (Dwarf_Op), 1);
201 result->atom = DW_OP_plus_uconst;
202 result->number = offset;
206 /* Insert a record in the search tree so we can find it again later. */
207 struct loc_s *newp = libdw_alloc (attr->cu->dbg,
208 struct loc_s, sizeof (struct loc_s),
210 newp->addr = attr->valp;
214 found = tsearch (newp, &attr->cu->locs, loc_compare);
217 assert ((*found)->nloc == 1);
221 *llbuf = (*found)->loc;
230 __libdw_intern_expression (Dwarf *dbg, bool other_byte_order,
231 unsigned int address_size, unsigned int ref_size,
232 void **cache, const Dwarf_Block *block,
233 bool cfap, bool valuep,
234 Dwarf_Op **llbuf, size_t *listlen, int sec_index)
236 /* Empty location expressions don't have any ops to intern. */
237 if (block->length == 0)
243 /* Check whether we already looked at this list. */
244 struct loc_s fake = { .addr = block->data };
245 struct loc_s **found = tfind (&fake, cache, loc_compare);
248 /* We already saw it. */
249 *llbuf = (*found)->loc;
250 *listlen = (*found)->nloc;
254 assert (*listlen > 1);
255 assert ((*llbuf)[*listlen - 1].atom == DW_OP_stack_value);
261 const unsigned char *data = block->data;
262 const unsigned char *const end_data = data + block->length;
264 const struct { bool other_byte_order; } bo = { other_byte_order };
266 struct loclist *loclist = NULL;
269 /* Stack allocate at most this many locs. */
270 #define MAX_STACK_LOCS 256
271 struct loclist stack_locs[MAX_STACK_LOCS];
272 #define NEW_LOC() ({ struct loclist *ll; \
273 ll = (likely (n < MAX_STACK_LOCS) \
275 : malloc (sizeof (struct loclist))); \
276 if (unlikely (ll == NULL)) \
279 ll->next = loclist; \
285 /* Synthesize the operation to push the CFA before the expression. */
286 struct loclist *newloc = NEW_LOC ();
287 newloc->atom = DW_OP_call_frame_cfa;
293 /* Decode the opcodes. It is possible in some situations to have a
294 block of size zero. */
295 while (data < end_data)
297 struct loclist *newloc;
301 newloc->offset = data - block->data;
303 switch ((newloc->atom = *data++))
306 /* Address, depends on address size of CU. */
310 if (address_size == 4)
312 if (unlikely (data + 4 > end_data))
315 newloc->number = read_4ubyte_unaligned_inc (&bo, data);
319 if (unlikely (data + 8 > end_data))
322 newloc->number = read_8ubyte_unaligned_inc (&bo, data);
325 else if (__libdw_read_address_inc (dbg, sec_index, &data,
326 address_size, &newloc->number))
331 case DW_OP_GNU_variable_value:
332 /* DW_FORM_ref_addr, depends on offset size of CU. */
333 if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data,
367 case DW_OP_lit0 ... DW_OP_lit31:
368 case DW_OP_reg0 ... DW_OP_reg31:
370 case DW_OP_push_object_address:
371 case DW_OP_call_frame_cfa:
372 case DW_OP_form_tls_address:
373 case DW_OP_GNU_push_tls_address:
374 case DW_OP_stack_value:
380 case DW_OP_deref_size:
381 case DW_OP_xderef_size:
382 if (unlikely (data >= end_data))
385 __libdw_seterrno (DWARF_E_INVALID_DWARF);
387 /* Free any dynamicly allocated loclists, if any. */
388 while (n > MAX_STACK_LOCS)
390 struct loclist *loc = loclist;
398 newloc->number = *data++;
402 if (unlikely (data >= end_data))
405 newloc->number = *((int8_t *) data);
410 if (unlikely (data + 2 > end_data))
413 newloc->number = read_2ubyte_unaligned_inc (&bo, data);
420 if (unlikely (data + 2 > end_data))
423 newloc->number = read_2sbyte_unaligned_inc (&bo, data);
427 if (unlikely (data + 4 > end_data))
430 newloc->number = read_4ubyte_unaligned_inc (&bo, data);
435 case DW_OP_GNU_parameter_ref:
436 if (unlikely (data + 4 > end_data))
439 newloc->number = read_4sbyte_unaligned_inc (&bo, data);
443 if (unlikely (data + 8 > end_data))
446 newloc->number = read_8ubyte_unaligned_inc (&bo, data);
450 if (unlikely (data + 8 > end_data))
453 newloc->number = read_8sbyte_unaligned_inc (&bo, data);
457 case DW_OP_plus_uconst:
461 case DW_OP_GNU_convert:
462 case DW_OP_reinterpret:
463 case DW_OP_GNU_reinterpret:
465 case DW_OP_GNU_addr_index:
467 case DW_OP_GNU_const_index:
468 get_uleb128 (newloc->number, data, end_data);
472 case DW_OP_breg0 ... DW_OP_breg31:
474 get_sleb128 (newloc->number, data, end_data);
478 get_uleb128 (newloc->number, data, end_data);
479 if (unlikely (data >= end_data))
481 get_sleb128 (newloc->number2, data, end_data);
484 case DW_OP_bit_piece:
485 case DW_OP_regval_type:
486 case DW_OP_GNU_regval_type:
487 get_uleb128 (newloc->number, data, end_data);
488 if (unlikely (data >= end_data))
490 get_uleb128 (newloc->number2, data, end_data);
493 case DW_OP_implicit_value:
494 case DW_OP_entry_value:
495 case DW_OP_GNU_entry_value:
496 /* This cannot be used in a CFI expression. */
497 if (unlikely (dbg == NULL))
500 /* start of block inc. len. */
501 newloc->number2 = (Dwarf_Word) (uintptr_t) data;
502 get_uleb128 (newloc->number, data, end_data); /* Block length. */
503 if (unlikely ((Dwarf_Word) (end_data - data) < newloc->number))
505 data += newloc->number; /* Skip the block. */
508 case DW_OP_implicit_pointer:
509 case DW_OP_GNU_implicit_pointer:
510 /* DW_FORM_ref_addr, depends on offset size of CU. */
511 if (dbg == NULL || __libdw_read_offset_inc (dbg, sec_index, &data,
516 if (unlikely (data >= end_data))
518 get_uleb128 (newloc->number2, data, end_data); /* Byte offset. */
521 case DW_OP_deref_type:
522 case DW_OP_GNU_deref_type:
523 case DW_OP_xderef_type:
524 if (unlikely (data + 1 >= end_data))
526 newloc->number = *data++;
527 get_uleb128 (newloc->number2, data, end_data);
530 case DW_OP_const_type:
531 case DW_OP_GNU_const_type:
534 get_uleb128 (newloc->number, data, end_data);
535 if (unlikely (data >= end_data))
538 /* start of block inc. len. */
539 newloc->number2 = (Dwarf_Word) (uintptr_t) data;
541 if (unlikely ((Dwarf_Word) (end_data - data) < size))
543 data += size; /* Skip the block. */
552 if (unlikely (n == 0))
554 /* This is not allowed.
555 It would mean an empty location expression, which we handled
556 already as a special case above. */
562 struct loclist *newloc = NEW_LOC ();
563 newloc->atom = DW_OP_stack_value;
566 newloc->offset = data - block->data;
569 /* Allocate the array. */
572 result = libdw_alloc (dbg, Dwarf_Op, sizeof (Dwarf_Op), n);
575 result = malloc (sizeof *result * n);
579 __libdw_seterrno (DWARF_E_NOMEM);
584 /* Store the result. */
590 /* We populate the array from the back since the list is backwards. */
592 result[n].atom = loclist->atom;
593 result[n].number = loclist->number;
594 result[n].number2 = loclist->number2;
595 result[n].offset = loclist->offset;
597 if (result[n].atom == DW_OP_implicit_value)
599 int store = store_implicit_value (dbg, cache, &result[n]);
600 if (unlikely (store != 0))
609 struct loclist *loc = loclist;
610 loclist = loclist->next;
611 if (unlikely (n + 1 > MAX_STACK_LOCS))
616 /* Insert a record in the search tree so that we can find it again later. */
619 newp = libdw_alloc (dbg, struct loc_s, sizeof (struct loc_s), 1);
622 newp = malloc (sizeof *newp);
630 newp->addr = block->data;
632 newp->nloc = *listlen;
633 (void) tsearch (newp, cache, loc_compare);
640 getlocation (struct Dwarf_CU *cu, const Dwarf_Block *block,
641 Dwarf_Op **llbuf, size_t *listlen, int sec_index)
643 /* Empty location expressions don't have any ops to intern.
644 Note that synthetic empty_cu doesn't have an associated DWARF dbg. */
645 if (block->length == 0)
651 return __libdw_intern_expression (cu->dbg, cu->dbg->other_byte_order,
652 cu->address_size, (cu->version == 2
657 llbuf, listlen, sec_index);
661 dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **llbuf, size_t *listlen)
663 if (! attr_ok (attr))
666 int result = check_constant_offset (attr, llbuf, listlen);
670 /* If it has a block form, it's a single location expression.
671 Except for DW_FORM_data16, which is a 128bit constant. */
672 if (attr->form == DW_FORM_data16)
674 __libdw_seterrno (DWARF_E_NO_BLOCK);
678 if (INTUSE(dwarf_formblock) (attr, &block) != 0)
681 return getlocation (attr->cu, &block, llbuf, listlen, cu_sec_idx (attr->cu));
685 __libdw_cu_base_address (Dwarf_CU *cu)
687 if (cu->base_address == (Dwarf_Addr) -1)
691 /* Fetch the CU's base address. */
692 Dwarf_Die cudie = CUDIE (cu);
694 /* Find the base address of the compilation unit. It will
695 normally be specified by DW_AT_low_pc. In DWARF-3 draft 4,
696 the base address could be overridden by DW_AT_entry_pc. It's
697 been removed, but GCC emits DW_AT_entry_pc and not DW_AT_lowpc
698 for compilation units with discontinuous ranges. */
699 Dwarf_Attribute attr_mem;
700 if (INTUSE(dwarf_lowpc) (&cudie, &base) != 0
701 && INTUSE(dwarf_formaddr) (INTUSE(dwarf_attr) (&cudie,
706 /* The compiler provided no base address when it should
707 have. Buggy GCC does this when it used absolute
708 addresses in the location list and no DW_AT_ranges. */
711 cu->base_address = base;
714 return cu->base_address;
718 initial_offset (Dwarf_Attribute *attr, ptrdiff_t *offset)
720 size_t secidx = (attr->cu->version < 5
721 ? IDX_debug_loc : IDX_debug_loclists);
723 Dwarf_Word start_offset;
724 if (attr->form == DW_FORM_loclistx)
727 Dwarf_CU *cu = attr->cu;
728 const unsigned char *datap = attr->valp;
729 const unsigned char *endp = cu->endp;
732 __libdw_seterrno (DWARF_E_INVALID_DWARF);
735 get_uleb128 (idx, datap, endp);
737 Elf_Data *data = cu->dbg->sectiondata[secidx];
738 if (data == NULL && cu->unit_type == DW_UT_split_compile)
740 cu = __libdw_find_split_unit (cu);
742 data = cu->dbg->sectiondata[secidx];
747 __libdw_seterrno (secidx == IDX_debug_loc
748 ? DWARF_E_NO_DEBUG_LOC
749 : DWARF_E_NO_DEBUG_LOCLISTS);
753 Dwarf_Off loc_base_off = __libdw_cu_locs_base (cu);
755 /* The section should at least contain room for one offset. */
756 size_t sec_size = cu->dbg->sectiondata[secidx]->d_size;
757 size_t offset_size = cu->offset_size;
758 if (offset_size > sec_size)
761 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
765 /* And the base offset should be at least inside the section. */
766 if (loc_base_off > (sec_size - offset_size))
769 size_t max_idx = (sec_size - offset_size - loc_base_off) / offset_size;
773 datap = (cu->dbg->sectiondata[secidx]->d_buf
774 + loc_base_off + (idx * offset_size));
775 if (offset_size == 4)
776 start_offset = read_4ubyte_unaligned (cu->dbg, datap);
778 start_offset = read_8ubyte_unaligned (cu->dbg, datap);
780 start_offset += loc_base_off;
784 if (__libdw_formptr (attr, secidx,
785 (secidx == IDX_debug_loc
786 ? DWARF_E_NO_DEBUG_LOC
787 : DWARF_E_NO_DEBUG_LOCLISTS),
788 NULL, &start_offset) == NULL)
792 *offset = start_offset;
797 getlocations_addr (Dwarf_Attribute *attr, ptrdiff_t offset,
798 Dwarf_Addr *basep, Dwarf_Addr *startp, Dwarf_Addr *endp,
799 Dwarf_Addr address, const Elf_Data *locs, Dwarf_Op **expr,
802 Dwarf_CU *cu = attr->cu;
803 Dwarf *dbg = cu->dbg;
804 size_t secidx = cu->version < 5 ? IDX_debug_loc : IDX_debug_loclists;
805 const unsigned char *readp = locs->d_buf + offset;
806 const unsigned char *readendp = locs->d_buf + locs->d_size;
812 switch (__libdw_read_begin_end_pair_inc (cu, secidx,
815 &begin, &end, basep))
817 case 0: /* got location range. */
819 case 1: /* base address setup. */
821 case 2: /* end of loclist */
827 /* We have a location expression. */
829 if (secidx == IDX_debug_loc)
831 if (readendp - readp < 2)
834 __libdw_seterrno (DWARF_E_INVALID_DWARF);
837 block.length = read_2ubyte_unaligned_inc (dbg, readp);
841 if (readendp - readp < 1)
843 get_uleb128 (block.length, readp, readendp);
845 block.data = (unsigned char *) readp;
846 if (readendp - readp < (ptrdiff_t) block.length)
848 readp += block.length;
850 /* Note these addresses include any base (if necessary) already. */
854 /* If address is minus one we want them all, otherwise only matching. */
855 if (address != (Dwarf_Word) -1 && (address < *startp || address >= *endp))
858 if (getlocation (cu, &block, expr, exprlen, secidx) != 0)
861 return readp - (unsigned char *) locs->d_buf;
865 dwarf_getlocation_addr (Dwarf_Attribute *attr, Dwarf_Addr address,
866 Dwarf_Op **llbufs, size_t *listlens, size_t maxlocs)
868 if (! attr_ok (attr))
874 /* If it has a block form, it's a single location expression.
875 Except for DW_FORM_data16, which is a 128bit constant. */
877 if (attr->form != DW_FORM_data16
878 && INTUSE(dwarf_formblock) (attr, &block) == 0)
882 if (llbufs != NULL &&
883 getlocation (attr->cu, &block, &llbufs[0], &listlens[0],
884 cu_sec_idx (attr->cu)) != 0)
886 return listlens[0] == 0 ? 0 : 1;
889 if (attr->form != DW_FORM_data16)
891 int error = INTUSE(dwarf_errno) ();
892 if (unlikely (error != DWARF_E_NO_BLOCK))
894 __libdw_seterrno (error);
899 int result = check_constant_offset (attr, &llbufs[0], &listlens[0]);
903 Dwarf_Addr base, start, end;
909 /* This is a true loclistptr, fetch the initial base address and offset. */
910 base = __libdw_cu_base_address (attr->cu);
911 if (base == (Dwarf_Addr) -1)
914 if (initial_offset (attr, &off) != 0)
917 size_t secidx = attr->cu->version < 5 ? IDX_debug_loc : IDX_debug_loclists;
918 const Elf_Data *d = attr->cu->dbg->sectiondata[secidx];
921 && (off = getlocations_addr (attr, off, &base, &start, &end,
922 address, d, &expr, &expr_len)) > 0)
924 /* This one matches the address. */
928 listlens[got] = expr_len;
933 /* We might stop early, so off can be zero or positive on success. */
941 dwarf_getlocations (Dwarf_Attribute *attr, ptrdiff_t offset, Dwarf_Addr *basep,
942 Dwarf_Addr *startp, Dwarf_Addr *endp, Dwarf_Op **expr,
945 if (! attr_ok (attr))
948 /* 1 is an invalid offset, meaning no more locations. */
954 /* If it has a block form, it's a single location expression.
955 Except for DW_FORM_data16, which is a 128bit constant. */
957 if (attr->form != DW_FORM_data16
958 && INTUSE(dwarf_formblock) (attr, &block) == 0)
960 if (getlocation (attr->cu, &block, expr, exprlen,
961 cu_sec_idx (attr->cu)) != 0)
964 /* This is the one and only location covering everything. */
970 if (attr->form != DW_FORM_data16)
972 int error = INTUSE(dwarf_errno) ();
973 if (unlikely (error != DWARF_E_NO_BLOCK))
975 __libdw_seterrno (error);
980 int result = check_constant_offset (attr, expr, exprlen);
985 /* This is the one and only location covering everything. */
993 /* We must be looking at a true loclistptr, fetch the initial
994 base address and offset. */
995 *basep = __libdw_cu_base_address (attr->cu);
996 if (*basep == (Dwarf_Addr) -1)
999 if (initial_offset (attr, &offset) != 0)
1003 size_t secidx = attr->cu->version < 5 ? IDX_debug_loc : IDX_debug_loclists;
1004 const Elf_Data *d = attr->cu->dbg->sectiondata[secidx];
1006 return getlocations_addr (attr, offset, basep, startp, endp,
1007 (Dwarf_Word) -1, d, expr, exprlen);