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