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 * $FreeBSD$
27 */
28
29#include <stdlib.h>
30#include "_libdwarf.h"
31
32static int64_t
33dwarf_decode_sleb128(uint8_t **dp)
34{
35	int64_t ret = 0;
36	uint8_t b;
37	int shift = 0;
38
39	uint8_t *src = *dp;
40
41	do {
42		b = *src++;
43
44		ret |= ((b & 0x7f) << shift);
45
46		shift += 7;
47	} while ((b & 0x80) != 0);
48
49	if (shift < 64 && (b & 0x40) != 0)
50		ret |= (-1 << shift);
51
52	*dp = src;
53
54	return ret;
55}
56
57static uint64_t
58dwarf_decode_uleb128(uint8_t **dp)
59{
60	uint64_t ret = 0;
61	uint8_t b;
62	int shift = 0;
63
64	uint8_t *src = *dp;
65
66	do {
67		b = *src++;
68
69		ret |= ((b & 0x7f) << shift);
70
71		shift += 7;
72	} while ((b & 0x80) != 0);
73
74	*dp = src;
75
76	return ret;
77}
78
79/*
80 * Given an array of bytes of length 'len' representing a
81 * DWARF expression, compute the number of operations based
82 * on there being one byte describing the operation and
83 * zero or more bytes of operands as defined in the standard
84 * for each operation type.
85 */
86int
87dwarf_op_num(uint8_t pointer_size, uint8_t *p, int len)
88{
89	int count = 0;
90	int64_t sval;
91	uint64_t uval;
92	uint8_t *last = p + len;
93
94	/*
95	 * Process each byte. If an error occurs, then the
96	 * count will be set to -1.
97	 */
98	while (p < last && count >= 0) {
99		count++;
100
101		switch (*p++) {
102		/* Operations with no operands. */
103		case DW_OP_deref:
104		case DW_OP_reg0:
105		case DW_OP_reg1:
106		case DW_OP_reg2:
107		case DW_OP_reg3:
108		case DW_OP_reg4:
109		case DW_OP_reg5:
110		case DW_OP_reg6:
111		case DW_OP_reg7:
112		case DW_OP_reg8:
113		case DW_OP_reg9:
114		case DW_OP_reg10:
115		case DW_OP_reg11:
116		case DW_OP_reg12:
117		case DW_OP_reg13:
118		case DW_OP_reg14:
119		case DW_OP_reg15:
120		case DW_OP_reg16:
121		case DW_OP_reg17:
122		case DW_OP_reg18:
123		case DW_OP_reg19:
124		case DW_OP_reg20:
125		case DW_OP_reg21:
126		case DW_OP_reg22:
127		case DW_OP_reg23:
128		case DW_OP_reg24:
129		case DW_OP_reg25:
130		case DW_OP_reg26:
131		case DW_OP_reg27:
132		case DW_OP_reg28:
133		case DW_OP_reg29:
134		case DW_OP_reg30:
135		case DW_OP_reg31:
136
137		case DW_OP_lit0:
138		case DW_OP_lit1:
139		case DW_OP_lit2:
140		case DW_OP_lit3:
141		case DW_OP_lit4:
142		case DW_OP_lit5:
143		case DW_OP_lit6:
144		case DW_OP_lit7:
145		case DW_OP_lit8:
146		case DW_OP_lit9:
147		case DW_OP_lit10:
148		case DW_OP_lit11:
149		case DW_OP_lit12:
150		case DW_OP_lit13:
151		case DW_OP_lit14:
152		case DW_OP_lit15:
153		case DW_OP_lit16:
154		case DW_OP_lit17:
155		case DW_OP_lit18:
156		case DW_OP_lit19:
157		case DW_OP_lit20:
158		case DW_OP_lit21:
159		case DW_OP_lit22:
160		case DW_OP_lit23:
161		case DW_OP_lit24:
162		case DW_OP_lit25:
163		case DW_OP_lit26:
164		case DW_OP_lit27:
165		case DW_OP_lit28:
166		case DW_OP_lit29:
167		case DW_OP_lit30:
168		case DW_OP_lit31:
169
170		case DW_OP_dup:
171		case DW_OP_drop:
172
173		case DW_OP_over:
174
175		case DW_OP_swap:
176		case DW_OP_rot:
177		case DW_OP_xderef:
178
179		case DW_OP_abs:
180		case DW_OP_and:
181		case DW_OP_div:
182		case DW_OP_minus:
183		case DW_OP_mod:
184		case DW_OP_mul:
185		case DW_OP_neg:
186		case DW_OP_not:
187		case DW_OP_or:
188		case DW_OP_plus:
189
190		case DW_OP_shl:
191		case DW_OP_shr:
192		case DW_OP_shra:
193		case DW_OP_xor:
194
195		case DW_OP_eq:
196		case DW_OP_ge:
197		case DW_OP_gt:
198		case DW_OP_le:
199		case DW_OP_lt:
200		case DW_OP_ne:
201
202		case DW_OP_nop:
203			break;
204
205		/* Operations with 1-byte operands. */
206		case DW_OP_const1u:
207		case DW_OP_const1s:
208		case DW_OP_pick:
209		case DW_OP_deref_size:
210		case DW_OP_xderef_size:
211			p++;
212			break;
213
214		/* Operations with 2-byte operands. */
215		case DW_OP_const2u:
216		case DW_OP_const2s:
217		case DW_OP_bra:
218		case DW_OP_skip:
219			p += 2;
220			break;
221
222		/* Operations with 4-byte operands. */
223		case DW_OP_const4u:
224		case DW_OP_const4s:
225			p += 4;
226			break;
227
228		/* Operations with 8-byte operands. */
229		case DW_OP_const8u:
230		case DW_OP_const8s:
231			p += 8;
232			break;
233
234		/* Operations with an unsigned LEB128 operand. */
235		case DW_OP_constu:
236		case DW_OP_plus_uconst:
237		case DW_OP_regx:
238		case DW_OP_piece:
239			uval = dwarf_decode_uleb128(&p);
240			break;
241
242		/* Operations with a signed LEB128 operand. */
243		case DW_OP_consts:
244		case DW_OP_breg0:
245		case DW_OP_breg1:
246		case DW_OP_breg2:
247		case DW_OP_breg3:
248		case DW_OP_breg4:
249		case DW_OP_breg5:
250		case DW_OP_breg6:
251		case DW_OP_breg7:
252		case DW_OP_breg8:
253		case DW_OP_breg9:
254		case DW_OP_breg10:
255		case DW_OP_breg11:
256		case DW_OP_breg12:
257		case DW_OP_breg13:
258		case DW_OP_breg14:
259		case DW_OP_breg15:
260		case DW_OP_breg16:
261		case DW_OP_breg17:
262		case DW_OP_breg18:
263		case DW_OP_breg19:
264		case DW_OP_breg20:
265		case DW_OP_breg21:
266		case DW_OP_breg22:
267		case DW_OP_breg23:
268		case DW_OP_breg24:
269		case DW_OP_breg25:
270		case DW_OP_breg26:
271		case DW_OP_breg27:
272		case DW_OP_breg28:
273		case DW_OP_breg29:
274		case DW_OP_breg30:
275		case DW_OP_breg31:
276		case DW_OP_fbreg:
277			sval = dwarf_decode_sleb128(&p);
278			break;
279
280		/*
281		 * Operations with an unsigned LEB128 operand
282		 * followed by a signed LEB128 operand.
283		 */
284		case DW_OP_bregx:
285			uval = dwarf_decode_uleb128(&p);
286			sval = dwarf_decode_sleb128(&p);
287			break;
288
289		/* Target address size operand. */
290		case DW_OP_addr:
291			p += pointer_size;
292			break;
293
294		/* All other operations cause an error. */
295		default:
296			count = -1;
297			break;
298		}
299	}
300
301	return count;
302}
303
304static int
305dwarf_loc_fill(Dwarf_Locdesc *lbuf, uint8_t pointer_size, uint8_t *p, int len)
306{
307	int count = 0;
308	int ret = DWARF_E_NONE;
309	uint64_t operand1;
310	uint64_t operand2;
311	uint8_t *last = p + len;
312
313	/*
314	 * Process each byte. If an error occurs, then the
315	 * count will be set to -1.
316	 */
317	while (p < last && ret == DWARF_E_NONE) {
318		operand1 = 0;
319		operand2 = 0;
320
321		lbuf->ld_s[count].lr_atom = *p;
322
323		switch (*p++) {
324		/* Operations with no operands. */
325		case DW_OP_deref:
326		case DW_OP_reg0:
327		case DW_OP_reg1:
328		case DW_OP_reg2:
329		case DW_OP_reg3:
330		case DW_OP_reg4:
331		case DW_OP_reg5:
332		case DW_OP_reg6:
333		case DW_OP_reg7:
334		case DW_OP_reg8:
335		case DW_OP_reg9:
336		case DW_OP_reg10:
337		case DW_OP_reg11:
338		case DW_OP_reg12:
339		case DW_OP_reg13:
340		case DW_OP_reg14:
341		case DW_OP_reg15:
342		case DW_OP_reg16:
343		case DW_OP_reg17:
344		case DW_OP_reg18:
345		case DW_OP_reg19:
346		case DW_OP_reg20:
347		case DW_OP_reg21:
348		case DW_OP_reg22:
349		case DW_OP_reg23:
350		case DW_OP_reg24:
351		case DW_OP_reg25:
352		case DW_OP_reg26:
353		case DW_OP_reg27:
354		case DW_OP_reg28:
355		case DW_OP_reg29:
356		case DW_OP_reg30:
357		case DW_OP_reg31:
358
359		case DW_OP_lit0:
360		case DW_OP_lit1:
361		case DW_OP_lit2:
362		case DW_OP_lit3:
363		case DW_OP_lit4:
364		case DW_OP_lit5:
365		case DW_OP_lit6:
366		case DW_OP_lit7:
367		case DW_OP_lit8:
368		case DW_OP_lit9:
369		case DW_OP_lit10:
370		case DW_OP_lit11:
371		case DW_OP_lit12:
372		case DW_OP_lit13:
373		case DW_OP_lit14:
374		case DW_OP_lit15:
375		case DW_OP_lit16:
376		case DW_OP_lit17:
377		case DW_OP_lit18:
378		case DW_OP_lit19:
379		case DW_OP_lit20:
380		case DW_OP_lit21:
381		case DW_OP_lit22:
382		case DW_OP_lit23:
383		case DW_OP_lit24:
384		case DW_OP_lit25:
385		case DW_OP_lit26:
386		case DW_OP_lit27:
387		case DW_OP_lit28:
388		case DW_OP_lit29:
389		case DW_OP_lit30:
390		case DW_OP_lit31:
391
392		case DW_OP_dup:
393		case DW_OP_drop:
394
395		case DW_OP_over:
396
397		case DW_OP_swap:
398		case DW_OP_rot:
399		case DW_OP_xderef:
400
401		case DW_OP_abs:
402		case DW_OP_and:
403		case DW_OP_div:
404		case DW_OP_minus:
405		case DW_OP_mod:
406		case DW_OP_mul:
407		case DW_OP_neg:
408		case DW_OP_not:
409		case DW_OP_or:
410		case DW_OP_plus:
411
412		case DW_OP_shl:
413		case DW_OP_shr:
414		case DW_OP_shra:
415		case DW_OP_xor:
416
417		case DW_OP_eq:
418		case DW_OP_ge:
419		case DW_OP_gt:
420		case DW_OP_le:
421		case DW_OP_lt:
422		case DW_OP_ne:
423
424		case DW_OP_nop:
425			break;
426
427		/* Operations with 1-byte operands. */
428		case DW_OP_const1u:
429		case DW_OP_const1s:
430		case DW_OP_pick:
431		case DW_OP_deref_size:
432		case DW_OP_xderef_size:
433			operand1 = *p++;
434			break;
435
436		/* Operations with 2-byte operands. */
437		case DW_OP_const2u:
438		case DW_OP_const2s:
439		case DW_OP_bra:
440		case DW_OP_skip:
441			p += 2;
442			break;
443
444		/* Operations with 4-byte operands. */
445		case DW_OP_const4u:
446		case DW_OP_const4s:
447			p += 4;
448			break;
449
450		/* Operations with 8-byte operands. */
451		case DW_OP_const8u:
452		case DW_OP_const8s:
453			p += 8;
454			break;
455
456		/* Operations with an unsigned LEB128 operand. */
457		case DW_OP_constu:
458		case DW_OP_plus_uconst:
459		case DW_OP_regx:
460		case DW_OP_piece:
461			operand1 = dwarf_decode_uleb128(&p);
462			break;
463
464		/* Operations with a signed LEB128 operand. */
465		case DW_OP_consts:
466		case DW_OP_breg0:
467		case DW_OP_breg1:
468		case DW_OP_breg2:
469		case DW_OP_breg3:
470		case DW_OP_breg4:
471		case DW_OP_breg5:
472		case DW_OP_breg6:
473		case DW_OP_breg7:
474		case DW_OP_breg8:
475		case DW_OP_breg9:
476		case DW_OP_breg10:
477		case DW_OP_breg11:
478		case DW_OP_breg12:
479		case DW_OP_breg13:
480		case DW_OP_breg14:
481		case DW_OP_breg15:
482		case DW_OP_breg16:
483		case DW_OP_breg17:
484		case DW_OP_breg18:
485		case DW_OP_breg19:
486		case DW_OP_breg20:
487		case DW_OP_breg21:
488		case DW_OP_breg22:
489		case DW_OP_breg23:
490		case DW_OP_breg24:
491		case DW_OP_breg25:
492		case DW_OP_breg26:
493		case DW_OP_breg27:
494		case DW_OP_breg28:
495		case DW_OP_breg29:
496		case DW_OP_breg30:
497		case DW_OP_breg31:
498		case DW_OP_fbreg:
499			operand1 = dwarf_decode_sleb128(&p);
500			break;
501
502		/*
503		 * Operations with an unsigned LEB128 operand
504		 * followed by a signed LEB128 operand.
505		 */
506		case DW_OP_bregx:
507			operand1 = dwarf_decode_uleb128(&p);
508			operand2 = dwarf_decode_sleb128(&p);
509			break;
510
511		/* Target address size operand. */
512		case DW_OP_addr:
513			p += pointer_size;
514			break;
515
516		/* All other operations cause an error. */
517		default:
518			break;
519		}
520
521		lbuf->ld_s[count].lr_number = operand1;
522		lbuf->ld_s[count].lr_number2 = operand2;
523
524		count++;
525	}
526
527	return ret;
528}
529
530int
531dwarf_locdesc(Dwarf_Die die, uint64_t attr, Dwarf_Locdesc **llbuf, Dwarf_Signed *lenp, Dwarf_Error *err)
532{
533	Dwarf_AttrValue av;
534	Dwarf_Locdesc *lbuf;
535	int num;
536	int ret = DWARF_E_NONE;
537
538	if (err == NULL)
539		return DWARF_E_ERROR;
540
541	if (die == NULL || llbuf == NULL || lenp == NULL) {
542		DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
543		return DWARF_E_ARGUMENT;
544	}
545
546	if ((av = dwarf_attrval_find(die, attr)) == NULL) {
547		DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
548		ret = DWARF_E_NO_ENTRY;
549	} else if ((lbuf = calloc(sizeof(Dwarf_Locdesc), 1)) == NULL) {
550		DWARF_SET_ERROR(err, DWARF_E_MEMORY);
551		ret = DWARF_E_MEMORY;
552	} else {
553		*lenp = 0;
554		switch (av->av_form) {
555		case DW_FORM_block:
556		case DW_FORM_block1:
557		case DW_FORM_block2:
558		case DW_FORM_block4:
559			/* Compute the number of locations: */
560			if ((num = dwarf_op_num(die->die_cu->cu_pointer_size,
561			    av->u[1].u8p, av->u[0].u64)) < 0) {
562				DWARF_SET_ERROR(err, DWARF_E_INVALID_EXPR);
563				ret = DWARF_E_INVALID_EXPR;
564
565			/* Allocate an array of location structures. */
566			} else if ((lbuf->ld_s =
567			    calloc(sizeof(Dwarf_Loc), num)) == NULL) {
568				DWARF_SET_ERROR(err, DWARF_E_MEMORY);
569				ret = DWARF_E_MEMORY;
570
571			/* Fill the array of location structures. */
572			} else if ((ret = dwarf_loc_fill(lbuf,
573			    die->die_cu->cu_pointer_size,
574			    av->u[1].u8p, av->u[0].u64)) != DWARF_E_NONE) {
575				free(lbuf->ld_s);
576			} else
577				/* Only one descriptor is returned. */
578				*lenp = 1;
579			break;
580		default:
581			printf("%s(%d): form %s not handled\n",__func__,
582			    __LINE__,get_form_desc(av->av_form));
583			DWARF_SET_ERROR(err, DWARF_E_NOT_IMPLEMENTED);
584			ret = DWARF_E_ERROR;
585		}
586
587		if (ret == DWARF_E_NONE) {
588			*llbuf = lbuf;
589		} else
590			free(lbuf);
591	}
592
593	return ret;
594}
595
596int
597dwarf_locdesc_free(Dwarf_Locdesc *lbuf, Dwarf_Error *err)
598{
599	if (err == NULL)
600		return DWARF_E_ERROR;
601
602	if (lbuf == NULL) {
603		DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
604		return DWARF_E_ARGUMENT;
605	}
606
607	if (lbuf->ld_s != NULL)
608		free(lbuf->ld_s);
609
610	free(lbuf);
611
612	return DWARF_E_NONE;
613}
614