1 /* $NetBSD: libdwarf_loc.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "_libdwarf.h"
30
31 __RCSID("$NetBSD: libdwarf_loc.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32 ELFTC_VCSID("Id: libdwarf_loc.c 2070 2011-10-27 03:05:32Z jkoshy ");
33
34 /*
35 * Given an array of bytes of length 'len' representing a
36 * DWARF expression, compute the number of operations based
37 * on there being one byte describing the operation and
38 * zero or more bytes of operands as defined in the standard
39 * for each operation type. Also, if lbuf is non-null, store
40 * the opcode and oprand in it.
41 */
42 static int
_dwarf_loc_fill_loc(Dwarf_Debug dbg,Dwarf_Locdesc * lbuf,uint8_t pointer_size,uint8_t * p,int len)43 _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size,
44 uint8_t *p, int len)
45 {
46 int count;
47 uint64_t operand1;
48 uint64_t operand2;
49 uint8_t *ps, *pe;
50
51 count = 0;
52 ps = p;
53 pe = p + len;
54
55 /*
56 * Process each byte. If an error occurs, then the
57 * count will be set to -1.
58 */
59 while (p < pe) {
60
61 operand1 = 0;
62 operand2 = 0;
63
64 if (lbuf != NULL) {
65 lbuf->ld_s[count].lr_atom = *p;
66 lbuf->ld_s[count].lr_offset = p - ps;
67 }
68
69 switch (*p++) {
70 /* Operations with no operands. */
71 case DW_OP_deref:
72 case DW_OP_reg0:
73 case DW_OP_reg1:
74 case DW_OP_reg2:
75 case DW_OP_reg3:
76 case DW_OP_reg4:
77 case DW_OP_reg5:
78 case DW_OP_reg6:
79 case DW_OP_reg7:
80 case DW_OP_reg8:
81 case DW_OP_reg9:
82 case DW_OP_reg10:
83 case DW_OP_reg11:
84 case DW_OP_reg12:
85 case DW_OP_reg13:
86 case DW_OP_reg14:
87 case DW_OP_reg15:
88 case DW_OP_reg16:
89 case DW_OP_reg17:
90 case DW_OP_reg18:
91 case DW_OP_reg19:
92 case DW_OP_reg20:
93 case DW_OP_reg21:
94 case DW_OP_reg22:
95 case DW_OP_reg23:
96 case DW_OP_reg24:
97 case DW_OP_reg25:
98 case DW_OP_reg26:
99 case DW_OP_reg27:
100 case DW_OP_reg28:
101 case DW_OP_reg29:
102 case DW_OP_reg30:
103 case DW_OP_reg31:
104
105 case DW_OP_lit0:
106 case DW_OP_lit1:
107 case DW_OP_lit2:
108 case DW_OP_lit3:
109 case DW_OP_lit4:
110 case DW_OP_lit5:
111 case DW_OP_lit6:
112 case DW_OP_lit7:
113 case DW_OP_lit8:
114 case DW_OP_lit9:
115 case DW_OP_lit10:
116 case DW_OP_lit11:
117 case DW_OP_lit12:
118 case DW_OP_lit13:
119 case DW_OP_lit14:
120 case DW_OP_lit15:
121 case DW_OP_lit16:
122 case DW_OP_lit17:
123 case DW_OP_lit18:
124 case DW_OP_lit19:
125 case DW_OP_lit20:
126 case DW_OP_lit21:
127 case DW_OP_lit22:
128 case DW_OP_lit23:
129 case DW_OP_lit24:
130 case DW_OP_lit25:
131 case DW_OP_lit26:
132 case DW_OP_lit27:
133 case DW_OP_lit28:
134 case DW_OP_lit29:
135 case DW_OP_lit30:
136 case DW_OP_lit31:
137
138 case DW_OP_dup:
139 case DW_OP_drop:
140
141 case DW_OP_over:
142
143 case DW_OP_swap:
144 case DW_OP_rot:
145 case DW_OP_xderef:
146
147 case DW_OP_abs:
148 case DW_OP_and:
149 case DW_OP_div:
150 case DW_OP_minus:
151 case DW_OP_mod:
152 case DW_OP_mul:
153 case DW_OP_neg:
154 case DW_OP_not:
155 case DW_OP_or:
156 case DW_OP_plus:
157
158 case DW_OP_shl:
159 case DW_OP_shr:
160 case DW_OP_shra:
161 case DW_OP_xor:
162
163 case DW_OP_eq:
164 case DW_OP_ge:
165 case DW_OP_gt:
166 case DW_OP_le:
167 case DW_OP_lt:
168 case DW_OP_ne:
169
170 case DW_OP_nop:
171 case DW_OP_form_tls_address:
172 case DW_OP_call_frame_cfa:
173 case DW_OP_stack_value:
174 case DW_OP_GNU_push_tls_address:
175 break;
176
177 /* Operations with 1-byte operands. */
178 case DW_OP_const1u:
179 case DW_OP_const1s:
180 case DW_OP_pick:
181 case DW_OP_deref_size:
182 case DW_OP_xderef_size:
183 operand1 = *p++;
184 break;
185
186 /* Operations with 2-byte operands. */
187 case DW_OP_call2:
188 case DW_OP_const2u:
189 case DW_OP_const2s:
190 case DW_OP_bra:
191 case DW_OP_skip:
192 operand1 = dbg->decode(&p, 2);
193 break;
194
195 /* Operations with 4-byte operands. */
196 case DW_OP_call4:
197 case DW_OP_const4u:
198 case DW_OP_const4s:
199 operand1 = dbg->decode(&p, 4);
200 break;
201
202 /* Operations with 8-byte operands. */
203 case DW_OP_const8u:
204 case DW_OP_const8s:
205 operand1 = dbg->decode(&p, 8);
206 break;
207
208 /* Operations with an unsigned LEB128 operand. */
209 case DW_OP_constu:
210 case DW_OP_plus_uconst:
211 case DW_OP_regx:
212 case DW_OP_piece:
213 operand1 = _dwarf_decode_uleb128(&p);
214 break;
215
216 /* Operations with a signed LEB128 operand. */
217 case DW_OP_consts:
218 case DW_OP_breg0:
219 case DW_OP_breg1:
220 case DW_OP_breg2:
221 case DW_OP_breg3:
222 case DW_OP_breg4:
223 case DW_OP_breg5:
224 case DW_OP_breg6:
225 case DW_OP_breg7:
226 case DW_OP_breg8:
227 case DW_OP_breg9:
228 case DW_OP_breg10:
229 case DW_OP_breg11:
230 case DW_OP_breg12:
231 case DW_OP_breg13:
232 case DW_OP_breg14:
233 case DW_OP_breg15:
234 case DW_OP_breg16:
235 case DW_OP_breg17:
236 case DW_OP_breg18:
237 case DW_OP_breg19:
238 case DW_OP_breg20:
239 case DW_OP_breg21:
240 case DW_OP_breg22:
241 case DW_OP_breg23:
242 case DW_OP_breg24:
243 case DW_OP_breg25:
244 case DW_OP_breg26:
245 case DW_OP_breg27:
246 case DW_OP_breg28:
247 case DW_OP_breg29:
248 case DW_OP_breg30:
249 case DW_OP_breg31:
250 case DW_OP_fbreg:
251 operand1 = _dwarf_decode_sleb128(&p);
252 break;
253
254 /*
255 * Oeration with two unsigned LEB128 operands.
256 */
257 case DW_OP_bit_piece:
258 operand1 = _dwarf_decode_uleb128(&p);
259 operand2 = _dwarf_decode_uleb128(&p);
260 break;
261
262 /*
263 * Operations with an unsigned LEB128 operand
264 * followed by a signed LEB128 operand.
265 */
266 case DW_OP_bregx:
267 operand1 = _dwarf_decode_uleb128(&p);
268 operand2 = _dwarf_decode_sleb128(&p);
269 break;
270
271 /*
272 * Operation with an unsigned LEB128 operand
273 * followed by a block. Store a pointer to the
274 * block in the operand2.
275 */
276 case DW_OP_implicit_value:
277 operand1 = _dwarf_decode_uleb128(&p);
278 operand2 = (Dwarf_Unsigned) (uintptr_t) p;
279 p += operand1;
280 break;
281
282 /* Target address size operand. */
283 case DW_OP_addr:
284 operand1 = dbg->decode(&p, pointer_size);
285 break;
286
287 /*
288 * XXX Opcode DW_OP_call_ref has an operand with size
289 * "dwarf_size". Here we use dbg->dbg_offset_size
290 * as "dwarf_size" to be compatible with SGI libdwarf.
291 * However note that dbg->dbg_offset_size is just
292 * a "guess" value so the parsing result of
293 * DW_OP_call_ref might not be correct at all. XXX
294 */
295 case DW_OP_call_ref:
296 operand1 = dbg->decode(&p, dbg->dbg_offset_size);
297 break;
298
299 /* All other operations cause an error. */
300 default:
301 count = -1;
302 break;
303 }
304
305 if (lbuf != NULL) {
306 lbuf->ld_s[count].lr_number = operand1;
307 lbuf->ld_s[count].lr_number2 = operand2;
308 }
309
310 count++;
311 }
312
313 return (count);
314 }
315
316 int
_dwarf_loc_expr_add_atom(Dwarf_Debug dbg,uint8_t * out,uint8_t * end,Dwarf_Small atom,Dwarf_Unsigned operand1,Dwarf_Unsigned operand2,int * length,Dwarf_Error * error)317 _dwarf_loc_expr_add_atom(Dwarf_Debug dbg, uint8_t *out, uint8_t *end,
318 Dwarf_Small atom, Dwarf_Unsigned operand1, Dwarf_Unsigned operand2,
319 int *length, Dwarf_Error *error)
320 {
321 uint8_t buf[64];
322 uint8_t *p, *pe;
323 uint64_t offset;
324 int len;
325
326 if (out != NULL && end != NULL) {
327 p = out;
328 pe = end;
329 } else {
330 p = out = buf;
331 pe = &buf[sizeof(buf)];
332 }
333
334 switch (atom) {
335 /* Operations with no operands. */
336 case DW_OP_deref:
337 case DW_OP_reg0:
338 case DW_OP_reg1:
339 case DW_OP_reg2:
340 case DW_OP_reg3:
341 case DW_OP_reg4:
342 case DW_OP_reg5:
343 case DW_OP_reg6:
344 case DW_OP_reg7:
345 case DW_OP_reg8:
346 case DW_OP_reg9:
347 case DW_OP_reg10:
348 case DW_OP_reg11:
349 case DW_OP_reg12:
350 case DW_OP_reg13:
351 case DW_OP_reg14:
352 case DW_OP_reg15:
353 case DW_OP_reg16:
354 case DW_OP_reg17:
355 case DW_OP_reg18:
356 case DW_OP_reg19:
357 case DW_OP_reg20:
358 case DW_OP_reg21:
359 case DW_OP_reg22:
360 case DW_OP_reg23:
361 case DW_OP_reg24:
362 case DW_OP_reg25:
363 case DW_OP_reg26:
364 case DW_OP_reg27:
365 case DW_OP_reg28:
366 case DW_OP_reg29:
367 case DW_OP_reg30:
368 case DW_OP_reg31:
369
370 case DW_OP_lit0:
371 case DW_OP_lit1:
372 case DW_OP_lit2:
373 case DW_OP_lit3:
374 case DW_OP_lit4:
375 case DW_OP_lit5:
376 case DW_OP_lit6:
377 case DW_OP_lit7:
378 case DW_OP_lit8:
379 case DW_OP_lit9:
380 case DW_OP_lit10:
381 case DW_OP_lit11:
382 case DW_OP_lit12:
383 case DW_OP_lit13:
384 case DW_OP_lit14:
385 case DW_OP_lit15:
386 case DW_OP_lit16:
387 case DW_OP_lit17:
388 case DW_OP_lit18:
389 case DW_OP_lit19:
390 case DW_OP_lit20:
391 case DW_OP_lit21:
392 case DW_OP_lit22:
393 case DW_OP_lit23:
394 case DW_OP_lit24:
395 case DW_OP_lit25:
396 case DW_OP_lit26:
397 case DW_OP_lit27:
398 case DW_OP_lit28:
399 case DW_OP_lit29:
400 case DW_OP_lit30:
401 case DW_OP_lit31:
402
403 case DW_OP_dup:
404 case DW_OP_drop:
405
406 case DW_OP_over:
407
408 case DW_OP_swap:
409 case DW_OP_rot:
410 case DW_OP_xderef:
411
412 case DW_OP_abs:
413 case DW_OP_and:
414 case DW_OP_div:
415 case DW_OP_minus:
416 case DW_OP_mod:
417 case DW_OP_mul:
418 case DW_OP_neg:
419 case DW_OP_not:
420 case DW_OP_or:
421 case DW_OP_plus:
422
423 case DW_OP_shl:
424 case DW_OP_shr:
425 case DW_OP_shra:
426 case DW_OP_xor:
427
428 case DW_OP_eq:
429 case DW_OP_ge:
430 case DW_OP_gt:
431 case DW_OP_le:
432 case DW_OP_lt:
433 case DW_OP_ne:
434
435 case DW_OP_nop:
436 case DW_OP_GNU_push_tls_address:
437 *p++ = atom;
438 break;
439
440 /* Operations with 1-byte operands. */
441 case DW_OP_const1u:
442 case DW_OP_const1s:
443 case DW_OP_pick:
444 case DW_OP_deref_size:
445 case DW_OP_xderef_size:
446 *p++ = atom;
447 *p++ = (uint8_t) operand1;
448 break;
449
450 /* Operations with 2-byte operands. */
451 case DW_OP_const2u:
452 case DW_OP_const2s:
453 case DW_OP_bra:
454 case DW_OP_skip:
455 *p++ = atom;
456 offset = 0;
457 dbg->write(p, &offset, operand1, 2);
458 p += 2;
459 break;
460
461 /* Operations with 4-byte operands. */
462 case DW_OP_const4u:
463 case DW_OP_const4s:
464 *p++ = atom;
465 offset = 0;
466 dbg->write(p, &offset, operand1, 4);
467 p += 4;
468 break;
469
470 /* Operations with 8-byte operands. */
471 case DW_OP_const8u:
472 case DW_OP_const8s:
473 *p++ = atom;
474 offset = 0;
475 dbg->write(p, &offset, operand1, 8);
476 p += 8;
477 break;
478
479 /* Operations with an unsigned LEB128 operand. */
480 case DW_OP_constu:
481 case DW_OP_plus_uconst:
482 case DW_OP_regx:
483 case DW_OP_piece:
484 *p++ = atom;
485 len = _dwarf_write_uleb128(p, pe, operand1);
486 assert(len > 0);
487 p += len;
488 break;
489
490 /* Operations with a signed LEB128 operand. */
491 case DW_OP_consts:
492 case DW_OP_breg0:
493 case DW_OP_breg1:
494 case DW_OP_breg2:
495 case DW_OP_breg3:
496 case DW_OP_breg4:
497 case DW_OP_breg5:
498 case DW_OP_breg6:
499 case DW_OP_breg7:
500 case DW_OP_breg8:
501 case DW_OP_breg9:
502 case DW_OP_breg10:
503 case DW_OP_breg11:
504 case DW_OP_breg12:
505 case DW_OP_breg13:
506 case DW_OP_breg14:
507 case DW_OP_breg15:
508 case DW_OP_breg16:
509 case DW_OP_breg17:
510 case DW_OP_breg18:
511 case DW_OP_breg19:
512 case DW_OP_breg20:
513 case DW_OP_breg21:
514 case DW_OP_breg22:
515 case DW_OP_breg23:
516 case DW_OP_breg24:
517 case DW_OP_breg25:
518 case DW_OP_breg26:
519 case DW_OP_breg27:
520 case DW_OP_breg28:
521 case DW_OP_breg29:
522 case DW_OP_breg30:
523 case DW_OP_breg31:
524 case DW_OP_fbreg:
525 *p++ = atom;
526 len = _dwarf_write_sleb128(p, pe, operand1);
527 assert(len > 0);
528 p += len;
529 break;
530
531 /*
532 * Operations with an unsigned LEB128 operand
533 * followed by a signed LEB128 operand.
534 */
535 case DW_OP_bregx:
536 *p++ = atom;
537 len = _dwarf_write_uleb128(p, pe, operand1);
538 assert(len > 0);
539 p += len;
540 len = _dwarf_write_sleb128(p, pe, operand2);
541 assert(len > 0);
542 p += len;
543 break;
544
545 /* Target address size operand. */
546 case DW_OP_addr:
547 *p++ = atom;
548 offset = 0;
549 dbg->write(p, &offset, operand1, dbg->dbg_pointer_size);
550 p += dbg->dbg_pointer_size;
551 break;
552
553 /* All other operations cause an error. */
554 default:
555 DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD);
556 return (DW_DLE_LOC_EXPR_BAD);
557 }
558
559 if (length)
560 *length = p - out;
561
562 return (DW_DLE_NONE);
563 }
564
565 int
_dwarf_loc_fill_locdesc(Dwarf_Debug dbg,Dwarf_Locdesc * llbuf,uint8_t * in,uint64_t in_len,uint8_t pointer_size,Dwarf_Error * error)566 _dwarf_loc_fill_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *llbuf, uint8_t *in,
567 uint64_t in_len, uint8_t pointer_size, Dwarf_Error *error)
568 {
569 int num;
570
571 assert(llbuf != NULL);
572 assert(in != NULL);
573 assert(in_len > 0);
574
575 /* Compute the number of locations. */
576 if ((num = _dwarf_loc_fill_loc(dbg, NULL, pointer_size, in, in_len)) <
577 0) {
578 DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD);
579 return (DW_DLE_LOC_EXPR_BAD);
580 }
581
582 llbuf->ld_cents = num;
583 if (num <= 0)
584 return (DW_DLE_NONE);
585
586 if ((llbuf->ld_s = calloc(num, sizeof(Dwarf_Loc))) == NULL) {
587 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
588 return (DW_DLE_MEMORY);
589 }
590
591 (void) _dwarf_loc_fill_loc(dbg, llbuf, pointer_size, in, in_len);
592
593 return (DW_DLE_NONE);
594 }
595
596 int
_dwarf_loc_fill_locexpr(Dwarf_Debug dbg,Dwarf_Locdesc ** ret_llbuf,uint8_t * in,uint64_t in_len,uint8_t pointer_size,Dwarf_Error * error)597 _dwarf_loc_fill_locexpr(Dwarf_Debug dbg, Dwarf_Locdesc **ret_llbuf, uint8_t *in,
598 uint64_t in_len, uint8_t pointer_size, Dwarf_Error *error)
599 {
600 Dwarf_Locdesc *llbuf;
601 int ret;
602
603 if ((llbuf = malloc(sizeof(Dwarf_Locdesc))) == NULL) {
604 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
605 return (DW_DLE_MEMORY);
606 }
607 llbuf->ld_lopc = 0;
608 llbuf->ld_hipc = ~0ULL;
609 llbuf->ld_s = NULL;
610
611 ret = _dwarf_loc_fill_locdesc(dbg, llbuf, in, in_len, pointer_size,
612 error);
613 if (ret != DW_DLE_NONE) {
614 free(llbuf);
615 return (ret);
616 }
617
618 *ret_llbuf = llbuf;
619
620 return (ret);
621 }
622
623 int
_dwarf_loc_add(Dwarf_Die die,Dwarf_Attribute at,Dwarf_Error * error)624 _dwarf_loc_add(Dwarf_Die die, Dwarf_Attribute at, Dwarf_Error *error)
625 {
626 Dwarf_Debug dbg;
627 Dwarf_CU cu;
628 int ret;
629
630 assert(at->at_ld == NULL);
631 assert(at->u[1].u8p != NULL);
632 assert(at->u[0].u64 > 0);
633
634 cu = die->die_cu;
635 assert(cu != NULL);
636
637 dbg = cu->cu_dbg;
638 assert(dbg != NULL);
639
640 ret = _dwarf_loc_fill_locexpr(dbg, &at->at_ld, at->u[1].u8p,
641 at->u[0].u64, cu->cu_pointer_size, error);
642
643 return (ret);
644 }
645