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